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
.plugins
.OpenStack
.Aodh
import alarming
as alarm_req
33 from osm_mon
.plugins
.OpenStack
.common
import Common
35 log
= logging
.getLogger(__name__
)
38 class Message(object):
39 """A class to mock a message object value for alarm requests."""
42 """Initialize a mocked message instance."""
43 self
.topic
= 'alarm_request'
45 self
.value
= json
.dumps({'vim_uuid': 'test_id', 'mock_value': 'mock_details'})
48 class TestAlarmKeys(unittest
.TestCase
):
49 """Integration test for alarm request keys."""
52 """Setup the tests for alarm request keys."""
53 super(TestAlarmKeys
, self
).setUp()
54 self
.alarming
= alarm_req
.Alarming()
55 self
.alarming
.common
= Common()
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
64 self
.alarming
.alarming(message
)
66 get_token
.assert_called_with('test_id')
67 get_endpoint
.assert_any_call('alarming', 'test_id')
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
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'}})
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')
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
92 message
.key
= 'list_alarm_request'
93 message
.value
= json
.dumps({'vim_uuid': 'test_id', 'alarm_list_request': 'my_alarm_details'})
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')
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
106 message
.key
= 'acknowledge_alarm'
107 message
.value
= json
.dumps({'vim_uuid': 'test_id',
109 {'alarm_uuid': 'my_alarm_id'}})
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')
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
122 message
.key
= 'create_alarm_request'
123 message
.value
= json
.dumps({'vim_uuid': 'test_id', 'alarm_create_request': 'alarm_details'})
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')