1 # Copyright 2017 iIntel Research and Development Ireland Limited
2 # *************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
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
22 """Tests for all metric request message keys."""
30 from osm_mon
.core
.auth
import AuthManager
31 from osm_mon
.core
.message_bus
.producer
import KafkaProducer
32 from osm_mon
.plugins
.OpenStack
.Gnocchi
import metrics
as metric_req
33 from osm_mon
.plugins
.OpenStack
.common
import Common
35 log
= logging
.getLogger(__name__
)
38 class Message(object):
39 """A class to mock a message object value for metric requests."""
42 """Initialize a mocked message instance."""
43 self
.topic
= "metric_request"
45 self
.value
= json
.dumps({"mock_message": "message_details"})
48 @mock.patch
.object(KafkaProducer
, 'publish', mock
.Mock())
49 class TestMetricReq(unittest
.TestCase
):
50 """Integration test for metric request keys."""
53 """Setup the tests for metric request keys."""
54 super(TestMetricReq
, self
).setUp()
55 self
.metrics
= metric_req
.Metrics()
57 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
58 @mock.patch
.object(Common
, "get_endpoint", mock
.Mock())
59 @mock.patch
.object(metric_req
.Metrics
, "delete_metric")
60 @mock.patch
.object(metric_req
.Metrics
, "get_metric_id")
61 @mock.patch
.object(AuthManager
, "get_credentials")
62 def test_delete_metric_key(self
, get_creds
, get_metric_id
, del_metric
):
63 """Test the functionality for a delete metric request."""
64 # Mock a message value and key
66 message
.key
= "delete_metric_request"
67 message
.value
= json
.dumps({"metric_name": "disk_write_ops", "resource_uuid": "my_r_id", "correlation_id": 1})
69 get_creds
.return_value
= type('obj', (object,), {
70 'config': '{"insecure":true}'
72 del_metric
.return_value
= True
74 # Call the metric functionality and check delete request
75 get_metric_id
.return_value
= "my_metric_id"
76 self
.metrics
.metric_calls(message
, 'test_id')
77 del_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, "my_metric_id", False)
79 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
80 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
81 @mock.patch
.object(metric_req
.Metrics
, "list_metrics")
82 @mock.patch
.object(AuthManager
, "get_credentials")
83 def test_list_metric_key(self
, get_creds
, list_metrics
):
84 """Test the functionality for a list metric request."""
85 # Mock a message with list metric key and value
87 message
.key
= "list_metric_request"
88 message
.value
= json
.dumps({"metrics_list_request": {"correlation_id": 1}})
90 get_creds
.return_value
= type('obj', (object,), {
91 'config': '{"insecure":true}'
94 list_metrics
.return_value
= []
96 # Call the metric functionality and check list functionality
97 self
.metrics
.metric_calls(message
, 'test_id')
98 list_metrics
.assert_called_with(mock
.ANY
, mock
.ANY
, {"correlation_id": 1}, False)
100 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
101 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
102 @mock.patch
.object(metric_req
.Metrics
, "read_metric_data")
103 @mock.patch
.object(metric_req
.Metrics
, "list_metrics")
104 @mock.patch
.object(metric_req
.Metrics
, "delete_metric")
105 @mock.patch
.object(metric_req
.Metrics
, "configure_metric")
106 @mock.patch
.object(AuthManager
, "get_credentials")
107 @mock.patch
.object(Common
, "perform_request")
108 def test_update_metric_key(self
, perf_req
, get_creds
, config_metric
, delete_metric
, list_metrics
,
110 """Test the functionality for an update metric request."""
111 # Mock a message with update metric key and value
113 message
.key
= "update_metric_request"
114 message
.value
= json
.dumps({"metric_create_request":
115 {"correlation_id": 1,
116 "metric_name": "my_metric",
117 "resource_uuid": "my_r_id"}})
119 get_creds
.return_value
= type('obj', (object,), {
120 'config': '{"insecure":true}'
123 perf_req
.return_value
= type('obj', (object,), {'text': '{"metric_id":"1"}'})
125 # Call metric functionality and confirm no function is called
126 # Gnocchi does not support updating a metric configuration
127 self
.metrics
.metric_calls(message
, 'test_id')
128 config_metric
.assert_not_called()
129 list_metrics
.assert_not_called()
130 delete_metric
.assert_not_called()
131 read_data
.assert_not_called()
133 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
134 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
135 @mock.patch
.object(metric_req
.Metrics
, "configure_metric")
136 @mock.patch
.object(AuthManager
, "get_credentials")
137 def test_config_metric_key(self
, get_credentials
, config_metric
):
138 """Test the functionality for a create metric request."""
139 # Mock a message with create metric key and value
141 message
.key
= "create_metric_request"
142 message
.value
= json
.dumps({"metric_create_request": {"correlation_id": 123}})
143 get_credentials
.return_value
= type('obj', (object,), {'config': '{"insecure":true}'})
144 # Call metric functionality and check config metric
145 config_metric
.return_value
= "metric_id", "resource_id", True
146 self
.metrics
.metric_calls(message
, 'test_id')
147 config_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, {"correlation_id": 123}, False)
149 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
150 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
151 @mock.patch
.object(metric_req
.Metrics
, "read_metric_data")
152 @mock.patch
.object(AuthManager
, "get_credentials")
153 def test_read_data_key(self
, get_creds
, read_data
):
154 """Test the functionality for a read metric data request."""
155 # Mock a message with a read data key and value
157 message
.key
= "read_metric_data_request"
158 message
.value
= json
.dumps({"alarm_uuid": "alarm_id"})
160 get_creds
.return_value
= type('obj', (object,), {
161 'config': '{"insecure":true}'
164 # Call metric functionality and check read data metrics
165 read_data
.return_value
= "time_stamps", "data_values"
166 self
.metrics
.metric_calls(message
, 'test_id')
167 read_data
.assert_called_with(
168 mock
.ANY
, mock
.ANY
, json
.loads(message
.value
), False)