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
, DatabaseManager
34 from osm_mon
.core
.message_bus
.producer
import KafkaProducer
35 from osm_mon
.plugins
.OpenStack
.Aodh
import alarming
as alarm_req
36 from osm_mon
.plugins
.OpenStack
.common
import Common
38 log
= logging
.getLogger(__name__
)
40 mock_creds
= VimCredentials()
41 mock_creds
.config
= '{}'
44 class Message(object):
45 """A class to mock a message object value for alarm requests."""
48 """Initialize a mocked message instance."""
49 self
.topic
= 'alarm_request'
51 self
.value
= json
.dumps({'mock_value': 'mock_details'})
54 @mock.patch
.object(KafkaProducer
, 'publish', mock
.Mock())
55 class TestAlarmKeys(unittest
.TestCase
):
56 """Integration test for alarm request keys."""
59 """Setup the tests for alarm request keys."""
60 super(TestAlarmKeys
, self
).setUp()
61 self
.alarming
= alarm_req
.Alarming()
62 self
.alarming
.common
= Common()
64 @mock.patch
.object(AuthManager
, 'get_credentials')
65 @mock.patch
.object(Common
, 'get_endpoint')
66 @mock.patch
.object(Common
, 'get_auth_token')
67 def test_alarming_authentication(self
, get_token
, get_endpoint
, get_creds
):
68 """Test getting an auth_token and endpoint for alarm requests."""
69 # if auth_token is None environment variables are used to authenticate
72 get_creds
.return_value
= mock_creds
74 self
.alarming
.alarming(message
, 'test_id')
76 get_token
.assert_called_with('test_id', verify_ssl
=True)
77 get_endpoint
.assert_any_call('alarming', 'test_id', verify_ssl
=True)
79 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
80 @mock.patch
.object(Common
, 'get_auth_token', mock
.Mock())
81 @mock.patch
.object(AuthManager
, 'get_credentials')
82 @mock.patch
.object(alarm_req
.Alarming
, 'delete_alarm')
83 def test_delete_alarm_key(self
, del_alarm
, get_creds
):
84 """Test the functionality for a create alarm request."""
85 # Mock a message value and key
87 message
.key
= 'delete_alarm_request'
88 message
.value
= json
.dumps({'alarm_delete_request': {
90 'alarm_uuid': 'my_alarm_id'
93 get_creds
.return_value
= mock_creds
94 del_alarm
.return_value
= {}
96 # Call the alarming functionality and check delete request
97 self
.alarming
.alarming(message
, 'test_id')
98 del_alarm
.assert_called_with(mock
.ANY
, mock
.ANY
, 'my_alarm_id', True)
100 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
101 @mock.patch
.object(Common
, 'get_auth_token', mock
.Mock())
102 @mock.patch
.object(AuthManager
, 'get_credentials')
103 @mock.patch
.object(alarm_req
.Alarming
, 'list_alarms')
104 def test_list_alarm_key(self
, list_alarm
, get_creds
):
105 """Test the functionality for a list alarm request."""
106 # Mock a message with list alarm key and value
108 message
.key
= 'list_alarm_request'
109 message
.value
= json
.dumps({'alarm_list_request': {'correlation_id': 1}})
111 get_creds
.return_value
= mock_creds
113 list_alarm
.return_value
= []
115 # Call the alarming functionality and check list functionality
116 self
.alarming
.alarming(message
, 'test_id')
117 list_alarm
.assert_called_with(mock
.ANY
, mock
.ANY
, {'correlation_id': 1}, True)
119 @mock.patch
.object(Common
, 'get_auth_token', mock
.Mock())
120 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
121 @mock.patch
.object(AuthManager
, 'get_credentials')
122 @mock.patch
.object(alarm_req
.Alarming
, 'update_alarm_state')
123 def test_ack_alarm_key(self
, ack_alarm
, get_creds
):
124 """Test the functionality for an acknowledge alarm request."""
125 # Mock a message with acknowledge alarm key and value
127 message
.key
= 'acknowledge_alarm'
128 message
.value
= json
.dumps({'ack_details':
129 {'alarm_uuid': 'my_alarm_id'}})
131 get_creds
.return_value
= mock_creds
133 # Call alarming functionality and check acknowledge functionality
134 self
.alarming
.alarming(message
, 'test_id')
135 ack_alarm
.assert_called_with(mock
.ANY
, mock
.ANY
, 'my_alarm_id', True)
137 @mock.patch
.object(Common
, 'get_auth_token', mock
.Mock())
138 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
139 @mock.patch
.object(DatabaseManager
, 'save_alarm', mock
.Mock())
140 @mock.patch
.object(Common
, "perform_request")
141 @mock.patch
.object(AuthManager
, 'get_credentials')
142 @mock.patch
.object(alarm_req
.Alarming
, 'configure_alarm')
143 def test_config_alarm_key(self
, config_alarm
, get_creds
, perf_req
):
144 """Test the functionality for a create alarm request."""
145 # Mock a message with config alarm key and value
147 message
.key
= 'create_alarm_request'
148 message
.value
= json
.dumps({'alarm_create_request': {'correlation_id': 1, 'threshold_value': 50,
149 'operation': 'GT', 'metric_name': 'cpu_utilization',
151 'vnf_member_index': '1',
153 'resource_uuid': '123'}})
154 mock_perf_req_return_value
= {"metrics": {"cpu_util": 123}}
155 perf_req
.return_value
= type('obj', (object,), {'text': json
.dumps(mock_perf_req_return_value
, sort_keys
=True)})
156 get_creds
.return_value
= mock_creds
158 # Call alarming functionality and check config alarm call
159 config_alarm
.return_value
= 'my_alarm_id'
160 self
.alarming
.alarming(message
, 'test_id')
161 config_alarm
.assert_called_with(mock
.ANY
, mock
.ANY
, {'correlation_id': 1, 'threshold_value': 50,
163 'metric_name': 'cpu_utilization',
165 'vnf_member_index': '1', 'ns_id': '1',
166 'resource_uuid': '123'}, {}, True)