1 # Copyright 2019 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.
17 from django.test import TestCase, Client
18 from rest_framework import status
19 from lcm.ns.biz.ns_terminate import TerminateNsService
20 from lcm.pub.database.models import NfInstModel, NSInstModel
21 from lcm.ns.tests import VNFD_MODEL_DICT
24 class TestTerminateNsApi(TestCase):
26 self.client = Client()
27 self.url = "/api/nslcm/v1/ns_instances/%s/terminate"
28 self.ns_inst_id = str(uuid.uuid4())
30 self.vnffg_id = str(uuid.uuid4())
31 self.vim_id = str(uuid.uuid4())
32 self.job_id = str(uuid.uuid4())
33 self.nf_uuid = '1-1-1'
34 self.tenant = "tenantname"
39 NfInstModel.objects.create(
40 nfinstid=self.nf_inst_id,
44 ns_inst_id='1-1-1,2-2-2',
51 mnfinstid=self.nf_uuid,
53 vnfd_model=json.dumps(VNFD_MODEL_DICT))
56 NSInstModel.objects.all().delete()
57 NfInstModel.objects.all().delete()
59 @mock.patch.object(TerminateNsService, 'run')
60 def test_terminate_vnf(self, mock_run):
61 mock_run.re.return_value = "1"
62 req_data = {"terminationTime": "2019-03-25T09:10:35.610"}
63 response = self.client.post(self.url % self.ns_inst_id, data=req_data)
64 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)
65 self.assertIsNotNone(response['Location'])
66 response = self.client.get(response['Location'], format='json')
67 self.assertEqual(response.status_code, status.HTTP_200_OK)
69 def test_method_not_allowed(self):
70 response = self.client.put(self.url % '1', data={}, format='json')
71 self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
72 response = self.client.patch(self.url % '1', data={}, format='json')
73 self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
74 response = self.client.delete(self.url % '1', data={}, format='json')
75 self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
76 response = self.client.get(self.url % '1', data={}, format='json')
77 self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)