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
20 from lcm.ns.biz.ns_terminate import TerminateNsService
21 from lcm.pub.database.models import NfInstModel, NSInstModel
24 class TestTerminateNsApi(TestCase):
26 self.client = Client()
27 self.url = "/api/nslcm/v1/ns_instances/%s/terminate"
28 # self.ns_inst_id = '1'
29 self.ns_inst_id = str(uuid.uuid4())
31 self.vnffg_id = str(uuid.uuid4())
32 self.vim_id = str(uuid.uuid4())
33 self.job_id = str(uuid.uuid4())
34 self.nf_uuid = '1-1-1'
35 self.tenant = "tenantname"
36 model = {"metadata": {
40 "vnfdVersion": "V00001",
41 "vnfVersion": "V5.10.20",
44 "description": "PGW VNFD description",
46 "vnfExtendType": "driver"
48 NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
49 NfInstModel.objects.create(nfinstid=self.nf_inst_id, nf_name='name_1', vnf_id='1',
50 vnfm_inst_id='1', ns_inst_id='1-1-1,2-2-2',
51 max_cpu='14', max_ram='12296', max_hd='101', max_shd="20", max_net=10,
52 status='null', mnfinstid=self.nf_uuid, package_id='pkg1',
53 vnfd_model=str(model))
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.failUnlessEqual(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.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
72 response = self.client.patch(self.url % '1', data={}, format='json')
73 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
74 response = self.client.delete(self.url % '1', data={}, format='json')
75 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
76 response = self.client.get(self.url % '1', data={}, format='json')
77 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)