2017-08-25 16:35:02 +00:00
import os
import os . path as p
import sys
import time
import datetime
import pytest
from contextlib import contextmanager
sys . path . insert ( 0 , os . path . dirname ( os . path . dirname ( os . path . abspath ( __file__ ) ) ) )
from helpers . cluster import ClickHouseCluster
from helpers . test_tools import TSV
2018-01-15 14:13:19 +00:00
from helpers . client import QueryRuntimeException
2017-08-25 16:35:02 +00:00
@pytest.fixture ( scope = " module " )
def started_cluster ( ) :
global cluster
global instance
try :
cluster = ClickHouseCluster ( __file__ )
cluster . add_instance ( ' ch1 ' , config_dir = " configs " )
cluster . start ( )
instance = cluster . instances [ ' ch1 ' ]
instance . query ( ' CREATE DATABASE dictionaries ENGINE = Dictionary ' )
instance . query ( ' CREATE TABLE dictionary_source (id UInt64, value UInt8) ENGINE = Memory ' )
#print instance.query('SELECT * FROM system.dictionaries FORMAT Vertical')
2018-01-15 14:13:19 +00:00
print " Started " , instance . ip_address
2017-08-25 16:35:02 +00:00
yield cluster
finally :
2018-01-15 14:13:19 +00:00
pass
2017-08-25 16:35:02 +00:00
cluster . shutdown ( )
def test_SYSTEM_RELOAD_DICTIONARY ( started_cluster ) :
instance = cluster . instances [ ' ch1 ' ]
instance . query ( " SYSTEM RELOAD DICTIONARIES " )
assert TSV ( instance . query ( " SELECT dictHas( ' clickhouse_flat ' , toUInt64(0)), dictHas( ' clickhouse_flat ' , toUInt64(1)) " ) ) == TSV ( " 0 \t 0 \n " )
instance . query ( " INSERT INTO dictionary_source VALUES (0, 0) " )
assert TSV ( instance . query ( " SELECT dictGetUInt8( ' clickhouse_cache ' , ' value ' , toUInt64(0)), dictHas( ' clickhouse_cache ' , toUInt64(1)) " ) ) == TSV ( " 0 \t 0 \n " )
instance . query ( " INSERT INTO dictionary_source VALUES (1, 1) " )
assert TSV ( instance . query ( " SELECT dictGetUInt8( ' clickhouse_cache ' , ' value ' , toUInt64(0)), dictHas( ' clickhouse_cache ' , toUInt64(1)) " ) ) == TSV ( " 0 \t 0 \n " )
instance . query ( " SYSTEM RELOAD DICTIONARY clickhouse_cache " )
assert TSV ( instance . query ( " SELECT dictGetUInt8( ' clickhouse_cache ' , ' value ' , toUInt64(0)), dictGetUInt8( ' clickhouse_cache ' , ' value ' , toUInt64(1)) " ) ) == TSV ( " 0 \t 1 \n " )
assert TSV ( instance . query ( " SELECT dictHas( ' clickhouse_flat ' , toUInt64(0)), dictHas( ' clickhouse_flat ' , toUInt64(1)) " ) ) == TSV ( " 0 \t 0 \n " )
instance . query ( " SYSTEM RELOAD DICTIONARIES " )
assert TSV ( instance . query ( " SELECT dictGetUInt8( ' clickhouse_cache ' , ' value ' , toUInt64(0)), dictGetUInt8( ' clickhouse_cache ' , ' value ' , toUInt64(1)) " ) ) == TSV ( " 0 \t 1 \n " )
assert TSV ( instance . query ( " SELECT dictGetUInt8( ' clickhouse_flat ' , ' value ' , toUInt64(0)), dictGetUInt8( ' clickhouse_flat ' , ' value ' , toUInt64(1)) " ) ) == TSV ( " 0 \t 1 \n " )
def test_DROP_DNS_CACHE ( started_cluster ) :
instance = cluster . instances [ ' ch1 ' ]
2018-09-03 15:26:11 +00:00
instance . exec_in_container ( [ ' bash ' , ' -c ' , ' echo 127.0.0.1 localhost > /etc/hosts ' ] , privileged = True , user = ' root ' )
instance . exec_in_container ( [ ' bash ' , ' -c ' , ' echo ::1 localhost >> /etc/hosts ' ] , privileged = True , user = ' root ' )
instance . exec_in_container ( [ ' bash ' , ' -c ' , ' echo 127.255.255.255 lost_host >> /etc/hosts ' ] , privileged = True , user = ' root ' )
2018-03-26 14:12:07 +00:00
instance . query ( " SYSTEM DROP DNS CACHE " )
2017-08-25 16:35:02 +00:00
2018-01-15 14:13:19 +00:00
with pytest . raises ( QueryRuntimeException ) :
instance . query ( " SELECT * FROM remote( ' lost_host ' , ' system ' , ' one ' ) " )
instance . query ( " CREATE TABLE distributed_lost_host (dummy UInt8) ENGINE = Distributed(lost_host_cluster, ' system ' , ' one ' ) " )
with pytest . raises ( QueryRuntimeException ) :
instance . query ( " SELECT * FROM distributed_lost_host " )
2018-09-03 15:26:11 +00:00
instance . exec_in_container ( [ ' bash ' , ' -c ' , ' echo 127.0.0.1 localhost > /etc/hosts ' ] , privileged = True , user = ' root ' )
instance . exec_in_container ( [ ' bash ' , ' -c ' , ' echo ::1 localhost >> /etc/hosts ' ] , privileged = True , user = ' root ' )
instance . exec_in_container ( [ ' bash ' , ' -c ' , ' echo 127.0.0.1 lost_host >> /etc/hosts ' ] , privileged = True , user = ' root ' )
2017-08-25 16:35:02 +00:00
instance . query ( " SYSTEM DROP DNS CACHE " )
2018-01-15 14:13:19 +00:00
instance . query ( " SELECT * FROM remote( ' lost_host ' , ' system ' , ' one ' ) " )
instance . query ( " SELECT * FROM distributed_lost_host " )
2018-03-07 13:52:09 +00:00
assert TSV ( instance . query ( " SELECT DISTINCT host_name, host_address FROM system.clusters WHERE cluster= ' lost_host_cluster ' " ) ) == TSV ( " lost_host \t 127.0.0.1 \n " )
2017-08-25 16:35:02 +00:00
2018-03-13 11:47:32 +00:00
def test_RELOAD_CONFIG_AND_MACROS ( started_cluster ) :
macros = " <yandex><macros><mac>ro</mac></macros></yandex> "
create_macros = ' echo " {} " > /etc/clickhouse-server/config.d/macros.xml ' . format ( macros )
instance = cluster . instances [ ' ch1 ' ]
instance . exec_in_container ( [ ' bash ' , ' -c ' , create_macros ] , privileged = True , user = ' root ' )
instance . query ( " SYSTEM RELOAD CONFIG " )
assert TSV ( instance . query ( " select * from system.macros " ) ) == TSV ( " mac \t ro \n " )
2019-06-19 12:28:34 +00:00
def test_SYSTEM_FLUSH_LOGS ( started_cluster ) :
instance = cluster . instances [ ' ch1 ' ]
2020-05-26 20:27:31 +00:00
instance . query ( '''
SET log_queries = 0 ;
2020-05-27 08:25:36 +00:00
SYSTEM FLUSH LOGS ;
2020-05-26 20:27:31 +00:00
TRUNCATE TABLE system . query_log ;
''' )
2019-06-19 12:28:34 +00:00
for i in range ( 4 ) :
# Sleep to execute flushing from background thread at first query
# by expiration of flush_interval_millisecond and test probable race condition.
time . sleep ( 0.5 )
result = instance . query ( '''
SELECT 1 FORMAT Null ;
SET log_queries = 0 ;
SYSTEM FLUSH LOGS ;
SELECT count ( ) FROM system . query_log ; ''' )
2020-05-26 20:27:31 +00:00
instance . query ( '''
SET log_queries = 0 ;
2020-05-27 08:25:36 +00:00
SYSTEM FLUSH LOGS ;
2020-05-26 20:27:31 +00:00
TRUNCATE TABLE system . query_log ;
''' )
2019-06-19 12:28:34 +00:00
assert TSV ( result ) == TSV ( ' 4 ' )
2017-08-25 16:35:02 +00:00
if __name__ == ' __main__ ' :
with contextmanager ( started_cluster ) ( ) as cluster :
for name , instance in cluster . instances . items ( ) :
print name , instance . ip_address
raw_input ( " Cluster created, press any key to destroy... " )