add postgresql tests

This commit is contained in:
Emmanuel Garette 2022-07-17 23:01:46 +02:00
parent a254ce3b77
commit 6b8e8ff6fa
6 changed files with 92 additions and 3 deletions

View file

@ -10,4 +10,3 @@ CREATE DATABASE IF NOT EXISTS %%name CHARACTER SET utf8;
GRANT ALL PRIVILEGES ON %%name.* TO '%%name'@'%%server' IDENTIFIED BY '%%password';
%end for
FLUSH PRIVILEGES;

View file

@ -13,6 +13,7 @@
<file>/etc/pki/ca-trust/source/anchors/ca_PostgreSQL.crt</file>
<file>/etc/pki/tls/certs/postgresql.crt</file>
<file owner="root" group="postgres" mode="440">/etc/pki/tls/private/postgresql.key</file>
<file>/tests/postgresql.yml</file>
</service>
</services>
<variables>

View file

@ -88,6 +88,7 @@ local all postgres ident map=pg_map
# IPv4 local connections:
#>GNUNUX
# host all all 127.0.0.1/32 ident
hostssl rougail_test rougail_test %%gateway_eth0/32 md5
%for %%server in %%accounts.remotes
hostssl %%normalize_family(%%server) %%normalize_family(%%server) %%server md5
%end for

View file

@ -1,7 +1,12 @@
%set %%new_accounts = [('rougail_test', %%get_password(server_name=%%domain_name_eth0, username='rougail_test', description="remote", type="cleartext", hide=%%hide_secret, temporary=True))]
%for %%server in %%accounts.remotes
%set %%name = %%normalize_family(%%server)
%set %%password = %%accounts["remote_" + %%name]["password_" + %%name]
%%new_accounts.append((%%name, %%password))%slurp
%end for
%for %%name, %%password in %%new_accounts
CREATE DATABASE "%%name";
CREATE ROLE "%%name" WITH LOGIN ENCRYPTED PASSWORD '%%accounts["remote_" + %%name]["password_" + %%name]';
ALTER USER "%%name" PASSWORD '%%accounts["remote_" + %%name]["password_" + %%name]';
CREATE ROLE "%%name" WITH LOGIN ENCRYPTED PASSWORD '%%password';
ALTER USER "%%name" PASSWORD '%%password';
GRANT ALL PRIVILEGES ON DATABASE "%%name" TO "%%name";
%end for

View file

@ -0,0 +1,4 @@
address: %%ip_eth0
user: rougail_test
password: %%get_password(server_name=%%domain_name_eth0, username='rougail_test', description="remote", type="cleartext", hide=%%hide_secret, temporary=True)
dbname: rougail_test

View file

@ -0,0 +1,79 @@
from yaml import load, SafeLoader
from os import environ
from pytest import raises
from psycopg2 import connect, OperationalError
def test_postgresql_wrong_password():
conf_file = f'{environ["MACHINE_TEST_DIR"]}/postgresql.yml'
with open(conf_file) as yaml:
data = load(yaml, Loader=SafeLoader)
with raises(OperationalError):
connect(host=data['address'], user=data['user'], password='a', database=data['dbname'])
def test_postgresql_connection():
conf_file = f'{environ["MACHINE_TEST_DIR"]}/postgresql.yml'
with open(conf_file) as yaml:
data = load(yaml, Loader=SafeLoader)
db = connect(host=data['address'], user=data['user'], password=data['password'], database=data['dbname'])
db.close()
def test_postgresql_migration():
conf_file = f'{environ["MACHINE_TEST_DIR"]}/postgresql.yml'
with open(conf_file) as yaml:
data = load(yaml, Loader=SafeLoader)
db = connect(host=data['address'], user=data['user'], password=data['password'], database=data['dbname'])
cursor = db.cursor()
if 'FIRST_RUN' in environ:
sql = """CREATE TABLE test (col CHAR(20) NOT NULL)"""
cursor.execute(sql)
sql = """INSERT INTO test (col) VALUES ('test')"""
cursor.execute(sql)
db.commit()
sql = """SELECT * FROM test"""
cursor.execute(sql)
results = cursor.fetchall()
assert len(results) == 1
results[0] == ('test',)
cursor.close()
db.close()
def test_postgresql_insert():
conf_file = f'{environ["MACHINE_TEST_DIR"]}/postgresql.yml'
with open(conf_file) as yaml:
data = load(yaml, Loader=SafeLoader)
db = connect(host=data['address'], user=data['user'], password=data['password'], database=data['dbname'])
cursor = db.cursor()
sql = """INSERT INTO test (col) VALUES ('test2')"""
cursor.execute(sql)
db.commit()
#
sql = """SELECT * FROM test WHERE col = 'test2'"""
cursor.execute(sql)
results = cursor.fetchall()
assert len(results) == 1
results[0] == ('test2',)
cursor.close()
db.close()
def test_postgresql_delete():
conf_file = f'{environ["MACHINE_TEST_DIR"]}/postgresql.yml'
with open(conf_file) as yaml:
data = load(yaml, Loader=SafeLoader)
db = connect(host=data['address'], user=data['user'], password=data['password'], database=data['dbname'])
cursor = db.cursor()
sql = """DELETE FROM test WHERE col = 'test2'"""
cursor.execute(sql)
db.commit()
#
sql = """SELECT * FROM test WHERE col = 'test2'"""
cursor.execute(sql)
results = cursor.fetchall()
assert len(results) == 0
cursor.close()
db.close()