Add additional unit tests in NS tests
[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
16 import mock
17 import uuid
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_MODEL_STATUS, JOB_TYPE
23 from lcm.ns.const import NS_INST_STATUS
24 from lcm.pub.utils import restcall
25 from lcm.pub.utils import toscautil
26 from lcm.ns.ns_manual_scale import NSManualScaleService
27 from lcm.pub.exceptions import NSLCMException
28
29 class TestNsManualScale(TestCase):
30     def setUp(self):
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)
35         NSDModel(id=self.ns_package_id, nsd_id=self.nsd_id, name='name').save()
36
37         self.client = Client()
38         self.context = '{"vnfs": ["a", "b"], "sfcs": ["c"], "vls": ["d", "e", "f"]}'
39         NSInstModel(id=self.ns_inst_id, name="abc",nspackage_id="7", nsd_id="111").save()
40
41     def tearDown(self):
42         NSInstModel.objects.filter().delete()
43
44
45     """
46     @mock.patch.object(restcall, 'call_req')
47     @mock.patch.object(toscautil, 'convert_nsd_model')
48     def test_ns_instant_ok(self, mock_convert_nsd_model, mock_call_req):
49         mock_convert_nsd_model.return_value = self.context
50         mock_vals = {
51             "/api/catalog/v1/csars/7/files?relativePath=abc.yaml":
52                 [0, '{"downloadUri":"http://test.yaml", "localPath":"./test.yaml"}', '200'],
53             "/api/tosca/v1/indirect/plan":
54                 [0, '{"description":"", "metadata":{}, "nodes":[]}', '200'],
55             "/api/catalog/v1/servicetemplates/2/operations":
56                 [0, '[{"name":"LCM", "processId":"{http://www.open-o.org/tosca/nfv/2015/12}init-16"}]', '200'],
57             "/api/wso2bpel/v1/process/instance":
58                 [0, '{"status": 1}', '200']}
59
60         def side_effect(*args):
61             return mock_vals[args[4]]
62
63         mock_call_req.side_effect = side_effect
64
65         data = {'iaUrl': "", 'vnfmId': "", 'context': "{\"e\":{\"f\":\"4\"}}", 'statusUrl': "",
66                 'serviceTemplateId': "", 'roUrl': "", 'containerapiUrl': "", 'flavor': "",
67                 'nsInstanceId': "123", 'instanceId': "234", 'resourceUrl': "", 'callbackId': "",
68                 'additionalParamForVnf': "[{\"b\":1},{\"c\":{\"d\":\"2\"}}]",
69                 'additionalParamForNs': "[{\"a\":3},{\"e\":{\"f\":\"4\"}}]", 'flavorParams': ""}
70         resp = self.client.post("/api/nslcm/v1/ns/123/instantiate", data, format='json')
71         self.assertEqual(resp.status_code, status.HTTP_200_OK)
72     """
73     @mock.patch.object(NSManualScaleService, 'run')
74     def test_ns_manual_scale(self, mock_run):
75         data = {
76             'nsdid': self.nsd_id,
77             'nsname': 'ns',
78             'description': 'description'}
79         response = self.client.post("/api/nslcm/v1/ns/%s/scale"%self.nsd_id, data=data)
80         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
81
82     @mock.patch.object(restcall, 'call_req')
83     def test_ns_manual_scale_thread(self, mock_call):
84
85         data = {
86             'nsdid': self.nsd_id,
87             'nsname': 'ns',
88             'description': 'description'}
89         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
90         self.assertTrue(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.ACTIVE)
91
92     def test_swagger_ok(self):
93         resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
94         self.assertEqual(resp.status_code, status.HTTP_200_OK)
95
96     @mock.patch.object(NSManualScaleService, 'start')
97     def test_ns_manual_scale_empty_data(self, mock_start):
98         mock_start.side_effect = NSLCMException("NS scale failed.")
99
100         data = {}
101
102         response = self.client.post("/api/nslcm/v1/ns/%s/scale" % self.nsd_id, data=data)
103         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
104         self.assertIn("error", response.data)
105
106     @mock.patch.object(NSManualScaleService, 'start')
107     def test_ns_manual_scale_non_existing_nsd_id(self, mock_start):
108         mock_start.side_effect = NSLCMException("NS scale failed.")
109
110         nsd_id = '1111'
111
112         data = {
113             'nsdid': nsd_id,
114             'nsname': 'ns',
115             'description': 'description'}
116
117         response = self.client.post("/api/nslcm/v1/ns/%s/scale" % nsd_id, data=data)
118         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
119         self.assertIn("error", response.data)