Merge "Move tests: unit/network_services/{lib/,collector/,*.py}"
[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
21
22 class MockVnfAprrox(object):
23
24     def __init__(self):
25         self.result = {}
26         self.name = "vnf__1"
27
28     def collect_kpi(self):
29         self.result = {
30             'pkt_in_up_stream': 100,
31             'pkt_drop_up_stream': 5,
32             'pkt_in_down_stream': 50,
33             'pkt_drop_down_stream': 40
34         }
35         return self.result
36
37
38 class CollectorTestCase(unittest.TestCase):
39
40     NODES = {
41         'node1': {},
42         'node2': {
43             'ip': '1.2.3.4',
44             'collectd': {
45                 'plugins': {'abc': 12, 'def': 34},
46                 'interval': 987,
47             },
48         },
49     }
50     TRAFFIC_PROFILE = {
51         'key1': 'value1',
52     }
53
54     def setUp(self):
55         vnf = MockVnfAprrox()
56         self.ssh_patch = mock.patch('yardstick.network_services.nfvi.resource.ssh', autospec=True)
57         mock_ssh = self.ssh_patch.start()
58         mock_instance = mock.Mock()
59         mock_instance.execute.return_value = 0, '', ''
60         mock_ssh.AutoConnectSSH.from_node.return_value = mock_instance
61         self.collector = subscriber.Collector([vnf], self.NODES, self.TRAFFIC_PROFILE, 1800)
62
63     def tearDown(self):
64         self.ssh_patch.stop()
65
66     def test___init__(self, *_):
67         vnf = MockVnfAprrox()
68         collector = subscriber.Collector([vnf], {}, {})
69         self.assertEqual(len(collector.vnfs), 1)
70         self.assertEqual(collector.traffic_profile, {})
71
72     def test___init___with_data(self, *_):
73         self.assertEqual(len(self.collector.vnfs), 1)
74         self.assertDictEqual(self.collector.traffic_profile, self.TRAFFIC_PROFILE)
75         self.assertEqual(len(self.collector.resource_profiles), 1)
76
77     def test___init___negative(self, *_):
78         pass
79
80     def test_start(self, *_):
81         self.assertIsNone(self.collector.start())
82
83     def test_stop(self, *_):
84         self.assertIsNone(self.collector.stop())
85
86     def test_get_kpi(self, *_):
87         result = self.collector.get_kpi()
88
89         self.assertEqual(result["vnf__1"]["pkt_in_up_stream"], 100)
90         self.assertEqual(result["vnf__1"]["pkt_drop_up_stream"], 5)
91         self.assertEqual(result["vnf__1"]["pkt_in_down_stream"], 50)
92         self.assertEqual(result["vnf__1"]["pkt_drop_down_stream"], 40)
93         self.assertIn('node2', result)