ClickHouse/tests/integration/test_http_handlers_config/test.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

627 lines
19 KiB
Python
Raw Normal View History

import contextlib
import os
2024-09-27 10:19:39 +00:00
import urllib.error
import urllib.parse
import urllib.request
from helpers.cluster import ClickHouseCluster
class SimpleCluster:
def close(self):
self.cluster.shutdown()
def __init__(self, cluster, name, config_dir):
self.cluster = cluster
self.instance = self.add_instance(name, config_dir)
cluster.start()
def add_instance(self, name, config_dir):
2020-04-27 23:02:58 +00:00
return self.cluster.add_instance(
name,
main_configs=[os.path.join(config_dir, "config.xml")],
user_configs=["users.d/users.yaml"],
2020-04-27 23:02:58 +00:00
)
def test_dynamic_query_handler():
with contextlib.closing(
SimpleCluster(
ClickHouseCluster(__file__), "dynamic_handler", "test_dynamic_handler"
)
) as cluster:
2020-10-02 16:54:07 +00:00
test_query = urllib.parse.quote_plus(
"SELECT * FROM system.settings WHERE name = 'max_threads'"
)
assert (
404
== cluster.instance.http_request(
"?max_threads=1", method="GET", headers={"XXX": "xxx"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_dynamic_handler_get?max_threads=1",
method="POST",
headers={"XXX": "xxx"},
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_dynamic_handler_get?max_threads=1",
method="GET",
headers={"XXX": "bad"},
).status_code
)
assert (
400
== cluster.instance.http_request(
"test_dynamic_handler_get?max_threads=1",
method="GET",
headers={"XXX": "xxx"},
).status_code
)
2022-03-08 20:42:15 +00:00
res_default = cluster.instance.http_request(
"test_dynamic_handler_get?max_threads=1&get_dynamic_handler_query="
+ test_query,
method="GET",
headers={"XXX": "xxx"},
)
2022-03-08 20:42:15 +00:00
assert 200 == res_default.status_code
assert (
"text/tab-separated-values; charset=UTF-8"
== res_default.headers["content-type"]
)
2022-03-08 20:42:15 +00:00
res_custom_ct = cluster.instance.http_request(
"test_dynamic_handler_get_custom_content_type?max_threads=1&get_dynamic_handler_query="
+ test_query,
method="GET",
headers={"XXX": "xxx"},
)
2022-03-08 20:42:15 +00:00
assert 200 == res_custom_ct.status_code
2024-05-28 14:58:03 +00:00
assert (
"application/whatever; charset=cp1337"
== res_custom_ct.headers["content-type"]
)
2024-05-13 07:59:32 +00:00
assert "it works" == res_custom_ct.headers["X-Test-Http-Response-Headers-Works"]
2024-05-28 14:58:03 +00:00
assert (
"also works"
== res_custom_ct.headers["X-Test-Http-Response-Headers-Even-Multiple"]
)
assert cluster.instance.http_request(
"test_dynamic_handler_auth_with_password?query=select+currentUser()"
).content, "with_password"
assert cluster.instance.http_request(
"test_dynamic_handler_auth_with_password_fail?query=select+currentUser()"
).status_code, 403
assert cluster.instance.http_request(
"test_dynamic_handler_auth_without_password?query=select+currentUser()"
).content, "without_password"
2020-04-27 20:51:39 +00:00
def test_predefined_query_handler():
with contextlib.closing(
SimpleCluster(
ClickHouseCluster(__file__), "predefined_handler", "test_predefined_handler"
)
) as cluster:
assert (
404
== cluster.instance.http_request(
"?max_threads=1", method="GET", headers={"XXX": "xxx"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_predefined_handler_get?max_threads=1",
method="GET",
headers={"XXX": "bad"},
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_predefined_handler_get?max_threads=1",
method="POST",
headers={"XXX": "xxx"},
).status_code
)
assert (
500
== cluster.instance.http_request(
"test_predefined_handler_get?max_threads=1",
method="GET",
headers={"XXX": "xxx"},
).status_code
)
2020-04-26 11:06:14 +00:00
2022-03-08 20:42:15 +00:00
res1 = cluster.instance.http_request(
"test_predefined_handler_get?max_threads=1&setting_name=max_threads",
method="GET",
headers={"XXX": "xxx"},
)
assert b"max_threads\t1\n" == res1.content
2020-10-02 16:54:07 +00:00
assert (
"text/tab-separated-values; charset=UTF-8" == res1.headers["content-type"]
)
2020-04-26 11:06:14 +00:00
2022-03-08 20:42:15 +00:00
res2 = cluster.instance.http_request(
"query_param_with_url/max_threads?max_threads=1&max_final_threads=1",
headers={"XXX": "max_final_threads"},
)
assert b"max_final_threads\t1\nmax_threads\t1\n" == res2.content
assert "application/generic+one" == res2.headers["content-type"]
2024-05-13 07:59:32 +00:00
assert "it works" == res2.headers["X-Test-Http-Response-Headers-Works"]
2024-05-28 14:58:03 +00:00
assert (
"also works" == res2.headers["X-Test-Http-Response-Headers-Even-Multiple"]
)
2020-04-26 11:06:14 +00:00
2023-03-02 13:23:22 +00:00
cluster.instance.query(
"CREATE TABLE test_table (id UInt32, data String) Engine=TinyLog"
)
res3 = cluster.instance.http_request(
"test_predefined_handler_post_body?id=100",
method="POST",
2023-03-02 13:23:22 +00:00
data="TEST".encode("utf8"),
)
assert res3.status_code == 200
assert cluster.instance.query("SELECT * FROM test_table") == "100\tTEST\n"
cluster.instance.query("DROP TABLE test_table")
res4 = cluster.instance.http_request(
"test_predefined_handler_get?max_threads=1&param_setting_name=max_threads",
method="GET",
headers={"XXX": "xxx"},
)
assert b"max_threads\t1\n" == res1.content
assert cluster.instance.http_request(
"test_predefined_handler_auth_with_password"
).content, "with_password"
assert cluster.instance.http_request(
"test_predefined_handler_auth_with_password_fail"
).status_code, 403
assert cluster.instance.http_request(
"test_predefined_handler_auth_without_password"
).content, "without_password"
2020-04-26 11:06:14 +00:00
def test_fixed_static_handler():
with contextlib.closing(
SimpleCluster(
ClickHouseCluster(__file__), "static_handler", "test_static_handler"
)
) as cluster:
2020-04-26 11:06:14 +00:00
assert (
404
== cluster.instance.http_request(
"", method="GET", headers={"XXX": "xxx"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_get_fixed_static_handler", method="GET", headers={"XXX": "bad"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_get_fixed_static_handler", method="POST", headers={"XXX": "xxx"}
).status_code
)
assert (
402
== cluster.instance.http_request(
"test_get_fixed_static_handler", method="GET", headers={"XXX": "xxx"}
).status_code
)
assert (
"text/html; charset=UTF-8"
== cluster.instance.http_request(
"test_get_fixed_static_handler", method="GET", headers={"XXX": "xxx"}
).headers["Content-Type"]
)
2020-10-02 16:54:07 +00:00
assert (
b"Test get static handler and fix content"
== cluster.instance.http_request(
"test_get_fixed_static_handler", method="GET", headers={"XXX": "xxx"}
).content
)
2024-05-13 07:59:32 +00:00
assert (
"it works"
== cluster.instance.http_request(
"test_get_fixed_static_handler", method="GET", headers={"XXX": "xxx"}
).headers["X-Test-Http-Response-Headers-Works"]
)
assert (
"also works"
== cluster.instance.http_request(
"test_get_fixed_static_handler", method="GET", headers={"XXX": "xxx"}
).headers["X-Test-Http-Response-Headers-Even-Multiple"]
)
2020-04-26 11:06:14 +00:00
def test_config_static_handler():
with contextlib.closing(
SimpleCluster(
ClickHouseCluster(__file__), "static_handler", "test_static_handler"
)
) as cluster:
2020-04-26 11:06:14 +00:00
assert (
404
== cluster.instance.http_request(
"", method="GET", headers={"XXX": "xxx"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_get_config_static_handler", method="GET", headers={"XXX": "bad"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_get_config_static_handler", method="POST", headers={"XXX": "xxx"}
).status_code
)
2020-04-26 11:06:14 +00:00
# check default status code
assert (
200
== cluster.instance.http_request(
"test_get_config_static_handler", method="GET", headers={"XXX": "xxx"}
).status_code
)
assert (
"text/plain; charset=UTF-8"
== cluster.instance.http_request(
"test_get_config_static_handler", method="GET", headers={"XXX": "xxx"}
).headers["Content-Type"]
)
2020-10-02 16:54:07 +00:00
assert (
b"Test get static handler and config content"
== cluster.instance.http_request(
"test_get_config_static_handler", method="GET", headers={"XXX": "xxx"}
).content
)
2020-04-26 11:06:14 +00:00
def test_absolute_path_static_handler():
with contextlib.closing(
SimpleCluster(
ClickHouseCluster(__file__), "static_handler", "test_static_handler"
)
) as cluster:
2020-04-26 11:06:14 +00:00
cluster.instance.exec_in_container(
[
"bash",
"-c",
'echo "<html><body>Absolute Path File</body></html>" > /var/lib/clickhouse/user_files/absolute_path_file.html',
],
2020-04-26 11:06:14 +00:00
privileged=True,
user="root",
)
2020-04-26 11:06:14 +00:00
assert (
404
== cluster.instance.http_request(
"", method="GET", headers={"XXX": "xxx"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_get_absolute_path_static_handler",
method="GET",
headers={"XXX": "bad"},
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_get_absolute_path_static_handler",
method="POST",
headers={"XXX": "xxx"},
).status_code
)
2020-04-26 11:06:14 +00:00
# check default status code
assert (
200
== cluster.instance.http_request(
"test_get_absolute_path_static_handler",
method="GET",
headers={"XXX": "xxx"},
).status_code
)
assert (
"text/html; charset=UTF-8"
== cluster.instance.http_request(
"test_get_absolute_path_static_handler",
method="GET",
headers={"XXX": "xxx"},
).headers["Content-Type"]
)
2020-10-02 16:54:07 +00:00
assert (
b"<html><body>Absolute Path File</body></html>\n"
== cluster.instance.http_request(
"test_get_absolute_path_static_handler",
method="GET",
headers={"XXX": "xxx"},
).content
)
2020-04-26 11:06:14 +00:00
def test_relative_path_static_handler():
with contextlib.closing(
SimpleCluster(
ClickHouseCluster(__file__), "static_handler", "test_static_handler"
)
) as cluster:
2020-04-26 11:06:14 +00:00
cluster.instance.exec_in_container(
[
"bash",
"-c",
'echo "<html><body>Relative Path File</body></html>" > /var/lib/clickhouse/user_files/relative_path_file.html',
],
2020-04-26 11:06:14 +00:00
privileged=True,
user="root",
)
2020-04-26 11:06:14 +00:00
assert (
404
== cluster.instance.http_request(
"", method="GET", headers={"XXX": "xxx"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_get_relative_path_static_handler",
method="GET",
headers={"XXX": "bad"},
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_get_relative_path_static_handler",
method="POST",
headers={"XXX": "xxx"},
).status_code
)
2020-04-26 11:06:14 +00:00
# check default status code
assert (
200
== cluster.instance.http_request(
"test_get_relative_path_static_handler",
method="GET",
headers={"XXX": "xxx"},
).status_code
)
assert (
"text/html; charset=UTF-8"
== cluster.instance.http_request(
"test_get_relative_path_static_handler",
method="GET",
headers={"XXX": "xxx"},
).headers["Content-Type"]
)
2020-10-02 16:54:07 +00:00
assert (
b"<html><body>Relative Path File</body></html>\n"
== cluster.instance.http_request(
"test_get_relative_path_static_handler",
method="GET",
headers={"XXX": "xxx"},
).content
)
2020-06-12 10:19:03 +00:00
def test_defaults_http_handlers():
with contextlib.closing(
SimpleCluster(
ClickHouseCluster(__file__), "defaults_handlers", "test_defaults_handlers"
)
) as cluster:
2020-06-12 10:19:03 +00:00
assert 200 == cluster.instance.http_request("", method="GET").status_code
2020-10-02 16:54:07 +00:00
assert (
b"Default server response"
== cluster.instance.http_request("", method="GET").content
)
2020-06-12 10:19:03 +00:00
assert 200 == cluster.instance.http_request("ping", method="GET").status_code
2020-10-02 16:54:07 +00:00
assert b"Ok.\n" == cluster.instance.http_request("ping", method="GET").content
2020-08-14 13:16:07 +00:00
assert (
200
== cluster.instance.http_request(
"replicas_status", method="get"
).status_code
)
2020-10-02 16:54:07 +00:00
assert (
b"Ok.\n"
== cluster.instance.http_request("replicas_status", method="get").content
)
2020-08-14 13:16:07 +00:00
assert (
200
2020-10-02 16:54:07 +00:00
== cluster.instance.http_request(
"replicas_status?verbose=1", method="get"
2020-08-14 13:16:07 +00:00
).status_code
)
assert (
b""
2020-08-14 13:16:07 +00:00
== cluster.instance.http_request(
"replicas_status?verbose=1", method="get"
2020-10-02 16:54:07 +00:00
).content
)
2020-10-02 16:54:07 +00:00
assert (
200
2020-10-02 16:54:07 +00:00
== cluster.instance.http_request(
2020-06-15 11:33:44 +00:00
"?query=SELECT+1", method="GET"
).status_code
)
2020-06-15 11:33:44 +00:00
assert (
2020-10-02 16:54:07 +00:00
b"1\n"
== cluster.instance.http_request("?query=SELECT+1", method="GET").content
)
2020-06-15 11:33:44 +00:00
2023-10-11 20:53:49 +00:00
assert (
404
== cluster.instance.http_request(
"/nonexistent?query=SELECT+1", method="GET"
).status_code
)
2023-10-20 12:40:28 +00:00
def test_defaults_http_handlers_config_order():
def check_predefined_query_handler():
assert (
200
== cluster.instance.http_request(
"?query=SELECT+1", method="GET"
).status_code
)
assert (
b"1\n"
== cluster.instance.http_request("?query=SELECT+1", method="GET").content
)
response = cluster.instance.http_request(
"test_predefined_handler_get?max_threads=1&setting_name=max_threads",
method="GET",
headers={"XXX": "xxx"},
)
assert b"max_threads\t1\n" == response.content
assert (
2023-10-20 12:40:28 +00:00
"text/tab-separated-values; charset=UTF-8"
== response.headers["content-type"]
)
with contextlib.closing(
SimpleCluster(
2023-10-20 12:40:28 +00:00
ClickHouseCluster(__file__),
"defaults_handlers_config_order_first",
"test_defaults_handlers_config_order/defaults_first",
)
) as cluster:
check_predefined_query_handler()
with contextlib.closing(
SimpleCluster(
2023-10-20 12:40:28 +00:00
ClickHouseCluster(__file__),
"defaults_handlers_config_order_first",
"test_defaults_handlers_config_order/defaults_last",
)
) as cluster:
check_predefined_query_handler()
2023-10-20 12:40:28 +00:00
def test_prometheus_handler():
with contextlib.closing(
SimpleCluster(
ClickHouseCluster(__file__), "prometheus_handler", "test_prometheus_handler"
)
) as cluster:
assert (
404
== cluster.instance.http_request(
"", method="GET", headers={"XXX": "xxx"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_prometheus", method="GET", headers={"XXX": "bad"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_prometheus", method="POST", headers={"XXX": "xxx"}
).status_code
)
assert (
200
== cluster.instance.http_request(
"test_prometheus", method="GET", headers={"XXX": "xxx"}
).status_code
)
2020-10-02 16:54:07 +00:00
assert (
b"ClickHouseProfileEvents_Query"
in cluster.instance.http_request(
"test_prometheus", method="GET", headers={"XXX": "xxx"}
).content
)
def test_replicas_status_handler():
with contextlib.closing(
SimpleCluster(
ClickHouseCluster(__file__),
"replicas_status_handler",
"test_replicas_status_handler",
)
) as cluster:
assert (
404
== cluster.instance.http_request(
"", method="GET", headers={"XXX": "xxx"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_replicas_status", method="GET", headers={"XXX": "bad"}
).status_code
)
assert (
404
== cluster.instance.http_request(
"test_replicas_status", method="POST", headers={"XXX": "xxx"}
).status_code
)
assert (
200
== cluster.instance.http_request(
"test_replicas_status", method="GET", headers={"XXX": "xxx"}
).status_code
)
2020-10-02 16:54:07 +00:00
assert (
b"Ok.\n"
== cluster.instance.http_request(
"test_replicas_status", method="GET", headers={"XXX": "xxx"}
).content
)