f324f627d9099c067fd3a3240b5a7712c51b27fc
[yardstick.git] / tests / unit / network_services / collector / test_subscriber.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 # Unittest for yardstick.network_services.collector.subscriber
19
20 from __future__ import absolute_import
21 import unittest
22 import mock
23
24 from yardstick.network_services.collector import subscriber
25
26
27 class MockVnfAprrox(object):
28
29     def __init__(self):
30         self.result = {}
31         self.name = "vnf__1"
32
33     def collect_kpi(self):
34         self.result = {
35             'pkt_in_up_stream': 100,
36             'pkt_drop_up_stream': 5,
37             'pkt_in_down_stream': 50,
38             'pkt_drop_down_stream': 40
39         }
40         return self.result
41
42
43 class CollectorTestCase(unittest.TestCase):
44
45     NODES = {
46         'node1': {},
47         'node2': {
48             'ip': '1.2.3.4',
49             'collectd': {
50                 'plugins': {'abc': 12, 'def': 34},
51                 'interval': 987,
52             },
53         },
54     }
55     TRAFFIC_PROFILE = {
56         'key1': 'value1',
57     }
58
59     def setUp(self):
60         vnf = MockVnfAprrox()
61         self.ssh_patch = mock.patch('yardstick.network_services.nfvi.resource.ssh', autospec=True)
62         mock_ssh = self.ssh_patch.start()
63         mock_instance = mock.Mock()
64         mock_instance.execute.return_value = 0, '', ''
65         mock_ssh.AutoConnectSSH.from_node.return_value = mock_instance
66         self.collector = subscriber.Collector([vnf], self.NODES, self.TRAFFIC_PROFILE, 1800)
67
68     def tearDown(self):
69         self.ssh_patch.stop()
70
71     def test___init__(self, *_):
72         vnf = MockVnfAprrox()
73         collector = subscriber.Collector([vnf], {}, {})
74         self.assertEqual(len(collector.vnfs), 1)
75         self.assertEqual(collector.traffic_profile, {})
76
77     def test___init___with_data(self, *_):
78         self.assertEqual(len(self.collector.vnfs), 1)
79         self.assertDictEqual(self.collector.traffic_profile, self.TRAFFIC_PROFILE)
80         self.assertEqual(len(self.collector.resource_profiles), 1)
81
82     def test___init___negative(self, *_):
83         pass
84
85     def test_start(self, *_):
86         self.assertIsNone(self.collector.start())
87
88     def test_stop(self, *_):
89         self.assertIsNone(self.collector.stop())
90
91     def test_get_kpi(self, *_):
92         result = self.collector.get_kpi()
93
94         self.assertEqual(result["vnf__1"]["pkt_in_up_stream"], 100)
95         self.assertEqual(result["vnf__1"]["pkt_drop_up_stream"], 5)
96         self.assertEqual(result["vnf__1"]["pkt_in_down_stream"], 50)
97         self.assertEqual(result["vnf__1"]["pkt_drop_down_stream"], 40)
98         self.assertIn('node2', result)