Merge "Create Dockerfile to create a yardstick-image of docker"
[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 import copy
16 import mock
17 import unittest
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 = {
42         'context1': [{'name': 'node1',
43                       'ip': '1.2.3.4',
44                       'collectd': {
45                           'plugins': {'abc': 12, 'def': 34},
46                           'interval': 987}
47                       }
48         ]
49     }
50
51     def setUp(self):
52         vnf = MockVnfAprrox()
53         vnf.start_collect = mock.Mock()
54         vnf.stop_collect = mock.Mock()
55         self.ssh_patch = mock.patch.object(ssh, 'AutoConnectSSH')
56         mock_ssh = self.ssh_patch.start()
57         mock_instance = mock.Mock()
58         mock_instance.execute.return_value = 0, '', ''
59         mock_ssh.from_node.return_value = mock_instance
60         self.collector = subscriber.Collector([vnf], self.NODES)
61
62     def tearDown(self):
63         self.ssh_patch.stop()
64
65     def test___init__(self, *args):
66         vnf = MockVnfAprrox()
67         collector = subscriber.Collector([vnf], self.NODES)
68         self.assertEqual(len(collector.vnfs), 1)
69         self.assertEqual(len(collector.nodes), 1)
70
71     def test___init__no_node_information(self, *args):
72         vnf = MockVnfAprrox()
73         nodes = copy.deepcopy(self.NODES)
74         nodes['context1'].append(None)
75         collector = subscriber.Collector([vnf], nodes)
76         self.assertEqual(len(collector.vnfs), 1)
77         self.assertEqual(len(collector.nodes), 1)
78
79     def test___init__no_node_information_in_context(self, *args):
80         vnf = MockVnfAprrox()
81         nodes = copy.deepcopy(self.NODES)
82         nodes['context1'] = None
83         collector = subscriber.Collector([vnf], nodes)
84         self.assertEqual(len(collector.vnfs), 1)
85         self.assertEqual(len(collector.nodes), 1)
86
87     def test_start(self, *args):
88         resource_profile = mock.MagicMock()
89         self.collector.resource_profiles = {'key': resource_profile}
90         self.collector.bin_path = 'path'
91
92         self.assertIsNone(self.collector.start())
93         for vnf in self.collector.vnfs:
94             vnf.start_collect.assert_called_once()
95
96         for resource_profile in self.collector.resource_profiles.values():
97             resource_profile.initiate_systemagent.assert_called_once_with('path')
98             resource_profile.start.assert_called_once()
99             resource_profile.amqp_process_for_nfvi_kpi.assert_called_once()
100
101     def test_stop(self, *_):
102         resource_profile = mock.MagicMock()
103         self.collector.resource_profiles = {'key': resource_profile}
104
105         self.assertIsNone(self.collector.stop())
106         for vnf in self.collector.vnfs:
107             vnf.stop_collect.assert_called_once()
108
109         for resource in self.collector.resource_profiles.values():
110             resource.stop.assert_called_once()
111
112     def test_get_kpi(self, *args):
113         result = self.collector.get_kpi()
114
115         self.assertEqual(2, len(result))
116         self.assertEqual(4, len(result["vnf__1"]))
117         self.assertEqual(result["vnf__1"]["pkt_in_up_stream"], 100)
118         self.assertEqual(result["vnf__1"]["pkt_drop_up_stream"], 5)
119         self.assertEqual(result["vnf__1"]["pkt_in_down_stream"], 50)
120         self.assertEqual(result["vnf__1"]["pkt_drop_down_stream"], 40)