From e5bd13b8c12ec0c0d0cacd6191baf122b9f512be Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sun, 15 Aug 2021 20:05:23 +0100 Subject: [PATCH] Test and fix Postgres helper --- .drone.yml | 7 ++- .../datman_helper_postgres/backup.py | 11 +++-- datman-helper-postgres/setup.py | 2 +- testsuite/datmantests/test_postgres_helper.py | 45 +++++++++++++++++++ testsuite/scripts-dev/lint.sh | 18 -------- 5 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 testsuite/datmantests/test_postgres_helper.py delete mode 100755 testsuite/scripts-dev/lint.sh diff --git a/.drone.yml b/.drone.yml index 1732259..3d63927 100644 --- a/.drone.yml +++ b/.drone.yml @@ -29,11 +29,14 @@ steps: - name: test suite image: rust:1.54.0-slim-bullseye commands: - - apt-get -qq update && apt-get -yqq install pkg-config libssl-dev build-essential libsqlite3-dev python3.9 python3.9-venv + - apt-get -qq update && apt-get -yqq install pkg-config libssl-dev build-essential libsqlite3-dev python3.9 python3.9-venv postgresql postgresql-client + - sudo -u postgres createuser root + - sudo -u postgres createdb -O root testsuitedb + - psql testsuitedb -c 'CREATE TABLE testsuitetable ();' - cargo install -q --path yama - cargo install -q --path datman - python3.9 -m venv testsuite/.venv - - ./testsuite/.venv/bin/pip install -e testsuite + - ./testsuite/.venv/bin/pip install -e testsuite -e datman-helper-postgres - cd testsuite && ./.venv/bin/green - name: deploy manual diff --git a/datman-helper-postgres/datman_helper_postgres/backup.py b/datman-helper-postgres/datman_helper_postgres/backup.py index 126f820..0ff9f55 100644 --- a/datman-helper-postgres/datman_helper_postgres/backup.py +++ b/datman-helper-postgres/datman_helper_postgres/backup.py @@ -1,5 +1,6 @@ import json import os +import pwd import subprocess import sys from subprocess import PIPE @@ -27,7 +28,7 @@ def cli(): use_lz4 = request_info.get("use_lz4_for_ssh", True) if host_to_use is not None: - hostname = subprocess.check_output("hostname").strip() + hostname = subprocess.check_output("hostname").decode().strip() if hostname == host_to_use: host_to_use = None @@ -40,9 +41,11 @@ def cli(): else: command.append(f"{host_to_use}") elif user_to_use is not None: - command.append("sudo") - command.append("-u") - command.append(user_to_use) + current_username = pwd.getpwuid(os.getuid()).pw_name + if current_username != user_to_use: + command.append("sudo") + command.append("-u") + command.append(user_to_use) command.append("pg_dump") command.append(database_to_use) diff --git a/datman-helper-postgres/setup.py b/datman-helper-postgres/setup.py index 62631df..833fcd8 100644 --- a/datman-helper-postgres/setup.py +++ b/datman-helper-postgres/setup.py @@ -6,7 +6,7 @@ import os import sys from shutil import rmtree -from setuptools import find_packages, setup, Command +from setuptools import Command, find_packages, setup # Package meta-data. NAME = "datman_helper_postgres" diff --git a/testsuite/datmantests/test_postgres_helper.py b/testsuite/datmantests/test_postgres_helper.py new file mode 100644 index 0000000..a550833 --- /dev/null +++ b/testsuite/datmantests/test_postgres_helper.py @@ -0,0 +1,45 @@ +import json +import os +import subprocess +from unittest import TestCase + + +class TestPostgresHelper(TestCase): + def setUp(self): + if "TEST_POSTGRES" not in os.environ: + self.skipTest( + "TEST_POSTGRES environment variable not set. " + "Should be set to a Postgres host, database, user combination, comma-separated." + ) + + def test_helper_fails_on_bad_connection(self): + proc = subprocess.Popen("datman-helper-postgres-backup", stdin=subprocess.PIPE) + proc.stdin.write( + json.dumps( + {"database": "mydatabase", "host": "notmyhost", "user": "bobjones"} + ).encode() + ) + proc.stdin.close() + self.assertNotEqual(proc.wait(), 0) + + def test_helper_succeeds_on_correct_config(self): + pg_host, pg_database, pg_user = os.environ["TEST_POSTGRES"].split(",") + + proc = subprocess.Popen( + "datman-helper-postgres-backup", + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + ) + proc.stdin.write( + json.dumps( + { + "database": pg_database or None, + "host": pg_host or None, + "user": pg_user or None, + } + ).encode() + ) + proc.stdin.close() + stdout = proc.stdout.read() + self.assertEqual(proc.wait(), 0) + self.assertIn(b"CREATE TABLE", stdout) diff --git a/testsuite/scripts-dev/lint.sh b/testsuite/scripts-dev/lint.sh deleted file mode 100755 index bfbe962..0000000 --- a/testsuite/scripts-dev/lint.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh -eu - -if [ $# -ge 1 ] -then - files=$* -else - files="setup.py datmantests helpers yamatests" -fi - -echo "Linting these locations: $files" -echo " ===== Running isort ===== " -isort $files -echo " ===== Running black ===== " -black $files -echo " ===== Running flake8 ===== " -flake8 $files -#echo " ===== Running mypy ===== " -#mypy $files