def test_empty_warnings(self): con = Connection() with warnings.catch_warnings(record=True) as w: con._raise_warnings(()) con._raise_warnings([]) self.assertEqual(w, [])
def test_meta_header(self): conn = Connection(meta_header=True) assert conn.meta_header is True conn = Connection(meta_header=False) assert conn.meta_header is False with pytest.raises(TypeError) as e: Connection(meta_header=1) assert str(e.value) == "meta_header must be of type bool"
def test_raises_warnings_when_folded(self): con = Connection() with warnings.catch_warnings(record=True) as warn: con._raise_warnings([ '299 Elasticsearch-7.6.1-aa751 "warning",' '299 Elasticsearch-7.6.1-aa751 "folded"', ]) self.assertEqual([str(w.message) for w in warn], ["warning", "folded"])
def test_parse_cloud_id(self): # Embedded port in cloud_id con = Connection( cloud_id="cluster:d2VzdGV1cm9wZS5henVyZS5lbGFzdGljLWNsb3VkLmNvbTo5MjQzJGM2NjM3ZjMxMmM1MjQzY2RhN2RlZDZlOTllM2QyYzE5" ) self.assertEqual( con.host, "https://c6637f312c5243cda7ded6e99e3d2c19.westeurope.azure.elastic-cloud.com:9243", ) self.assertEqual(con.port, 9243) self.assertEqual( con.hostname, "c6637f312c5243cda7ded6e99e3d2c19.westeurope.azure.elastic-cloud.com", ) # Embedded port but overridden con = Connection( cloud_id="cluster:d2VzdGV1cm9wZS5henVyZS5lbGFzdGljLWNsb3VkLmNvbTo5MjQzJGM2NjM3ZjMxMmM1MjQzY2RhN2RlZDZlOTllM2QyYzE5", port=443, ) self.assertEqual( con.host, "https://c6637f312c5243cda7ded6e99e3d2c19.westeurope.azure.elastic-cloud.com:443", ) self.assertEqual(con.port, 443) self.assertEqual( con.hostname, "c6637f312c5243cda7ded6e99e3d2c19.westeurope.azure.elastic-cloud.com", ) # Port is 443, removed by default. con = Connection( cloud_id="cluster:d2VzdGV1cm9wZS5henVyZS5lbGFzdGljLWNsb3VkLmNvbSRlN2RlOWYxMzQ1ZTQ0OTAyODNkOTAzYmU1YjZmOTE5ZQ==" ) self.assertEqual( con.host, "https://e7de9f1345e4490283d903be5b6f919e.westeurope.azure.elastic-cloud.com", ) self.assertEqual(con.port, None) self.assertEqual( con.hostname, "e7de9f1345e4490283d903be5b6f919e.westeurope.azure.elastic-cloud.com", ) # No port, contains Kibana UUID con = Connection( cloud_id="cluster:d2VzdGV1cm9wZS5henVyZS5lbGFzdGljLWNsb3VkLmNvbSQ4YWY3ZWUzNTQyMGY0NThlOTAzMDI2YjQwNjQwODFmMiQyMDA2MTU1NmM1NDA0OTg2YmZmOTU3ZDg0YTZlYjUxZg==" ) self.assertEqual( con.host, "https://8af7ee35420f458e903026b4064081f2.westeurope.azure.elastic-cloud.com", ) self.assertEqual(con.port, None) self.assertEqual( con.hostname, "8af7ee35420f458e903026b4064081f2.westeurope.azure.elastic-cloud.com", )
def test_new_connection_is_not_marked_dead(self): # Create 10 connections pool = ConnectionPool([(Connection(), {}) for _ in range(10)]) # Pass in a new connection that is not in the pool to mark as dead new_connection = Connection() pool.mark_dead(new_connection) # Nothing should be marked dead self.assertEquals(0, len(pool.dead_count))
def test_parse_cloud_id(self): # Embedded port in cloud_id con = Connection(cloud_id=CLOUD_ID_PORT_AND_KIBANA) self.assertEqual( con.host, "https://c6637f312c5243cda7ded6e99e3d2c19.westeurope.azure.elastic-cloud.com:9243", ) self.assertEqual(con.port, 9243) self.assertEqual( con.hostname, "c6637f312c5243cda7ded6e99e3d2c19.westeurope.azure.elastic-cloud.com", ) # Embedded port but overridden con = Connection( cloud_id=CLOUD_ID_PORT_AND_KIBANA, port=443, ) self.assertEqual( con.host, "https://c6637f312c5243cda7ded6e99e3d2c19.westeurope.azure.elastic-cloud.com:443", ) self.assertEqual(con.port, 443) self.assertEqual( con.hostname, "c6637f312c5243cda7ded6e99e3d2c19.westeurope.azure.elastic-cloud.com", ) # Port is 443, removed by default. con = Connection(cloud_id=CLOUD_ID_PORT_443) self.assertEqual( con.host, "https://e7de9f1345e4490283d903be5b6f919e.westeurope.azure.elastic-cloud.com", ) self.assertEqual(con.port, None) self.assertEqual( con.hostname, "e7de9f1345e4490283d903be5b6f919e.westeurope.azure.elastic-cloud.com", ) # No port, contains Kibana UUID con = Connection(cloud_id=CLOUD_ID_KIBANA) self.assertEqual( con.host, "https://8af7ee35420f458e903026b4064081f2.westeurope.azure.elastic-cloud.com", ) self.assertEqual(con.port, None) self.assertEqual( con.hostname, "8af7ee35420f458e903026b4064081f2.westeurope.azure.elastic-cloud.com", )
def test_compatibility_accept_header(self): try: conn = Connection() assert "accept" not in conn.headers os.environ["ELASTIC_CLIENT_APIVERSIONING"] = "0" conn = Connection() assert "accept" not in conn.headers os.environ["ELASTIC_CLIENT_APIVERSIONING"] = "1" conn = Connection() assert (conn.headers["accept"] == "application/vnd.elasticsearch+json;compatible-with=8") finally: os.environ.pop("ELASTIC_CLIENT_APIVERSIONING")
def test_ipv6_host_and_port(self): for kwargs, expected_host in [ ({"host": "::1"}, "http://[::1]:9200"), ({"host": "::1", "port": 443}, "http://[::1]:443"), ({"host": "::1", "use_ssl": True}, "https://[::1]:9200"), ({"host": "127.0.0.1", "port": 1234}, "http://127.0.0.1:1234"), ({"host": "localhost", "use_ssl": True}, "https://localhost:9200"), ]: conn = Connection(**kwargs) assert conn.host == expected_host
def test_recording(self, *args): response = self.panel.process_request(self.request) Connection().log_request_success("GET", "asdasd", "asdasd", "{}", 200, "adssad", 1) self.assertIsNotNone(response) self.panel.generate_stats(self.request, response) stats = self.panel.get_stats() self.assertIn("records", stats) self.assertEqual(len(stats["records"]), 1)
def test_raises_warnings(self): con = Connection() with warnings.catch_warnings(record=True) as warn: con._raise_warnings( ['299 Elasticsearch-7.6.1-aa751 "this is deprecated"']) self.assertEqual([str(w.message) for w in warn], ["this is deprecated"]) with warnings.catch_warnings(record=True) as warn: con._raise_warnings([ '299 Elasticsearch-7.6.1-aa751 "this is also deprecated"', '299 Elasticsearch-7.6.1-aa751 "this is also deprecated"', '299 Elasticsearch-7.6.1-aa751 "guess what? deprecated"', ]) self.assertEqual( [str(w.message) for w in warn], ["this is also deprecated", "guess what? deprecated"], )