Merge "DPDK site change"
[yardstick.git] / yardstick / tests / unit / network_services / collector / test_subscriber.py
1 # Copyright (c) 2016-2017 Intel 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
16 import unittest
17 import mock
18
19 from yardstick.network_services.collector import subscriber
20 from yardstick import ssh
21
22
23 class MockVnfAprrox(object):
24
25     def __init__(self):
26         self.result = {}
27         self.name = "vnf__1"
28
29     def collect_kpi(self):
30         self.result = {
31             'pkt_in_up_stream': 100,
32             'pkt_drop_up_stream': 5,
33             'pkt_in_down_stream': 50,
34             'pkt_drop_down_stream': 40
35         }
36         return self.result
37
38
39 class CollectorTestCase(unittest.TestCase):
40
41     NODES = {'context1': [{'name': 'node1',
42                            'ip': '1.2.3.4',
43                            'collectd': {
44                                'plugins': {'abc': 12, 'def': 34},
45                                'interval': 987
46                            },
47                            }]
48              }
49
50     def setUp(self):
51         vnf = MockVnfAprrox()
52         vnf.start_collect = mock.Mock()
53         vnf.stop_collect = mock.Mock()
54         self.ssh_patch = mock.patch.object(ssh, 'AutoConnectSSH')
55         mock_ssh = self.ssh_patch.start()
56         mock_instance = mock.Mock()
57         mock_instance.execute.return_value = 0, '', ''
58         mock_ssh.from_node.return_value = mock_instance
59         self.collector = subscriber.Collector([vnf], self.NODES)
60
61     def tearDown(self):
62         self.ssh_patch.stop()
63
64     def test___init__(self, *_):
65         vnf = MockVnfAprrox()
66         collector = subscriber.Collector([vnf], self.NODES)
67         self.assertEqual(len(collector.vnfs), 1)
68         self.assertEqual(len(collector.nodes), 1)
69
70     def test_start(self, *_):
71         resource_profile = mock.MagicMock()
72         self.collector.resource_profiles = {'key': resource_profile}
73         self.collector.bin_path = 'path'
74
75         self.assertIsNone(self.collector.start())
76         for vnf in self.collector.vnfs:
77             vnf.start_collect.assert_called_once()
78
79         for resource_profile in self.collector.resource_profiles.values():
80             resource_profile.initiate_systemagent.assert_called_once_with('path')
81             resource_profile.start.assert_called_once()
82             resource_profile.amqp_process_for_nfvi_kpi.assert_called_once()
83
84     def test_stop(self, *_):
85         resource_profile = mock.MagicMock()
86         self.collector.resource_profiles = {'key': resource_profile}
87
88         self.assertIsNone(self.collector.stop())
89         for vnf in self.collector.vnfs:
90             vnf.stop_collect.assert_called_once()
91
92         for resource in self.collector.resource_profiles.values():
93             resource.stop.assert_called_once()
94
95     def test_get_kpi(self, *_):
96         result = self.collector.get_kpi()
97
98         self.assertEqual(2, len(result))
99         self.assertEqual(4, len(result["vnf__1"]))
100         self.assertEqual(result["vnf__1"]["pkt_in_up_stream"], 100)
101         self.assertEqual(result["vnf__1"]["pkt_drop_up_stream"], 5)
102         self.assertEqual(result["vnf__1"]["pkt_in_down_stream"], 50)
103         self.assertEqual(result["vnf__1"]["pkt_drop_down_stream"], 40)