aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/network_services/nfvi/test_resource.py
blob: eba38c68831637c2a5eb2c5dfaa0da6da5006009 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Copyright (c) 2016-2017 Intel 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.

from __future__ import absolute_import
import unittest
import mock

from yardstick.network_services.nfvi.resource import ResourceProfile
from yardstick.network_services.nfvi import resource, collectd


class TestResourceProfile(unittest.TestCase):
    VNFD = {'vnfd:vnfd-catalog':
            {'vnfd':
             [{'short-name': 'VpeVnf',
               'vdu':
               [{'routing_table':
                 [{'network': '172.16.100.20',
                   'netmask': '255.255.255.0',
                   'gateway': '172.16.100.20',
                   'if': 'xe0'},
                  {'network': '172.16.40.20',
                   'netmask': '255.255.255.0',
                   'gateway': '172.16.40.20',
                   'if': 'xe1'}],
                 'description': 'VPE approximation using DPDK',
                 'name': 'vpevnf-baremetal',
                 'nd_route_tbl':
                 [{'network': '0064:ff9b:0:0:0:0:9810:6414',
                   'netmask': '112',
                   'gateway': '0064:ff9b:0:0:0:0:9810:6414',
                   'if': 'xe0'},
                  {'network': '0064:ff9b:0:0:0:0:9810:2814',
                   'netmask': '112',
                   'gateway': '0064:ff9b:0:0:0:0:9810:2814',
                   'if': 'xe1'}],
                 'id': 'vpevnf-baremetal',
                 'external-interface':
                 [{'virtual-interface':
                   {'dst_mac': '3c:fd:fe:9e:64:38',
                    'vpci': '0000:05:00.0',
                    'local_ip': '172.16.100.19',
                    'type': 'PCI-PASSTHROUGH',
                    'netmask': '255.255.255.0',
                    'dpdk_port_num': 0,
                    'bandwidth': '10 Gbps',
                    'dst_ip': '172.16.100.20',
                    'local_mac': '3c:fd:fe:a1:2b:80'},
                   'vnfd-connection-point-ref': 'xe0',
                   'name': 'xe0'},
                  {'virtual-interface':
                   {'dst_mac': '00:1e:67:d0:60:5c',
                    'vpci': '0000:05:00.1',
                    'local_ip': '172.16.40.19',
                    'type': 'PCI-PASSTHROUGH',
                    'netmask': '255.255.255.0',
                    'dpdk_port_num': 1,
                    'bandwidth': '10 Gbps',
                    'dst_ip': '172.16.40.20',
                    'local_mac': '3c:fd:fe:a1:2b:81'},
                   'vnfd-connection-point-ref': 'xe1',
                   'name': 'xe1'}]}],
               'description': 'Vpe approximation using DPDK',
               'mgmt-interface':
                   {'vdu-id': 'vpevnf-baremetal',
                    'host': '127.0.0.1',
                    'password': 'r00t',
                    'user': 'root',
                    'ip': '127.0.0.1'},
               'benchmark':
                   {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
               'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
                                    {'type': 'VPORT', 'name': 'xe1'}],
               'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}

    def setUp(self):
        with mock.patch("yardstick.ssh.AutoConnectSSH") as ssh:
            self.ssh_mock = mock.Mock(autospec=ssh.SSH)
            self.ssh_mock.execute = \
                mock.Mock(return_value=(0, {}, ""))
            ssh.from_node.return_value = self.ssh_mock

            mgmt = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]['mgmt-interface']
            # interfaces = \
            #    self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]['vdu'][0]['external-interface']
            port_names = \
                self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]['vdu'][0]['external-interface']
            self.resource_profile = \
                ResourceProfile(mgmt, port_names, [1, 2, 3])
            self.resource_profile.connection = self.ssh_mock

    def test___init__(self):
        self.assertEqual(True, self.resource_profile.enable)

    def test_check_if_sa_running(self):
        self.assertEqual(self.resource_profile.check_if_sa_running("collectd"),
                         [True, {}])

    def test_get_cpu_data(self):
        reskey = ["", "cpufreq", "cpufreq-0"]
        value = "metric:10"
        val = self.resource_profile.get_cpu_data(reskey[1], reskey[2], value)
        self.assertIsNotNone(val)

    def test_get_cpu_data_error(self):
        reskey = ["", "", ""]
        value = "metric:10"
        val = self.resource_profile.get_cpu_data(reskey[0], reskey[1], value)
        self.assertEqual(val, ('error', 'Invalid', '', ''))

    def test__start_collectd(self):
            self.assertIsNone(
                self.resource_profile._start_collectd(self.ssh_mock, "/opt/nsb_bin"))

    def test__prepare_collectd_conf(self):
            self.assertIsNone(
                self.resource_profile._prepare_collectd_conf("/opt/nsb_bin"))


    @mock.patch("yardstick.network_services.nfvi.resource.open")
    @mock.patch("yardstick.network_services.nfvi.resource.os")
    def test__provide_config_file(self, mock_open, mock_os):
        loadplugin = range(5)
        port_names = range(5)
        kwargs = {
            "interval": '25',
            "loadplugin": loadplugin,
            "port_names": port_names,
        }
        self.resource_profile._provide_config_file("/opt/nsb_bin", "collectd.conf", kwargs)
        self.ssh_mock.execute.assert_called_once()


    @mock.patch("yardstick.network_services.nfvi.resource.open")
    def test_initiate_systemagent(self, mock_open):
        self.resource_profile._start_collectd = mock.Mock()
        self.assertIsNone(
            self.resource_profile.initiate_systemagent("/opt/nsb_bin"))

    def test__parse_hugepages(self):
        reskey = ["cpu", "cpuFreq"]
        value = "timestamp:12345"
        res = self.resource_profile.parse_hugepages(reskey, value)
        self.assertEqual({'cpu/cpuFreq': '12345'}, res)

    def test__parse_dpdkstat(self):
        reskey = ["dpdk0", "0"]
        value = "tx:12345"
        res = self.resource_profile.parse_dpdkstat(reskey, value)
        self.assertEqual({'dpdk0/0': '12345'}, res)

    def test__parse_virt(self):
        reskey = ["vm0", "cpu"]
        value = "load:45"
        res = self.resource_profile.parse_virt(reskey, value)
        self.assertEqual({'vm0/cpu': '45'}, res)

    def test__parse_ovs_stats(self):
        reskey = ["ovs", "stats"]
        value = "tx:45"
        res = self.resource_profile.parse_ovs_stats(reskey, value)
        self.assertEqual({'ovs/stats': '45'}, res)

    def test_parse_collectd_result(self):
        res = self.resource_profile.parse_collectd_result({}, [0, 1, 2])
        expected_result = {'cpu': {}, 'dpdkstat': {}, 'hugepages': {},
                           'memory': {}, 'ovs_stats': {}, 'timestamp': '',
                           'intel_pmu': {},
                           'virt': {}}
        self.assertDictEqual(res, expected_result)

    def test_parse_collectd_result_cpu(self):
        metric = {"nsb_stats/cpu/0/ipc": "101"}
        self.resource_profile.get_cpu_data = mock.Mock(return_value=[1,
                                                                     "ipc",
                                                                     "1234",
                                                                     ""])
        res = self.resource_profile.parse_collectd_result(metric, [0, 1, 2])
        expected_result = {'cpu': {1: {'ipc': '1234'}}, 'dpdkstat': {}, 'hugepages': {},
                           'memory': {}, 'ovs_stats': {}, 'timestamp': '',
                           'intel_pmu': {},
                           'virt': {}}
        self.assertDictEqual(res, expected_result)

    def test_parse_collectd_result_memory(self):
        metric = {"nsb_stats/memory/bw": "101"}
        res = self.resource_profile.parse_collectd_result(metric, [0, 1, 2])
        expected_result = {'cpu': {}, 'dpdkstat': {}, 'hugepages': {},
                           'memory': {'bw': '101'}, 'ovs_stats': {}, 'timestamp': '',
                           'intel_pmu': {},
                           'virt': {}}
        self.assertDictEqual(res, expected_result)

    def test_parse_collectd_result_hugepage(self):
        # amqp returns bytes
        metric = {b"nsb_stats/hugepages/free": b"101"}
        self.resource_profile.parse_hugepages = \
        mock.Mock(return_value={"free": "101"})
        res = self.resource_profile.parse_collectd_result(metric, [0, 1, 2])
        expected_result = {'cpu': {}, 'dpdkstat': {}, 'hugepages': {'free': '101'},
                           'memory': {}, 'ovs_stats': {}, 'timestamp': '',
                           'intel_pmu': {},
                           'virt': {}}
        self.assertDictEqual(res, expected_result)

    def test_parse_collectd_result_dpdk_virt_ovs(self):
        metric = {b"nsb_stats/dpdkstat/tx": b"101",
                  b"nsb_stats/ovs_stats/tx": b"101",
                  b"nsb_stats/virt/virt/memory": b"101"}
        self.resource_profile.parse_dpdkstat = \
            mock.Mock(return_value={"tx": "101"})
        self.resource_profile.parse_virt = \
            mock.Mock(return_value={"memory": "101"})
        self.resource_profile.parse_ovs_stats = \
            mock.Mock(return_value={"tx": "101"})
        res = self.resource_profile.parse_collectd_result(metric, [0, 1, 2])
        expected_result = {'cpu': {}, 'dpdkstat': {'tx': '101'}, 'hugepages': {},
                           'memory': {}, 'ovs_stats': {'tx': '101'}, 'timestamp': '',
                           'intel_pmu': {},
                           'virt': {'memory': '101'}}
        self.assertDictEqual(res, expected_result)

    def test_amqp_process_for_nfvi_kpi(self):
        self.resource_profile.amqp_client = \
            mock.MagicMock(side_effect=[None, mock.MagicMock()])
        self.resource_profile.run_collectd_amqp = \
            mock.Mock(return_value=0)
        res = self.resource_profile.amqp_process_for_nfvi_kpi()
        self.assertEqual(None, res)

    def test_amqp_collect_nfvi_kpi(self):
        self.resource_profile.amqp_client = \
            mock.MagicMock(side_effect=[None, mock.MagicMock()])
        self.resource_profile.run_collectd_amqp = \
            mock.Mock(return_value=0)
        self.resource_profile.parse_collectd_result = mock.Mock()
        res = self.resource_profile.amqp_collect_nfvi_kpi()
        self.assertIsNotNone(res)

    def test_run_collectd_amqp(self):
        resource.AmqpConsumer = mock.Mock(autospec=collectd)
        self.assertIsNone(self.resource_profile.run_collectd_amqp())

    def test_start(self):
        self.assertIsNone(self.resource_profile.start())

    def test_stop(self):
        self.assertIsNone(self.resource_profile.stop())

if __name__ == '__main__':
    unittest.main()