feat(sol006): sol006 migration
[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 = mock.Mock()
45 mock_gnocchi_client.metric.get_measures.return_value = [(datetime.datetime(2019, 4, 12, 15, 43,
46 tzinfo=datetime.timezone(
47 datetime.timedelta(0),
48 '+00:00')), 60.0, 0.0345442539),
49 (datetime.datetime(2019, 4, 12, 15, 44,
50 tzinfo=datetime.timezone(
51 datetime.timedelta(0),
52 '+00:00')), 60.0, 600000000)]
53 build_gnocchi_client.return_value = mock_gnocchi_client
54
55 backend = GnocchiBackend({'_id': 'test_uuid'})
56 value = backend._collect_instance_metric('cpu', 'test_resource_id')
57 self.assertEqual(value, 1.0)
58 mock_gnocchi_client.metric.get_measures.assert_called_once_with('cpu',
59 aggregation="rate:mean",
60 start=mock.ANY,
61 resource_id='test_resource_id')
62
63 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
64 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
65 def test_collect_gnocchi_non_rate_instance(self, build_gnocchi_client, _):
66 mock_gnocchi_client = mock.Mock()
67 mock_gnocchi_client.metric.get_measures.return_value = [(datetime.datetime(2019, 4, 12, 15, 43,
68 tzinfo=datetime.timezone(
69 datetime.timedelta(0),
70 '+00:00')), 60.0, 0.0345442539),
71 (datetime.datetime(2019, 4, 12, 15, 44,
72 tzinfo=datetime.timezone(
73 datetime.timedelta(0),
74 '+00:00')), 60.0, 128)]
75 build_gnocchi_client.return_value = mock_gnocchi_client
76
77 backend = GnocchiBackend({'_id': 'test_uuid'})
78 value = backend._collect_instance_metric('memory.usage', 'test_resource_id')
79 self.assertEqual(value, 128)
80 mock_gnocchi_client.metric.get_measures.assert_called_once_with('memory.usage',
81 aggregation=None,
82 start=mock.ANY,
83 resource_id='test_resource_id')
84
85 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
86 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
87 def test_collect_gnocchi_no_metric(self, build_gnocchi_client, _):
88 mock_gnocchi_client = mock.Mock()
89 mock_gnocchi_client.metric.get_measures.side_effect = gnocchiclient.exceptions.NotFound()
90 build_gnocchi_client.return_value = mock_gnocchi_client
91
92 backend = GnocchiBackend({'_id': 'test_uuid'})
93 value = backend._collect_instance_metric('memory.usage', 'test_resource_id')
94 self.assertIsNone(value)
95 mock_gnocchi_client.metric.get_measures.assert_called_once_with('memory.usage',
96 aggregation=None,
97 start=mock.ANY,
98 resource_id='test_resource_id')
99
100 @mock.patch.object(GnocchiBackend, '_build_neutron_client')
101 @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
102 def test_collect_interface_all_metric(self, build_gnocchi_client, build_neutron_client):
103 mock_gnocchi_client = mock.Mock()
104 mock_gnocchi_client.resource.search.return_value = [{'id': 'test_id_1'}, {'id': 'test_id_2'}]
105 mock_gnocchi_client.metric.get_measures.return_value = [(datetime.datetime(2019, 4, 12, 15, 43,
106 tzinfo=datetime.timezone(
107 datetime.timedelta(0),
108 '+00:00')), 60.0, 0.0345442539),
109 (datetime.datetime(2019, 4, 12, 15, 44,
110 tzinfo=datetime.timezone(
111 datetime.timedelta(0),
112 '+00:00')), 60.0, 0.0333070363)]
113
114 build_gnocchi_client.return_value = mock_gnocchi_client
115
116 backend = GnocchiBackend({'_id': 'test_uuid'})
117 value = backend._collect_interface_all_metric('packets_received', 'test_resource_id')
118 self.assertEqual(value, 0.0666140726)
119 mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_1',
120 limit=1)
121 mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_2',
122 limit=1)