def test_nested_range_sort__success(self, filter, expected, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{ "name": TEST_INDEX, "filter": None }, { "name": "name", "filter": filter }] attributes = "name" mock_object_1 = {"name": "a1"} mock_object_3 = {"name": "a3"} mock_object_5 = {"name": "a5"} mock_object_2 = {"name": "a2"} mock_object_4 = {"name": "a4"} mock_entry = [ mock_object_1, mock_object_3, mock_object_5, mock_object_2, mock_object_4 ] self.register_mock_get(requests_mock, "", [mock_entry]) results = es_reader.query(objects, attributes) assert results == expected
def test_query_attribute__bad_type(self): es_reader = EsReader() mock_object = ["name", "object name"] value = es_reader.query_attribute(mock_object, "name") assert value is None
def test_query_attribute__not_found(self): es_reader = EsReader() mock_object = {"name": "object name"} value = es_reader.query_attribute(mock_object, "foobar") assert value is None
def test_connect__no_params(self): es_reader = EsReader() with pytest.raises(SessionError) as e: es_reader.connect() assert "parameter is required" in str(e)
def test_index_range_sort__success(self, filter, params, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": filter}] attributes = "timestamp" mock_entry_1 = {"timestamp": 100} mock_entry_3 = {"timestamp": 300} mock_entry_5 = {"timestamp": 500} mock_entry_2 = {"timestamp": 200} mock_entry_4 = {"timestamp": 400} self.register_mock_get(requests_mock, params, [ mock_entry_1, mock_entry_3, mock_entry_5, mock_entry_2, mock_entry_4 ]) result = es_reader.query(objects, attributes) if params.endswith("size=0"): assert result == [] else: assert result == [100, 300, 500, 200, 400]
def test_nested_object_multiple__no_child(self, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [ { "name": TEST_INDEX, "filter": None }, { "name": "disks", "filter": None }, ] attributes = "name" mock_entry_1 = {"timestamp": 100, "cpu": "10.1"} mock_entry_2 = { "timestamp": 200, "cpu": "20.2", "disks": [{ "name": "cf3:" }, { "name": "cf4:" }] } self.register_mock_get(requests_mock, "", [mock_entry_1, mock_entry_2]) result = es_reader.query(objects, attributes) assert result == ["cf3:", "cf4:"]
def test_connect__too_many_params(self): es_reader = EsReader() with pytest.raises(SessionError) as e: es_reader.connect(TEST_HOST, TEST_PORT, "extra") assert "Too many arguments" in str(e)
def test_nested__no_attr_multiple(self, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{ "name": TEST_INDEX, "filter": None }, { "name": "objects", "filter": None }] attributes = ["name", "foobar"] mock_object_1 = { "name": "a1", "dhcpleaseinterval": 300, "avatartype": "URL" } mock_entry = {"objects": [mock_object_1]} self.register_mock_get(requests_mock, "", [mock_entry]) results = es_reader.query(objects, attributes) assert results == [{"name": "a1"}]
def test_query_attribute__success(self): es_reader = EsReader() mock_object = {"name": "object name"} value = es_reader.query_attribute(mock_object, "name") assert value == "object name"
def test_start__no_params(self): es_reader = EsReader() with pytest.raises(MissingSessionParamsError) as e: es_reader.start_session() assert "without parameters" in str(e)
def test_connect__other_port(self): es_reader = EsReader() es_reader.connect(TEST_HOST, TEST_PORT) assert es_reader.session_params == { "address": TEST_HOST, "port": TEST_PORT }
def test_query__no_session(self): es_reader = EsReader() objects = [{"name": TEST_INDEX, "filter": None}] attributes = "timestamp" with pytest.raises(MissingSessionParamsError) as e: es_reader.query(objects, attributes) assert "without parameters" in str(e)
def test_nested_attr_filter__success(self, filter, expected, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{ "name": TEST_INDEX, "filter": None }, { "name": "objects", "filter": filter }] attributes = "name" mock_object_1 = { "name": "a1", "dhcpleaseinterval": 300, "avatartype": "URL" } mock_object_3 = { "name": "a3", "dhcpleaseinterval": 400, "avatartype": "URL" } mock_object_5 = { "name": "a5", "dhcpleaseinterval": 500, "avatartype": "URL" } mock_object_2 = { "name": "a2", "dhcpleaseinterval": 100, "avatartype": "BASE64" } mock_object_4 = { "name": "a4", "dhcpleaseinterval": 200, "avatartype": "BASE64" } mock_entry = { "objects": [ mock_object_1, mock_object_3, mock_object_5, mock_object_2, mock_object_4 ] } self.register_mock_get(requests_mock, "", [mock_entry]) results = es_reader.query(objects, attributes) assert results == expected
def test_index_single__none_found(self, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": None}] attributes = "timestamp" self.register_mock_get(requests_mock, "", []) result = es_reader.query(objects, attributes) assert result == []
def test_attrs_multiple__unknown(self, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": None}] attributes = ["timestamp", "memory", "unknown"] mock_entry_1 = {"timestamp": 100, "cpu": 10.1, "memory": 500.2} self.register_mock_get(requests_mock, "", [mock_entry_1]) result = es_reader.query(objects, attributes) assert result == [{"timestamp": 100, "memory": 500.2}]
def test_attrs_single__success(self, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": None}] attributes = "timestamp" mock_entry_1 = {"timestamp": 100, "cpu": "10.1"} self.register_mock_get(requests_mock, "", [mock_entry_1]) result = es_reader.query(objects, attributes) assert result == [100]
def _register_query_readers(self, query): if self.spec_path is not None: vsd_writer = VsdWriter() vsd_writer.add_api_specification_path(self.spec_path) query.register_reader("vsd", vsd_writer) es_reader = EsReader() query.register_reader("es", es_reader)
def test_connect__success(self): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) es_reader.start_session() es_reader.connect("otherhost") assert es_reader.session_params == { "address": "otherhost", "port": 9200 }
def test_index_multiple_2__success(self, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": None}] attributes = ["timestamp", "cpu"] mock_entry_1 = {"timestamp": 100, "cpu": "10.1"} mock_entry_2 = {"timestamp": 200, "cpu": "20.2"} mock_entry_3 = {"timestamp": 300, "cpu": "30.3"} self.register_mock_get(requests_mock, "", [mock_entry_1, mock_entry_2, mock_entry_3]) result = es_reader.query(objects, attributes) assert result == [mock_entry_1, mock_entry_2, mock_entry_3]
def test_index_paging__success(self, filter, params_per_call, num_per_call, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": filter}] attributes = "name" mock_entry = {"name": "test"} for params, num_items in zip(params_per_call, num_per_call): self.register_mock_get(requests_mock, params, [mock_entry] * num_items) results = es_reader.query(objects, attributes) assert len(results) == sum(num_per_call) if len(results) > 0: assert results[0] == "test"
def test_nested__not_dict(self, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{ "name": TEST_INDEX, "filter": None }, { "name": "not_a_dict", "filter": None }] attributes = "name" mock_object_1 = "not_a_dict" mock_entry = {"objects": [mock_object_1]} self.register_mock_get(requests_mock, "", [mock_entry]) results = es_reader.query(objects, attributes) assert results == []
def test_start__success(self): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) es_reader.start_session() assert es_reader.session_params == { "address": TEST_HOST, "port": TEST_PORT }
def test_index_range_sort__negative_end(self): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": {"%end": -1}}] attributes = "timestamp" with pytest.raises(EsError) as e: es_reader.query(objects, attributes) assert "negative indicies" in str(e)
def test_index_range_sort__invalid_filter(self): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": {"%foobar": "not cool"}}] attributes = "timestamp" with pytest.raises(EsError) as e: es_reader.query(objects, attributes) assert "Invalid filter" in str(e)
def test_index_range_sort__attr_list(self): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": {"timestamp": [100, 200]}}] attributes = "timestamp" with pytest.raises(EsError) as e: es_reader.query(objects, attributes) assert "does not support" in str(e)
def test_index_single__http_error(self, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": None}] attributes = "timestamp" self.register_mock_get(requests_mock, "", [], status_code=403) with pytest.raises(EsError) as e: es_reader.query(objects, attributes) assert "Status code 403" in str(e)
def test_cache_index__success(self, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{"name": TEST_INDEX, "filter": None}] attributes = "timestamp" mock_entry_1 = {"timestamp": 100, "cpu": "10.1"} mock_entry_2 = {"timestamp": 200, "cpu": "20.2"} mock_entry_3 = {"timestamp": 300, "cpu": "30.3"} self.register_mock_get(requests_mock, "", [mock_entry_1, mock_entry_2, mock_entry_3]) result = es_reader.query(objects, attributes) assert result == [100, 200, 300] attributes = "cpu" self.register_mock_get(requests_mock, "", ["Should be cached"], status_code=403) result = es_reader.query(objects, attributes) assert result == ["10.1", "20.2", "30.3"] objects = [{"name": TEST_INDEX, "filter": {"%sort_desc": "timestamp"}}] self.register_mock_get(requests_mock, "sort=timestamp:desc&from=0&size=100", [mock_entry_3, mock_entry_2, mock_entry_1]) result = es_reader.query(objects, attributes) assert result == ["30.3", "20.2", "10.1"]
def setup_es_connection(self, address, port=9200, alias=None): """ Setup ES Connection: Sets up a connection to an ElasticSearch. This connection supports only queries and not configuration. ``address`` Address of the ElasticSearch to query ``port`` Port for the ElasticSearch. Default is 9200 ``alias`` Provide a handle name for this connection which can be used to switch connections. This is useful if managing multiple ESs. If not specified, a new unique alias is assigned and returned. """ es_reader = EsReader() es_reader.set_logger(logger) es_reader.set_session_params(address, port) name = self._add_object_with_alias(self.es_readers, es_reader, alias) self.current_reader = es_reader return name
def test_group_both_filtered__success(self, requests_mock): es_reader = EsReader() es_reader.set_session_params(TEST_HOST, TEST_PORT) objects = [{ "name": TEST_INDEX, "filter": { "%group": "dhcpleaseinterval" } }, { "name": "domains", "filter": { "%group": "bgpenabled", "bgpenabled": True } }] attributes = "name" mock_entry_1 = { "name": "enterprise_1", "dhcpleaseinterval": 10, "domains": [{ "name": "domain_1", "bgpenabled": True }, { "name": "domain_2", "bgpenabled": False }] } mock_entry_2 = { "name": "enterprise_2", "dhcpleaseinterval": 20, "domains": [] } mock_entry_3 = { "name": "enterprise_3", "dhcpleaseinterval": 10, "domains": [{ "name": "domain_3", "bgpenabled": True }, { "name": "domain_4", "bgpenabled": True }] } mock_entry_4 = { "name": "enterprise_4", "dhcpleaseinterval": 30, "domains": [{ "name": "domain_5", "bgpenabled": True }, { "name": "domain_6", "bgpenabled": True }] } self.register_mock_get( requests_mock, "", [mock_entry_1, mock_entry_2, mock_entry_3, mock_entry_4]) results = es_reader.query(objects, attributes) assert results == [[ 10, [[True, ["domain_1", "domain_3", "domain_4"]]] ], [20, [[True, []]]], [30, [[True, ["domain_5", "domain_6"]]]]]
def test_stop__success(self): es_reader = EsReader() es_reader.stop_session()