Ejemplo n.º 1
0
 def test_delete(self):
     with mock.patch('requests.Session', return_value=self.mock_session):
         client = warp10client.Warp10Client(write_token=self.write_token,
                                            read_token=self.read_token,
                                            warp10_api_url=self.warp10_url)
         self.assertRaises(NotImplementedError, client.delete,
                           self.metric_write)
Ejemplo n.º 2
0
    def test_get_resp_200(self):
        self.mock_response.status_code = 200
        self.mock_session.post = mock.Mock(return_value=self.mock_response)
        string_args = [
            self.metric_name, self.resource_id, self.project_id, self.app,
            "{}", self.expected_timestamp, self.expected_value
        ]

        resp_content = '[[{{"c":"{}","l":{{"resource_id":"{}",'\
                       '"project_id":"{}",".app":"{}"}},"a":{}'\
                       ',"v":[[{},{}]]}}]]'.format(*string_args)

        self.mock_response.content = resp_content
        expected_tags = {
            'resource_id': str(self.resource_id),
            'project_id': str(self.project_id),
            '.app': self.app
        }

        with mock.patch('requests.Session', return_value=self.mock_session):
            client = warp10client.Warp10Client(write_token=self.write_token,
                                               read_token=self.read_token,
                                               warp10_api_url=self.warp10_url)
            timeserie = client.get(self.metric_get)
            self.assertIsInstance(timeserie, Timeserie)
            self.assertEqual(timeserie.metrics[0].name, self.metric_name)
            self.assertEqual(timeserie.metrics[0].value, self.expected_value)
            self.assertEqual(timeserie.metrics[0]._tags, expected_tags)
            self.assertEqual(timeserie.metrics[0].position.timestamp,
                             self.expected_timestamp)
Ejemplo n.º 3
0
    def test__call__get_method(self):
        with mock.patch('warp10client.client.Warp10Client._get_method',
                        side_effect=warp10client.client.CallException):

            client = warp10client.Warp10Client(write_token=self.write_token,
                                               read_token=self.read_token,
                                               warp10_api_url=self.warp10_url)
            self.assertRaises(warp10client.client.CallException, client._call,
                              self.metric_write)
Ejemplo n.º 4
0
    def test_get_resp_503(self):
        self.mock_response.status_code = 503
        self.mock_session.post = mock.Mock(return_value=self.mock_response)

        with mock.patch('requests.Session', return_value=self.mock_session):
            client = warp10client.Warp10Client(write_token=self.write_token,
                                               read_token=self.read_token,
                                               warp10_api_url=self.warp10_url)
            self.assertRaises(requests.RequestException, client.get,
                              self.metric_get)
Ejemplo n.º 5
0
 def __init__(self, cfg):
     cfg['type'] = 'warp10'
     super().__init__(cfg)
     self.read_token = cfg['read_token']
     self.write_token = cfg['write_token']
     self.global_prefix = cfg.get('global_prefix')
     self.warp10 = warp10client.Warp10Client(
         warp10_api_url=cfg['url'],
         read_token=self.read_token,
         write_token=self.write_token,
     )
Ejemplo n.º 6
0
    def test_exists_non_exist(self):
        mock_response = mock.Mock()
        mock_response.status_code.return_value = 200

        resp_content = '[[]]'
        mock_response.content = resp_content

        with mock.patch('requests.Session',
                        return_value=self.mock_session), mock.patch(
                            'warp10client.client.Warp10Client._call',
                            return_value=mock_response):
            client = warp10client.Warp10Client(write_token=self.write_token,
                                               read_token=self.read_token,
                                               warp10_api_url=self.warp10_url)
            self.assertFalse(client.exists(self.metric_get))
Ejemplo n.º 7
0
    def test_call_fetch(self):
        self.mock_response.status_code = 200
        self.mock_session.post = mock.Mock(return_value=self.mock_response)
        expected_url = self.warp10_url + '/exec'
        expected_headers = {
            'X-Warp10-Token': self.read_token,
        }

        with mock.patch('requests.Session', return_value=self.mock_session):
            client = warp10client.Warp10Client(write_token=self.write_token,
                                               read_token=self.read_token,
                                               warp10_api_url=self.warp10_url)
            client._call(self.metric_get, call_type='fetch')
            self.mock_session.post.assert_called_with(expected_url,
                                                      data=mock.ANY,
                                                      headers=expected_headers)
Ejemplo n.º 8
0
    def test_call_ingress(self):
        self.mock_response.status_code = 200
        self.mock_session.post = mock.Mock(return_value=self.mock_response)
        expected_url = self.warp10_url + '/update'
        expected_headers = {
            'X-Warp10-Token': self.write_token,
            'Content-Type': 'text/plain'
        }

        with mock.patch('requests.Session', return_value=self.mock_session):
            client = warp10client.Warp10Client(write_token=self.write_token,
                                               read_token=self.read_token,
                                               warp10_api_url=self.warp10_url)
            client._call(self.metric_write, call_type='ingress')
            self.mock_session.post.assert_called_with(expected_url,
                                                      data=mock.ANY,
                                                      headers=expected_headers)
Ejemplo n.º 9
0
    def test_exists_metric_exists(self):
        mock_response = mock.Mock()
        mock_response.status_code.return_value = 200
        string_args = [
            self.metric_name, self.resource_id, self.project_id, self.app,
            "{}", self.expected_timestamp, self.expected_value
        ]

        resp_content = '[[{{"c":"{}","l":{{"resource_id":"{}",'\
                       '"project_id":"{}",".app":"{}"}},"a":{}'\
                       ',"v":[[{},{}]]}}]]'.format(*string_args)

        mock_response.content = resp_content

        with mock.patch('requests.Session',
                        return_value=self.mock_session), mock.patch(
                            'warp10client.client.Warp10Client._call',
                            return_value=mock_response):
            client = warp10client.Warp10Client(write_token=self.write_token,
                                               read_token=self.read_token,
                                               warp10_api_url=self.warp10_url)
            self.assertTrue(client.exists(self.metric_get))
Ejemplo n.º 10
0
# To check metrics
metric_check = {
    'name': 'cpu_util',
    'tags': {
        'resource_id': '18d94676-077c-4c13-b000-27fd603f3056',
        'project_id': '8069f876e7d444249ef04b9a74090711',
    },
}

# arguments need to authorize in metrics backend
kwargs = {
    'write_token': write_token,
    'read_token': read_token,
    'warp10_api_url': warp10_api_url,
}

client = warp10client.Warp10Client(**kwargs)

# Consider to create timeseries, new object with included metrics as each point
# Thats goooood idea.
metric_get_test = client.get(metric_get)

metric_exists = client.exists(metric_check)

metric_obj = warp10client.Metric(**metric_write)

metric_send = client.set(metric_write)

# delete method is not yet implemented
# metric_send = client.delete(metric_write)