Add Dev Mode and fix test race conditions.

This commit is contained in:
Ben Rog-Wilhelm 2022-11-09 21:29:18 -06:00 committed by GitHub
parent 947453869e
commit adc24ec91e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 14 deletions

View file

@ -1,4 +1,6 @@
FROM python:3.11
###################################################################
# Base/release container
FROM python:3.11 AS release
ARG DEBIAN_FRONTEND=noninteractive
@ -19,3 +21,16 @@ EXPOSE 80/tcp
CMD [ "/usr/bin/supervisord", "-c", "/etc/supervisord.conf" ]
###################################################################
# Dev container
FROM release AS dev
# we don't actually do anything different yet
###################################################################
# Utility container for running commands (tests, most notably)
FROM release AS operation
# don't run the server itself, just start up the environment and assume we'll exec things from the outside
CMD sleep infinity

View file

@ -0,0 +1,5 @@
services:
files:
container_name: "themotte"
build:
target: operation

View file

@ -5,6 +5,7 @@ services:
container_name: "themotte"
build:
context: .
target: dev
volumes:
- "./:/service"
env_file: env
@ -21,11 +22,13 @@ services:
- postgres
redis:
container_name: "themotte_redis"
image: redis
ports:
- "6379:6379"
postgres:
container_name: "themotte_postgres"
image: postgres:12.3
# command: ["postgres", "-c", "log_statement=all"]
# uncomment this if u wanna output all SQL queries to the console

View file

@ -1,7 +1,7 @@
import sys, subprocess, time
def _execute(command,**kwargs):
check = kwargs.get('check',False)
check = kwargs.get('check', True)
on_stdout_line = kwargs.get('on_stdout_line', None)
on_stderr_line = kwargs.get('on_stderr_line', None)
with subprocess.Popen(
@ -51,29 +51,59 @@ def _docker(command, **kwargs):
' && '.join(command)
], **kwargs)
def _running():
command = ['docker','container','inspect','-f','{{.State.Status}}','themotte']
result = _execute(command,check=False).stdout.strip()
return result == "running"
def _status_single(server):
command = ['docker', 'container', 'inspect', '-f', '{{.State.Status}}', server]
result = _execute(command, check=False).stdout.strip()
print(f"{server}: {result}")
return result
# this should really be yanked out of the docker-compose somehow
_containers = ["themotte", "themotte_postgres", "themotte_redis"]
def _all_running():
for container in _containers:
if _status_single(container) != "running":
return False
return True
def _any_exited():
for container in _containers:
if _status_single(container) == "exited":
return True
return False
def _start():
command = ['docker-compose','up','--build','-d']
result = _execute(command,check=True)
time.sleep(1)
command = [
'docker-compose',
'-f', 'docker-compose.yml',
'-f', 'docker-compose-operation.yml',
'up',
'--build',
'-d',
]
result = _execute(command)
while not _all_running():
if _any_exited():
raise RuntimeError("Server exited prematurely")
time.sleep(1)
return result
def _stop():
command = ['docker-compose','down']
result = _execute(command,check=True)
result = _execute(command)
time.sleep(1)
return result
def _operation(name, command):
# check if running and start if not
running = _running()
running = _all_running()
if not running:
print("Starting containers...")
print("Starting containers . . .")
_start()
# run operation in docker container
@ -85,7 +115,7 @@ def _operation(name, command):
)
if not running:
print("Stopping containers...")
print("Stopping containers . . .")
_stop()
return result

View file

@ -4,7 +4,7 @@ import sys
from common import _operation
def run_test(args):
result = _operation("tests",[
result = _operation("tests", [
"FLASK_APP=files/cli:app python3 -m flask db upgrade",
"python3 -m pytest -s",
])