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.
18 from django.test import TestCase
19 from rest_framework import status
20 from rest_framework.test import APIClient
22 from lcm.ns.biz.ns_instant import BuildInWorkflowThread
23 from lcm.ns.biz.ns_instant import InstantNSService
24 from lcm.pub.database.models import NSInstModel
25 from lcm.pub.utils import restcall
28 class TestNsInstant(TestCase):
30 self.client = APIClient()
31 NSInstModel.objects.filter().delete()
32 self.url = "/api/nslcm/v1/ns/2/instantiate"
34 "additionalParamForNs": {
35 "sdnControllerId": "2"
37 "locationConstraints": [{
38 "vnfProfileId": "vnfd1",
39 "locationConstraints": {
44 self.nsd_model = json.dumps({
62 self.updated_nsd_model = {
82 self.vnfms = json.dumps({
87 self.vnfm = json.dumps({
91 "certificate-url": "http://127.0.0.0/ztevnfm/v1/auth",
92 "esr-system-info-list": {
97 "service-url": "http://127.0.0.0/ztevnfm/v1",
99 "password": "admin123"
103 NSInstModel(id="2", nspackage_id="7", nsd_id="2", status="active").save()
108 @mock.patch.object(restcall, 'call_req')
109 @mock.patch.object(BuildInWorkflowThread, 'run')
110 def test_ns_instantiate_when_succeed_to_enter_workflow(self, mock_run, mock_call_req):
111 mock_call_req.side_effect = [
112 [0, self.nsd_model, '200'],
113 [0, self.vnfms, '200'],
114 [0, self.vnfm, '200']
116 resp = self.client.post(self.url, data=self.req_data, format='json')
117 self.failUnlessEqual(status.HTTP_200_OK, resp.status_code)
118 self.assertIn("jobId", resp.data)
119 upd_nsd_model = NSInstModel.objects.filter(id="2").first().nsd_model
120 self.assertEqual(self.updated_nsd_model, json.loads(upd_nsd_model))
122 @mock.patch.object(InstantNSService, 'do_biz')
123 def test_ns_instantiate_normal(self, mock_do_biz):
124 mock_do_biz.return_value = dict(data={'jobId': "1"}, status=status.HTTP_200_OK)
125 resp = self.client.post(self.url, data=self.req_data, format='json')
126 self.failUnlessEqual(status.HTTP_200_OK, resp.status_code)
127 self.assertEqual({'jobId': "1"}, resp.data)
129 @mock.patch.object(restcall, 'call_req')
130 def test_ns_instantiate_when_fail_to_parse_nsd(self, mock_call_req):
131 mock_call_req.return_value = [1, "Failed to parse nsd", '500']
132 resp = self.client.post(self.url, data=self.req_data, format='json')
133 self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
134 self.assertIn("error", resp.data)