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
.plugins
.OpenStack
.Gnocchi
import metrics
as metric_req
34 from osm_mon
.plugins
.OpenStack
.common
import Common
36 log
= logging
.getLogger(__name__
)
39 class Message(object):
40 """A class to mock a message object value for metric requests."""
43 """Initialize a mocked message instance."""
44 self
.topic
= "metric_request"
46 self
.value
= json
.dumps({"mock_message": "message_details"})
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
.common
= Common()
56 self
.metrics
= metric_req
.Metrics()
58 @mock.patch
.object(Common
, "_authenticate")
59 def test_access_cred_metric_auth(self
, auth
):
60 """Test authentication with access credentials."""
63 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
65 auth
.assert_not_called
66 self
.assertEqual(self
.metrics
.auth_token
, "my_auth_token")
68 @mock.patch
.object(Common
, "_authenticate")
69 def test_env_metric_auth(self
, auth
):
70 """Test authentication with environment variables."""
73 self
.metrics
.metric_calls(message
, self
.common
, None)
75 auth
.assert_called_with()
77 @mock.patch
.object(metric_req
.Metrics
, "delete_metric")
78 def test_delete_metric_key(self
, del_metric
):
79 """Test the functionality for a delete metric request."""
80 # Mock a message value and key
82 message
.key
= "delete_metric_request"
83 message
.value
= json
.dumps({"metric_uuid": "my_metric_id"})
85 # Call the metric functionality and check delete request
86 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
88 del_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, "my_metric_id")
90 @mock.patch
.object(metric_req
.Metrics
, "list_metrics")
91 def test_list_metric_key(self
, list_metrics
):
92 """Test the functionality for a list metric request."""
93 # Mock a message with list metric key and value
95 message
.key
= "list_metric_request"
96 message
.value
= json
.dumps({"metrics_list_request": "metric_details"})
98 # Call the metric functionality and check list functionality
99 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
100 list_metrics
.assert_called_with(mock
.ANY
, mock
.ANY
, "metric_details")
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 def test_update_metric_key(self
, config_metric
, delete_metric
, list_metrics
,
108 """Test the functionality for an update metric request."""
109 # Mock a message with update metric key and value
111 message
.key
= "update_metric_request"
112 message
.value
= json
.dumps({"metric_create":
113 {"metric_name": "my_metric",
114 "resource_uuid": "my_r_id"}})
116 # Call metric functionality and confirm no function is called
117 # Gnocchi does not support updating a metric configuration
118 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
119 config_metric
.assert_not_called
120 list_metrics
.assert_not_called
121 delete_metric
.assert_not_called
122 read_data
.assert_not_called
124 @mock.patch
.object(metric_req
.Metrics
, "configure_metric")
125 def test_config_metric_key(self
, config_metric
):
126 """Test the functionality for a create metric request."""
127 # Mock a message with create metric key and value
129 message
.key
= "create_metric_request"
130 message
.value
= json
.dumps({"metric_create": "metric_details"})
132 # Call metric functionality and check config metric
133 config_metric
.return_value
= "metric_id", "resource_id", True
134 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
135 config_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, "metric_details")
137 @mock.patch
.object(metric_req
.Metrics
, "read_metric_data")
138 def test_read_data_key(self
, read_data
):
139 """Test the functionality for a read metric data request."""
140 # Mock a message with a read data key and value
142 message
.key
= "read_metric_data_request"
143 message
.value
= json
.dumps({"alarm_uuid": "alarm_id"})
145 # Call metric functionality and check read data metrics
146 read_data
.return_value
= "time_stamps", "data_values"
147 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
148 read_data
.assert_called_with(
149 mock
.ANY
, mock
.ANY
, json
.loads(message
.value
))