aboutsummaryrefslogtreecommitdiffstats
path: root/lcm/ns/tests/test_scaleaspect.py
blob: f44e7544dc4ee9b3a2ce9b161f9abe5f08c75fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright 2016-2018 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import os

from django.test import TestCase
from lcm.pub.database.models import NSInstModel, NfInstModel
from lcm.ns_vnfs.enum import VNF_STATUS
from lcm.ns.biz.scaleaspect import mapping_conv, get_vnf_instance_id_list, check_scale_list, get_scale_vnf_data, \
    get_nsdId, check_and_set_params, get_scale_vnf_data_info_list, get_vnf_scale_info, get_scale_vnf_data_from_json


class TestScaleAspect(TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        NSInstModel.objects.all().delete()
        NfInstModel.objects.all().delete()

    def test_mapping_conv(self):
        keyword_map = {'a': 1, 'b': 2, 'c': 3, 'd': {'d.1': 'd.1'}}
        rest_return = {'b': 2, 'C': 3}
        resp_data = mapping_conv(keyword_map, rest_return)
        self.assertEqual(resp_data, {'a': '', 'b': 2, 'c': 3, 'd': {'d.1': ''}})

    def test_get_vnf_instance_id_list_when_no_model(self):
        self.assertEqual(get_vnf_instance_id_list('package_1'), [])

    def test_check_scale_list_when_empty_vnf_scale_list(self):
        try:
            check_scale_list([], 'ns_instanceId_01', '', '')
        except Exception as e:
            self.assertEqual(e.args, ('The scaling option[ns=ns_instanceId_01, aspect=, step=] does not exist. '
                                      'Pls check the config file.',))

    @mock.patch.object(os.path, 'abspath')
    def test_get_scale_vnf_data(self, mock_abspath):
        mock_abspath.return_value = 'lcm/1/2/3.py'
        scale_ns_data = {'scaleNsByStepsData': {'aspectId': 'TIC_EDGE_IMS', 'numberOfSteps': 1,
                                                'scalingDirection': 'r'}}
        ns_instance_id = 'ns_instanceId_01'
        scale_vnf_data_list = get_scale_vnf_data(scale_ns_data, ns_instance_id)
        success_list = [
            {
                'vnfInstanceId': 'nf_zte_cscf',
                'scaleByStepData': {
                    'type': 'r',
                    'aspectId': 'gsu',
                    'numberOfSteps': '1'
                }
            },
            {
                'vnfInstanceId': 'nf_zte_hss',
                'scaleByStepData': {
                    'type': 'r',
                    'aspectId': 'gpu',
                    'numberOfSteps': '3'
                }
            }
        ]
        self.assertEqual(scale_vnf_data_list, success_list)

    def test_get_nsd_id_when_none(self):
        self.assertEqual(get_nsdId('ns_instance_id_01'), None)

    def test_check_and_set_params_when_scale_ns_data_is_none(self):
        try:
            check_and_set_params(None, '')
        except Exception as e:
            self.assertEqual(e.args, ('Error! scaleNsData in the request is Empty!',))

    def test_check_and_set_params_when_scale_ns_by_steps_data_is_none(self):
        scale_ns_data = {'scaleNsByStepsData': None}
        try:
            check_and_set_params(scale_ns_data, '')
        except Exception as e:
            self.assertEqual(e.args, ('Error! scaleNsByStepsData in the request is Empty!',))

    @mock.patch.object(os.path, 'abspath')
    def test_get_scale_vnf_data_info_list(self, mock_abspath):
        NSInstModel(id='ns_instanceId_02', nsd_id='02').save()
        NfInstModel(package_id='nf_hw_cscf', status=VNF_STATUS.ACTIVE, nfinstid='nfinst_01').save()
        mock_abspath.return_value = 'lcm/1/2/3.py'
        scale_ns_data = {
            'scaleNsByStepsData': {'aspectId': 'TIC_EDGE_HW', 'numberOfSteps': 4, 'scalingDirection': 'r'}}
        ns_instance_id = 'ns_instanceId_02'
        scale_vnf_data_info_list = get_scale_vnf_data_info_list(scale_ns_data, ns_instance_id)
        success_list = [
            {
                'vnfInstanceId': 'nfinst_01',
                'scaleByStepData': {
                    'type': 'r',
                    'aspectId': 'gsu',
                    'numberOfSteps': '1'
                }
            }
        ]
        self.assertEqual(scale_vnf_data_info_list, success_list)

    def test_get_vnf_scale_info_when_return_none(self):
        filename = 'lcm/ns/tests/data/scalemapping.json'
        ns_instance_id = 'ns_instanceId_03'
        self.assertEqual(get_vnf_scale_info(filename, ns_instance_id, '', ''), None)

    def test_get_scale_vnf_data_from_json_when_return_none(self):
        self.assertEqual(get_scale_vnf_data_from_json({}, '', '', ''), None)