From 72ace2bf0be4c7d51f30690a9a409364877f0772 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Thu, 21 Mar 2024 13:30:53 +0000 Subject: [PATCH] Add an integration test for roles Add an app with roles to the integration tests. We'll deploy two web containers and one worker. The worker just sleeps, so we are testing that the container has booted. --- test/integration/docker-compose.yml | 4 ++ test/integration/docker/deployer/Dockerfile | 2 + .../docker/deployer/app_with_roles/.env.erb | 1 + .../app_with_roles/.kamal/hooks/docker-setup | 3 ++ .../app_with_roles/.kamal/hooks/post-deploy | 3 ++ .../.kamal/hooks/post-traefik-reboot | 3 ++ .../app_with_roles/.kamal/hooks/pre-build | 3 ++ .../app_with_roles/.kamal/hooks/pre-connect | 8 ++++ .../app_with_roles/.kamal/hooks/pre-deploy | 3 ++ .../.kamal/hooks/pre-traefik-reboot | 3 ++ .../docker/deployer/app_with_roles/Dockerfile | 9 +++++ .../deployer/app_with_roles/config/deploy.yml | 37 +++++++++++++++++++ .../deployer/app_with_roles/default.conf | 17 +++++++++ test/integration/main_test.rb | 20 ++++++++++ 14 files changed, 116 insertions(+) create mode 100644 test/integration/docker/deployer/app_with_roles/.env.erb create mode 100755 test/integration/docker/deployer/app_with_roles/.kamal/hooks/docker-setup create mode 100755 test/integration/docker/deployer/app_with_roles/.kamal/hooks/post-deploy create mode 100755 test/integration/docker/deployer/app_with_roles/.kamal/hooks/post-traefik-reboot create mode 100755 test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-build create mode 100755 test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-connect create mode 100755 test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-deploy create mode 100755 test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-traefik-reboot create mode 100644 test/integration/docker/deployer/app_with_roles/Dockerfile create mode 100644 test/integration/docker/deployer/app_with_roles/config/deploy.yml create mode 100644 test/integration/docker/deployer/app_with_roles/default.conf diff --git a/test/integration/docker-compose.yml b/test/integration/docker-compose.yml index ae28e2f0..da67f164 100644 --- a/test/integration/docker-compose.yml +++ b/test/integration/docker-compose.yml @@ -62,3 +62,7 @@ services: context: docker/load_balancer ports: - "12345:80" + depends_on: + - vm1 + - vm2 + - vm3 diff --git a/test/integration/docker/deployer/Dockerfile b/test/integration/docker/deployer/Dockerfile index de908e6a..bb6b462a 100644 --- a/test/integration/docker/deployer/Dockerfile +++ b/test/integration/docker/deployer/Dockerfile @@ -18,6 +18,7 @@ RUN apt-get update --fix-missing && apt-get install -y docker-ce docker-ce-cli c COPY *.sh . COPY app/ app/ +COPY app_with_roles/ app_with_roles/ RUN rm -rf /root/.ssh RUN ln -s /shared/ssh /root/.ssh @@ -26,6 +27,7 @@ RUN mkdir -p /etc/docker/certs.d/registry:4443 && ln -s /shared/certs/domain.crt RUN git config --global user.email "deployer@example.com" RUN git config --global user.name "Deployer" RUN cd app && git init && echo ".env" >> .gitignore && git add . && git commit -am "Initial version" +RUN cd app_with_roles && git init && echo ".env" >> .gitignore && git add . && git commit -am "Initial version" HEALTHCHECK --interval=1s CMD pgrep sleep diff --git a/test/integration/docker/deployer/app_with_roles/.env.erb b/test/integration/docker/deployer/app_with_roles/.env.erb new file mode 100644 index 00000000..dcd2fcf5 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/.env.erb @@ -0,0 +1 @@ +SECRET_TOKEN=1234 diff --git a/test/integration/docker/deployer/app_with_roles/.kamal/hooks/docker-setup b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/docker-setup new file mode 100755 index 00000000..2c218dc5 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/docker-setup @@ -0,0 +1,3 @@ +#!/bin/sh +echo "Docker set up!" +mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/docker-setup diff --git a/test/integration/docker/deployer/app_with_roles/.kamal/hooks/post-deploy b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/post-deploy new file mode 100755 index 00000000..0fcc6920 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/post-deploy @@ -0,0 +1,3 @@ +#!/bin/sh +echo "Deployed!" +mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/post-deploy diff --git a/test/integration/docker/deployer/app_with_roles/.kamal/hooks/post-traefik-reboot b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/post-traefik-reboot new file mode 100755 index 00000000..598ddb63 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/post-traefik-reboot @@ -0,0 +1,3 @@ +#!/bin/sh +echo "Rebooted Traefik on ${KAMAL_HOSTS}" +mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/post-traefik-reboot diff --git a/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-build b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-build new file mode 100755 index 00000000..0cf78d88 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-build @@ -0,0 +1,3 @@ +#!/bin/sh +echo "About to build and push..." +mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-build diff --git a/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-connect b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-connect new file mode 100755 index 00000000..10095286 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-connect @@ -0,0 +1,8 @@ +#!/bin/sh + +echo "About to lock..." +if [ "$KAMAL_HOSTS" != "vm1,vm2,vm3" ]; then + echo "Expected hosts to be 'vm1,vm2,vm3', got $KAMAL_HOSTS" + exit 1 +fi +mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-connect diff --git a/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-deploy b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-deploy new file mode 100755 index 00000000..32fa04c6 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-deploy @@ -0,0 +1,3 @@ +#!/bin/sh +echo "Deployed!" +mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-deploy diff --git a/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-traefik-reboot b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-traefik-reboot new file mode 100755 index 00000000..81269d24 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-traefik-reboot @@ -0,0 +1,3 @@ +#!/bin/sh +echo "Rebooting Traefik on ${KAMAL_HOSTS}..." +mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-traefik-reboot diff --git a/test/integration/docker/deployer/app_with_roles/Dockerfile b/test/integration/docker/deployer/app_with_roles/Dockerfile new file mode 100644 index 00000000..dc270aa9 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/Dockerfile @@ -0,0 +1,9 @@ +FROM registry:4443/nginx:1-alpine-slim + +COPY default.conf /etc/nginx/conf.d/default.conf + +ARG COMMIT_SHA +RUN echo $COMMIT_SHA > /usr/share/nginx/html/version +RUN mkdir -p /usr/share/nginx/html/versions && echo "version" > /usr/share/nginx/html/versions/$COMMIT_SHA +RUN mkdir -p /usr/share/nginx/html/versions && echo "hidden" > /usr/share/nginx/html/versions/.hidden + diff --git a/test/integration/docker/deployer/app_with_roles/config/deploy.yml b/test/integration/docker/deployer/app_with_roles/config/deploy.yml new file mode 100644 index 00000000..004ffb25 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/config/deploy.yml @@ -0,0 +1,37 @@ +service: app +image: app +servers: + web: + hosts: + - vm1 + - vm2 + workers: + hosts: + - vm3 + cmd: sleep infinity + +asset_path: /usr/share/nginx/html/versions + +registry: + server: registry:4443 + username: root + password: root +builder: + multiarch: false + args: + COMMIT_SHA: <%= `git rev-parse HEAD` %> +healthcheck: + cmd: wget -qO- http://localhost > /dev/null || exit 1 +traefik: + args: + accesslog: true + accesslog.format: json + image: registry:4443/traefik:v2.10 +accessories: + busybox: + service: custom-busybox + image: registry:4443/busybox:1.36.0 + cmd: sh -c 'echo "Starting busybox..."; trap exit term; while true; do sleep 1; done' + roles: + - web +stop_wait_time: 1 diff --git a/test/integration/docker/deployer/app_with_roles/default.conf b/test/integration/docker/deployer/app_with_roles/default.conf new file mode 100644 index 00000000..e37a9bc1 --- /dev/null +++ b/test/integration/docker/deployer/app_with_roles/default.conf @@ -0,0 +1,17 @@ +server { + listen 80; + listen [::]:80; + server_name localhost; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + } + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } +} diff --git a/test/integration/main_test.rb b/test/integration/main_test.rb index 22f60860..7c858a17 100644 --- a/test/integration/main_test.rb +++ b/test/integration/main_test.rb @@ -42,6 +42,22 @@ class MainTest < IntegrationTest assert_no_remote_env_file end + test "app with roles" do + @app = "app_with_roles" + + kamal :envify + + version = latest_app_version + + assert_app_is_down + + kamal :deploy + + assert_app_is_up version: version + assert_hooks_ran "pre-connect", "pre-build", "pre-deploy", "post-deploy" + assert_container_running host: :vm3, name: "app-workers-#{version}" + end + test "config" do config = YAML.load(kamal(:config, capture: true)) version = latest_app_version @@ -115,4 +131,8 @@ class MainTest < IntegrationTest assert vm1_image_ids.any? assert vm1_container_ids.any? end + + def assert_container_running(host:, name:) + assert docker_compose("exec #{host} docker ps --filter=name=#{name} -q", capture: true).strip.present? + end end