From 7b281900f9f7e8753efd44751092215819f2b24e Mon Sep 17 00:00:00 2001 From: anastasia Date: Thu, 13 May 2021 17:52:31 +0300 Subject: [PATCH] Add a function to change postgresql.conf in python tests. Add test_config as an example --- test_runner/fixtures/zenith_fixtures.py | 25 ++++++++++++++++--- test_runner/test_config.py | 33 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 test_runner/test_config.py diff --git a/test_runner/fixtures/zenith_fixtures.py b/test_runner/fixtures/zenith_fixtures.py index 4c44c166d5..69c4d7b2dd 100644 --- a/test_runner/fixtures/zenith_fixtures.py +++ b/test_runner/fixtures/zenith_fixtures.py @@ -126,26 +126,43 @@ def pageserver(zenith_cli): class Postgres: """ An object representing a running postgres daemon. """ - def __init__(self, zenith_cli): + def __init__(self, zenith_cli, repo_dir): self.zenith_cli = zenith_cli self.running = False + self.host = 'localhost' + self.port = 55432 + self.repo_dir = repo_dir # path to conf is /pgdatadirs/pg1/postgresql.conf - def create_start(self): + def create_start(self, config_lines=None): """ create the pg data directory, and start the server """ self.zenith_cli.run(['pg', 'create']) + if config_lines is None: + config_lines = [] + self.config(config_lines) # FIXME: where did the name pg1 come from? self.zenith_cli.run(['pg', 'start', 'pg1']) self.running = True + + #lines should be an array of valid postgresql.conf rows + def config(self, lines): + #TODO use real node name, not just guessed pg1 + filename = 'pgdatadirs/pg1/postgresql.conf' + config_name = os.path.join(self.repo_dir, filename) + with open(config_name, 'a') as conf: + for line in lines: + conf.write(line) + conf.write('\n') + def stop(self): if self.running: self.zenith_cli.run(['pg', 'stop', 'pg1']) @zenfixture -def postgres(zenith_cli): - pg = Postgres(zenith_cli) +def postgres(zenith_cli, repo_dir): + pg = Postgres(zenith_cli, repo_dir) yield pg # After the yield comes any cleanup code we need. print('Starting postgres cleanup') diff --git a/test_runner/test_config.py b/test_runner/test_config.py new file mode 100644 index 0000000000..1f186363c8 --- /dev/null +++ b/test_runner/test_config.py @@ -0,0 +1,33 @@ +import pytest +import os +import getpass +import psycopg2 + +pytest_plugins = ("fixtures.zenith_fixtures") + + +def test_config(zenith_cli, pageserver, postgres, pg_bin): + zenith_cli.run_init() + pageserver.start() + print('pageserver is running') + + # change config + postgres.create_start(['log_min_messages=debug1']) + + print('postgres is running') + + username = getpass.getuser() + conn_str = 'host={} port={} dbname=postgres user={}'.format( + postgres.host, postgres.port, username) + print('conn_str is', conn_str) + pg_conn = psycopg2.connect(conn_str) + pg_conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) + cur = pg_conn.cursor() + + #check that config change was applied + cur.execute('SELECT name, setting from pg_settings WHERE source!=%s and source!=%s', ("default","override",)) + for record in cur: + if record[0] == 'log_min_messages': + assert(record[1] == 'debug1') + + pg_conn.close()