Test and fix Postgres helper

This commit is contained in:
Olivier 'reivilibre' 2021-08-15 20:05:23 +01:00
parent 6d24ccb248
commit e5bd13b8c1
5 changed files with 58 additions and 25 deletions

View File

@ -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

View File

@ -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,6 +41,8 @@ def cli():
else:
command.append(f"{host_to_use}")
elif user_to_use is not None:
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)

View File

@ -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"

View File

@ -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)

View File

@ -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