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 # pylint: disable=E1101
27 from unittest
import TestCase
, mock
31 from osm_mon
.collector
.vnf_collectors
.openstack
import GnocchiBackend
32 from osm_mon
.core
.config
import Config
35 class CollectorTest(TestCase
):
38 self
.config
= Config()
43 @mock.patch
.object(GnocchiBackend
, '_build_neutron_client')
44 @mock.patch
.object(GnocchiBackend
, '_build_gnocchi_client')
45 def test_collect_gnocchi_rate_instance(self
, build_gnocchi_client
, _
):
46 mock_gnocchi_client
= mock
.Mock()
47 mock_gnocchi_client
.metric
= mock
.Mock()
48 mock_gnocchi_client
.metric
.get_measures
.return_value
= [(datetime
.datetime(2019, 4, 12, 15, 43,
49 tzinfo
=datetime
.timezone(
50 datetime
.timedelta(0),
51 '+00:00')), 60.0, 0.0345442539),
52 (datetime
.datetime(2019, 4, 12, 15, 44,
53 tzinfo
=datetime
.timezone(
54 datetime
.timedelta(0),
55 '+00:00')), 60.0, 600000000)]
56 build_gnocchi_client
.return_value
= mock_gnocchi_client
58 backend
= GnocchiBackend({'_id': 'test_uuid'})
59 value
= backend
._collect
_instance
_metric
('cpu', 'test_resource_id')
60 self
.assertEqual(value
, 1.0)
61 mock_gnocchi_client
.metric
.get_measures
.assert_called_once_with('cpu',
62 aggregation
="rate:mean",
64 resource_id
='test_resource_id')
66 @mock.patch
.object(GnocchiBackend
, '_build_neutron_client')
67 @mock.patch
.object(GnocchiBackend
, '_build_gnocchi_client')
68 def test_collect_gnocchi_non_rate_instance(self
, build_gnocchi_client
, _
):
69 mock_gnocchi_client
= mock
.Mock()
70 mock_gnocchi_client
.metric
.get_measures
.return_value
= [(datetime
.datetime(2019, 4, 12, 15, 43,
71 tzinfo
=datetime
.timezone(
72 datetime
.timedelta(0),
73 '+00:00')), 60.0, 0.0345442539),
74 (datetime
.datetime(2019, 4, 12, 15, 44,
75 tzinfo
=datetime
.timezone(
76 datetime
.timedelta(0),
77 '+00:00')), 60.0, 128)]
78 build_gnocchi_client
.return_value
= mock_gnocchi_client
80 backend
= GnocchiBackend({'_id': 'test_uuid'})
81 value
= backend
._collect
_instance
_metric
('memory.usage', 'test_resource_id')
82 self
.assertEqual(value
, 128)
83 mock_gnocchi_client
.metric
.get_measures
.assert_called_once_with('memory.usage',
86 resource_id
='test_resource_id')
88 @mock.patch
.object(GnocchiBackend
, '_build_neutron_client')
89 @mock.patch
.object(GnocchiBackend
, '_build_gnocchi_client')
90 def test_collect_gnocchi_no_metric(self
, build_gnocchi_client
, _
):
91 mock_gnocchi_client
= mock
.Mock()
92 mock_gnocchi_client
.metric
.get_measures
.side_effect
= gnocchiclient
.exceptions
.NotFound()
93 build_gnocchi_client
.return_value
= mock_gnocchi_client
95 backend
= GnocchiBackend({'_id': 'test_uuid'})
96 value
= backend
._collect
_instance
_metric
('memory.usage', 'test_resource_id')
97 self
.assertIsNone(value
)
98 mock_gnocchi_client
.metric
.get_measures
.assert_called_once_with('memory.usage',
101 resource_id
='test_resource_id')
103 @mock.patch
.object(GnocchiBackend
, '_build_neutron_client')
104 @mock.patch
.object(GnocchiBackend
, '_build_gnocchi_client')
105 def test_collect_interface_all_metric(self
, build_gnocchi_client
, build_neutron_client
):
106 mock_gnocchi_client
= mock
.Mock()
107 mock_gnocchi_client
.resource
.search
.return_value
= [{'id': 'test_id_1'}, {'id': 'test_id_2'}]
108 mock_gnocchi_client
.metric
.get_measures
.return_value
= [(datetime
.datetime(2019, 4, 12, 15, 43,
109 tzinfo
=datetime
.timezone(
110 datetime
.timedelta(0),
111 '+00:00')), 60.0, 0.0345442539),
112 (datetime
.datetime(2019, 4, 12, 15, 44,
113 tzinfo
=datetime
.timezone(
114 datetime
.timedelta(0),
115 '+00:00')), 60.0, 0.0333070363)]
117 build_gnocchi_client
.return_value
= mock_gnocchi_client
119 backend
= GnocchiBackend({'_id': 'test_uuid'})
120 value
= backend
._collect
_interface
_all
_metric
('packets_received', 'test_resource_id')
121 self
.assertEqual(value
, 0.0666140726)
122 mock_gnocchi_client
.metric
.get_measures
.assert_any_call('packets_received', resource_id
='test_id_1',
124 mock_gnocchi_client
.metric
.get_measures
.assert_any_call('packets_received', resource_id
='test_id_2',