Merge "fix API inconsistency in ext VLs"
[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.ns_inst_id = str(uuid.uuid4())
33         self.job_id = JobUtil.create_job("NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
34
35         self.client = Client()
36         NSInstModel(id=self.ns_inst_id, name="abc", nspackage_id="7", nsd_id="111").save()
37
38     def tearDown(self):
39         NSInstModel.objects.filter().delete()
40
41     @mock.patch.object(NSManualScaleService, 'run')
42     def test_ns_manual_scale(self, mock_run):
43         data = {
44             "scaleType": "SCALE_NS",
45             "scaleNsData": [{
46                 "scaleNsByStepsData": [{
47                     "aspectId": "1",
48                     "numberOfSteps": 1,
49                     "scalingDirection": "0"
50                 }]
51             }]
52         }
53         response = self.client.post("/api/nslcm/v1/ns/%s/scale" % self.ns_inst_id, data=data)
54         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
55
56     @mock.patch.object(restcall, 'call_req')
57     def test_ns_manual_scale_thread(self, mock_call):
58         data = {
59             "scaleType": "SCALE_NS",
60             "scaleNsData": [{
61                 "scaleNsByStepsData": [{
62                     "aspectId": "1",
63                     "numberOfSteps": 1,
64                     "scalingDirection": "0"
65                 }]
66             }]
67         }
68         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
69         self.assertTrue(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.ACTIVE)
70
71     def test_swagger_ok(self):
72         resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
73         self.assertEqual(resp.status_code, status.HTTP_200_OK)
74
75     @mock.patch.object(NSManualScaleService, 'start')
76     def test_ns_manual_scale_empty_data(self, mock_start):
77         mock_start.side_effect = NSLCMException("NS scale failed.")
78         response = self.client.post("/api/nslcm/v1/ns/%s/scale" % self.ns_inst_id, data={})
79         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
80         self.assertIn("error", response.data)
81
82     @mock.patch.object(NSManualScaleService, 'start')
83     def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
84         mock_start.side_effect = NSLCMException("NS scale failed.")
85         data = {
86             "scaleType": "SCALE_NS",
87             "scaleNsData": [{
88                 "scaleNsByStepsData": [{
89                     "aspectId": "1",
90                     "numberOfSteps": 1,
91                     "scalingDirection": "0"
92                 }]
93             }]
94         }
95         response = self.client.post("/api/nslcm/v1/ns/11/scale", data=data)
96         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
97         self.assertIn("error", response.data)