From 032779b2b8ee7cbee84f92b0dccad17e9ecab66f Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Tue, 14 Nov 2023 19:03:36 +0100 Subject: [PATCH 1/6] Add integration test --- .github/workflows/test.yml | 24 +++++++++++++ lib/action/pipeline_awaiter.rb | 1 + lib/action/step/start_runner.rb | 1 + test/await_healthy.sh | 35 +++++++++++++++++++ test/docker-compose.yml | 19 ++++++++++ test/healthcheck_and_setup.sh | 62 +++++++++++++++++++++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100755 test/await_healthy.sh create mode 100644 test/docker-compose.yml create mode 100755 test/healthcheck_and_setup.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..dba12a0 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,24 @@ +name: Run Integration test + +on: + push: + pull_request: + +jobs: + integration-test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - run: docker compose up -d + working-directory: test + - run: test/await_healthy.sh + - uses: ./ + name: Run test + with: + GL_SERVER_URL: http://127.0.0.1:8080 + GL_PROJECT_ID: '1000' + GL_RUNNER_TOKEN: some_long_runner_token + GL_API_TOKEN: TEST1234567890123456 + - run: docker compose down + working-directory: test diff --git a/lib/action/pipeline_awaiter.rb b/lib/action/pipeline_awaiter.rb index cc7743e..32448fb 100644 --- a/lib/action/pipeline_awaiter.rb +++ b/lib/action/pipeline_awaiter.rb @@ -26,6 +26,7 @@ def wait! puts "Pipeline succeeded in #{duration} minutes!" return else + puts puts "Pipeline #{status} in #{duration} minutes!" raise PipelineFailed, 'Pipeline did not succeed!' end diff --git a/lib/action/step/start_runner.rb b/lib/action/step/start_runner.rb index fa7f5e3..fd692e4 100644 --- a/lib/action/step/start_runner.rb +++ b/lib/action/step/start_runner.rb @@ -13,6 +13,7 @@ def execute 'Binds' => [ '/var/run/docker.sock:/var/run/docker.sock:ro' ], + 'NetworkMode' => 'host' } ) diff --git a/test/await_healthy.sh b/test/await_healthy.sh new file mode 100755 index 0000000..d0088dd --- /dev/null +++ b/test/await_healthy.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +CONTAINER_ENGINE="${CONTAINER_ENGINE:-docker}" +GITLAB_BASE_URL="${GITLAB_BASE_URL:-http://127.0.0.1:8080}" + +set -e + +if [ "$CONTAINER_ENGINE" != "docker" ]; then + echo "Using container engine $CONTAINER_ENGINE" +fi + +printf 'Waiting for GitLab container to become healthy' + +until test -n "$($CONTAINER_ENGINE ps --quiet --filter label=gitlab-pipeline-action/owned --filter health=healthy)"; do + printf '.' + sleep 5 +done + +echo +echo "GitLab is healthy at $GITLAB_BASE_URL" + +# Print the version, since it is useful debugging information. +curl --silent --show-error --header "Private-Token: TEST1234567890123456" "$GITLAB_BASE_URL/api/v4/version" + +echo +printf 'Waiting for Test project to be imported' + +import_status="" +while [[ "$import_status" != "finished" ]]; do + printf '.' + import_status=$(curl --silent --show-error --header "Private-Token: TEST1234567890123456" "$GITLAB_BASE_URL/api/v4/projects/1000" | jq -r .import_status) + sleep 5 +done + +echo diff --git a/test/docker-compose.yml b/test/docker-compose.yml new file mode 100644 index 0000000..3fa0f5a --- /dev/null +++ b/test/docker-compose.yml @@ -0,0 +1,19 @@ +services: + gitlab-ce: + image: gitlab/gitlab-ce:${GITLAB_CE_VERSION:-latest} + restart: always + ports: + - 8080:80 + environment: + GITLAB_ROOT_PASSWORD: routrout + GITLAB_OMNIBUS_CONFIG: | + external_url 'http://127.0.0.1:8080' + nginx[:listen_port] = 80 + labels: + gitlab-pipeline-action/owned: '' + volumes: + - ./healthcheck_and_setup.sh:/healthcheck_and_setup.sh:Z + healthcheck: + test: /healthcheck_and_setup.sh + interval: 10s + timeout: 3m diff --git a/test/healthcheck_and_setup.sh b/test/healthcheck_and_setup.sh new file mode 100755 index 0000000..d67a032 --- /dev/null +++ b/test/healthcheck_and_setup.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env sh + +# This script is intended to be used as a Docker HEALTHCHECK for the GitLab container. +# It prepares GitLab prior to running tests. +# +# This is a known workaround for docker-compose lacking lifecycle hooks. +# See: https://github.com/docker/compose/issues/1809#issuecomment-657815188 + +set -e + +# Check for a successful HTTP status code from GitLab. +curl --silent --show-error --fail --output /dev/null 127.0.0.1:80 + +# Because this script runs on a regular health check interval, +# this file functions as a marker that tells us if initialization already finished. +done=/var/gitlab-test-initialized + +test -f $done || { + echo 'Initializing GitLab for tests' + + echo 'Creating access token' + ( + printf 'token = PersonalAccessToken.create(' + printf 'user_id: 1, ' + printf 'scopes: [:api, :write_repository], ' + printf 'expires_at: Time.now + 30.days, ' + printf 'name: :token);' + printf "token.set_token('TEST1234567890123456');" + printf 'token.save!;' + + printf 'settings = ApplicationSetting.current;' + printf 'settings.import_sources << "git";' + printf 'settings.save!;' + + printf 'Projects::CreateService.new(User.find(1), {' + printf 'namespace_id: User.first.namespace.id,' + printf 'name: "Test",' + printf 'path: "test",' + printf 'id: 1000,' + printf 'import_type: "git",' + printf 'import_url: "https://github.com/Taucher2003/Gitlab-Pipeline-Action.git"' + printf '}).execute;' + + printf 'Ci::Runners::CreateRunnerService.new(' + printf 'user: User.first,' + printf 'params: {' + printf 'runner_type: "instance_type",' + printf 'token: "some_long_runner_token"' + printf '}' + printf ').execute;' + + printf 'Ci::ChangeVariableService.new(' + printf 'container: Project.find(1000),' + printf 'current_user: User.first,' + printf 'params: { action: :create, variable_params: { key: "GIT_STRATEGY", value: "none", protected: false } }' + printf ').execute;' + ) | gitlab-rails console + + touch $done +} + +echo 'GitLab is ready for tests' From 18503bf9b52cd4b7fbbaae58d43272832657cbe5 Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Tue, 14 Nov 2023 22:08:52 +0100 Subject: [PATCH 2/6] Update gitlab host --- .github/workflows/test.yml | 2 +- lib/action/step/start_runner.rb | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dba12a0..d8d4af9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,7 +16,7 @@ jobs: - uses: ./ name: Run test with: - GL_SERVER_URL: http://127.0.0.1:8080 + GL_SERVER_URL: http://172.17.0.1:8080 GL_PROJECT_ID: '1000' GL_RUNNER_TOKEN: some_long_runner_token GL_API_TOKEN: TEST1234567890123456 diff --git a/lib/action/step/start_runner.rb b/lib/action/step/start_runner.rb index fd692e4..fa7f5e3 100644 --- a/lib/action/step/start_runner.rb +++ b/lib/action/step/start_runner.rb @@ -13,7 +13,6 @@ def execute 'Binds' => [ '/var/run/docker.sock:/var/run/docker.sock:ro' ], - 'NetworkMode' => 'host' } ) From 1d0e7e10ce03605f57bee03606c25f324e99c413 Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Wed, 15 Nov 2023 08:45:12 +0100 Subject: [PATCH 3/6] Add job log as debug output --- .github/workflows/test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d8d4af9..ee6d453 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,5 +20,8 @@ jobs: GL_PROJECT_ID: '1000' GL_RUNNER_TOKEN: some_long_runner_token GL_API_TOKEN: TEST1234567890123456 + - run: 'curl --silent --header "Private-Token: TEST1234567890123456" "http://127.17.0.1:8080/api/v4/projects/1000/jobs/1/trace"' + if: always() - run: docker compose down + if: always() working-directory: test From cd3a748fc392d804dbfac6b7e47d44da1d71642e Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Wed, 15 Nov 2023 12:33:44 +0100 Subject: [PATCH 4/6] Add testing pipeline --- test/.gitlab-ci.yml | 3 +++ test/healthcheck_and_setup.sh | 1 + 2 files changed, 4 insertions(+) create mode 100644 test/.gitlab-ci.yml diff --git a/test/.gitlab-ci.yml b/test/.gitlab-ci.yml new file mode 100644 index 0000000..e6f6b04 --- /dev/null +++ b/test/.gitlab-ci.yml @@ -0,0 +1,3 @@ +job: + script: + - echo $CI_COMMIT_SHA diff --git a/test/healthcheck_and_setup.sh b/test/healthcheck_and_setup.sh index d67a032..f26dab2 100755 --- a/test/healthcheck_and_setup.sh +++ b/test/healthcheck_and_setup.sh @@ -36,6 +36,7 @@ test -f $done || { printf 'namespace_id: User.first.namespace.id,' printf 'name: "Test",' printf 'path: "test",' + printf 'ci_config_path: "test/.gitlab-ci.yml",' printf 'id: 1000,' printf 'import_type: "git",' printf 'import_url: "https://github.com/Taucher2003/Gitlab-Pipeline-Action.git"' From 243d8de4e3ac49235bb7dd4fc4b3256d36b392a8 Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Wed, 15 Nov 2023 12:58:35 +0100 Subject: [PATCH 5/6] Add matrix for gitlab version --- .github/workflows/test.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ee6d453..a3fbc50 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,10 +7,16 @@ on: jobs: integration-test: runs-on: ubuntu-latest + strategy: + matrix: + gitlab_version: + - 16.5.2-ce.0 + - 16.4.2-ee.0 + - 16.3.6-ce.0 steps: - uses: actions/checkout@v4 - - run: docker compose up -d + - run: GITLAB_CE_VERSION=${{ matrix.gitlab_version }} docker compose up -d working-directory: test - run: test/await_healthy.sh - uses: ./ From 9387beb18dba19ed1df7011f349104342270ba7c Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Wed, 15 Nov 2023 13:01:49 +0100 Subject: [PATCH 6/6] Fix image tag for 16.4.2 --- .github/workflows/test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a3fbc50..cc9b92d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,10 +8,11 @@ jobs: integration-test: runs-on: ubuntu-latest strategy: + fail-fast: false matrix: gitlab_version: - 16.5.2-ce.0 - - 16.4.2-ee.0 + - 16.4.2-ce.0 - 16.3.6-ce.0 steps: