Adds vdu_id to message bus models
[osm/MON.git] / osm_mon / test / OpenStack / unit / 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.core.auth import AuthManager
33 from osm_mon.core.database import VimCredentials
34 from osm_mon.plugins.OpenStack.Aodh import alarming as alarm_req
35 from osm_mon.plugins.OpenStack.common import Common
36
37 log = logging.getLogger(__name__)
38
39 mock_creds = VimCredentials()
40 mock_creds.config = '{}'
41
42
43 class Message(object):
44 """A class to mock a message object value for alarm requests."""
45
46 def __init__(self):
47 """Initialize a mocked message instance."""
48 self.topic = 'alarm_request'
49 self.key = None
50 self.value = json.dumps({'vim_uuid': 'test_id', 'mock_value': 'mock_details'})
51
52
53 class TestAlarmKeys(unittest.TestCase):
54 """Integration test for alarm request keys."""
55
56 def setUp(self):
57 """Setup the tests for alarm request keys."""
58 super(TestAlarmKeys, self).setUp()
59 self.alarming = alarm_req.Alarming()
60 self.alarming.common = Common()
61
62 @mock.patch.object(AuthManager, 'get_credentials')
63 @mock.patch.object(Common, 'get_endpoint')
64 @mock.patch.object(Common, 'get_auth_token')
65 def test_alarming_authentication(self, get_token, get_endpoint, get_creds):
66 """Test getting an auth_token and endpoint for alarm requests."""
67 # if auth_token is None environment variables are used to authenticate
68 message = Message()
69
70 get_creds.return_value = mock_creds
71
72 self.alarming.alarming(message)
73
74 get_token.assert_called_with('test_id')
75 get_endpoint.assert_any_call('alarming', 'test_id')
76
77 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
78 @mock.patch.object(Common, 'get_auth_token', mock.Mock())
79 @mock.patch.object(AuthManager, 'get_credentials')
80 @mock.patch.object(alarm_req.Alarming, 'delete_alarm')
81 def test_delete_alarm_key(self, del_alarm, get_creds):
82 """Test the functionality for a create alarm request."""
83 # Mock a message value and key
84 message = Message()
85 message.key = 'delete_alarm_request'
86 message.value = json.dumps({'vim_uuid': 'test_id',
87 'alarm_delete_request':
88 {'alarm_uuid': 'my_alarm_id'}})
89
90 get_creds.return_value = mock_creds
91
92 # Call the alarming functionality and check delete request
93 self.alarming.alarming(message)
94 del_alarm.assert_called_with(mock.ANY, mock.ANY, 'my_alarm_id')
95
96 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
97 @mock.patch.object(Common, 'get_auth_token', mock.Mock())
98 @mock.patch.object(AuthManager, 'get_credentials')
99 @mock.patch.object(alarm_req.Alarming, 'list_alarms')
100 def test_list_alarm_key(self, list_alarm, get_creds):
101 """Test the functionality for a list alarm request."""
102 # Mock a message with list alarm key and value
103 message = Message()
104 message.key = 'list_alarm_request'
105 message.value = json.dumps({'vim_uuid': 'test_id', 'alarm_list_request': 'my_alarm_details'})
106
107 get_creds.return_value = mock_creds
108
109 # Call the alarming functionality and check list functionality
110 self.alarming.alarming(message)
111 list_alarm.assert_called_with(mock.ANY, mock.ANY, 'my_alarm_details')
112
113 @mock.patch.object(Common, 'get_auth_token', mock.Mock())
114 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
115 @mock.patch.object(AuthManager, 'get_credentials')
116 @mock.patch.object(alarm_req.Alarming, 'update_alarm_state')
117 def test_ack_alarm_key(self, ack_alarm, get_creds):
118 """Test the functionality for an acknowledge alarm request."""
119 # Mock a message with acknowledge alarm key and value
120 message = Message()
121 message.key = 'acknowledge_alarm'
122 message.value = json.dumps({'vim_uuid': 'test_id',
123 'ack_details':
124 {'alarm_uuid': 'my_alarm_id'}})
125
126 get_creds.return_value = mock_creds
127
128 # Call alarming functionality and check acknowledge functionality
129 self.alarming.alarming(message)
130 ack_alarm.assert_called_with(mock.ANY, mock.ANY, 'my_alarm_id')
131
132 @mock.patch.object(Common, 'get_auth_token', mock.Mock())
133 @mock.patch.object(Common, 'get_endpoint', mock.Mock())
134 @mock.patch.object(AuthManager, 'get_credentials')
135 @mock.patch.object(alarm_req.Alarming, 'configure_alarm')
136 def test_config_alarm_key(self, config_alarm, get_creds):
137 """Test the functionality for a create alarm request."""
138 # Mock a message with config alarm key and value
139 message = Message()
140 message.key = 'create_alarm_request'
141 message.value = json.dumps({'vim_uuid': 'test_id', 'alarm_create_request': 'alarm_details'})
142
143 get_creds.return_value = mock_creds
144
145 # Call alarming functionality and check config alarm call
146 config_alarm.return_value = 'my_alarm_id', True
147 self.alarming.alarming(message)
148 config_alarm.assert_called_with(mock.ANY, mock.ANY, mock.ANY, 'alarm_details', {})