Adds support for vdu_name, ns_id and vnf_member_index
[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.core.message_bus.producer import KafkaProducer
33 from osm_mon.plugins.OpenStack.Gnocchi import metrics as metric_req
34
35 from osm_mon.plugins.OpenStack.common import Common
36
37 log = logging.getLogger(__name__)
38
39
40 class Message(object):
41 """A class to mock a message object value for metric requests."""
42
43 def __init__(self):
44 """Initialize a mocked message instance."""
45 self.topic = "metric_request"
46 self.key = None
47 self.value = json.dumps({"mock_message": "message_details"})
48
49
50 @mock.patch.object(KafkaProducer, 'publish', mock.Mock())
51 class TestMetricReq(unittest.TestCase):
52 """Integration test for metric request keys."""
53
54 def setUp(self):
55 """Setup the tests for metric request keys."""
56 super(TestMetricReq, self).setUp()
57 self.metrics = metric_req.Metrics()
58
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
66 message = Message()
67 message.key = "delete_metric_request"
68 message.value = json.dumps({"metric_name": "disk_write_ops", "resource_uuid": "my_r_id", "correlation_id": 1})
69
70 del_metric.return_value = True
71
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")
76
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
83 message = Message()
84 message.key = "list_metric_request"
85 message.value = json.dumps({"metrics_list_request": {"correlation_id": 1}})
86
87 list_metrics.return_value = []
88
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})
92
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,
100 read_data):
101 """Test the functionality for an update metric request."""
102 # Mock a message with update metric key and value
103 message = Message()
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"}})
109
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()
117
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
124 message = Message()
125 message.key = "create_metric_request"
126 message.value = json.dumps({"metric_create_request": "metric_details"})
127
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")
132
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
139 message = Message()
140 message.key = "read_metric_data_request"
141 message.value = json.dumps({"alarm_uuid": "alarm_id"})
142
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))