1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from rest_framework import status
19 from django.test import TestCase
20 from django.test import Client
21 from lcm.pub.database.models import NSDModel, NSInstModel
22 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
23 from lcm.ns.const import NS_INST_STATUS
24 from lcm.pub.utils import restcall
25 from lcm.ns.ns_manual_scale import NSManualScaleService
26 from lcm.pub.exceptions import NSLCMException
29 class TestNsManualScale(TestCase):
31 self.nsd_id = str(uuid.uuid4())
32 self.ns_package_id = str(uuid.uuid4())
33 self.ns_inst_id = str(uuid.uuid4())
34 self.job_id = JobUtil.create_job("NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
36 self.client = Client()
37 self.context = '{"vnfs": ["a", "b"], "sfcs": ["c"], "vls": ["d", "e", "f"]}'
38 NSInstModel(id=self.ns_inst_id, name="abc",nspackage_id="7", nsd_id="111").save()
41 NSInstModel.objects.filter().delete()
44 @mock.patch.object(restcall, 'call_req')
45 @mock.patch.object(toscautil, 'convert_nsd_model')
46 def test_ns_instant_ok(self, mock_convert_nsd_model, mock_call_req):
47 mock_convert_nsd_model.return_value = self.context
49 "/api/catalog/v1/csars/7/files?relativePath=abc.yaml":
50 [0, '{"downloadUri":"http://test.yaml", "localPath":"./test.yaml"}', '200'],
51 "/api/tosca/v1/indirect/plan":
52 [0, '{"description":"", "metadata":{}, "nodes":[]}', '200'],
53 "/api/catalog/v1/servicetemplates/2/operations":
54 [0, '[{"name":"LCM", "processId":"{http://www.open-o.org/tosca/nfv/2015/12}init-16"}]', '200'],
55 "/api/wso2bpel/v1/process/instance":
56 [0, '{"status": 1}', '200']}
58 def side_effect(*args):
59 return mock_vals[args[4]]
61 mock_call_req.side_effect = side_effect
63 data = {'iaUrl': "", 'vnfmId': "", 'context': "{\"e\":{\"f\":\"4\"}}", 'statusUrl': "",
64 'serviceTemplateId': "", 'roUrl': "", 'containerapiUrl': "", 'flavor': "",
65 'nsInstanceId': "123", 'instanceId': "234", 'resourceUrl': "", 'callbackId': "",
66 'additionalParamForVnf': "[{\"b\":1},{\"c\":{\"d\":\"2\"}}]",
67 'additionalParamForNs': "[{\"a\":3},{\"e\":{\"f\":\"4\"}}]", 'flavorParams': ""}
68 resp = self.client.post("/api/nslcm/v1/ns/123/instantiate", data, format='json')
69 self.assertEqual(resp.status_code, status.HTTP_200_OK)
71 @mock.patch.object(NSManualScaleService, 'run')
72 def test_ns_manual_scale(self, mock_run):
76 'description': 'description'}
77 response = self.client.post("/api/nslcm/v1/ns/%s/scale" % self.nsd_id, data=data)
78 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
80 @mock.patch.object(restcall, 'call_req')
81 def test_ns_manual_scale_thread(self, mock_call):
86 'description': 'description'}
87 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
88 self.assertTrue(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.ACTIVE)
90 def test_swagger_ok(self):
91 resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
92 self.assertEqual(resp.status_code, status.HTTP_200_OK)
94 @mock.patch.object(NSManualScaleService, 'start')
95 def test_ns_manual_scale_empty_data(self, mock_start):
96 mock_start.side_effect = NSLCMException("NS scale failed.")
100 response = self.client.post("/api/nslcm/v1/ns/%s/scale" % self.nsd_id, data=data)
101 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
102 self.assertIn("error", response.data)
104 @mock.patch.object(NSManualScaleService, 'start')
105 def test_ns_manual_scale_non_existing_nsd_id(self, mock_start):
106 mock_start.side_effect = NSLCMException("NS scale failed.")
113 'description': 'description'}
115 response = self.client.post("/api/nslcm/v1/ns/%s/scale" % nsd_id, data=data)
116 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
117 self.assertIn("error", response.data)