1f2d06a419b7704bb9b8058b172e145eedb07f08
[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 import gnocchiclient
27
28 from osm_mon.collector.vnf_collectors.openstack import GnocchiBackend
29 from osm_mon.core.config import Config
30
31
32 class CollectorTest(TestCase):
33 def setUp(self):
34 super().setUp()
35 self.config = Config()
36
37 def tearDown(self):
38 super().tearDown()
39
40 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
41 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
42 def test_collect_gnocchi_rate_instance(self, build_gnocchi_client, _):
43 mock_gnocchi_client = mock.Mock()
44 mock_gnocchi_client.metric.get_measures.return_value = [(datetime.datetime(2019, 4, 12, 15, 43,
45 tzinfo=datetime.timezone(
46 datetime.timedelta(0),
47 '+00:00')), 60.0, 0.0345442539),
48 (datetime.datetime(2019, 4, 12, 15, 44,
49 tzinfo=datetime.timezone(
50 datetime.timedelta(0),
51 '+00:00')), 60.0, 600000000)]
52 build_gnocchi_client.return_value = mock_gnocchi_client
53
54 backend = GnocchiBackend({'_id': 'test_uuid'})
55 value = backend._collect_instance_metric('cpu', 'test_resource_id')
56 self.assertEqual(value, 1.0)
57 mock_gnocchi_client.metric.get_measures.assert_called_once_with('cpu',
58 aggregation="rate:mean",
59 start=mock.ANY,
60 resource_id='test_resource_id')
61
62 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
63 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
64 def test_collect_gnocchi_non_rate_instance(self, build_gnocchi_client, _):
65 mock_gnocchi_client = mock.Mock()
66 mock_gnocchi_client.metric.get_measures.return_value = [(datetime.datetime(2019, 4, 12, 15, 43,
67 tzinfo=datetime.timezone(
68 datetime.timedelta(0),
69 '+00:00')), 60.0, 0.0345442539),
70 (datetime.datetime(2019, 4, 12, 15, 44,
71 tzinfo=datetime.timezone(
72 datetime.timedelta(0),
73 '+00:00')), 60.0, 128)]
74 build_gnocchi_client.return_value = mock_gnocchi_client
75
76 backend = GnocchiBackend({'_id': 'test_uuid'})
77 value = backend._collect_instance_metric('memory.usage', 'test_resource_id')
78 self.assertEqual(value, 128)
79 mock_gnocchi_client.metric.get_measures.assert_called_once_with('memory.usage',
80 aggregation=None,
81 start=mock.ANY,
82 resource_id='test_resource_id')
83
84 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
85 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
86 def test_collect_gnocchi_no_metric(self, build_gnocchi_client, _):
87 mock_gnocchi_client = mock.Mock()
88 mock_gnocchi_client.metric.get_measures.side_effect = gnocchiclient.exceptions.NotFound()
89 build_gnocchi_client.return_value = mock_gnocchi_client
90
91 backend = GnocchiBackend({'_id': 'test_uuid'})
92 value = backend._collect_instance_metric('memory.usage', 'test_resource_id')
93 self.assertIsNone(value)
94 mock_gnocchi_client.metric.get_measures.assert_called_once_with('memory.usage',
95 aggregation=None,
96 start=mock.ANY,
97 resource_id='test_resource_id')
98
99 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
100 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
101 def test_collect_interface_one_metric(self, build_gnocchi_client, build_neutron_client):
102 mock_gnocchi_client = mock.Mock()
103 mock_gnocchi_client.resource.search.return_value = [{'id': 'test_id'}]
104 mock_gnocchi_client.metric.get_measures.return_value = [(datetime.datetime(2019, 4, 12, 15, 43,
105 tzinfo=datetime.timezone(
106 datetime.timedelta(0),
107 '+00:00')), 60.0, 0.0345442539),
108 (datetime.datetime(2019, 4, 12, 15, 44,
109 tzinfo=datetime.timezone(
110 datetime.timedelta(0),
111 '+00:00')), 60.0, 0.0333070363)]
112 mock_neutron_client = mock.Mock()
113 mock_neutron_client.list_ports.return_value = {'ports': [
114 {'binding:vnic_type': 'normal', 'created_at': '2019-04-15T17:11:39Z',
115 'tenant_id': '88b78c76e01f4b228e68a06f8ebec6da',
116 'binding:vif_details': {'port_filter': True, 'ovs_hybrid_plug': True, 'datapath_type': 'system'},
117 'revision_number': 6, 'updated_at': '2019-04-15T17:11:48Z', 'binding:profile': {},
118 'mac_address': 'fa:16:3e:1c:e3:00', 'status': 'ACTIVE', 'project_id': '88b78c76e01f4b228e68a06f8ebec6da',
119 'network_id': '7f34edad-9064-4de8-b535-b14f9b715ed9', 'port_security_enabled': True, 'tags': [],
120 'description': '', 'security_groups': ['3d60c37e-6229-487b-858d-3aff6d98d66f'], 'admin_state_up': True,
121 'binding:vif_type': 'ovs', 'allowed_address_pairs': [], 'name': 'eth0',
122 'id': '1c6f9a06-6b88-45f3-8d32-dc1216436f0a', 'binding:host_id': 's111412',
123 'fixed_ips': [{'ip_address': '10.0.0.51', 'subnet_id': '4c3fa16c-3e22-43f4-9798-7b10593aff93'}],
124 'device_owner': 'compute:nova', 'device_id': '5cf5bbc4-e4f8-4e22-8bbf-6970218e774d',
125 'extra_dhcp_opts': []}]}
126
127 build_gnocchi_client.return_value = mock_gnocchi_client
128 build_neutron_client.return_value = mock_neutron_client
129
130 backend = GnocchiBackend({'_id': 'test_uuid'})
131 value = backend._collect_interface_one_metric('packets_received', 'test_resource_id', 'eth0')
132 self.assertEqual(value, 0.0333070363)
133 mock_gnocchi_client.metric.get_measures.assert_called_once_with('packets_received', resource_id='test_id',
134 limit=1)
135 mock_neutron_client.list_ports.assert_called_once_with(device_id='test_resource_id', name='eth0')
136
137 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
138 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
139 def test_collect_interface_all_metric(self, build_gnocchi_client, build_neutron_client):
140 mock_gnocchi_client = mock.Mock()
141 mock_gnocchi_client.resource.search.return_value = [{'id': 'test_id_1'}, {'id': 'test_id_2'}]
142 mock_gnocchi_client.metric.get_measures.return_value = [(datetime.datetime(2019, 4, 12, 15, 43,
143 tzinfo=datetime.timezone(
144 datetime.timedelta(0),
145 '+00:00')), 60.0, 0.0345442539),
146 (datetime.datetime(2019, 4, 12, 15, 44,
147 tzinfo=datetime.timezone(
148 datetime.timedelta(0),
149 '+00:00')), 60.0, 0.0333070363)]
150
151 build_gnocchi_client.return_value = mock_gnocchi_client
152
153 backend = GnocchiBackend({'_id': 'test_uuid'})
154 value = backend._collect_interface_all_metric('packets_received', 'test_resource_id')
155 self.assertEqual(value, 0.0666140726)
156 mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_1',
157 limit=1)
158 mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_2',
159 limit=1)