9af14925effc84c9f390cb1ed5ca8956364f1bdf
[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
24 # pylint: disable=E1101
25
26 import datetime
27 from unittest import TestCase, mock
28
29 import gnocchiclient
30
31 from osm_mon.collector.vnf_collectors.openstack import GnocchiBackend
32 from osm_mon.core.config import Config
33
34
35 class CollectorTest(TestCase):
36 def setUp(self):
37 super().setUp()
38 self.config = Config()
39
40 def tearDown(self):
41 super().tearDown()
42
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
57
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",
63 start=mock.ANY,
64 resource_id='test_resource_id')
65
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
79
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',
84 aggregation=None,
85 start=mock.ANY,
86 resource_id='test_resource_id')
87
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
94
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',
99 aggregation=None,
100 start=mock.ANY,
101 resource_id='test_resource_id')
102
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)]
116
117 build_gnocchi_client.return_value = mock_gnocchi_client
118
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',
123 limit=1)
124 mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_2',
125 limit=1)