diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 3b6d0f8..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,9 +0,0 @@ -[build] -target = "x86_64-unknown-linux-musl" - -[target.x86_64-unknown-linux-musl] -rustflags = [ - "-Ctarget-feature=+crt-static", - "-Clink-args=-Istatic/include", - "-L", "static/lib", -] diff --git a/.gitignore b/.gitignore index f8abd6f..fa726f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ -/target +target/ **/*.rs.bk -/.idea -/data \ No newline at end of file +.idea/ +data/ +static/ +**/*.pyc \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..523cf6b --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +.PHONY: build run clean + +CARGO=./scripts/cargo_musl.sh + +clean: + ${CARGO} clean + +build: + cd muslrust + docker build -t unki/musllibs ./muslrust + cd - + ${CARGO} build $@ + +run: build + ${CARGO} run \ No newline at end of file diff --git a/bin/u_server/Dockerfile b/bin/u_server/Dockerfile deleted file mode 100644 index 0a2a76f..0000000 --- a/bin/u_server/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM centos:7 - -CMD yum update \ No newline at end of file diff --git a/integration/.env b/integration/.env index 47bc1e8..f22b345 100644 --- a/integration/.env +++ b/integration/.env @@ -1,2 +1,2 @@ -DATABASE_URL=postgres://postgres:12348756@u_db/u_db -PG_PASSWORD=12348756 \ No newline at end of file +PG_PASSWORD=12348756 +DATABASE_URL=postgres://postgres:${PG_PASSWORD}@u_db/u_db diff --git a/integration/docker-compose.yml b/integration/docker-compose.yml index c4c3ec6..a080377 100644 --- a/integration/docker-compose.yml +++ b/integration/docker-compose.yml @@ -11,26 +11,39 @@ services: - u_net volumes: - ../target/x86_64-unknown-linux-musl/release/u_server:/u_server - - ./.env:/.env - command: /u_server + - ../:/unki/ + working_dir: /unki + command: bash -c "diesel setup && diesel migration run && /u_server" depends_on: u_db: - condition: service_started + condition: service_healthy expose: - '63714' environment: RUST_LOG: warp + env_file: + - .env + healthcheck: + test: /bin/ss -tlpn | grep 63714 + interval: 5s + timeout: 2s + retries: 2 u_db: - image: postgres:13.3 + image: unki/u_db networks: - u_net - volumes: - - ../data:/var/lib/postgresql/data + #volumes: + # - ../data:/var/lib/postgresql/data expose: - '5432' environment: - POSTGRES_PASSWORD=${PG_PASSWORD} + healthcheck: + test: /bin/ss -tlpn | grep 5432 + interval: 5s + timeout: 2s + retries: 2 u_agent_1: image: unki/u_agent @@ -41,7 +54,7 @@ services: command: /u_agent u_server depends_on: u_server: - condition: service_started + condition: service_healthy u_agent_2: image: unki/u_agent @@ -52,7 +65,7 @@ services: command: /u_agent u_server depends_on: u_server: - condition: service_started + condition: service_healthy tests_runner: image: unki/tests_runner @@ -60,6 +73,12 @@ services: - u_net volumes: - ../:/unki/ + working_dir: + /unki depends_on: + u_agent_1: + condition: service_started + u_agent_2: + condition: service_started u_server: - condition: service_started \ No newline at end of file + condition: service_healthy \ No newline at end of file diff --git a/integration/docker.py b/integration/docker.py index d2f8115..23a07b3 100644 --- a/integration/docker.py +++ b/integration/docker.py @@ -1,15 +1,23 @@ import subprocess from utils import * +BASE_IMAGE_DIR = 'images' + DOCKERFILES = { 'u_agent': { - 'ctx': '../bin/u_agent', + 'ctx': BASE_IMAGE_DIR, + 'dockerfile_prefix': 'u_agent' }, 'u_server': { - 'ctx': '../bin/u_server' + 'ctx': BASE_IMAGE_DIR, + 'dockerfile_prefix': 'u_server' + }, + 'u_db': { + 'ctx': BASE_IMAGE_DIR, + 'dockerfile_prefix': 'u_db' }, 'tests_runner': { - 'ctx': './', + 'ctx': BASE_IMAGE_DIR, 'dockerfile_prefix': 'tests_runner' }, } @@ -18,10 +26,8 @@ DOCKERFILES = { def docker(args): try: cmd = ['docker'] + args - #log(f'Running docker cmd: {cmd}') - return subprocess.check_output( - cmd - ) + log(f'Running docker command: {cmd}') + return subprocess.check_output(cmd) except subprocess.CalledProcessError as e: err(str(e)) raise diff --git a/integration/docker_compose.py b/integration/docker_compose.py index 82fc66a..0b71cdc 100644 --- a/integration/docker_compose.py +++ b/integration/docker_compose.py @@ -1,4 +1,5 @@ import subprocess +import shlex from utils import * from docker import docker, check_state, print_errors @@ -13,7 +14,9 @@ class Compose: ] def __init__(self): - self.cmd_container = 'tests_runner' + self.container_tpl = 'integration_%s_1' + self.cmd_container = self.container_tpl % 'tests_runner' + self.ALL_CONTAINERS = [self.container_tpl % c for c in self.ALL_CONTAINERS] def _call(self, *args): subprocess.check_call([ @@ -24,7 +27,7 @@ class Compose: def up(self): log('Instanciating cluster') - self._call('up') + self._call('up', '-d') log('Ok') def down(self): @@ -39,13 +42,14 @@ class Compose: def run(self, cmd): container = self.cmd_container + if isinstance(cmd, str): + cmd = shlex.split(cmd) log(f'Running command "{cmd}" in container {container}') docker([ 'exec', '-i', - f"'{container}'", - f"'{cmd}'" - ]) + container + ] + cmd) log('Ok') def is_alive(self): diff --git a/integration/images/tests_runner.Dockerfile b/integration/images/tests_runner.Dockerfile new file mode 100644 index 0000000..2411bd6 --- /dev/null +++ b/integration/images/tests_runner.Dockerfile @@ -0,0 +1,4 @@ +FROM rust:1.53 + +RUN rustup target add x86_64-unknown-linux-musl +CMD ["sleep", "3600"] \ No newline at end of file diff --git a/bin/u_agent/Dockerfile b/integration/images/u_agent.Dockerfile similarity index 100% rename from bin/u_agent/Dockerfile rename to integration/images/u_agent.Dockerfile diff --git a/integration/images/u_db.Dockerfile b/integration/images/u_db.Dockerfile new file mode 100644 index 0000000..8577c8d --- /dev/null +++ b/integration/images/u_db.Dockerfile @@ -0,0 +1,3 @@ +FROM postgres:13.3 + +RUN apt update && apt -y upgrade && apt install -y iproute2 \ No newline at end of file diff --git a/integration/images/u_server.Dockerfile b/integration/images/u_server.Dockerfile new file mode 100644 index 0000000..ea82ccd --- /dev/null +++ b/integration/images/u_server.Dockerfile @@ -0,0 +1,3 @@ +FROM rust:1.53 + +RUN cargo install diesel_cli --no-default-features --features postgres \ No newline at end of file diff --git a/integration/integration_tests.py b/integration/integration_tests.py index 57f111e..e4fe460 100644 --- a/integration/integration_tests.py +++ b/integration/integration_tests.py @@ -15,9 +15,11 @@ def abort_handler(s, _): def run_tests(): + force_rebuild = '--rebuild' in sys.argv + preserve_containers = '--preserve' in sys.argv for s in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP): signal.signal(s, abort_handler) - rebuild_images_if_needed() + rebuild_images_if_needed(force_rebuild) try: cluster.up() cluster.is_alive() @@ -26,7 +28,8 @@ def run_tests(): err(e) sys.exit(1) finally: - cluster.down() + if not preserve_containers: + cluster.down() if __name__ == '__main__': diff --git a/integration/integration_tests.sh b/integration/integration_tests.sh index 234fac0..3f3eca1 100755 --- a/integration/integration_tests.sh +++ b/integration/integration_tests.sh @@ -1,5 +1,3 @@ #!/bin/bash set -e -CARGO=cargo -$CARGO build --release --target x86_64-unknown-linux-musl -python integration_tests.py +python integration_tests.py $@ diff --git a/integration/tests_runner.Dockerfile b/integration/tests_runner.Dockerfile deleted file mode 100644 index 0367673..0000000 --- a/integration/tests_runner.Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM rust:1.53 - -CMD rustup target add x86_64-unknown-linux-musl \ No newline at end of file diff --git a/scripts/cargo_musl.sh b/scripts/cargo_musl.sh index b2c6abf..5e1395f 100755 --- a/scripts/cargo_musl.sh +++ b/scripts/cargo_musl.sh @@ -6,6 +6,6 @@ docker run \ -v cargo-cache:/root/.cargo/registry \ -w /volume \ -it \ - test/musllibs \ + unki/musllibs \ cargo $@