blob: a3ab3dd82364d69edaaaedb641f69436e4fe94a3 [file] [log] [blame]
Helena McGoughfe92f842017-11-17 14:57:08 +00001# 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
24import json
25
26import logging
27
28import unittest
29
30import mock
31
32from plugins.OpenStack.Gnocchi import metrics as metric_req
33
34from plugins.OpenStack.common import Common
35
36__author__ = "Helena McGough"
37
38log = logging.getLogger(__name__)
39
40
41class Message(object):
42 """A class to mock a message object value for metric requests."""
43
44 def __init__(self):
45 """Initialize a mocked message instance."""
46 self.topic = "metric_request"
47 self.key = None
48 self.value = json.dumps({"mock_message": "message_details"})
49
50
51class 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.common = Common()
58 self.metrics = metric_req.Metrics()
59
60 @mock.patch.object(Common, "_authenticate")
61 def test_access_cred_metric_auth(self, auth):
62 """Test authentication with access credentials."""
63 message = Message()
64
65 self.metrics.metric_calls(message, self.common, "my_auth_token")
66
67 auth.assert_not_called
68 self.assertEqual(self.metrics.auth_token, "my_auth_token")
69
70 @mock.patch.object(Common, "_authenticate")
71 def test_env_metric_auth(self, auth):
72 """Test authentication with environment variables."""
73 message = Message()
74
75 self.metrics.metric_calls(message, self.common, None)
76
77 auth.assert_called_with()
78
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
83 message = Message()
84 message.key = "delete_metric_request"
85 message.value = json.dumps({"metric_uuid": "my_metric_id"})
86
87 # Call the metric functionality and check delete request
88 self.metrics.metric_calls(message, self.common, "my_auth_token")
89
90 del_metric.assert_called_with(mock.ANY, mock.ANY, "my_metric_id")
91
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
96 message = Message()
97 message.key = "list_metric_request"
98 message.value = json.dumps({"metrics_list_request": "metric_details"})
99
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")
103
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,
109 read_data):
110 """Test the functionality for an update metric request."""
111 # Mock a message with update metric key and value
112 message = Message()
113 message.key = "update_metric_request"
114 message.value = json.dumps({"metric_create":
115 {"metric_name": "my_metric",
116 "resource_uuid": "my_r_id"}})
117
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
125
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
130 message = Message()
131 message.key = "create_metric_request"
132 message.value = json.dumps({"metric_create": "metric_details"})
133
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")
138
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
143 message = Message()
144 message.key = "read_metric_data_request"
145 message.value = json.dumps({"alarm_uuid": "alarm_id"})
146
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))