1 # Copyright 2016 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.
15 from rest_framework import status
16 from django.test import TestCase
17 from rest_framework.test import APIClient
21 from lcm.pub.database.models import NSInstModel
22 from lcm.ns.ns_instant import InstantNSService
23 from lcm.ns.ns_instant import BuildInWorkflowThread
24 from lcm.pub.utils import restcall
27 class TestNsInstant(TestCase):
29 self.client = APIClient()
30 NSInstModel.objects.filter().delete()
31 self.url = "/api/nslcm/v1/ns/2/instantiate"
33 "additionalParamForNs": {
34 "sdnControllerId": "2"
36 "locationConstraints": [{
37 "vnfProfileId": "vnfd1",
38 "locationConstraints": {
43 self.nsd_model = json.dumps({
61 self.updated_nsd_model = {
81 self.vnfms = json.dumps({
86 self.vnfm = json.dumps({
90 "certificate-url": "http://127.0.0.0/ztevnfm/v1/auth",
91 "esr-system-info-list": {
96 "service-url": "http://127.0.0.0/ztevnfm/v1",
98 "password": "admin123"
102 NSInstModel(id="2", nspackage_id="7", nsd_id="2", status="active").save()
107 @mock.patch.object(restcall, 'call_req')
108 @mock.patch.object(BuildInWorkflowThread, 'run')
109 def test_ns_instantiate_when_succeed_to_enter_workflow(self, mock_run, mock_call_req):
110 mock_call_req.side_effect = [
111 [0, self.nsd_model, '200'],
112 [0, self.vnfms, '200'],
113 [0, self.vnfm, '200']
115 resp = self.client.post(self.url, data=self.req_data, format='json')
116 self.failUnlessEqual(status.HTTP_200_OK, resp.status_code)
117 self.assertIn("jobId", resp.data)
118 upd_nsd_model = NSInstModel.objects.filter(id="2").first().nsd_model
119 self.assertEqual(self.updated_nsd_model, json.loads(upd_nsd_model))
121 @mock.patch.object(InstantNSService, 'do_biz')
122 def test_ns_instantiate_normal(self, mock_do_biz):
123 mock_do_biz.return_value = dict(data={'jobId': "1"}, status=status.HTTP_200_OK)
124 resp = self.client.post(self.url, data=self.req_data, format='json')
125 self.failUnlessEqual(status.HTTP_200_OK, resp.status_code)
126 self.assertEqual({'jobId': "1"}, resp.data)
128 @mock.patch.object(restcall, 'call_req')
129 def test_ns_instantiate_when_fail_to_parse_nsd(self, mock_call_req):
130 mock_call_req.return_value = [1, "Failed to parse nsd", '500']
131 resp = self.client.post(self.url, data=self.req_data, format='json')
132 self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
133 self.assertIn("error", resp.data)