Пример #1
0
    def test_write_auth_failed(self, data, config, collectd, LOGGER, ClientV3,
                               post):
        """Test authentication failure"""

        # tell the auth client to rise an exception
        ClientV3.side_effect = KeystoneException(
            "Missing name 'xxx' in received services", "exception",
            "services list")

        # init instance
        instance = plugin.Plugin(collectd=collectd, config=config)

        # write the value
        instance.write(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
        post.assert_not_called()
Пример #2
0
    def test_exception_runtime_error(self, data, config, collectd, LOGGER,
                                     Writer, ClientV3, post):
        """Test exception raised during write and shutdown"""

        writer = Writer.return_value
        writer.flush.side_effect = RuntimeError('Test shutdown error')

        # init instance
        instance = plugin.Plugin(collectd=collectd, config=config)

        self.assertRaises(RuntimeError, instance.shutdown)
Пример #3
0
    def test_reauthentication(self, data, config, collectd, ClientV3, post,
                              get_metric_id):
        """Test re-authentication"""
        # init instance
        instance = plugin.Plugin(collectd=collectd, config=config)

        # the sender used by the instance

        get_metric_id.return_value = 'my-metric-id'

        # 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"]

        post.return_value = response_ok

        client = ClientV3.return_value
        client.auth_token = 'Test auth token'

        # write the value
        instance.write(data)

        # verify the auth token
        post.assert_called_once_with(mock.ANY,
                                     data=mock.ANY,
                                     headers={
                                         u'Content-type': mock.ANY,
                                         u'X-Auth-Token': 'Test auth token'
                                     },
                                     timeout=1.0)

        # POST response is unauthorized -> new token needs to be acquired
        post.side_effect = [response_unauthorized, response_ok]

        # set a new auth token
        client.auth_token = 'New test auth token'

        instance.write(data)

        # verify the auth token:
        call_list = post.call_args_list
        # POST called three times
        self.assertEqual(len(call_list), 3)

        # the second call contains the old token
        token = call_list[1][1]['headers']['X-Auth-Token']
        self.assertEqual(token, 'Test auth token')
        # the third call contains the new token
        token = call_list[2][1]['headers']['X-Auth-Token']
        self.assertEqual(token, 'New test auth token')
Пример #4
0
    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')

        # ieit instance
        instance = plugin.Plugin(collectd=collectd, config=config)

        # write the value
        self.assertRaises(requests.RequestException, instance.write, data)
Пример #5
0
    def test_write(self, data, config, collectd, ClientV3, post,
                   get_metric_id):
        """Test collectd data writing"""

        auth_client = ClientV3.return_value
        auth_client.get_service_endpoint.return_value = \
            'https://test-gnocchi.tld'

        post.return_value.status_code = common_sender.Sender.HTTP_CREATED
        post.return_value.text = 'Created'

        get_metric_id.return_value = 'my-metric-id'

        # init instance
        instance = plugin.Plugin(collectd=collectd, config=config)

        # write the first value
        instance.write(data)
        collectd.error.assert_not_called()

        # no value has been sent to ceilometer
        post.assert_not_called()

        # send the second value
        instance.write(data)
        collectd.error.assert_not_called()

        # authentication client has been created
        ClientV3.assert_called_once()

        # and values has been sent
        post.assert_called_once_with('https://test-gnocchi.tld' +
                                     '/v1/metric/my-metric-id/measures',
                                     data=match.json([{
                                         "value":
                                         1234,
                                         "timestamp":
                                         "1973-11-29T21:33:09",
                                     }, {
                                         "value":
                                         1234,
                                         "timestamp":
                                         "1973-11-29T21:33:09",
                                     }]),
                                     headers={
                                         'Content-type': 'application/json',
                                         'X-Auth-Token': auth_client.auth_token
                                     },
                                     timeout=1.0)

        # reset post method
        post.reset_mock()

        # write another values
        instance.write(data)
        collectd.error.assert_not_called()

        # nothing has been sent
        post.assert_not_called()

        # call shutdown
        instance.shutdown()

        # no errors
        collectd.error.assert_not_called()

        # previously written value has been sent
        post.assert_called_once_with('https://test-gnocchi.tld' +
                                     '/v1/metric/my-metric-id/measures',
                                     data=match.json([{
                                         "value":
                                         1234,
                                         "timestamp":
                                         "1973-11-29T21:33:09",
                                     }]),
                                     headers={
                                         'Content-type': 'application/json',
                                         'X-Auth-Token': auth_client.auth_token
                                     },
                                     timeout=1.0)