Implements multivim support in the OpenStack plugin
[osm/MON.git] / osm_mon / test / OpenStack / test_metric_req.py
1 # Copyright 2017 iIntel Research and Development Ireland Limited
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: helena.mcgough@intel.com or adrian.hoban@intel.com
21 ##
22 """Tests for all metric request message keys."""
23
24 import json
25
26 import logging
27
28 import unittest
29
30 import mock
31
32 from osm_mon.plugins.OpenStack.Gnocchi import metrics as metric_req
33
34 from osm_mon.plugins.OpenStack.common import Common
35
36 log = logging.getLogger(__name__)
37
38
39 class Message(object):
40 """A class to mock a message object value for metric requests."""
41
42 def __init__(self):
43 """Initialize a mocked message instance."""
44 self.topic = "metric_request"
45 self.key = None
46 self.value = json.dumps({"vim_uuid": "test_id", "mock_message": "message_details"})
47
48
49 class TestMetricReq(unittest.TestCase):
50 """Integration test for metric request keys."""
51
52 def setUp(self):
53 """Setup the tests for metric request keys."""
54 super(TestMetricReq, self).setUp()
55 self.metrics = metric_req.Metrics()
56
57 @mock.patch.object(Common, 'get_endpoint')
58 @mock.patch.object(Common, "get_auth_token")
59 def test_access_cred_metric_auth(self, get_token, get_endpoint):
60 """Test authentication with access credentials."""
61 message = Message()
62
63 self.metrics.metric_calls(message)
64
65 get_token.assert_called_with('test_id')
66 get_endpoint.assert_any_call('metric', 'test_id')
67
68 @mock.patch.object(Common, "get_auth_token", mock.Mock())
69 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
70 @mock.patch.object(metric_req.Metrics, "delete_metric")
71 def test_delete_metric_key(self, del_metric):
72 """Test the functionality for a delete metric request."""
73 # Mock a message value and key
74 message = Message()
75 message.key = "delete_metric_request"
76 message.value = json.dumps({"vim_uuid": "test_id", "metric_uuid": "my_metric_id"})
77
78 # Call the metric functionality and check delete request
79 self.metrics.metric_calls(message)
80 del_metric.assert_called_with(mock.ANY, mock.ANY, "my_metric_id")
81
82 @mock.patch.object(Common, "get_auth_token", mock.Mock())
83 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
84 @mock.patch.object(metric_req.Metrics, "list_metrics")
85 def test_list_metric_key(self, list_metrics):
86 """Test the functionality for a list metric request."""
87 # Mock a message with list metric key and value
88 message = Message()
89 message.key = "list_metric_request"
90 message.value = json.dumps({"vim_uuid": "test_id", "metrics_list_request": "metric_details"})
91
92 # Call the metric functionality and check list functionality
93 self.metrics.metric_calls(message)
94 list_metrics.assert_called_with(mock.ANY, mock.ANY, "metric_details")
95
96 @mock.patch.object(Common, "get_auth_token", mock.Mock())
97 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
98 @mock.patch.object(metric_req.Metrics, "read_metric_data")
99 @mock.patch.object(metric_req.Metrics, "list_metrics")
100 @mock.patch.object(metric_req.Metrics, "delete_metric")
101 @mock.patch.object(metric_req.Metrics, "configure_metric")
102 def test_update_metric_key(self, config_metric, delete_metric, list_metrics,
103 read_data):
104 """Test the functionality for an update metric request."""
105 # Mock a message with update metric key and value
106 message = Message()
107 message.key = "update_metric_request"
108 message.value = json.dumps({"vim_uuid": "test_id",
109 "metric_create":
110 {"metric_name": "my_metric",
111 "resource_uuid": "my_r_id"}})
112
113 # Call metric functionality and confirm no function is called
114 # Gnocchi does not support updating a metric configuration
115 self.metrics.metric_calls(message)
116 config_metric.assert_not_called()
117 list_metrics.assert_not_called()
118 delete_metric.assert_not_called()
119 read_data.assert_not_called()
120
121 @mock.patch.object(Common, "get_auth_token", mock.Mock())
122 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
123 @mock.patch.object(metric_req.Metrics, "configure_metric")
124 def test_config_metric_key(self, config_metric):
125 """Test the functionality for a create metric request."""
126 # Mock a message with create metric key and value
127 message = Message()
128 message.key = "create_metric_request"
129 message.value = json.dumps({"vim_uuid": "test_id", "metric_create": "metric_details"})
130
131 # Call metric functionality and check config metric
132 config_metric.return_value = "metric_id", "resource_id", True
133 self.metrics.metric_calls(message)
134 config_metric.assert_called_with(mock.ANY, mock.ANY, "metric_details")
135
136 @mock.patch.object(Common, "get_auth_token", mock.Mock())
137 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
138 @mock.patch.object(metric_req.Metrics, "read_metric_data")
139 def test_read_data_key(self, read_data):
140 """Test the functionality for a read metric data request."""
141 # Mock a message with a read data key and value
142 message = Message()
143 message.key = "read_metric_data_request"
144 message.value = json.dumps({"vim_uuid": "test_id", "alarm_uuid": "alarm_id"})
145
146 # Call metric functionality and check read data metrics
147 read_data.return_value = "time_stamps", "data_values"
148 self.metrics.metric_calls(message)
149 read_data.assert_called_with(
150 mock.ANY, mock.ANY, json.loads(message.value))