Merge "Minor Bug Fixes"
[osm/MON.git] / osm_mon / test / OpenStack / test_alarm_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 alarm 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.Aodh import alarming as alarm_req
33 from osm_mon.plugins.OpenStack.common import Common
34
35 log = logging.getLogger(__name__)
36
37
38 class Message(object):
39 """A class to mock a message object value for alarm requests."""
40
41 def __init__(self):
42 """Initialize a mocked message instance."""
43 self.topic = "alarm_request"
44 self.key = None
45 self.value = json.dumps({"mock_value": "mock_details"})
46
47
48 class TestAlarmKeys(unittest.TestCase):
49 """Integration test for alarm request keys."""
50
51 def setUp(self):
52 """Setup the tests for alarm request keys."""
53 super(TestAlarmKeys, self).setUp()
54 self.alarming = alarm_req.Alarming()
55 self.alarming.common = Common()
56
57 @mock.patch.object(Common, "_authenticate")
58 def test_alarming_env_authentication(self, auth):
59 """Test getting an auth_token and endpoint for alarm requests."""
60 # if auth_token is None environment variables are used to authenticare
61 message = Message()
62
63 self.alarming.alarming(message, self.alarming.common, None)
64
65 auth.assert_called_with()
66
67 @mock.patch.object(Common, "_authenticate")
68 def test_acccess_cred_auth(self, auth):
69 """Test receiving auth_token from access creds."""
70 message = Message()
71
72 self.alarming.alarming(message, self.alarming.common, "my_auth_token")
73
74 auth.assert_not_called
75 self.assertEqual(self.alarming.auth_token, "my_auth_token")
76
77 @mock.patch.object(alarm_req.Alarming, "delete_alarm")
78 def test_delete_alarm_key(self, del_alarm):
79 """Test the functionality for a create alarm request."""
80 # Mock a message value and key
81 message = Message()
82 message.key = "delete_alarm_request"
83 message.value = json.dumps({"alarm_delete_request":
84 {"alarm_uuid": "my_alarm_id"}})
85
86 # Call the alarming functionality and check delete request
87 self.alarming.alarming(message, self.alarming.common, "my_auth_token")
88
89 del_alarm.assert_called_with(mock.ANY, mock.ANY, "my_alarm_id")
90
91 @mock.patch.object(alarm_req.Alarming, "list_alarms")
92 def test_list_alarm_key(self, list_alarm):
93 """Test the functionality for a list alarm request."""
94 # Mock a message with list alarm key and value
95 message = Message()
96 message.key = "list_alarm_request"
97 message.value = json.dumps({"alarm_list_request": "my_alarm_details"})
98
99 # Call the alarming functionality and check list functionality
100 self.alarming.alarming(message, self.alarming.common, "my_auth_token")
101 list_alarm.assert_called_with(mock.ANY, mock.ANY, "my_alarm_details")
102
103 @mock.patch.object(alarm_req.Alarming, "update_alarm_state")
104 def test_ack_alarm_key(self, ack_alarm):
105 """Test the functionality for an acknowledge alarm request."""
106 # Mock a message with acknowledge alarm key and value
107 message = Message()
108 message.key = "acknowledge_alarm"
109 message.value = json.dumps({"ack_details":
110 {"alarm_uuid": "my_alarm_id"}})
111
112 # Call alarming functionality and check acknowledge functionality
113 self.alarming.alarming(message, self.alarming.common, "my_auth_token")
114 ack_alarm.assert_called_with(mock.ANY, mock.ANY, "my_alarm_id")
115
116 @mock.patch.object(alarm_req.Alarming, "configure_alarm")
117 def test_config_alarm_key(self, config_alarm):
118 """Test the functionality for a create alarm request."""
119 # Mock a message with config alarm key and value
120 message = Message()
121 message.key = "create_alarm_request"
122 message.value = json.dumps({"alarm_create_request": "alarm_details"})
123
124 # Call alarming functionality and check config alarm call
125 config_alarm.return_value = "my_alarm_id", True
126 self.alarming.alarming(message, self.alarming.common, "my_auth_token")
127 config_alarm.assert_called_with(mock.ANY, mock.ANY, "alarm_details")