def test_elasticsearch_search_error(monkeypatch):
    resp = resp_mock()

    get = requests_mock(resp, failure=requests.Timeout)
    monkeypatch.setattr('requests.get', get)

    url = 'http://es/'
    es = ElasticsearchWrapper(url)

    with pytest.raises(HttpError):
        es.search()

    get = requests_mock(resp, failure=requests.ConnectionError)
    monkeypatch.setattr('requests.get', get)

    with pytest.raises(HttpError):
        es.search()

    # Unhandled exception
    get = requests_mock(resp, failure=Exception)
    monkeypatch.setattr('requests.get', get)

    with pytest.raises(Exception) as ex:
        es.search()

        assert ex is not HttpError
示例#2
0
    def count_logs(self, q='', body=None, duration_in_mins=5):
        """
        Perform count query on AppDynamics ES logs.

        :param q: Query string used in search.
        :type q: str

        :param body: (dict) holding an ES query DSL.
        :type body: dict

        :param duration_in_mins: Duration in mins before current time. Default is 5 mins.
        :type duration_in_mins: int

        :return: Query match count.
        :rtype: int
        """
        if not self.es_url:
            raise RuntimeError(
                'AppDynamics plugin improperly configured. ES URL is required to query logs!'
            )

        q = self.__get_search_q(q, duration_in_mins)

        indices = ['{}*'.format(self.index_prefix)]

        res = ElasticsearchWrapper(url=self.es_url,
                                   oauth2=self.__oauth2).count(indices=indices,
                                                               q=q,
                                                               body=body)

        logger.debug('Received ES count result: {}'.format(res))

        return res['count']
def test_elasticsearch_search_no_source_with_size(monkeypatch):
    resp = resp_mock()
    get = requests_mock(resp)
    monkeypatch.setattr('requests.get', get)

    url = 'http://es/'
    es = ElasticsearchWrapper(url)

    result = es.search(source=False, size=100)

    assert result == resp.json.return_value

    get.assert_called_with(get_full_url(url),
                           headers={'User-Agent': get_user_agent()},
                           params={'size': 100, '_source': 'false'},
                           timeout=10)
def test_elasticsearch_count(monkeypatch):
    resp = resp_mock()
    get = requests_mock(resp)
    monkeypatch.setattr('requests.get', get)

    url = 'http://es/'
    es = ElasticsearchWrapper(url)

    q = 'status:404'
    result = es.count(q=q)

    assert result == resp.json.return_value

    get.assert_called_with(get_full_url(url, query_type=TYPE_COUNT),
                           headers={'User-Agent': get_user_agent()},
                           params={'q': q},
                           timeout=10)
def test_elasticsearch_search(monkeypatch):
    resp = resp_mock()
    get = requests_mock(resp)
    monkeypatch.setattr('requests.get', get)

    url = 'http://es/'
    es = ElasticsearchWrapper(url)

    q = 'my-search'
    result = es.search(q=q)

    assert result == resp.json.return_value

    get.assert_called_with(get_full_url(url),
                           headers={'User-Agent': get_user_agent()},
                           params={'q': q, 'size': DEFAULT_SIZE, '_source': 'true'},
                           timeout=10)
def test_elasticsearch_health(monkeypatch):
    resp = resp_mock()
    get = requests_mock(resp)
    monkeypatch.setattr('requests.get', get)

    # mock tokens
    token = 'TOKEN-123'
    monkeypatch.setattr('tokens.get', lambda x: token)

    url = 'http://es/'
    es = ElasticsearchWrapper(url, oauth2=True)

    result = es.health()

    assert result == resp.json.return_value

    get.assert_called_with(get_full_url(url, health=True),
                           headers={'User-Agent': get_user_agent(), 'Authorization': 'Bearer {}'.format(token)},
                           params=None,
                           timeout=10)
def test_elasticsearch_search_body(monkeypatch):
    resp = resp_mock()
    post = requests_mock(resp)
    monkeypatch.setattr('requests.post', post)

    url = 'http://es/'
    es = ElasticsearchWrapper(url)

    body = {'query': {'query_string': {'query': ''}}}
    result = es.search(body=body)

    assert result == resp.json.return_value

    body['size'] = DEFAULT_SIZE

    post.assert_called_with(get_full_url(url),
                            params={},
                            json=body,
                            headers={'User-Agent': get_user_agent()},
                            timeout=10)
def test_elasticsearch_search_no_source_body_with_size(monkeypatch):
    resp = resp_mock()
    post = requests_mock(resp)
    monkeypatch.setattr('requests.post', post)

    url = 'http://es/'
    es = ElasticsearchWrapper(url)

    body = {'query': {'query_string': {'query': ''}}}

    indices = ['logstash-2016-*', 'logstash-2015-*']
    result = es.search(indices=indices, body=body, source=False, size=100)

    assert result == resp.json.return_value

    body['size'] = 100
    body['_source'] = False

    post.assert_called_with(get_full_url(url, indices=indices),
                            params={},
                            json=body,
                            headers={'User-Agent': get_user_agent()},
                            timeout=10)
def test_elasticsearch_invalid_indices(monkeypatch):
    url = 'http://es/'
    es = ElasticsearchWrapper(url)

    with pytest.raises(Exception):
        es.search(indices={'index-1': 'not a list!'})

    with pytest.raises(Exception):
        es.search(indices=['index-*'] * MAX_INDICES + 1)
示例#10
0
def test_elasticsearch_invalid_size(monkeypatch):
    url = 'http://es/'
    es = ElasticsearchWrapper(url)

    with pytest.raises(Exception):
        es.search(size=MAX_SIZE + 1)

    with pytest.raises(Exception):
        es.search(size=-1)
示例#11
0
    def query_logs(self,
                   q='',
                   body=None,
                   size=100,
                   duration_in_mins=5,
                   raw_result=False):
        """
        Perform search query on AppDynamics ES logs.

        :param q: Query string used in search.
        :type q: str

        :param body: (dict) holding an ES query DSL.
        :type body: dict

        :param size: Number of hits to return. Default is 100.
        :type size: int

        :param duration_in_mins: Duration in mins before current time. Default is 5 mins.
        :type duration_in_mins: int

        :param raw_result: Return query raw result instead of only ``hits``. Default is False.
        :type raw_result: bool

        :return: ES query result ``hits``. If ``raw_result`` is True, then full query result will be returned.
        :rtype: list, dict
        """
        if not self.es_url:
            raise RuntimeError(
                'AppDynamics plugin improperly configured. ES URL is required to query logs!'
            )

        q = self.__get_search_q(q, duration_in_mins)

        indices = ['{}*'.format(self.index_prefix)]

        res = (ElasticsearchWrapper(
            url=self.es_url, oauth2=self.__oauth2).search(indices=indices,
                                                          q=q,
                                                          body=body,
                                                          size=size))

        return res['hits']['hits'] if not raw_result else res
示例#12
0
def test_elasticsearch_no_url():
    with pytest.raises(ConfigurationError):
        ElasticsearchWrapper()