1 # Copyright (c) 2016-2017 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 from yardstick.network_services.collector import subscriber
20 from yardstick import ssh
23 class MockVnfAprrox(object):
29 def collect_kpi(self):
31 'pkt_in_up_stream': 100,
32 'pkt_drop_up_stream': 5,
33 'pkt_in_down_stream': 50,
34 'pkt_drop_down_stream': 40
39 class CollectorTestCase(unittest.TestCase):
42 'context1': [{'name': 'node1',
45 'plugins': {'abc': 12, 'def': 34},
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)
65 def test___init__(self, *args):
67 collector = subscriber.Collector([vnf], self.NODES)
68 self.assertEqual(len(collector.vnfs), 1)
69 self.assertEqual(len(collector.nodes), 1)
71 def test___init__no_node_information(self, *args):
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)
79 def test___init__no_node_information_in_context(self, *args):
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)
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'
92 self.assertIsNone(self.collector.start())
93 for vnf in self.collector.vnfs:
94 vnf.start_collect.assert_called_once()
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()
101 def test_stop(self, *_):
102 resource_profile = mock.MagicMock()
103 self.collector.resource_profiles = {'key': resource_profile}
105 self.assertIsNone(self.collector.stop())
106 for vnf in self.collector.vnfs:
107 vnf.stop_collect.assert_called_once()
109 for resource in self.collector.resource_profiles.values():
110 resource.stop.assert_called_once()
112 def test_get_kpi(self, *args):
113 result = self.collector.get_kpi()
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)