Merge "fix nslcm middleware"
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_sol_ns_terminate_api.py
1 # Copyright 2019 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 import uuid
15
16 import mock
17 from django.test import TestCase, Client
18 from rest_framework import status
19
20 from lcm.ns.biz.ns_terminate import TerminateNsService
21 from lcm.pub.database.models import NfInstModel, NSInstModel
22
23
24 class TestTerminateNsApi(TestCase):
25     def setUp(self):
26         self.client = Client()
27         self.url = "/api/nslcm/v1/ns_instances/%s/terminate"
28         self.ns_inst_id = str(uuid.uuid4())
29         self.nf_inst_id = '1'
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"
35         model = {"metadata": {
36             "vnfdId": "1",
37             "vnfdName": "PGW001",
38             "vnfProvider": "zte",
39             "vnfdVersion": "V00001",
40             "vnfVersion": "V5.10.20",
41             "productType": "CN",
42             "vnfType": "PGW",
43             "description": "PGW VNFD description",
44             "isShared": True,
45             "vnfExtendType": "driver"
46         }}
47         NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
48         NfInstModel.objects.create(nfinstid=self.nf_inst_id, nf_name='name_1', vnf_id='1',
49                                    vnfm_inst_id='1', ns_inst_id='1-1-1,2-2-2',
50                                    max_cpu='14', max_ram='12296', max_hd='101', max_shd="20", max_net=10,
51                                    status='null', mnfinstid=self.nf_uuid, package_id='pkg1',
52                                    vnfd_model=str(model))
53
54     def tearDown(self):
55         NSInstModel.objects.all().delete()
56         NfInstModel.objects.all().delete()
57
58     @mock.patch.object(TerminateNsService, 'run')
59     def test_terminate_vnf(self, mock_run):
60         mock_run.re.return_value = "1"
61         req_data = {"terminationTime": "2019-03-25T09:10:35.610"}
62         response = self.client.post(self.url % self.ns_inst_id, data=req_data)
63         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
64         self.assertIsNotNone(response['Location'])
65         response = self.client.get(response['Location'], format='json')
66         self.assertEqual(response.status_code, status.HTTP_200_OK)
67
68     def test_method_not_allowed(self):
69         response = self.client.put(self.url % '1', data={}, format='json')
70         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
71         response = self.client.patch(self.url % '1', data={}, format='json')
72         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
73         response = self.client.delete(self.url % '1', data={}, format='json')
74         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
75         response = self.client.get(self.url % '1', data={}, format='json')
76         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)