move instantiate NS test json from code to indepandent file
[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 import mock
17 import os
18 from mock import MagicMock
19 from django.test import TestCase
20 from rest_framework import status
21 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, fileutil
26 from lcm.pub.config import config
27
28
29 class TestNsInstant(TestCase):
30
31     cur_path = os.path.dirname(os.path.abspath(__file__))
32     nsd_model = fileutil.read_json_file(cur_path + '/data/nsd_model.json')
33     nsd_model_json = json.dumps({"model": json.dumps(nsd_model)})
34     nsd_model_with_pnf_json = json.dumps(fileutil.read_json_file(cur_path + '/data/nsd_model_with_pnf.json'))
35     vnfm_list_in_aai = fileutil.read_json_file(cur_path + '/data/vnfm_list_in_aai.json')
36     vnfm_in_aai = fileutil.read_json_file(cur_path + '/data/vnfm_in_aai.json')
37     job = fileutil.read_json_file(cur_path + '/data/job.json')
38     instantiate_ns_with_pnf = fileutil.read_json_file(cur_path + '/data/instantiate_ns_with_pnf.json')
39     instantiate_ns_json = fileutil.read_json_file(cur_path + '/data/instantiate_ns.json')
40     vnfminfo = {"vnfmId": "1"}
41
42     def setUp(self):
43         self.client = APIClient()
44         NSInstModel.objects.filter().delete()
45         self.url = "/api/nslcm/v1/ns/%s/instantiate" % "2"
46         NSInstModel(id="2", nspackage_id="7", nsd_id="2", status="active").save()
47
48     def tearDown(self):
49         pass
50
51     @mock.patch.object(restcall, 'call_req')
52     @mock.patch('lcm.pub.msapi.sdc_run_catalog.parse_nsd', MagicMock(return_value=nsd_model_json))
53     @mock.patch.object(BuildInWorkflowThread, 'run')
54     def test_ns_instantiate_when_succeed_to_enter_workflow(self, mock_run, mock_call_req):
55         config.WORKFLOW_OPTION = "buildin"
56         mock_call_req.side_effect = [
57             [0, TestNsInstant.nsd_model_json, '200'],
58             [0, TestNsInstant.vnfm_list_in_aai, '200'],
59             [0, TestNsInstant.vnfm_in_aai, '200']
60         ]
61         instantiate_ns_json = fileutil.read_json_file(self.cur_path + '/data/instantiate_ns.json')
62         resp = self.client.post(self.url, data=instantiate_ns_json, format='json')
63         self.assertEqual(status.HTTP_200_OK, resp.status_code)
64         self.assertIn("jobId", resp.data)
65
66     @mock.patch.object(InstantNSService, 'do_biz')
67     def test_ns_instantiate_normal(self, mock_do_biz):
68         mock_do_biz.return_value = dict(data=TestNsInstant.job, status=status.HTTP_200_OK)
69         instantiate_ns_json = fileutil.read_json_file(self.cur_path + '/data/instantiate_ns.json')
70         resp = self.client.post(self.url, data=instantiate_ns_json, format='json')
71         self.failUnlessEqual(status.HTTP_200_OK, resp.status_code)
72         self.assertEqual(TestNsInstant.job, resp.data)
73
74     @mock.patch.object(restcall, 'call_req')
75     def test_ns_instantiate_when_fail_to_parse_nsd(self, mock_call_req):
76         mock_call_req.return_value = [1, "Failed to parse nsd", '500']
77         resp = self.client.post(self.url, data=TestNsInstant.instantiate_ns_json, format='json')
78         self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
79         self.assertIn("error", resp.data)
80
81     @mock.patch('lcm.ns.biz.ns_instantiate_flow.post_deal')
82     @mock.patch.object(restcall, 'call_req')
83     @mock.patch('lcm.ns.biz.ns_instantiate_flow.update_job')
84     @mock.patch('lcm.pub.msapi.sdc_run_catalog.parse_nsd', MagicMock(return_value=nsd_model_with_pnf_json))
85     @mock.patch('lcm.pub.msapi.extsys.select_vnfm', MagicMock(return_value=vnfminfo))
86     def test_ns_instantiate_with_pnf(self, mock_updata_job, mock_call_req, mock_post_deal):
87         config.WORKFLOW_OPTION = "grapflow"
88         NSInstModel(id="1", name="test_ns", nspackage_id="1", status="created").save()
89         ret = [0, json.JSONEncoder().encode(TestNsInstant.job), '200']
90         mock_call_req.side_effect = [ret for i in range(1, 20)]
91         ack = InstantNSService(1, TestNsInstant.instantiate_ns_with_pnf).do_biz()
92         self.assertEqual(ack['status'], status.HTTP_200_OK)
93
94     @mock.patch.object(restcall, 'call_req')
95     @mock.patch('lcm.pub.msapi.sdc_run_catalog.parse_nsd', MagicMock(return_value=nsd_model_with_pnf_json))
96     @mock.patch('lcm.pub.msapi.extsys.select_vnfm', MagicMock(return_value=vnfminfo))
97     def test_ns_instantiate_with_vimid_1(self, mock_call_req):
98         config.WORKFLOW_OPTION = "grapflow"
99         NSInstModel(id="1", name="test_ns", nspackage_id="1", status="created").save()
100         ret = [0, json.JSONEncoder().encode(TestNsInstant.job), '200']
101         mock_call_req.side_effect = [ret for i in range(1, 20)]
102         ack = InstantNSService(1, TestNsInstant.instantiate_ns_with_pnf).do_biz()
103         self.assertEqual(ack['status'], status.HTTP_200_OK)
104
105     @mock.patch.object(restcall, 'call_req')
106     @mock.patch('lcm.pub.msapi.sdc_run_catalog.parse_nsd', MagicMock(return_value=nsd_model_with_pnf_json))
107     @mock.patch('lcm.pub.msapi.extsys.select_vnfm', MagicMock(return_value=vnfminfo))
108     def test_ns_instantiate_with_different_vimid_2(self, mock_call_req):
109         config.WORKFLOW_OPTION = "grapflow"
110         NSInstModel(id="1", name="test_ns", nspackage_id="1", status="created").save()
111         ret = [0, json.JSONEncoder().encode(TestNsInstant.job), '200']
112         mock_call_req.side_effect = [ret for i in range(1, 20)]
113         ack = InstantNSService(1, TestNsInstant.instantiate_ns_with_pnf).do_biz()
114         self.assertEqual(ack['status'], status.HTTP_200_OK)