1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
24 from unittest
import TestCase
, mock
26 from osm_mon
.collector
.vnf_collectors
.openstack
import GnocchiBackend
27 from osm_mon
.core
.config
import Config
30 class CollectorTest(TestCase
):
33 self
.config
= Config()
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
52 backend
= GnocchiBackend('test_uuid', 60)
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', granularity
=60,
56 resource_id
='test_resource_id', start
=mock
.ANY
)
58 @mock.patch
.object(GnocchiBackend
, '_build_neutron_client')
59 @mock.patch
.object(GnocchiBackend
, '_build_gnocchi_client')
60 def test_collect_interface_one_metric(self
, build_gnocchi_client
, build_neutron_client
):
61 mock_gnocchi_client
= mock
.Mock()
62 mock_gnocchi_client
.resource
.search
.return_value
= [{'id': 'test_id'}]
63 mock_gnocchi_client
.metric
.get_measures
.return_value
= [(datetime
.datetime(2019, 4, 12, 15, 43,
64 tzinfo
=datetime
.timezone(
65 datetime
.timedelta(0),
66 '+00:00')), 60.0, 0.0345442539),
67 (datetime
.datetime(2019, 4, 12, 15, 44,
68 tzinfo
=datetime
.timezone(
69 datetime
.timedelta(0),
70 '+00:00')), 60.0, 0.0333070363)]
71 mock_neutron_client
= mock
.Mock()
72 mock_neutron_client
.list_ports
.return_value
= {'ports': [
73 {'binding:vnic_type': 'normal', 'created_at': '2019-04-15T17:11:39Z',
74 'tenant_id': '88b78c76e01f4b228e68a06f8ebec6da',
75 'binding:vif_details': {'port_filter': True, 'ovs_hybrid_plug': True, 'datapath_type': 'system'},
76 'revision_number': 6, 'updated_at': '2019-04-15T17:11:48Z', 'binding:profile': {},
77 'mac_address': 'fa:16:3e:1c:e3:00', 'status': 'ACTIVE', 'project_id': '88b78c76e01f4b228e68a06f8ebec6da',
78 'network_id': '7f34edad-9064-4de8-b535-b14f9b715ed9', 'port_security_enabled': True, 'tags': [],
79 'description': '', 'security_groups': ['3d60c37e-6229-487b-858d-3aff6d98d66f'], 'admin_state_up': True,
80 'binding:vif_type': 'ovs', 'allowed_address_pairs': [], 'name': 'eth0',
81 'id': '1c6f9a06-6b88-45f3-8d32-dc1216436f0a', 'binding:host_id': 's111412',
82 'fixed_ips': [{'ip_address': '10.0.0.51', 'subnet_id': '4c3fa16c-3e22-43f4-9798-7b10593aff93'}],
83 'device_owner': 'compute:nova', 'device_id': '5cf5bbc4-e4f8-4e22-8bbf-6970218e774d',
84 'extra_dhcp_opts': []}]}
86 build_gnocchi_client
.return_value
= mock_gnocchi_client
87 build_neutron_client
.return_value
= mock_neutron_client
89 backend
= GnocchiBackend('test_uuid', 60)
90 value
= backend
._collect
_interface
_one
_metric
('packets_received', 'test_resource_id', 'eth0')
91 self
.assertEqual(value
, 0.0333070363)
92 mock_gnocchi_client
.metric
.get_measures
.assert_called_once_with('packets_received', granularity
=60,
93 resource_id
='test_id', start
=mock
.ANY
)
94 mock_neutron_client
.list_ports
.assert_called_once_with(device_id
='test_resource_id', name
='eth0')
96 @mock.patch
.object(GnocchiBackend
, '_build_neutron_client')
97 @mock.patch
.object(GnocchiBackend
, '_build_gnocchi_client')
98 def test_collect_interface_all_metric(self
, build_gnocchi_client
, build_neutron_client
):
99 mock_gnocchi_client
= mock
.Mock()
100 mock_gnocchi_client
.resource
.search
.return_value
= [{'id': 'test_id_1'}, {'id': 'test_id_2'}]
101 mock_gnocchi_client
.metric
.get_measures
.return_value
= [(datetime
.datetime(2019, 4, 12, 15, 43,
102 tzinfo
=datetime
.timezone(
103 datetime
.timedelta(0),
104 '+00:00')), 60.0, 0.0345442539),
105 (datetime
.datetime(2019, 4, 12, 15, 44,
106 tzinfo
=datetime
.timezone(
107 datetime
.timedelta(0),
108 '+00:00')), 60.0, 0.0333070363)]
110 build_gnocchi_client
.return_value
= mock_gnocchi_client
112 backend
= GnocchiBackend('test_uuid', 60)
113 value
= backend
._collect
_interface
_all
_metric
('packets_received', 'test_resource_id')
114 self
.assertEqual(value
, 0.0666140726)
115 mock_gnocchi_client
.metric
.get_measures
.assert_any_call('packets_received', granularity
=60,
116 resource_id
='test_id_1', start
=mock
.ANY
)
117 mock_gnocchi_client
.metric
.get_measures
.assert_any_call('packets_received', granularity
=60,
118 resource_id
='test_id_2', start
=mock
.ANY
)