Merge "move scale NS test json from code to indepandent file"
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_ns_manual_scale.py
1 # Copyright 2017 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 os
17 import uuid
18
19 import mock
20 from rest_framework.test import APIClient
21 from django.test import TestCase
22 from rest_framework import status
23
24 from lcm.ns.biz.ns_manual_scale import NSManualScaleService
25 from lcm.ns.enum import NS_INST_STATUS
26 from lcm.pub.database.models import NSInstModel, JobModel, NfInstModel
27 from lcm.pub.exceptions import NSLCMException
28 from lcm.pub.msapi import catalog
29 from lcm.pub.utils import restcall, fileutil
30 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE, JOB_MODEL_STATUS
31
32
33 class TestNsManualScale(TestCase):
34
35     def setUp(self):
36         self.cur_path = os.path.dirname(os.path.abspath(__file__))
37         self.scaling_map_json = fileutil.read_json_file(self.cur_path + '/data/scalemapping.json')
38         self.ns_inst_id = str(uuid.uuid4())
39         self.job_id = JobUtil.create_job("NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
40         self.client = APIClient()
41         self.package_id = "7"
42         NSInstModel(
43             id=self.ns_inst_id,
44             name="abc",
45             nspackage_id=self.package_id,
46             nsd_id="111").save()
47
48     def tearDown(self):
49         NSInstModel.objects.filter().delete()
50         JobModel.objects.filter().delete()
51
52     def insert_new_ns(self):
53         ns_inst_id = str(uuid.uuid4())
54         job_id = JobUtil.create_job(
55             "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
56         package_id = "23"
57         NSInstModel(
58             id=ns_inst_id,
59             name="abc",
60             nspackage_id=package_id,
61             nsd_id=package_id).save()
62         return ns_inst_id, job_id
63
64     def insert_new_nf(self):
65         self.vnfm_inst_id = "1"
66         vnfd_model_json = fileutil.read_json_file(self.cur_path + '/data/vnfd_model.json')
67         NfInstModel.objects.create(
68             nfinstid="233",
69             nf_name="name_1",
70             vnf_id="1",
71             vnfm_inst_id="1",
72             ns_inst_id=self.ns_inst_id,
73             max_cpu='14',
74             max_ram='12296',
75             max_hd='101',
76             max_shd="20",
77             max_net=10,
78             status='active',
79             mnfinstid="ab34-3g5j-de13-ab85-ij93",
80             package_id="nf_zte_hss",
81             vnfd_model=json.dumps(vnfd_model_json))
82
83     @mock.patch.object(NSManualScaleService, 'run')
84     def test_ns_manual_scale(self, mock_run):
85         scale_ns_json = fileutil.read_json_file(self.cur_path + '/data/scale_ns.json')
86         response = self.client.post("/api/nslcm/v1/ns/%s/scale" % self.ns_inst_id, data=scale_ns_json, format='json')
87         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
88
89     def test_ns_manual_scale_error_scaletype(self):
90         scale_ns_json = fileutil.read_json_file(self.cur_path + '/data/scale_ns.json')
91         scale_ns_json["scaleType"] = "SCALE_ERR"
92         NSManualScaleService(self.ns_inst_id, scale_ns_json, self.job_id).run()
93         jobs = JobModel.objects.filter(jobid=self.job_id)
94         self.assertEqual(255, jobs[0].progress)
95
96     def test_ns_manual_scale_error_nsd_id(self):
97         scale_ns_json = fileutil.read_json_file(self.cur_path + '/data/scale_ns.json')
98         scale_ns_json["scaleNsData"][0]["scaleNsByStepsData"][0]["aspectId"] = "sss_zte"
99         NSManualScaleService(self.ns_inst_id, scale_ns_json, self.job_id).run()
100         jobs = JobModel.objects.filter(jobid=self.job_id)
101         self.assertEqual(255, jobs[0].progress)
102
103     def test_ns_manual_scale_error_aspect(self):
104         scale_ns_json = fileutil.read_json_file(self.cur_path + '/data/scale_ns.json')
105         scale_ns_json["scaleNsData"][0]["scaleNsByStepsData"][0]["aspectId"] = "sss_zte"
106         ns_inst_id, job_id = self.insert_new_ns()
107         job_id = JobUtil.create_job("NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
108         NSManualScaleService(ns_inst_id, scale_ns_json, job_id).run()
109         jobs = JobModel.objects.filter(jobid=job_id)
110         self.assertEqual(255, jobs[0].progress)
111
112     @mock.patch.object(catalog, 'get_scalingmap_json_package')
113     @mock.patch.object(NSManualScaleService, 'do_vnfs_scale')
114     def test_ns_manual_scale_success(self, mock_do_vnfs_scale, mock_get_scalingmap_json_package):
115         scale_ns_json = fileutil.read_json_file(self.cur_path + '/data/scale_ns.json')
116         scale_ns_json["scaleNsData"][0]["scaleNsByStepsData"][0]["aspectId"] = "TIC_EDGE_IMS"
117         mock_get_scalingmap_json_package.return_value = self.scaling_map_json
118         mock_do_vnfs_scale.return_value = JOB_MODEL_STATUS.FINISHED
119         ns_inst_id, job_id = self.insert_new_ns()
120         job_id = JobUtil.create_job("NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
121         self.insert_new_nf()
122         NSManualScaleService(ns_inst_id, scale_ns_json, job_id).run()
123         jobs = JobModel.objects.filter(jobid=job_id)
124         self.assertEqual(255, jobs[0].progress)
125
126     @mock.patch.object(restcall, 'call_req')
127     def test_ns_manual_scale_thread(self, mock_call):
128         scale_ns_json = fileutil.read_json_file(self.cur_path + '/data/scale_ns.json')
129         NSManualScaleService(self.ns_inst_id, scale_ns_json, self.job_id).run()
130         self.assertTrue(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.ACTIVE)
131
132     @mock.patch.object(NSManualScaleService, 'start')
133     def test_ns_manual_scale_empty_data(self, mock_start):
134         mock_start.side_effect = NSLCMException("NS scale failed.")
135         response = self.client.post("/api/nslcm/v1/ns/%s/scale" % self.ns_inst_id, data={}, format='json')
136         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
137         self.assertIn("error", response.data)
138
139     @mock.patch.object(NSManualScaleService, 'start')
140     def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
141         mock_start.side_effect = NSLCMException("NS scale failed.")
142         scale_ns_json = fileutil.read_json_file(self.cur_path + '/data/scale_ns.json')
143         response = self.client.post("/api/nslcm/v1/ns/11/scale", data=scale_ns_json, format='json')
144         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
145         self.assertIn("error", response.data)