15cf63b9769e30013feb8238a3db83ff8f406b14
1 # Copyright 2017 iIntel Research and Development Ireland Limited
2 # **************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
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
22 """Tests for all alarm request message keys."""
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
37 log
= logging
.getLogger(__name__
)
39 mock_creds
= VimCredentials()
40 mock_creds
.config
= '{}'
43 class Message(object):
44 """A class to mock a message object value for alarm requests."""
47 """Initialize a mocked message instance."""
48 self
.topic
= 'alarm_request'
50 self
.value
= json
.dumps({'vim_uuid': 'test_id', 'mock_value': 'mock_details'})
53 class TestAlarmKeys(unittest
.TestCase
):
54 """Integration test for alarm request keys."""
57 """Setup the tests for alarm request keys."""
58 super(TestAlarmKeys
, self
).setUp()
59 self
.alarming
= alarm_req
.Alarming()
60 self
.alarming
.common
= Common()
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
70 get_creds
.return_value
= mock_creds
72 self
.alarming
.alarming(message
)
74 get_token
.assert_called_with('test_id')
75 get_endpoint
.assert_any_call('alarming', 'test_id')
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
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'}})
90 get_creds
.return_value
= mock_creds
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')
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
104 message
.key
= 'list_alarm_request'
105 message
.value
= json
.dumps({'vim_uuid': 'test_id', 'alarm_list_request': 'my_alarm_details'})
107 get_creds
.return_value
= mock_creds
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')
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
121 message
.key
= 'acknowledge_alarm'
122 message
.value
= json
.dumps({'vim_uuid': 'test_id',
124 {'alarm_uuid': 'my_alarm_id'}})
126 get_creds
.return_value
= mock_creds
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')
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
140 message
.key
= 'create_alarm_request'
141 message
.value
= json
.dumps({'vim_uuid': 'test_id', 'alarm_create_request': 'alarm_details'})
143 get_creds
.return_value
= mock_creds
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', {})