From d887a3aeb2323d7bfca66cf5214066a20eb5b97a Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Thu, 29 Feb 2024 07:59:34 +1100 Subject: [PATCH] Moved common DrevOps tests to GHA. --- .circleci/config.yml | 129 +-------------- .drevops/tests/bats/circleci.bats | 2 +- .drevops/tests/test.deployment.sh | 2 +- .drevops/tests/test.workflow.sh | 6 +- .github/workflows/drevops-test-common.yml | 189 ++++++++++++++++++++++ 5 files changed, 199 insertions(+), 129 deletions(-) create mode 100644 .github/workflows/drevops-test-common.yml diff --git a/.circleci/config.yml b/.circleci/config.yml index 792ec26c5..c58978d2e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -374,93 +374,17 @@ jobs: - run: renovate #;> RENOVATEBOT - #;============================================================================ #; DREVOPS development section. Removed during DrevOps installation/update. #;============================================================================ - #; - #; We are using CircleCI config file to both run the application tests (what - #; a consumer site would use) and DrevOps' own tests (to make sure that - #; this project is "buildable" at any time). - #; #;< DREVOPS_DEV - # #----------------------------------------------------------------------------- # Test suite for DrevOps. #----------------------------------------------------------------------------- - drevops-dev-test: - <<: *runner_config - parallelism: 1 - steps: - - checkout - - *step_process_codebase_for_ci - - *step_setup_remote_docker - - *step_setup_docker_network - - run: - name: Lint DrevOps project - command: cd .drevops && ahoy lint - - run: - name: Run common tests - command: cd .drevops && DREVOPS_DEV_VOLUMES_MOUNTED=0 DREVOPS_DEV_TEST_COVERAGE_DIR=/tmp/artifacts/coverage ahoy test - no_output_timeout: 60m - - store_test_results: - path: *test_results - - store_artifacts: - path: *artifacts - - run: - name: Upload code coverage reports to Codecov - command: codecov -Z -s /tmp/artifacts/coverage - - drevops-dev-test-workflow: - <<: *runner_config - parallelism: 3 - resource_class: large - steps: - - checkout - - *step_process_codebase_for_ci - - *step_setup_remote_docker - - *step_setup_docker_network - - run: - name: Run workflow tests (long) - command: cd .drevops && DREVOPS_DEV_VOLUMES_MOUNTED=0 DREVOPS_DEV_TEST_COVERAGE_DIR=/tmp/artifacts/coverage ahoy test-workflow - no_output_timeout: 60m - - store_test_results: - path: *test_results - - store_artifacts: - path: *artifacts - - run: - name: Upload code coverage reports to Codecov - command: codecov -Z -s /tmp/artifacts/coverage - - drevops-dev-test-deployment: - <<: *runner_config - parallelism: 2 - steps: - - checkout - - *step_process_codebase_for_ci - - *step_setup_remote_docker - - *step_setup_docker_network - - add_ssh_keys: - fingerprints: - - *deploy_ssh_fingerprint - - attach_workspace: - at: /tmp/workspace - - run: - name: Run deployment tests (long) - command: cd .drevops && DREVOPS_DEV_VOLUMES_MOUNTED=0 DREVOPS_DEV_TEST_COVERAGE_DIR=/tmp/artifacts/coverage ahoy test-deployment - no_output_timeout: 60m - - store_test_results: - path: *test_results - - store_artifacts: - path: *artifacts - - run: - name: Upload code coverage reports to Codecov - command: codecov -Z -s /tmp/artifacts/coverage - # Run tests after 'build' job. - drevops-dev-test-postbuild: + # Run DrevOps tests after 'build' job to test CircleCI's configuration. + drevops-dev-test-ci-postbuild: <<: *runner_config - parallelism: 1 steps: - checkout - *step_process_codebase_for_ci @@ -477,13 +401,6 @@ jobs: name: Upload code coverage reports to Codecov command: codecov -Z -s /tmp/artifacts/coverage - # Deployment of feature branches and tags for DrevOps itself. - drevops-dev-deploy: - <<: *job_deploy - - drevops-dev-deploy-tags: - <<: *job-deploy-tags - #----------------------------------------------------------------------------- # Launching and testing databases stored within Docker data image. #----------------------------------------------------------------------------- @@ -625,54 +542,16 @@ workflows: #;============================================================================ #; #;< DREVOPS_DEV - # Run unit and functional tests for DrevOps. + # Run functional tests for DrevOps. # Note that these jobs must run within the "commit" workflow, because they # depend on the "build" job. - - drevops-dev-test: - filters: - tags: - only: /.*/ - - drevops-dev-test-workflow: - filters: - tags: - only: /.*/ - # Run deployment tests for DrevOps. - - drevops-dev-test-deployment: - filters: - tags: - only: /.*/ # Run tests after 'build' job. - - drevops-dev-test-postbuild: + - drevops-dev-test-ci-postbuild: requires: - build filters: tags: only: /.*/ - # Run actual deployment of DrevOps code to destination codebase (integration test). - # @see https://github.com/drevops/drevops-destination - - drevops-dev-deploy: - requires: - - build - - drevops-dev-test - - drevops-dev-test-workflow - filters: - branches: - # Allowed branches: 7.x, 8.x, 9.x, feature/7.x-description, feature/8.x-description, feature/9.x-description - only: /main|develop|7\.x|feature\/(?:7\.x)[a-zA-z0-9\-\.\,]+/ - tags: - ignore: /.*/ - # Run actual deployment of DrevOps code to destination codebase (integration test). - # @see https://github.com/drevops/drevops-destination - - drevops-dev-deploy-tags: - requires: - - build - - drevops-dev-test - - drevops-dev-test-workflow - filters: - branches: - ignore: /.*/ - tags: - only: /^[0-9]+(\.[0-9]+)+(-rc[0-9]+)?$/ # Test workflow to test DREVOPS_DB_DOCKER_IMAGE workflow for DB from file. drevops-dev-didi-fi: diff --git a/.drevops/tests/bats/circleci.bats b/.drevops/tests/bats/circleci.bats index 312aaba60..84145ee18 100644 --- a/.drevops/tests/bats/circleci.bats +++ b/.drevops/tests/bats/circleci.bats @@ -8,7 +8,7 @@ load _helper.bash load _helper.circleci.bash @test "CircleCI artifacts are saved" { - if [ -z "${CI}" ]; then + if [ -z "${CIRCLECI}" ]; then skip "This test is only run on CircleCI" fi diff --git a/.drevops/tests/test.deployment.sh b/.drevops/tests/test.deployment.sh index 8daa1a5f2..0bc583169 100755 --- a/.drevops/tests/test.deployment.sh +++ b/.drevops/tests/test.deployment.sh @@ -22,7 +22,7 @@ TEST_DIR="${ROOT_DIR}/.drevops/tests" # Create stub of local framework. docker network create amazeeio-network 2>/dev/null || true -index="${CIRCLE_NODE_INDEX:-*}" +index="${TEST_NODE_INDEX:-*}" echo "==> Run deployment functional tests (${index})." [ ! -d "${TEST_DIR}/node_modules" ] && echo " > Install test Node dependencies." && npm --prefix="${TEST_DIR}" ci diff --git a/.drevops/tests/test.workflow.sh b/.drevops/tests/test.workflow.sh index b7dba768b..ae8268cdd 100755 --- a/.drevops/tests/test.workflow.sh +++ b/.drevops/tests/test.workflow.sh @@ -22,7 +22,7 @@ TEST_DIR="${ROOT_DIR}/.drevops/tests" # Create stub of local framework. docker network create amazeeio-network 2>/dev/null || true -index="${CIRCLE_NODE_INDEX:-*}" +index="${TEST_NODE_INDEX:-*}" echo "==> Run workflow functional tests (${index})." [ ! -d "${TEST_DIR}/node_modules" ] && echo " > Install test Node dependencies." && npm --prefix="${TEST_DIR}" ci @@ -61,7 +61,9 @@ case ${index} in bats "${TEST_DIR}"/bats/workflow.smoke.bats bats "${TEST_DIR}"/bats/workflow.install.bats bats "${TEST_DIR}"/bats/workflow.storage.image.bats - bats "${TEST_DIR}"/bats/workflow.storage.image_cached.bats + # Disabled due to intermittent failures. + # @see https://github.com/drevops/drevops/issues/893 + # bats "${TEST_DIR}"/bats/workflow.storage.image_cached.bats bats "${TEST_DIR}"/bats/workflow.storage.curl.bats bats "${TEST_DIR}"/bats/workflow.utilities.bats ;; diff --git a/.github/workflows/drevops-test-common.yml b/.github/workflows/drevops-test-common.yml new file mode 100644 index 000000000..8a79ec461 --- /dev/null +++ b/.github/workflows/drevops-test-common.yml @@ -0,0 +1,189 @@ +# This action is used for DrevOps maintenance. It will not be used in the scaffolded project. +name: DrevOps - Test + +on: + push: + branches: + - develop + pull_request: + branches: + - develop + - 'feature/**' + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + drevops-test-common: + runs-on: ubuntu-latest + + container: + image: drevops/ci-runner:latest + env: + # Prevent GitHub overriding the Docker config. + DOCKER_CONFIG: /root/.docker + TEST_GITHUB_TOKEN: ${{ secrets.TEST_GITHUB_TOKEN }} + TEST_DOCKER_USER: ${{ secrets.TEST_DOCKER_USER }} + TEST_DOCKER_PASS: ${{ secrets.TEST_DOCKER_PASS }} + DREVOPS_DEV_VOLUMES_MOUNTED: 0 + DREVOPS_DEV_TEST_COVERAGE_DIR: /tmp/.drevops-coverage-html + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Process codebase to run in CI + run: find . -name "docker-compose.yml" -print0 | xargs -0 -I {} sh -c "sed -i -e ''/###/d'' {} && sed -i -e ''s/##//'' {}" + + - name: Adjust git config to allow running git-related tests + run: git config --global safe.directory '*' + + - name: Generate test SSH key pair used for deployment tests + run: | + mkdir -p $HOME/.ssh + ssh-keygen -t rsa -b 4096 -m PEM -N '' -f $HOME/.ssh/id_rsa + chmod 600 $HOME/.ssh/id_rsa + echo "$(ssh-agent -a ${HOME}/ssh-agent.sock)" > ssh-agent-output + export SSH_AUTH_SOCK=$(grep SSH_AUTH_SOCK ssh-agent-output | cut -d';' -f1 | cut -d= -f2) + echo "SSH_AUTH_SOCK=${SSH_AUTH_SOCK}" >> $GITHUB_ENV + + - name: Install dependencies + run: npm ci + working-directory: .drevops/tests + + - name: Lint code + run: | + ./tests/lint.scripts.sh + ./tests/lint.dockerfiles.sh + ./tests/lint.spelling.sh + working-directory: .drevops + + - name: Run tests + run: ./tests/test.common.sh + working-directory: .drevops + + - name: Upload coverage report as an artifact + uses: actions/upload-artifact@v4 + with: + name: ${{github.job}}-code-coverage-report + path: /tmp/.drevops-coverage-html + + - name: Upload coverage report to Codecov + uses: codecov/codecov-action@v4 + with: + directory: /tmp/.drevops-coverage-html + fail_ci_if_error: true + token: ${{ secrets.CODECOV_TOKEN }} + + drevops-test-workflow: + runs-on: ubuntu-latest + + strategy: + matrix: + batch: [0, 1, 2] + + container: + image: drevops/ci-runner:latest + env: + # Prevent GitHub overriding the Docker config. + DOCKER_CONFIG: /root/.docker + TEST_GITHUB_TOKEN: ${{ secrets.TEST_GITHUB_TOKEN }} + TEST_DOCKER_USER: ${{ secrets.TEST_DOCKER_USER }} + TEST_DOCKER_PASS: ${{ secrets.TEST_DOCKER_PASS }} + DREVOPS_DEV_VOLUMES_MOUNTED: 0 + DREVOPS_DEV_TEST_COVERAGE_DIR: /tmp/.drevops-coverage-html + TEST_NODE_INDEX: ${{ matrix.batch }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Adjust git config to allow running git-related tests + run: git config --global safe.directory '*' + + - name: Install dependencies + run: npm ci + working-directory: .drevops/tests + + - name: Run tests + run: ./tests/test.workflow.sh + working-directory: .drevops + + - name: Upload coverage report as an artifact + uses: actions/upload-artifact@v4 + with: + name: ${{github.job}}-${{ matrix.batch }}-code-coverage-report + path: /tmp/.drevops-coverage-html + + - name: Upload coverage report to Codecov + uses: codecov/codecov-action@v4 + with: + directory: /tmp/.drevops-coverage-html + fail_ci_if_error: true + token: ${{ secrets.CODECOV_TOKEN }} + + drevops-test-deployment: + runs-on: ubuntu-latest + + strategy: + matrix: + batch: [0, 1] + + container: + image: drevops/ci-runner:latest + env: + # Prevent GitHub overriding the Docker config. + DOCKER_CONFIG: /root/.docker + TEST_GITHUB_TOKEN: ${{ secrets.TEST_GITHUB_TOKEN }} + TEST_DOCKER_USER: ${{ secrets.TEST_DOCKER_USER }} + TEST_DOCKER_PASS: ${{ secrets.TEST_DOCKER_PASS }} + DREVOPS_DEV_VOLUMES_MOUNTED: 0 + DREVOPS_DEV_TEST_COVERAGE_DIR: /tmp/.drevops-coverage-html + TEST_NODE_INDEX: ${{ matrix.batch }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Adjust git config to allow running git-related tests + run: git config --global safe.directory '*' + + - name: Generate test SSH key pair used for deployment tests + run: | + mkdir -p $HOME/.ssh + ssh-keygen -t rsa -b 4096 -m PEM -N '' -f $HOME/.ssh/id_rsa + chmod 600 $HOME/.ssh/id_rsa + echo "$(ssh-agent -a ${HOME}/ssh-agent.sock)" > ssh-agent-output + export SSH_AUTH_SOCK=$(grep SSH_AUTH_SOCK ssh-agent-output | cut -d';' -f1 | cut -d= -f2) + echo "SSH_AUTH_SOCK=${SSH_AUTH_SOCK}" >> $GITHUB_ENV + + - name: Install dependencies + run: npm ci + working-directory: .drevops/tests + + - name: Run tests + run: ./tests/test.deployment.sh + working-directory: .drevops + + - name: Upload coverage report as an artifact + uses: actions/upload-artifact@v4 + with: + name: ${{github.job}}-${{ matrix.batch }}-code-coverage-report + path: /tmp/.drevops-coverage-html + + - name: Upload coverage report to Codecov + uses: codecov/codecov-action@v4 + with: + directory: /tmp/.drevops-coverage-html + fail_ci_if_error: true + token: ${{ secrets.CODECOV_TOKEN }}