Refactor ns scale UT cases of vfc-nfvo-lcm
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_ns_manual_scale.py
1 # Copyright 2017 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import uuid
16
17 import mock
18 from django.test import Client
19 from django.test import TestCase
20 from rest_framework import status
21
22 from lcm.ns.const import NS_INST_STATUS
23 from lcm.ns.ns_manual_scale import NSManualScaleService
24 from lcm.pub.database.models import NSInstModel
25 from lcm.pub.exceptions import NSLCMException
26 from lcm.pub.utils import restcall
27 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
28
29
30 class TestNsManualScale(TestCase):
31     def setUp(self):
32         self.nsd_id = str(uuid.uuid4())
33         self.ns_package_id = str(uuid.uuid4())
34         self.ns_inst_id = str(uuid.uuid4())
35         self.job_id = JobUtil.create_job("NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
36
37         self.client = Client()
38         NSInstModel(id=self.ns_inst_id, name="abc", nspackage_id="7", nsd_id="111").save()
39
40     def tearDown(self):
41         NSInstModel.objects.filter().delete()
42
43     @mock.patch.object(NSManualScaleService, 'run')
44     def test_ns_manual_scale(self, mock_run):
45         data = {
46             'nsdid': self.nsd_id,
47             'nsname': 'ns',
48             'description': 'description',
49             "scaleNsByStepsData": [{
50                 "scaleNsByStepsData": [{
51                     "aspectId": "1",
52                     "numberOfSteps": 1,
53                     "scalingDirection": "0"
54                 }]
55             }]
56         }
57         response = self.client.post("/api/nslcm/v1/ns/%s/scale" % self.nsd_id, data=data)
58         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
59
60     @mock.patch.object(restcall, 'call_req')
61     def test_ns_manual_scale_thread(self, mock_call):
62         data = {
63             'nsdid': self.nsd_id,
64             'nsname': 'ns',
65             'description': 'description',
66             "scaleNsByStepsData": [{
67                 "scaleNsByStepsData": [{
68                     "aspectId": "1",
69                     "numberOfSteps": 1,
70                     "scalingDirection": "0"
71                 }]
72             }]
73         }
74         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
75         self.assertTrue(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.ACTIVE)
76
77     def test_swagger_ok(self):
78         resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
79         self.assertEqual(resp.status_code, status.HTTP_200_OK)
80
81     @mock.patch.object(NSManualScaleService, 'start')
82     def test_ns_manual_scale_empty_data(self, mock_start):
83         mock_start.side_effect = NSLCMException("NS scale failed.")
84
85         data = {}
86
87         response = self.client.post("/api/nslcm/v1/ns/%s/scale" % self.nsd_id, data=data)
88         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
89         self.assertIn("error", response.data)
90
91     @mock.patch.object(NSManualScaleService, 'start')
92     def test_ns_manual_scale_non_existing_nsd_id(self, mock_start):
93         mock_start.side_effect = NSLCMException("NS scale failed.")
94         nsd_id = '1111'
95         data = {
96             'nsdid': nsd_id,
97             'nsname': 'ns',
98             'description': 'description',
99             "scaleNsByStepsData": [{
100                 "scaleNsByStepsData": [{
101                     "aspectId": "1",
102                     "numberOfSteps": 1,
103                     "scalingDirection": "0"
104                 }]
105             }]
106         }
107         response = self.client.post("/api/nslcm/v1/ns/%s/scale" % nsd_id, data=data)
108         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
109         self.assertIn("error", response.data)