a3ab3dd82364d69edaaaedb641f69436e4fe94a3
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 plugins
.OpenStack
.Gnocchi
import metrics
as metric_req
34 from plugins
.OpenStack
.common
import Common
36 __author__
= "Helena McGough"
38 log
= logging
.getLogger(__name__
)
41 class Message(object):
42 """A class to mock a message object value for metric requests."""
45 """Initialize a mocked message instance."""
46 self
.topic
= "metric_request"
48 self
.value
= json
.dumps({"mock_message": "message_details"})
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
.common
= Common()
58 self
.metrics
= metric_req
.Metrics()
60 @mock.patch
.object(Common
, "_authenticate")
61 def test_access_cred_metric_auth(self
, auth
):
62 """Test authentication with access credentials."""
65 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
67 auth
.assert_not_called
68 self
.assertEqual(self
.metrics
.auth_token
, "my_auth_token")
70 @mock.patch
.object(Common
, "_authenticate")
71 def test_env_metric_auth(self
, auth
):
72 """Test authentication with environment variables."""
75 self
.metrics
.metric_calls(message
, self
.common
, None)
77 auth
.assert_called_with()
79 @mock.patch
.object(metric_req
.Metrics
, "delete_metric")
80 def test_delete_metric_key(self
, del_metric
):
81 """Test the functionality for a delete metric request."""
82 # Mock a message value and key
84 message
.key
= "delete_metric_request"
85 message
.value
= json
.dumps({"metric_uuid": "my_metric_id"})
87 # Call the metric functionality and check delete request
88 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
90 del_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, "my_metric_id")
92 @mock.patch
.object(metric_req
.Metrics
, "list_metrics")
93 def test_list_metric_key(self
, list_metrics
):
94 """Test the functionality for a list metric request."""
95 # Mock a message with list metric key and value
97 message
.key
= "list_metric_request"
98 message
.value
= json
.dumps({"metrics_list_request": "metric_details"})
100 # Call the metric functionality and check list functionality
101 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
102 list_metrics
.assert_called_with(mock
.ANY
, mock
.ANY
, "metric_details")
104 @mock.patch
.object(metric_req
.Metrics
, "read_metric_data")
105 @mock.patch
.object(metric_req
.Metrics
, "list_metrics")
106 @mock.patch
.object(metric_req
.Metrics
, "delete_metric")
107 @mock.patch
.object(metric_req
.Metrics
, "configure_metric")
108 def test_update_metric_key(self
, 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":
115 {"metric_name": "my_metric",
116 "resource_uuid": "my_r_id"}})
118 # Call metric functionality and confirm no function is called
119 # Gnocchi does not support updating a metric configuration
120 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
121 config_metric
.assert_not_called
122 list_metrics
.assert_not_called
123 delete_metric
.assert_not_called
124 read_data
.assert_not_called
126 @mock.patch
.object(metric_req
.Metrics
, "configure_metric")
127 def test_config_metric_key(self
, config_metric
):
128 """Test the functionality for a create metric request."""
129 # Mock a message with create metric key and value
131 message
.key
= "create_metric_request"
132 message
.value
= json
.dumps({"metric_create": "metric_details"})
134 # Call metric functionality and check config metric
135 config_metric
.return_value
= "metric_id", "resource_id", True
136 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
137 config_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, "metric_details")
139 @mock.patch
.object(metric_req
.Metrics
, "read_metric_data")
140 def test_read_data_key(self
, read_data
):
141 """Test the functionality for a read metric data request."""
142 # Mock a message with a read data key and value
144 message
.key
= "read_metric_data_request"
145 message
.value
= json
.dumps({"alarm_uuid": "alarm_id"})
147 # Call metric functionality and check read data metrics
148 read_data
.return_value
= "time_stamps", "data_values"
149 self
.metrics
.metric_calls(message
, self
.common
, "my_auth_token")
150 read_data
.assert_called_with(
151 mock
.ANY
, mock
.ANY
, json
.loads(message
.value
))