5493ae79a2c05dd441061d248bc22fd8db723a19
[osm/MON.git] / osm_mon / tests / unit / collector / vnf_collectors / test_openstack.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
22 ##
23 import datetime
24 from unittest import TestCase, mock
25
26 from osm_mon.collector.vnf_collectors.openstack import GnocchiBackend
27 from osm_mon.core.config import Config
28
29
30 class CollectorTest(TestCase):
31 def setUp(self):
32 super().setUp()
33 self.config = Config()
34
35 def tearDown(self):
36 super().tearDown()
37
38 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
39 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
40 def test_collect_gnocchi_instance(self, build_gnocchi_client, build_neutron_client):
41 mock_gnocchi_client = mock.Mock()
42 mock_gnocchi_client.metric.get_measures.return_value = [(datetime.datetime(2019, 4, 12, 15, 43,
43 tzinfo=datetime.timezone(
44 datetime.timedelta(0),
45 '+00:00')), 60.0, 0.0345442539),
46 (datetime.datetime(2019, 4, 12, 15, 44,
47 tzinfo=datetime.timezone(
48 datetime.timedelta(0),
49 '+00:00')), 60.0, 0.0333070363)]
50 build_gnocchi_client.return_value = mock_gnocchi_client
51
52 backend = GnocchiBackend({'_id': 'test_uuid'})
53 value = backend._collect_instance_metric('cpu_utilization', 'test_resource_id')
54 self.assertEqual(value, 0.0333070363)
55 mock_gnocchi_client.metric.get_measures.assert_called_once_with('cpu_utilization',
56 limit=1,
57 resource_id='test_resource_id')
58
59 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
60 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
61 def test_collect_interface_one_metric(self, build_gnocchi_client, build_neutron_client):
62 mock_gnocchi_client = mock.Mock()
63 mock_gnocchi_client.resource.search.return_value = [{'id': 'test_id'}]
64 mock_gnocchi_client.metric.get_measures.return_value = [(datetime.datetime(2019, 4, 12, 15, 43,
65 tzinfo=datetime.timezone(
66 datetime.timedelta(0),
67 '+00:00')), 60.0, 0.0345442539),
68 (datetime.datetime(2019, 4, 12, 15, 44,
69 tzinfo=datetime.timezone(
70 datetime.timedelta(0),
71 '+00:00')), 60.0, 0.0333070363)]
72 mock_neutron_client = mock.Mock()
73 mock_neutron_client.list_ports.return_value = {'ports': [
74 {'binding:vnic_type': 'normal', 'created_at': '2019-04-15T17:11:39Z',
75 'tenant_id': '88b78c76e01f4b228e68a06f8ebec6da',
76 'binding:vif_details': {'port_filter': True, 'ovs_hybrid_plug': True, 'datapath_type': 'system'},
77 'revision_number': 6, 'updated_at': '2019-04-15T17:11:48Z', 'binding:profile': {},
78 'mac_address': 'fa:16:3e:1c:e3:00', 'status': 'ACTIVE', 'project_id': '88b78c76e01f4b228e68a06f8ebec6da',
79 'network_id': '7f34edad-9064-4de8-b535-b14f9b715ed9', 'port_security_enabled': True, 'tags': [],
80 'description': '', 'security_groups': ['3d60c37e-6229-487b-858d-3aff6d98d66f'], 'admin_state_up': True,
81 'binding:vif_type': 'ovs', 'allowed_address_pairs': [], 'name': 'eth0',
82 'id': '1c6f9a06-6b88-45f3-8d32-dc1216436f0a', 'binding:host_id': 's111412',
83 'fixed_ips': [{'ip_address': '10.0.0.51', 'subnet_id': '4c3fa16c-3e22-43f4-9798-7b10593aff93'}],
84 'device_owner': 'compute:nova', 'device_id': '5cf5bbc4-e4f8-4e22-8bbf-6970218e774d',
85 'extra_dhcp_opts': []}]}
86
87 build_gnocchi_client.return_value = mock_gnocchi_client
88 build_neutron_client.return_value = mock_neutron_client
89
90 backend = GnocchiBackend({'_id': 'test_uuid'})
91 value = backend._collect_interface_one_metric('packets_received', 'test_resource_id', 'eth0')
92 self.assertEqual(value, 0.0333070363)
93 mock_gnocchi_client.metric.get_measures.assert_called_once_with('packets_received', resource_id='test_id',
94 limit=1)
95 mock_neutron_client.list_ports.assert_called_once_with(device_id='test_resource_id', name='eth0')
96
97 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
98 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
99 def test_collect_interface_all_metric(self, build_gnocchi_client, build_neutron_client):
100 mock_gnocchi_client = mock.Mock()
101 mock_gnocchi_client.resource.search.return_value = [{'id': 'test_id_1'}, {'id': 'test_id_2'}]
102 mock_gnocchi_client.metric.get_measures.return_value = [(datetime.datetime(2019, 4, 12, 15, 43,
103 tzinfo=datetime.timezone(
104 datetime.timedelta(0),
105 '+00:00')), 60.0, 0.0345442539),
106 (datetime.datetime(2019, 4, 12, 15, 44,
107 tzinfo=datetime.timezone(
108 datetime.timedelta(0),
109 '+00:00')), 60.0, 0.0333070363)]
110
111 build_gnocchi_client.return_value = mock_gnocchi_client
112
113 backend = GnocchiBackend({'_id': 'test_uuid'})
114 value = backend._collect_interface_all_metric('packets_received', 'test_resource_id')
115 self.assertEqual(value, 0.0666140726)
116 mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_1',
117 limit=1)
118 mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_2',
119 limit=1)