2023-03-03 12:55:52 +00:00
|
|
|
import pytest
|
|
|
|
import os
|
|
|
|
from . import http_headers_echo_server
|
2024-03-31 23:19:00 +00:00
|
|
|
from . import redirect_server
|
2023-03-03 12:55:52 +00:00
|
|
|
|
|
|
|
from helpers.cluster import ClickHouseCluster
|
|
|
|
|
|
|
|
cluster = ClickHouseCluster(__file__)
|
|
|
|
server = cluster.add_instance("node")
|
|
|
|
|
|
|
|
|
2024-03-31 23:19:00 +00:00
|
|
|
def run_server(container_id, file_name, hostname, port, *args):
|
2023-03-03 12:55:52 +00:00
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
2024-03-31 23:19:00 +00:00
|
|
|
cluster.copy_file_to_container(
|
|
|
|
container_id,
|
|
|
|
os.path.join(script_dir, file_name),
|
|
|
|
f"/{file_name}",
|
2023-03-03 12:55:52 +00:00
|
|
|
)
|
|
|
|
|
2024-03-31 23:19:00 +00:00
|
|
|
cmd_args = [hostname, port] + list(args)
|
|
|
|
cmd_args_val = " ".join([str(x) for x in cmd_args])
|
|
|
|
|
|
|
|
cluster.exec_in_container(
|
|
|
|
container_id,
|
2023-03-03 12:55:52 +00:00
|
|
|
[
|
|
|
|
"bash",
|
|
|
|
"-c",
|
2024-03-31 23:19:00 +00:00
|
|
|
f"python3 /{file_name} {cmd_args_val} > {file_name}.log 2>&1",
|
2023-03-03 12:55:52 +00:00
|
|
|
],
|
|
|
|
detach=True,
|
|
|
|
user="root",
|
|
|
|
)
|
|
|
|
|
|
|
|
for _ in range(0, 10):
|
2024-03-31 23:19:00 +00:00
|
|
|
ping_response = cluster.exec_in_container(
|
|
|
|
container_id,
|
|
|
|
["curl", "-s", f"http://{hostname}:{port}/"],
|
2023-03-03 12:55:52 +00:00
|
|
|
nothrow=True,
|
|
|
|
)
|
|
|
|
|
2024-03-31 23:19:00 +00:00
|
|
|
if '{"status":"ok"}' in ping_response:
|
2023-03-03 12:55:52 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
print(ping_response)
|
|
|
|
|
|
|
|
raise Exception("Echo server is not responding")
|
|
|
|
|
|
|
|
|
2024-03-31 23:19:00 +00:00
|
|
|
def run_echo_server():
|
|
|
|
container_id = cluster.get_container_id("node")
|
|
|
|
run_server(container_id, "http_headers_echo_server.py", "localhost", 8000)
|
|
|
|
|
|
|
|
|
|
|
|
def run_redirect_server():
|
|
|
|
container_id = cluster.get_container_id("node")
|
|
|
|
run_server(container_id, "redirect_server.py", "localhost", 8080, "localhost", 8000)
|
|
|
|
|
|
|
|
|
2023-03-03 12:55:52 +00:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def started_cluster():
|
|
|
|
try:
|
|
|
|
cluster.start()
|
2024-03-31 23:19:00 +00:00
|
|
|
run_redirect_server()
|
2023-03-03 12:55:52 +00:00
|
|
|
run_echo_server()
|
2024-03-31 23:19:00 +00:00
|
|
|
|
2023-03-03 12:55:52 +00:00
|
|
|
yield cluster
|
|
|
|
finally:
|
|
|
|
cluster.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
def test_storage_url_http_headers(started_cluster):
|
|
|
|
query = "INSERT INTO TABLE FUNCTION url('http://localhost:8000/', JSON, 'a UInt64', headers('X-My-Custom-Header'='test-header')) VALUES (1)"
|
|
|
|
|
|
|
|
server.query(query)
|
|
|
|
|
|
|
|
result = server.exec_in_container(
|
|
|
|
["cat", http_headers_echo_server.RESULT_PATH], user="root"
|
|
|
|
)
|
|
|
|
|
|
|
|
print(result)
|
|
|
|
|
|
|
|
assert "X-My-Custom-Header: test-header" in result
|
2024-03-31 23:19:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_storage_url_redirected_headers(started_cluster):
|
|
|
|
query = """
|
|
|
|
SELECT
|
|
|
|
title::String as title,
|
|
|
|
theme::String as theme
|
|
|
|
FROM
|
|
|
|
url('http://127.0.0.1:8080/sample-data', 'JSONEachRow', 'title String, theme String')
|
|
|
|
SETTINGS http_max_tries=2, max_http_get_redirects=2
|
|
|
|
"""
|
|
|
|
|
|
|
|
result = server.query(query)
|
|
|
|
assert 2 == len(result.strip().split("\n"))
|
|
|
|
|
|
|
|
result_redirect = server.exec_in_container(
|
|
|
|
["cat", redirect_server.RESULT_PATH], user="root"
|
|
|
|
)
|
|
|
|
|
|
|
|
print(result_redirect)
|
|
|
|
|
|
|
|
assert "Host: 127.0.0.1" in result_redirect
|
|
|
|
assert "Host: localhost" not in result_redirect
|
|
|
|
|
|
|
|
result = server.exec_in_container(
|
|
|
|
["cat", http_headers_echo_server.RESULT_PATH], user="root"
|
|
|
|
)
|
|
|
|
|
|
|
|
print(result)
|
|
|
|
|
|
|
|
assert "Host: 127.0.0.1" not in result
|
|
|
|
assert "Host: localhost" in result
|