1 # Copyright 2017 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.
19 from django.test import Client
20 from rest_framework import status
22 from lcm.pub.utils import restcall
23 from lcm.pub.database.models import WFPlanModel, JobStatusModel
24 from lcm.pub.utils.jobutil import JobUtil
25 from lcm.workflows import build_in
27 class WorkflowViewTest(unittest.TestCase):
29 self.client = Client()
30 WFPlanModel.objects.filter().delete()
35 @mock.patch.object(restcall, 'upload_by_msb')
36 def test_deploy_workflow(self, mock_upload_by_msb):
37 mock_upload_by_msb.return_value = [0, json.JSONEncoder().encode({
43 response = self.client.post("/api/nslcm/v1/workflow",
44 {"filePath": os.path.abspath(__file__)}, format='json')
45 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
46 self.assertEqual(1, len(WFPlanModel.objects.filter(deployed_id="3")))
48 @mock.patch.object(restcall, 'upload_by_msb')
49 @mock.patch.object(restcall, 'call_req')
50 def test_force_deploy_workflow(self, mock_call_req, mock_upload_by_msb):
51 mock_call_req.return_value = [0, json.JSONEncoder().encode({
55 mock_upload_by_msb.return_value = [0, json.JSONEncoder().encode({
61 WFPlanModel(deployed_id="1", process_id="2", status="3", message="4").save()
62 response = self.client.post("/api/nslcm/v1/workflow",
63 {"filePath": os.path.abspath(__file__), "forceDeploy": "True"}, format='json')
64 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
65 self.assertEqual(0, len(WFPlanModel.objects.filter(deployed_id="1")))
66 self.assertEqual(1, len(WFPlanModel.objects.filter(deployed_id="4")))
68 def test_deploy_workflow_when_already_deployed(self):
69 WFPlanModel(deployed_id="1", process_id="2", status="3", message="4").save()
70 response = self.client.post("/api/nslcm/v1/workflow",
71 {"filePath": os.path.abspath(__file__)}, format='json')
72 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
73 self.assertEqual({'msg': 'Already deployed.'}, json.loads(response.content))
75 @mock.patch.object(restcall, 'call_req')
76 def test_buildin_workflow_normal(self, mock_call_req):
81 "nsInstanceId": ns_inst_id,
82 "object_context": '{"a": "b"}',
83 "object_additionalParamForNs": '{"c": "d"}',
84 "object_additionalParamForVnf": '{"e": "f"}',
88 "sdnControllerId": "2"
91 "api/nslcm/v1/ns/vls":
92 [0, json.JSONEncoder().encode({
97 "api/nslcm/v1/ns/vnfs":
98 [0, json.JSONEncoder().encode({
102 "api/nslcm/v1/ns/vnfs/2":
103 [0, json.JSONEncoder().encode({
104 "vnfStatus": "active"
106 "api/nslcm/v1/ns/sfcs":
107 [0, json.JSONEncoder().encode({
111 "api/nslcm/v1/ns/sfcs/3":
112 [0, json.JSONEncoder().encode({
113 "sfcStatus": "active"
115 "/api/nslcm/v1/jobs/11?responseId=0":
116 [0, json.JSONEncoder().encode({"responseDescriptor": {
119 "statusDescription": "ok"
121 "/api/nslcm/v1/jobs/111?responseId=0":
122 [0, json.JSONEncoder().encode({"responseDescriptor": {
125 "statusDescription": "ok"
127 "api/nslcm/v1/jobs/{jobId}".format(jobId=job_id):
129 "api/nslcm/v1/ns/{nsInstanceId}/postdeal".format(nsInstanceId=ns_inst_id):
133 def side_effect(*args):
134 return mock_vals[args[4]]
135 mock_call_req.side_effect = side_effect
137 self.assertTrue(build_in.run_ns_instantiate(wf_input))