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({"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
, "_authenticate")
58 def test_alarming_env_authentication(self
, auth
):
59 """Test getting an auth_token and endpoint for alarm requests."""
60 # if auth_token is None environment variables are used to authenticare
63 self
.alarming
.alarming(message
, self
.alarming
.common
, None)
65 auth
.assert_called_with()
67 @mock.patch
.object(Common
, "_authenticate")
68 def test_acccess_cred_auth(self
, auth
):
69 """Test receiving auth_token from access creds."""
72 self
.alarming
.alarming(message
, self
.alarming
.common
, "my_auth_token")
74 auth
.assert_not_called
75 self
.assertEqual(self
.alarming
.auth_token
, "my_auth_token")
77 @mock.patch
.object(alarm_req
.Alarming
, "delete_alarm")
78 def test_delete_alarm_key(self
, del_alarm
):
79 """Test the functionality for a create alarm request."""
80 # Mock a message value and key
82 message
.key
= "delete_alarm_request"
83 message
.value
= json
.dumps({"alarm_delete_request":
84 {"alarm_uuid": "my_alarm_id"}})
86 # Call the alarming functionality and check delete request
87 self
.alarming
.alarming(message
, self
.alarming
.common
, "my_auth_token")
89 del_alarm
.assert_called_with(mock
.ANY
, mock
.ANY
, "my_alarm_id")
91 @mock.patch
.object(alarm_req
.Alarming
, "list_alarms")
92 def test_list_alarm_key(self
, list_alarm
):
93 """Test the functionality for a list alarm request."""
94 # Mock a message with list alarm key and value
96 message
.key
= "list_alarm_request"
97 message
.value
= json
.dumps({"alarm_list_request": "my_alarm_details"})
99 # Call the alarming functionality and check list functionality
100 self
.alarming
.alarming(message
, self
.alarming
.common
, "my_auth_token")
101 list_alarm
.assert_called_with(mock
.ANY
, mock
.ANY
, "my_alarm_details")
103 @mock.patch
.object(alarm_req
.Alarming
, "update_alarm_state")
104 def test_ack_alarm_key(self
, ack_alarm
):
105 """Test the functionality for an acknowledge alarm request."""
106 # Mock a message with acknowledge alarm key and value
108 message
.key
= "acknowledge_alarm"
109 message
.value
= json
.dumps({"ack_details":
110 {"alarm_uuid": "my_alarm_id"}})
112 # Call alarming functionality and check acknowledge functionality
113 self
.alarming
.alarming(message
, self
.alarming
.common
, "my_auth_token")
114 ack_alarm
.assert_called_with(mock
.ANY
, mock
.ANY
, "my_alarm_id")
116 @mock.patch
.object(alarm_req
.Alarming
, "configure_alarm")
117 def test_config_alarm_key(self
, config_alarm
):
118 """Test the functionality for a create alarm request."""
119 # Mock a message with config alarm key and value
121 message
.key
= "create_alarm_request"
122 message
.value
= json
.dumps({"alarm_create_request": "alarm_details"})
124 # Call alarming functionality and check config alarm call
125 config_alarm
.return_value
= "my_alarm_id", True
126 self
.alarming
.alarming(message
, self
.alarming
.common
, "my_auth_token")
127 config_alarm
.assert_called_with(mock
.ANY
, mock
.ANY
, "alarm_details")