def test_update_alarm_no_id(self, data, config, collectd, ClientV3, put, _get_alarm_id, _create_alarm): """Test if the is no alarm id the alarm won't be updated. Set-up: create a client and an instance to send an update to throw a side-effect when looking for an id Test: send a notification for a new alarm Expected behaviour: - if an alarm is create an update request is not performed """ auth_client = ClientV3.return_value auth_client.get_service_endpoint.return_value = \ 'https://test-aodh.tld' instance = plugin.Plugin(collectd=collectd, config=config) # init values to send _get_alarm_id.return_value = None _create_alarm.return_value = 'my-alarm-id' # try and perform an update without an id instance.notify(data) put.assert_not_called() put.reset_mock()
def test_update_alarm(self, data, config, collectd, ClientV3, put, _get_alarm_state, _get_alarm_id): """Test the update alarm function. Set-up: get an alarm-id for some notification values to be sent Test: perform an update request Expected behaviour: - If alarm-id is present a put request is performed """ auth_client = ClientV3.return_value auth_client.get_service_endpoint.return_value = \ 'https://test-aodh.tld' # init instance instance = plugin.Plugin(collectd=collectd, config=config) # init values to send _get_alarm_id.return_value = 'my-alarm-id' _get_alarm_state.return_value = 'insufficient data' # notify aodh of the update instance.notify(data) # update the alarm with a put request put.assert_called_once_with('https://test-aodh.tld' + '/v2/alarms/my-alarm-id/state', data='"insufficient data"', headers={ 'Content-type': 'application/json', 'X-Auth-Token': auth_client.auth_token }, timeout=1.0) # reset method put.reset_mock()
def test_exception_value_error(self, data, config, collectd, LOGGER, Notifier, ClientV3): """Test exception raised during notify and shutdown.""" notifier = Notifier.return_value notifier.notify.side_effect = ValueError('Test notify error') # init instance instance = plugin.Plugin(collectd=collectd, config=config) self.assertRaises(ValueError, instance.notify, data)
def test_request_error(self, data, config, collectd, ClientV3, perf_req): """Test error raised by underlying requests module.""" # tell POST request to raise an exception perf_req.side_effect = requests.RequestException('Test POST exception') # init instance instance = plugin.Plugin(collectd=collectd, config=config) # the value self.assertRaises(requests.RequestException, instance.notify, data)
def test_reauthentication(self, data, config, collectd, ClientV3, put, _get_alarm_id, _get_alarm_state): """Test re-authentication for update request.""" # response returned on success response_ok = requests.Response() response_ok.status_code = requests.codes["OK"] # response returned on failure response_unauthorized = requests.Response() response_unauthorized.status_code = requests.codes["UNAUTHORIZED"] # set-up client client = ClientV3.return_value client.auth_token = 'Test auth token' client.get_service_endpoint.return_value = \ 'https://test-aodh.tld' # init instance attempt to update/create alarm instance = plugin.Plugin(collectd=collectd, config=config) put.return_value = response_ok _get_alarm_id.return_value = 'my-alarm-id' _get_alarm_state.return_value = 'insufficient data' # send notification to aodh instance.notify(data) # put/update is called put.assert_called_once_with('https://test-aodh.tld' + '/v2/alarms/my-alarm-id/state', data='"insufficient data"', headers={ u'Content-type': 'application/json', u'X-Auth-Token': 'Test auth token' }, timeout=1.0)
def test_notify_auth_failed(self, data, config, collectd, LOGGER, ClientV3, put): """Test authentication failure.""" # tell the auth client to raise an exception ClientV3.side_effect = KeystoneException( "Missing name 'xxx' in received services", "exception", "services list") # init instance instance = plugin.Plugin(collectd=collectd, config=config) # notify of another value the value instance.notify(data) LOGGER.error.assert_called_once_with( "Suspending error logs until successful auth") LOGGER.log.assert_called_once_with( logging.ERROR, "Authentication error: %s", "Missing name 'xxx' in received services\nReason: exception", exc_info=0) # no requests method has been called put.assert_not_called()
def test_exception_runtime_error(self, config, collectd, LOGGER, ClientV3): """Test exception raised during shutdown.""" # init instance instance = plugin.Plugin(collectd=collectd, config=config) instance.shutdown