b42e0d491fdbbbc1a1658eb32f7d64362dd7e82c
[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 = [
49 (
50 datetime.datetime(
51 2019,
52 4,
53 12,
54 15,
55 43,
56 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"),
57 ),
58 60.0,
59 0.0345442539,
60 ),
61 (
62 datetime.datetime(
63 2019,
64 4,
65 12,
66 15,
67 44,
68 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"),
69 ),
70 60.0,
71 600000000,
72 ),
73 ]
74 build_gnocchi_client.return_value = mock_gnocchi_client
75
76 backend = GnocchiBackend({"_id": "test_uuid"})
77 value = backend._collect_instance_metric("cpu", "test_resource_id")
78 self.assertEqual(value, 1.0)
79 mock_gnocchi_client.metric.get_measures.assert_called_once_with(
80 "cpu",
81 aggregation="rate:mean",
82 start=mock.ANY,
83 resource_id="test_resource_id",
84 )
85
86 @mock.patch.object(GnocchiBackend, "_build_neutron_client")
87 @mock.patch.object(GnocchiBackend, "_build_gnocchi_client")
88 def test_collect_gnocchi_non_rate_instance(self, build_gnocchi_client, _):
89 mock_gnocchi_client = mock.Mock()
90 mock_gnocchi_client.metric.get_measures.return_value = [
91 (
92 datetime.datetime(
93 2019,
94 4,
95 12,
96 15,
97 43,
98 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"),
99 ),
100 60.0,
101 0.0345442539,
102 ),
103 (
104 datetime.datetime(
105 2019,
106 4,
107 12,
108 15,
109 44,
110 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"),
111 ),
112 60.0,
113 128,
114 ),
115 ]
116 build_gnocchi_client.return_value = mock_gnocchi_client
117
118 backend = GnocchiBackend({"_id": "test_uuid"})
119 value = backend._collect_instance_metric("memory.usage", "test_resource_id")
120 self.assertEqual(value, 128)
121 mock_gnocchi_client.metric.get_measures.assert_called_once_with(
122 "memory.usage",
123 aggregation=None,
124 start=mock.ANY,
125 resource_id="test_resource_id",
126 )
127
128 @mock.patch.object(GnocchiBackend, "_build_neutron_client")
129 @mock.patch.object(GnocchiBackend, "_build_gnocchi_client")
130 def test_collect_gnocchi_no_metric(self, build_gnocchi_client, _):
131 mock_gnocchi_client = mock.Mock()
132 mock_gnocchi_client.metric.get_measures.side_effect = (
133 gnocchiclient.exceptions.NotFound()
134 )
135 build_gnocchi_client.return_value = mock_gnocchi_client
136
137 backend = GnocchiBackend({"_id": "test_uuid"})
138 value = backend._collect_instance_metric("memory.usage", "test_resource_id")
139 self.assertIsNone(value)
140 mock_gnocchi_client.metric.get_measures.assert_called_once_with(
141 "memory.usage",
142 aggregation=None,
143 start=mock.ANY,
144 resource_id="test_resource_id",
145 )
146
147 @mock.patch.object(GnocchiBackend, "_build_neutron_client")
148 @mock.patch.object(GnocchiBackend, "_build_gnocchi_client")
149 def test_collect_interface_all_metric(
150 self, build_gnocchi_client, build_neutron_client
151 ):
152 mock_gnocchi_client = mock.Mock()
153 mock_gnocchi_client.resource.search.return_value = [
154 {"id": "test_id_1"},
155 {"id": "test_id_2"},
156 ]
157 mock_gnocchi_client.metric.get_measures.return_value = [
158 (
159 datetime.datetime(
160 2019,
161 4,
162 12,
163 15,
164 43,
165 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"),
166 ),
167 60.0,
168 0.0345442539,
169 ),
170 (
171 datetime.datetime(
172 2019,
173 4,
174 12,
175 15,
176 44,
177 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"),
178 ),
179 60.0,
180 0.0333070363,
181 ),
182 ]
183
184 build_gnocchi_client.return_value = mock_gnocchi_client
185
186 backend = GnocchiBackend({"_id": "test_uuid"})
187 value = backend._collect_interface_all_metric(
188 "packets_received", "test_resource_id"
189 )
190 self.assertEqual(value, 0.0666140726)
191 mock_gnocchi_client.metric.get_measures.assert_any_call(
192 "packets_received", resource_id="test_id_1", limit=1
193 )
194 mock_gnocchi_client.metric.get_measures.assert_any_call(
195 "packets_received", resource_id="test_id_2", limit=1
196 )