Implements multivim support in the OpenStack plugin
[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({'vim_uuid': 'test_id', '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, 'get_endpoint')
58 @mock.patch.object(Common, 'get_auth_token')
59 def test_alarming_authentication(self, get_token, get_endpoint):
60 """Test getting an auth_token and endpoint for alarm requests."""
61 # if auth_token is None environment variables are used to authenticate
62 message = Message()
63
64 self.alarming.alarming(message)
65
66 get_token.assert_called_with('test_id')
67 get_endpoint.assert_any_call('alarming', 'test_id')
68
69 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
70 @mock.patch.object(Common, 'get_auth_token', mock.Mock())
71 @mock.patch.object(alarm_req.Alarming, 'delete_alarm')
72 def test_delete_alarm_key(self, del_alarm):
73 """Test the functionality for a create alarm request."""
74 # Mock a message value and key
75 message = Message()
76 message.key = 'delete_alarm_request'
77 message.value = json.dumps({'vim_uuid': 'test_id',
78 'alarm_delete_request':
79 {'alarm_uuid': 'my_alarm_id'}})
80
81 # Call the alarming functionality and check delete request
82 self.alarming.alarming(message)
83 del_alarm.assert_called_with(mock.ANY, mock.ANY, 'my_alarm_id')
84
85 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
86 @mock.patch.object(Common, 'get_auth_token', mock.Mock())
87 @mock.patch.object(alarm_req.Alarming, 'list_alarms')
88 def test_list_alarm_key(self, list_alarm):
89 """Test the functionality for a list alarm request."""
90 # Mock a message with list alarm key and value
91 message = Message()
92 message.key = 'list_alarm_request'
93 message.value = json.dumps({'vim_uuid': 'test_id', 'alarm_list_request': 'my_alarm_details'})
94
95 # Call the alarming functionality and check list functionality
96 self.alarming.alarming(message)
97 list_alarm.assert_called_with(mock.ANY, mock.ANY, 'my_alarm_details')
98
99 @mock.patch.object(Common, 'get_auth_token', mock.Mock())
100 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
101 @mock.patch.object(alarm_req.Alarming, 'update_alarm_state')
102 def test_ack_alarm_key(self, ack_alarm):
103 """Test the functionality for an acknowledge alarm request."""
104 # Mock a message with acknowledge alarm key and value
105 message = Message()
106 message.key = 'acknowledge_alarm'
107 message.value = json.dumps({'vim_uuid': 'test_id',
108 'ack_details':
109 {'alarm_uuid': 'my_alarm_id'}})
110
111 # Call alarming functionality and check acknowledge functionality
112 self.alarming.alarming(message)
113 ack_alarm.assert_called_with(mock.ANY, mock.ANY, 'my_alarm_id')
114
115 @mock.patch.object(Common, 'get_auth_token', mock.Mock())
116 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
117 @mock.patch.object(alarm_req.Alarming, 'configure_alarm')
118 def test_config_alarm_key(self, config_alarm):
119 """Test the functionality for a create alarm request."""
120 # Mock a message with config alarm key and value
121 message = Message()
122 message.key = 'create_alarm_request'
123 message.value = json.dumps({'vim_uuid': 'test_id', 'alarm_create_request': 'alarm_details'})
124
125 # Call alarming functionality and check config alarm call
126 config_alarm.return_value = 'my_alarm_id', True
127 self.alarming.alarming(message)
128 config_alarm.assert_called_with(mock.ANY, mock.ANY, mock.ANY, 'alarm_details')