def test_scalyr_timeseries_aligned(monkeypatch, fx_timeseries_aligned): kwargs, res, exp = fx_timeseries_aligned read_key = '123' post = MagicMock() post.return_value.json.return_value = res monkeypatch.setattr('requests.post', post) scalyr = ScalyrWrapper(read_key) result = scalyr.timeseries(**kwargs) assert result == exp query = get_query('facet', kwargs.get('function', 'count'), read_key, **kwargs) query.pop('queryType') final_q = {'token': query.pop('token'), 'queries': [query]} post.assert_called_with(scalyr._ScalyrWrapper__timeseries_url, json=final_q, headers={'Content-Type': 'application/json'})
def test_scalyr_timeseries_end(monkeypatch, begin_and_end): start, end = begin_and_end read_key = '123' post = MagicMock() post.return_value.json.return_value = dict({ 'status': 'success', 'results': [{ 'values': [1] }] }) monkeypatch.setattr('requests.post', post) scalyr = ScalyrWrapper(read_key) scalyr.timeseries('', minutes=start, end=end, align=0) query = get_query('facet', 'count', read_key, **{ 'minutes': start, 'end': end, 'align': 0 }) query.pop('queryType') final_q = {'token': query.pop('token'), 'queries': [query]} post.assert_called_with(scalyr._ScalyrWrapper__timeseries_url, json=final_q, headers={'Content-Type': 'application/json'})
def test_scalyr_facets(monkeypatch, fx_facets): kwargs, res = fx_facets read_key = '123' post = MagicMock() post.return_value.json.return_value = res monkeypatch.setattr('requests.post', post) scalyr = ScalyrWrapper(read_key) result = scalyr.facets(**kwargs) assert result == res if 'minutes' not in kwargs: kwargs['minutes'] = 30 query = get_query('facet', None, read_key, **kwargs) query.pop('function', None) query.pop('buckets', None) query['maxCount'] = kwargs.get('max_count', 5) post.assert_called_with(scalyr._ScalyrWrapper__facet_url, json=query, headers={'Content-Type': 'application/json'})
def test_scalyr_power_query(monkeypatch, fx_power_query): kwargs, request, response, expected_exception = fx_power_query post = MagicMock() post.return_value.json.return_value = response monkeypatch.setattr('requests.post', post) scalyr = ScalyrWrapper(SCALYR_READ_KEY) try: result = scalyr.power_query(**kwargs) assert result == response assert expected_exception is None post.assert_called_with(scalyr._ScalyrWrapper__power_query_url, json=request, headers={ 'Content-Type': 'application/json', 'errorStatus': 'always200' }) except AssertionError: raise except Exception as error: assert isinstance(error, type(expected_exception)) assert error.args == expected_exception.args
def test_scalyr_logs(monkeypatch, fx_logs): kwargs, res, exp = fx_logs read_key = '123' post = MagicMock() post.return_value.json.return_value = res monkeypatch.setattr('requests.post', post) def expected_query(): query = get_query('log', None, read_key, **kwargs) query.pop('function', None) query.pop('buckets', None) query['maxCount'] = kwargs.get('max_count', 100) if 'continuation_token' in kwargs: query['continuationToken'] = kwargs['continuation_token'] if 'columns' in kwargs: query['columns'] = ','.join(kwargs['columns']) if type(kwargs['columns']) is list else kwargs['columns'] return query scalyr = ScalyrWrapper(read_key) try: result = scalyr.logs(**kwargs) assert result == exp query = expected_query() post.assert_called_with( scalyr._ScalyrWrapper__query_url, json=query, headers={'Content-Type': 'application/json', 'errorStatus': 'always200'}) except CheckError as e: if not res: assert '{}'.format(e) == 'query "{}" is not allowed to be blank'.format(kwargs['query']) post.assert_not_called elif 'message' in res: assert '{}'.format(e) == res['message'] query = expected_query() post.assert_called_with( scalyr._ScalyrWrapper__query_url, json=query, headers={'Content-Type': 'application/json', 'errorStatus': 'always200'}) elif not exp: assert '{}'.format(e) == 'No logs or error message was returned from scalyr' query = expected_query() post.assert_called_with( scalyr._ScalyrWrapper__query_url, json=query, headers={'Content-Type': 'application/json', 'errorStatus': 'always200'}) else: raise
def test_scalyr_http_error(monkeypatch, method): post = MagicMock() post.return_value.raise_for_status.side_effect = requests.exceptions.HTTPError monkeypatch.setattr('requests.post', post) scalyr = ScalyrWrapper('123') m, args = method f = getattr(scalyr, m) with pytest.raises(requests.exceptions.HTTPError): f(*args)
def test_scalyr_eu_region(): read_key = '123' region = 'eu' numeric_url = 'https://eu.scalyr.com/api/numericQuery' timeseries_url = 'https://eu.scalyr.com/api/timeseriesQuery' facet_url = 'https://eu.scalyr.com/api/facetQuery' scalyr = ScalyrWrapper(read_key, region) assert numeric_url == scalyr._ScalyrWrapper__numeric_url assert timeseries_url == scalyr._ScalyrWrapper__timeseries_url assert facet_url == scalyr._ScalyrWrapper__facet_url
def test_scalyr_function(monkeypatch, fx_function): kwargs, res, exp = fx_function read_key = '123' post = MagicMock() post.return_value.json.return_value = res monkeypatch.setattr('requests.post', post) scalyr = ScalyrWrapper(read_key) count = scalyr.function(**kwargs) assert count == exp query = get_query('numeric', kwargs['function'], read_key, **kwargs) post.assert_called_with( scalyr._ScalyrWrapper__numeric_url, json=query, headers={'Content-Type': 'application/json'})
def test_scalyr_config_error(monkeypatch): with pytest.raises(ConfigurationError): ScalyrWrapper('')