sqlx/tests/docker.py
Thibs c4130d45e3 Add client SSL authentication using key-file for Postgres, MySQL and MariaDB (#1850)
* use native-tls API

* Add client cert and key to MySQL connector

* Add client ssl tests for PostgreSQL

* Add client ssl tests for MariaDB and MySQL

* Adapt GA tests

* Fix RUSTFLAGS to run all tests

* Remove containers to free the DB port before running SSL auth tests

* Fix CI bad naming

* Use docker-compose down to remove also the network

* Fix main rebase

* Stop trying to stop service using docker-compose, simply use docker cmd

* Fix RUSTFLAGS for Postgres

* Name the Docker images for MariaDB and MySQL so we can stop them using their name

* Add the exception for mysql 5.7 not supporting compatible TLS version with RusTLS

* Rebase fixes

* Set correctly tls struct (fix merge)

* Handle Elliptic Curve variant for private key

* Fix tests suite

* Fix features in CI

* Add tests for Postgres 15 + rebase

* Python tests: fix exception for MySQL 5.7 + remove unneeded for loops

* CI: run SSL tests only when building with TLS support

---------

Co-authored-by: Barry Simons <linuxuser586@gmail.com>
2023-02-21 13:25:25 -08:00

82 lines
2.2 KiB
Python

import subprocess
import sys
import time
from os import path
# base dir of sqlx workspace
dir_workspace = path.dirname(path.dirname(path.realpath(__file__)))
# dir of tests
dir_tests = path.join(dir_workspace, "tests")
# start database server and return a URL to use to connect
def start_database(driver, database, cwd):
if driver == "sqlite":
# short-circuit for sqlite
return f"sqlite://{path.join(cwd, database)}"
res = subprocess.run(
["docker-compose", "up", "-d", driver],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=dir_tests,
)
if res.returncode != 0:
print(res.stderr, file=sys.stderr)
if b"done" in res.stderr:
time.sleep(30)
# determine appropriate port for driver
if driver.startswith("mysql") or driver.startswith("mariadb"):
port = 3306
elif driver.startswith("postgres"):
port = 5432
else:
raise NotImplementedError
# find port
res = subprocess.run(
["docker", "inspect", f"-f='{{{{(index (index .NetworkSettings.Ports \"{port}/tcp\") 0).HostPort}}}}'",
f"sqlx_{driver}_1"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=dir_tests,
)
if res.returncode != 0:
print(res.stderr, file=sys.stderr)
port = int(res.stdout[1:-2].decode())
# need additional permissions to connect to MySQL when using SSL
res = subprocess.run(
["docker", "exec", f"sqlx_{driver}_1", "mysql", "-u", "root", "-e", "GRANT ALL PRIVILEGES ON *.* TO 'root' WITH GRANT OPTION;"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=dir_tests,
)
if res.returncode != 0:
print(res.stderr, file=sys.stderr)
# do not set password in URL if authenticating using SSL key file
if driver.endswith("client_ssl"):
password = ""
else:
password = ":password"
# construct appropriate database URL
if driver.startswith("mysql") or driver.startswith("mariadb"):
return f"mysql://root{password}@localhost:{port}/{database}"
elif driver.startswith("postgres"):
return f"postgres://postgres{password}@localhost:{port}/{database}"
else:
raise NotImplementedError