Add test for HTTP GET provides readonly access.

This commit is contained in:
Vitaly Baranov 2020-03-29 16:21:26 +03:00
parent 36773b661c
commit 5def95f05b
2 changed files with 39 additions and 2 deletions

View File

@ -10,6 +10,7 @@ import socket
import subprocess import subprocess
import time import time
import urllib import urllib
import httplib
import xml.dom.minidom import xml.dom.minidom
import logging import logging
import docker import docker
@ -652,7 +653,7 @@ class ClickHouseInstance:
return self.client.query_and_get_answer_with_error(sql, stdin, timeout, settings, user, password) return self.client.query_and_get_answer_with_error(sql, stdin, timeout, settings, user, password)
# Connects to the instance via HTTP interface, sends a query and returns the answer # Connects to the instance via HTTP interface, sends a query and returns the answer
def http_query(self, sql, data=None, params=None, user=None, password=None): def http_query(self, sql, data=None, params=None, user=None, password=None, expect_fail_and_get_error=False):
if params is None: if params is None:
params = {} params = {}
else: else:
@ -668,7 +669,23 @@ class ClickHouseInstance:
url = "http://" + auth + self.ip_address + ":8123/?" + urllib.urlencode(params) url = "http://" + auth + self.ip_address + ":8123/?" + urllib.urlencode(params)
return urllib.urlopen(url, data).read() open_result = urllib.urlopen(url, data)
def http_code_and_message():
return str(open_result.getcode()) + " " + httplib.responses[open_result.getcode()] + ": " + open_result.read()
if expect_fail_and_get_error:
if open_result.getcode() == 200:
raise Exception("ClickHouse HTTP server is expected to fail, but succeeded: " + open_result.read())
return http_code_and_message()
else:
if open_result.getcode() != 200:
raise Exception("ClickHouse HTTP server returned " + http_code_and_message())
return open_result.read()
# Connects to the instance via HTTP interface, sends a query, expects an error and return the error message
def http_query_and_get_error(self, sql, data=None, params=None, user=None, password=None):
return self.http_query(sql=sql, data=data, params=params, user=user, password=password, expect_fail_and_get_error=True)
def kill_clickhouse(self, stop_start_wait_sec=5): def kill_clickhouse(self, stop_start_wait_sec=5):
pid = self.get_process_pid("clickhouse") pid = self.get_process_pid("clickhouse")

View File

@ -0,0 +1,20 @@
import pytest
from helpers.cluster import ClickHouseCluster
cluster = ClickHouseCluster(__file__)
instance = cluster.add_instance('instance')
@pytest.fixture(scope="module", autouse=True)
def setup_nodes():
try:
cluster.start()
yield cluster
finally:
cluster.shutdown()
def test_http_get_is_readonly():
assert "Cannot execute query in readonly mode" in instance.http_query_and_get_error("CREATE TABLE xxx (a Date) ENGINE = MergeTree(a, a, 256)")
assert "Cannot modify 'readonly' setting in readonly mode" in instance.http_query_and_get_error("CREATE TABLE xxx (a Date) ENGINE = MergeTree(a, a, 256)", params={"readonly": 0})