Merge "Minor Bug Fixes"
[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({"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.common = Common()
56 self.metrics = metric_req.Metrics()
57
58 @mock.patch.object(Common, "_authenticate")
59 def test_access_cred_metric_auth(self, auth):
60 """Test authentication with access credentials."""
61 message = Message()
62
63 self.metrics.metric_calls(message, self.common, "my_auth_token")
64
65 auth.assert_not_called
66 self.assertEqual(self.metrics.auth_token, "my_auth_token")
67
68 @mock.patch.object(Common, "_authenticate")
69 def test_env_metric_auth(self, auth):
70 """Test authentication with environment variables."""
71 message = Message()
72
73 self.metrics.metric_calls(message, self.common, None)
74
75 auth.assert_called_with()
76
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
81 message = Message()
82 message.key = "delete_metric_request"
83 message.value = json.dumps({"metric_uuid": "my_metric_id"})
84
85 # Call the metric functionality and check delete request
86 self.metrics.metric_calls(message, self.common, "my_auth_token")
87
88 del_metric.assert_called_with(mock.ANY, mock.ANY, "my_metric_id")
89
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
94 message = Message()
95 message.key = "list_metric_request"
96 message.value = json.dumps({"metrics_list_request": "metric_details"})
97
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")
101
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,
107 read_data):
108 """Test the functionality for an update metric request."""
109 # Mock a message with update metric key and value
110 message = Message()
111 message.key = "update_metric_request"
112 message.value = json.dumps({"metric_create":
113 {"metric_name": "my_metric",
114 "resource_uuid": "my_r_id"}})
115
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
123
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
128 message = Message()
129 message.key = "create_metric_request"
130 message.value = json.dumps({"metric_create": "metric_details"})
131
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")
136
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
141 message = Message()
142 message.key = "read_metric_data_request"
143 message.value = json.dumps({"alarm_uuid": "alarm_id"})
144
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))