Adds vdu_id to message bus models
[osm/MON.git] / osm_mon / test / OpenStack / unit / 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({"vim_uuid": "test_id", "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.metrics = metric_req.Metrics()
56
57 @mock.patch.object(Common, 'get_endpoint')
58 @mock.patch.object(Common, "get_auth_token")
59 def test_access_cred_metric_auth(self, get_token, get_endpoint):
60 """Test authentication with access credentials."""
61 message = Message()
62
63 self.metrics.metric_calls(message)
64
65 get_token.assert_called_with('test_id')
66 get_endpoint.assert_any_call('metric', 'test_id')
67
68 @mock.patch.object(Common, "get_auth_token", mock.Mock())
69 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
70 @mock.patch.object(metric_req.Metrics, "delete_metric")
71 @mock.patch.object(metric_req.Metrics, "get_metric_id")
72 def test_delete_metric_key(self, get_metric_id, del_metric):
73 """Test the functionality for a delete metric request."""
74 # Mock a message value and key
75 message = Message()
76 message.key = "delete_metric_request"
77 message.value = json.dumps({"vim_uuid": "test_id", "metric_name": "disk_write_ops", "resource_uuid": "my_r_id"})
78
79 # Call the metric functionality and check delete request
80 get_metric_id.return_value = "my_metric_id"
81 self.metrics.metric_calls(message)
82 del_metric.assert_called_with(mock.ANY, mock.ANY, "my_metric_id")
83
84 @mock.patch.object(Common, "get_auth_token", mock.Mock())
85 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
86 @mock.patch.object(metric_req.Metrics, "list_metrics")
87 def test_list_metric_key(self, list_metrics):
88 """Test the functionality for a list metric request."""
89 # Mock a message with list metric key and value
90 message = Message()
91 message.key = "list_metric_request"
92 message.value = json.dumps({"vim_uuid": "test_id", "metrics_list_request": "metric_details"})
93
94 # Call the metric functionality and check list functionality
95 self.metrics.metric_calls(message)
96 list_metrics.assert_called_with(mock.ANY, mock.ANY, "metric_details")
97
98 @mock.patch.object(Common, "get_auth_token", mock.Mock())
99 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
100 @mock.patch.object(metric_req.Metrics, "read_metric_data")
101 @mock.patch.object(metric_req.Metrics, "list_metrics")
102 @mock.patch.object(metric_req.Metrics, "delete_metric")
103 @mock.patch.object(metric_req.Metrics, "configure_metric")
104 def test_update_metric_key(self, config_metric, delete_metric, list_metrics,
105 read_data):
106 """Test the functionality for an update metric request."""
107 # Mock a message with update metric key and value
108 message = Message()
109 message.key = "update_metric_request"
110 message.value = json.dumps({"vim_uuid": "test_id",
111 "metric_create":
112 {"metric_name": "my_metric",
113 "resource_uuid": "my_r_id"}})
114
115 # Call metric functionality and confirm no function is called
116 # Gnocchi does not support updating a metric configuration
117 self.metrics.metric_calls(message)
118 config_metric.assert_not_called()
119 list_metrics.assert_not_called()
120 delete_metric.assert_not_called()
121 read_data.assert_not_called()
122
123 @mock.patch.object(Common, "get_auth_token", mock.Mock())
124 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
125 @mock.patch.object(metric_req.Metrics, "configure_metric")
126 def test_config_metric_key(self, config_metric):
127 """Test the functionality for a create metric request."""
128 # Mock a message with create metric key and value
129 message = Message()
130 message.key = "create_metric_request"
131 message.value = json.dumps({"vim_uuid": "test_id", "metric_create": "metric_details"})
132
133 # Call metric functionality and check config metric
134 config_metric.return_value = "metric_id", "resource_id", True
135 self.metrics.metric_calls(message)
136 config_metric.assert_called_with(mock.ANY, mock.ANY, "metric_details")
137
138 @mock.patch.object(Common, "get_auth_token", mock.Mock())
139 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
140 @mock.patch.object(metric_req.Metrics, "read_metric_data")
141 def test_read_data_key(self, read_data):
142 """Test the functionality for a read metric data request."""
143 # Mock a message with a read data key and value
144 message = Message()
145 message.key = "read_metric_data_request"
146 message.value = json.dumps({"vim_uuid": "test_id", "alarm_uuid": "alarm_id"})
147
148 # Call metric functionality and check read data metrics
149 read_data.return_value = "time_stamps", "data_values"
150 self.metrics.metric_calls(message)
151 read_data.assert_called_with(
152 mock.ANY, mock.ANY, json.loads(message.value))