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."""
32 from osm_mon
.core
.message_bus
.producer
import KafkaProducer
33 from osm_mon
.plugins
.OpenStack
.Gnocchi
import metrics
as metric_req
35 from osm_mon
.plugins
.OpenStack
.common
import Common
37 log
= logging
.getLogger(__name__
)
40 class Message(object):
41 """A class to mock a message object value for metric requests."""
44 """Initialize a mocked message instance."""
45 self
.topic
= "metric_request"
47 self
.value
= json
.dumps({"mock_message": "message_details"})
50 @mock.patch
.object(KafkaProducer
, 'publish', mock
.Mock())
51 class TestMetricReq(unittest
.TestCase
):
52 """Integration test for metric request keys."""
55 """Setup the tests for metric request keys."""
56 super(TestMetricReq
, self
).setUp()
57 self
.metrics
= metric_req
.Metrics()
59 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
60 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
61 @mock.patch
.object(metric_req
.Metrics
, "delete_metric")
62 @mock.patch
.object(metric_req
.Metrics
, "get_metric_id")
63 def test_delete_metric_key(self
, get_metric_id
, del_metric
):
64 """Test the functionality for a delete metric request."""
65 # Mock a message value and key
67 message
.key
= "delete_metric_request"
68 message
.value
= json
.dumps({"metric_name": "disk_write_ops", "resource_uuid": "my_r_id", "correlation_id": 1})
70 del_metric
.return_value
= True
72 # Call the metric functionality and check delete request
73 get_metric_id
.return_value
= "my_metric_id"
74 self
.metrics
.metric_calls(message
, 'test_id')
75 del_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, "my_metric_id")
77 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
78 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
79 @mock.patch
.object(metric_req
.Metrics
, "list_metrics")
80 def test_list_metric_key(self
, list_metrics
):
81 """Test the functionality for a list metric request."""
82 # Mock a message with list metric key and value
84 message
.key
= "list_metric_request"
85 message
.value
= json
.dumps({"metrics_list_request": {"correlation_id": 1}})
87 list_metrics
.return_value
= []
89 # Call the metric functionality and check list functionality
90 self
.metrics
.metric_calls(message
, 'test_id')
91 list_metrics
.assert_called_with(mock
.ANY
, mock
.ANY
, {"correlation_id": 1})
93 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
94 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
95 @mock.patch
.object(metric_req
.Metrics
, "read_metric_data")
96 @mock.patch
.object(metric_req
.Metrics
, "list_metrics")
97 @mock.patch
.object(metric_req
.Metrics
, "delete_metric")
98 @mock.patch
.object(metric_req
.Metrics
, "configure_metric")
99 def test_update_metric_key(self
, config_metric
, delete_metric
, list_metrics
,
101 """Test the functionality for an update metric request."""
102 # Mock a message with update metric key and value
104 message
.key
= "update_metric_request"
105 message
.value
= json
.dumps({"metric_create_request":
106 {"correlation_id": 1,
107 "metric_name": "my_metric",
108 "resource_uuid": "my_r_id"}})
110 # Call metric functionality and confirm no function is called
111 # Gnocchi does not support updating a metric configuration
112 self
.metrics
.metric_calls(message
, 'test_id')
113 config_metric
.assert_not_called()
114 list_metrics
.assert_not_called()
115 delete_metric
.assert_not_called()
116 read_data
.assert_not_called()
118 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
119 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
120 @mock.patch
.object(metric_req
.Metrics
, "configure_metric")
121 def test_config_metric_key(self
, config_metric
):
122 """Test the functionality for a create metric request."""
123 # Mock a message with create metric key and value
125 message
.key
= "create_metric_request"
126 message
.value
= json
.dumps({"metric_create_request": "metric_details"})
128 # Call metric functionality and check config metric
129 config_metric
.return_value
= "metric_id", "resource_id", True
130 self
.metrics
.metric_calls(message
, 'test_id')
131 config_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, "metric_details")
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
, "read_metric_data")
136 def test_read_data_key(self
, read_data
):
137 """Test the functionality for a read metric data request."""
138 # Mock a message with a read data key and value
140 message
.key
= "read_metric_data_request"
141 message
.value
= json
.dumps({"alarm_uuid": "alarm_id"})
143 # Call metric functionality and check read data metrics
144 read_data
.return_value
= "time_stamps", "data_values"
145 self
.metrics
.metric_calls(message
, 'test_id')
146 read_data
.assert_called_with(
147 mock
.ANY
, mock
.ANY
, json
.loads(message
.value
))