示例#1
0
    def test_info_fails(self, mock_requests_get, mock__check_response):
        es = client.ElasticSearchClient(None)

        # case #1 - _check_response raises exception. it should not be caught
        exc = KeyError("foo")
        mock__check_response.side_effect = exc

        e = self.assertRaises(KeyError, es.info)
        self.assertEqual(exc, e)

        mock__check_response.reset_mock()
        mock__check_response.side_effect = None

        # case #2 - the response is ok, but the data is not a json-like obj
        resp = mock_requests_get.return_value
        resp.json.side_effect = ValueError()

        es = client.ElasticSearchClient(None)

        e = self.assertRaises(exceptions.RallyException, es.info)
        self.assertIn("The return data doesn't look like a json.",
                      e.format_message())

        resp.json.reset_mock()
        resp.json.side_effect = None
        # case #3 - the return data is a json, but doesn't include the version

        resp.json.return_value = {}
        e = self.assertRaises(exceptions.RallyException, es.info)
        self.assertIn("Failed to parse the received data.", e.format_message())
示例#2
0
    def test_check_response(self):
        es = client.ElasticSearchClient(None)

        resp = mock.Mock(status_code=200)
        self.assertIsNone(es._check_response(resp))

        resp.status_code = 300
        resp.json.side_effect = ValueError("Foo!!!!")
        resp.text = "something"

        e = self.assertRaises(exceptions.RallyException, es._check_response,
                              resp)
        self.assertEqual(
            "[HTTP 300] Failed to connect to ElasticSearch "
            "cluster: something", e.format_message())

        resp.json = mock.Mock(return_value={
            "error": {
                "reason": "I'm too lazy to process this request."
            }
        })

        e = self.assertRaises(exceptions.RallyException, es._check_response,
                              resp)
        self.assertEqual(
            "[HTTP 300] Failed to connect to ElasticSearch "
            "cluster: I'm too lazy to process this request.",
            e.format_message())
示例#3
0
    def test_create_index_es_5(self, mock_requests_put, mock__check_response):
        es = client.ElasticSearchClient(None)
        es._version = "5"
        i_name = "foo"
        i_type = "data"
        o_properties = {
            "prop1": {
                "type": "text"
            },
            "prop2": {
                "type": "keyword"
            }
        }
        # ensure that no transformation with properties will not be performed
        properties = copy.deepcopy(o_properties)
        es.create_index(i_name, i_type, properties)

        mock_requests_put.assert_called_once_with(
            "http://localhost:9200/%s" % i_name,
            json={"mappings": {
                i_type: {
                    "properties": o_properties
                }
            }})
        mock__check_response.assert_called_once_with(
            mock_requests_put.return_value, mock.ANY)
示例#4
0
 def __init__(self, tasks_results, output_destination, api=None):
     super(ElasticSearchExporter, self).__init__(tasks_results,
                                                 output_destination,
                                                 api=api)
     self._report = []
     self._remote = (output_destination is None
                     or (output_destination.startswith("http://")
                         or self.output_destination.startswith("https://")))
     if self._remote:
         self._client = client.ElasticSearchClient(self.output_destination)
示例#5
0
    def test_version(self, mock_info):
        es = client.ElasticSearchClient(None)

        es._version = "foo"
        self.assertEqual("foo", es.version())
        self.assertFalse(mock_info.called)

        es._version = None
        self.assertIsNone(es.version())
        mock_info.assert_called_once_with()
示例#6
0
    def test_list_indices(self, mock_requests_get, mock__check_response):
        mock_requests_get.return_value.text = "foo bar\n"
        es = client.ElasticSearchClient(None)

        self.assertEqual(["foo", "bar"], es.list_indices())

        mock_requests_get.assert_called_once_with(
            "http://localhost:9200/_cat/indices?v")
        mock__check_response.assert_called_once_with(
            mock_requests_get.return_value, mock.ANY)
示例#7
0
 def validate(self, context, config, plugin_cls, plugin_cfg):
     destination = plugin_cfg["destination"]
     if destination and (not destination.startswith("http://")
                         and not destination.startswith("https://")):
         # it is a path to a local file
         return
     es = client.ElasticSearchClient(destination)
     try:
         version = es.version()
     except exceptions.RallyException as e:
         # re-raise a proper exception to hide redundant traceback
         self.fail(e.format_message())
     if not (version.startswith("2.") or version.startswith("5.")
             or version.startswith("6.")):
         self.fail("The unsupported version detected %s." % version)
示例#8
0
    def test_push_documents(self, mock_requests_post):
        mock_requests_post.return_value.status_code = 200
        es = client.ElasticSearchClient(None)
        # decrease the size of chunks to not generate 10_001 number of docs
        es.CHUNK_LENGTH = 2

        documents = ["doc1", "doc2", "doc3"]

        es.push_documents(documents)

        headers = {"Content-Type": "application/x-ndjson"}

        self.assertEqual([
            mock.call("http://localhost:9200/_bulk",
                      data="doc1\ndoc2\n",
                      headers=headers),
            mock.call(
                "http://localhost:9200/_bulk", data="doc3\n", headers=headers)
        ], mock_requests_post.call_args_list)
示例#9
0
    def test_check_document(self, mock_requests_head, mock__check_response):
        es = client.ElasticSearchClient(None)
        resp = mock_requests_head.return_value

        resp.status_code = 200

        self.assertTrue(es.check_document("foo", "bar"))
        mock_requests_head.assert_called_once_with(
            "http://localhost:9200/foo/data/bar")
        self.assertFalse(mock__check_response.called)

        resp.status_code = 404

        self.assertFalse(es.check_document("foo", "bar"))
        self.assertFalse(mock__check_response.called)

        resp.status_code = 300
        self.assertIsNone(es.check_document("foo", "bar"))
        mock__check_response.assert_called_once_with(resp, mock.ANY)
示例#10
0
    def test_info(self, mock_requests_get):
        resp = mock_requests_get.return_value
        resp.status_code = 200
        data = {"version": {"number": "5.6.1"}}
        resp.json.return_value = data

        es = client.ElasticSearchClient(None)

        self.assertEqual(data, es.info())

        self.assertEqual("5.6.1", es._version)
        mock_requests_get.assert_called_once_with("http://localhost:9200")

        # check unification
        data = {"version": {"number": "2.4.1", "build_timestamp": "timestamp"}}
        resp.json.return_value = data
        self.assertEqual(
            {"version": {
                "number": "2.4.1",
                "build_date": "timestamp"
            }}, es.info())