d5ced46259b0f70aaf005ad28246bb1821f8aa5a
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_ns_instant.py
1 # Copyright 2016 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 json
16
17 import mock
18 from django.test import TestCase
19 from rest_framework import status
20 from rest_framework.test import APIClient
21
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
26
27
28 class TestNsInstant(TestCase):
29     def setUp(self):
30         self.client = APIClient()
31         NSInstModel.objects.filter().delete()
32         self.url = "/api/nslcm/v1/ns/2/instantiate"
33         self.req_data = {
34             "additionalParamForNs": {
35                 "sdnControllerId": "2"
36             },
37             "locationConstraints": [{
38                 "vnfProfileId": "vnfd1",
39                 "locationConstraints": {
40                     "vimId": "3"
41                 }
42             }]
43         }
44         self.nsd_model = json.dumps({
45             "model": json.dumps({
46                 "ns_vnfs": [{
47                     "vnf_id": "vnf1",
48                     "properties": {
49                         "id": "vnfd1",
50                         "nf_type": "xgw"
51                     },
52                     "dependencies": [{
53                         "vl_id": "5"
54                     }]
55                 }],
56                 "ns_vls": [{
57                     "vl_id": "5",
58                     "properties": {}
59                 }]
60             })
61         })
62         self.updated_nsd_model = {
63             "ns_vnfs": [{
64                 "dependencies": [{
65                     "vl_id": "5"
66                 }],
67                 "vnf_id": "vnf1",
68                 "properties": {
69                     "nf_type": "xgw",
70                     "id": "vnfd1"
71                 }
72             }],
73             "ns_vls": [{
74                 "vl_id": "5",
75                 "properties": {
76                     "location_info": {
77                         "vimid": "3"
78                     }
79                 }
80             }]
81         }
82         self.vnfms = json.dumps({
83             "esr-vnfm": [{
84                 "vnfm-id": "4"
85             }]
86         })
87         self.vnfm = json.dumps({
88             "type": "xgw",
89             "vim-id": "3",
90             "vnfm-id": "4",
91             "certificate-url": "http://127.0.0.0/ztevnfm/v1/auth",
92             "esr-system-info-list": {
93                 "esr-system-info": [{
94                     "type": "xgw",
95                     "vendor": "zte",
96                     "version": "1.0",
97                     "service-url": "http://127.0.0.0/ztevnfm/v1",
98                     "user-name": "admin",
99                     "password": "admin123"
100                 }]
101             }
102         })
103         NSInstModel(id="2", nspackage_id="7", nsd_id="2", status="active").save()
104
105     def tearDown(self):
106         pass
107
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']
115         ]
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))
121
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)
128
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)