SOL subscription test via independant json file
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_sol_ns_scale_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
15 import json
16 import uuid
17 import mock
18 from django.test import TestCase
19 from rest_framework import status
20 from rest_framework.test import APIClient
21 from lcm.ns.biz.ns_manual_scale import NSManualScaleService
22 from lcm.pub.database.models import NSInstModel, JobModel, NfInstModel
23 from lcm.pub.exceptions import NSLCMException
24 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
25 from lcm.ns.tests import VNFD_MODEL_DICT, SCALE_NS_DICT
26
27
28 class TestScaleNsApi(TestCase):
29     def setUp(self):
30         self.url = "/api/nslcm/v1/ns_instances/%s/scale"
31         self.ns_inst_id = str(uuid.uuid4())
32         self.job_id = JobUtil.create_job("NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
33         self.package_id = "7"
34         self.client = APIClient()
35         NSInstModel(
36             id=self.ns_inst_id,
37             name="abc",
38             nspackage_id=self.package_id,
39             nsd_id="111").save()
40
41     def tearDown(self):
42         NSInstModel.objects.filter().delete()
43         JobModel.objects.filter().delete()
44
45     def insert_new_ns(self):
46         ns_inst_id = str(uuid.uuid4())
47         job_id = JobUtil.create_job("NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
48         package_id = "23"
49         NSInstModel(
50             id=ns_inst_id,
51             name="abc",
52             nspackage_id=package_id,
53             nsd_id=package_id).save()
54         return ns_inst_id, job_id
55
56     def insert_new_nf(self):
57         self.nf_name = "name_1"
58         self.vnf_id = "1"
59         self.vnfm_inst_id = "1"
60         nf_inst_id = "233"
61         package_id = "nf_zte_hss"
62         nf_uuid = "ab34-3g5j-de13-ab85-ij93"
63         NfInstModel.objects.create(
64             nfinstid=nf_inst_id,
65             nf_name=self.nf_name,
66             vnf_id=self.vnf_id,
67             vnfm_inst_id=self.vnfm_inst_id,
68             ns_inst_id=self.ns_inst_id,
69             max_cpu='14',
70             max_ram='12296',
71             max_hd='101',
72             max_shd="20",
73             max_net=10,
74             status='active',
75             mnfinstid=nf_uuid,
76             package_id=package_id,
77             vnfd_model=json.dumps(VNFD_MODEL_DICT)
78         )
79
80     @mock.patch.object(NSManualScaleService, 'run')
81     def test_ns_scale(self, mock_run):
82         response = self.client.post(self.url % self.ns_inst_id, data=SCALE_NS_DICT)
83         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
84         self.assertIsNotNone(response['Location'])
85         response = self.client.get(response['Location'], format='json')
86         self.assertEqual(response.status_code, status.HTTP_200_OK)
87
88     @mock.patch.object(NSManualScaleService, 'start')
89     def test_ns_manual_scale_empty_data(self, mock_start):
90         mock_start.side_effect = NSLCMException("NS scale failed.")
91         response = self.client.post(self.url % self.ns_inst_id, data={}, format='json')
92         self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
93
94     @mock.patch.object(NSManualScaleService, 'start')
95     def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
96         mock_start.side_effect = NSLCMException("NS scale failed.")
97         response = self.client.post(self.url % '11', data=SCALE_NS_DICT)
98         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
99
100     def test_method_not_allowed(self):
101         response = self.client.put(self.url % '1', data={}, format='json')
102         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
103         response = self.client.patch(self.url % '1', data={}, format='json')
104         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
105         response = self.client.delete(self.url % '1', data={}, format='json')
106         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
107         response = self.client.get(self.url % '1', data={}, format='json')
108         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)