2018-12-19 20:29:52 +00:00
import docker
2018-12-07 01:40:35 +00:00
import os
import pwd
import pytest
import re
from helpers . cluster import ClickHouseCluster
2018-12-19 20:29:52 +00:00
def expect_failure_with_message ( expected_message ) :
2018-12-07 01:40:35 +00:00
cluster = ClickHouseCluster ( __file__ )
2018-12-19 20:29:52 +00:00
node = cluster . add_instance ( ' node ' , with_zookeeper = False )
2018-12-07 01:40:35 +00:00
with pytest . raises ( Exception ) :
cluster . start ( )
2018-12-19 20:29:52 +00:00
current_user_id = os . getuid ( )
other_user_id = pwd . getpwnam ( ' nobody ' ) . pw_uid
docker_api = docker . from_env ( ) . api
container = node . get_docker_handle ( )
container . start ( )
container . exec_run ( ' chown {0} /var/lib/clickhouse ' . format ( other_user_id ) , privileged = True )
container . exec_run ( ' clickhouse server --config-file=/etc/clickhouse-server/config.xml --log-file=/var/log/clickhouse-server/clickhouse-server.log --errorlog-file=/var/log/clickhouse-server/clickhouse-server.err.log ' , privileged = True )
2018-12-07 01:40:35 +00:00
cluster . shutdown ( ) # cleanup
2018-12-19 20:29:52 +00:00
with open ( os . path . join ( node . path , ' logs/clickhouse-server.err.log ' ) ) as log :
2018-12-07 01:40:35 +00:00
last_message = log . readlines ( ) [ - 1 ] . strip ( )
if re . search ( expected_message , last_message ) is None :
pytest . fail ( ' Expected the server to fail with a message " {} " , but the last message is " {} " ' . format ( expected_message , last_message ) )
def test_different_user ( ) :
current_user_id = os . getuid ( )
2018-12-19 20:29:52 +00:00
if current_user_id == 0 :
2018-12-07 01:40:35 +00:00
current_user_name = pwd . getpwuid ( current_user_id ) . pw_name
expect_failure_with_message (
2018-12-19 20:29:52 +00:00
' Effective user of the process \ (.* \ ) does not match the owner of the data \ (.* \ ) ' ,
2018-12-07 01:40:35 +00:00
)