Skip to content

Commit

Permalink
ci(acctest): proper concurrency support
Browse files Browse the repository at this point in the history
  • Loading branch information
Serpentiel committed Dec 13, 2023
1 parent 41c6ce9 commit 43678bd
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 8 deletions.
40 changes: 33 additions & 7 deletions .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,41 @@ permissions:
contents: read

concurrency:
group: ci-${{ github.ref }}
group: ci-${{ github.head_ref || github.ref }}
cancel-in-progress: true

jobs:
acceptance_tests:
setup_aiven_project:
runs-on: ubuntu-latest
outputs:
selected_project_name: ${{ steps.set_project_name.outputs.project_name }}
steps:
- uses: actions/checkout@v4

- name: Fetch Aiven Projects
id: fetch_projects
run: |
response=$(curl -s -H "Authorization: Bearer ${{ secrets.AIVEN_TOKEN }}" https://api.aiven.io/v1/project)
echo "projects=$response" >> $GITHUB_OUTPUT
- name: Select Project
id: set_project_name
run: |
projects=$(echo '${{ steps.fetch_projects.outputs.projects }}' | \
jq --arg prefix "${{ secrets.AIVEN_PROJECT_NAME }}" \
'[.projects[] | select(.project_name | startswith($prefix))]')
for project in $(echo "${projects}" | jq -r '.[].project_name'); do
services=$(curl -s -H "Authorization: Bearer ${{ secrets.AIVEN_TOKEN }}" \
"https://api.aiven.io/v1/project/$project/service")
if [ "$(echo "$services" | jq '.services | length')" -eq 0 ]; then
echo "project_name=$project" >> $GITHUB_OUTPUT
break
fi
done
acceptance_tests:
needs: setup_aiven_project
runs-on: ubuntu-latest
strategy:
max-parallel: 5
fail-fast: false
Expand Down Expand Up @@ -70,18 +98,16 @@ jobs:
- run: make test-acc
env:
AIVEN_TOKEN: ${{ secrets.AIVEN_TOKEN }}
AIVEN_PROJECT_NAME: ${{ secrets.AIVEN_PROJECT_NAME }}
AIVEN_PROJECT_NAME: ${{ needs.setup_aiven_project.outputs.selected_project_name }}
AIVEN_ORGANIZATION_NAME: ${{ secrets.AIVEN_ORGANIZATION_NAME }}
AIVEN_ACCOUNT_NAME: ${{ secrets.AIVEN_ORGANIZATION_NAME }}
PKG: ${{ matrix.pkg }}

sweep:
if: always()
needs: acceptance_tests

needs: [acceptance_tests, setup_aiven_project]
runs-on: ubuntu-latest
steps:

- uses: softprops/turnstyle@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -112,6 +138,6 @@ jobs:
command: make sweep
env:
AIVEN_TOKEN: ${{ secrets.AIVEN_TOKEN }}
AIVEN_PROJECT_NAME: ${{ secrets.AIVEN_PROJECT_NAME }}
AIVEN_PROJECT_NAME: ${{ needs.setup_aiven_project.outputs.selected_project_name }}
AIVEN_ORGANIZATION_NAME: ${{ secrets.AIVEN_ORGANIZATION_NAME }}
AIVEN_ACCOUNT_NAME: ${{ secrets.AIVEN_ORGANIZATION_NAME }}
51 changes: 51 additions & 0 deletions .github/workflows/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
This directory contains the tests for some critical functionality of the GitHub Actions workflow.

The tests are equivalent to the Bash scripts that are run in the workflow, but are atomic and can be run locally.

## Structure

The tests are organized into directories that correspond to the name of the workflow,
and the atomic Bash scripts have the name of the job that they test.

```text
test
├── README.md
└── acceptance-tests
├── README.md
└── setup_aiven_project.sh
└── ...
└── run_tests.sh
```

In the example above, the `acceptance-tests` directory contains the tests for the `acceptance-tests` workflow,
and the `setup_aiven_project.sh` script tests the `setup_aiven_project` job.

If a workflow has multiple jobs, then there will be multiple scripts in the directory.

Let's say that the `some-other-workflow` workflow has two jobs, `some_other_job` and `some_other_job_2`.
Then the directory structure would look like this:

```text
some-other-workflow/
├── README.md
└── some_other_job.sh
└── some_other_job_2.sh
```

The `README.md` file in each directory contains a brief description of the workflow and the job that it tests.

## Running the tests

To run the tests, you need to execute the `run_tests.sh` script in the root directory.

```bash
./run_tests.sh
```

The script will run all the tests that are available.

Alternatively, you can run the tests by running the `Makefile` in the root directory of this repository.

```bash
make test-workflows
```
7 changes: 7 additions & 0 deletions .github/workflows/test/acceptance-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
`acceptance-tests` is a directory that contains the tests for the `acceptance-tests` workflow.

This workflow is responsible for running the acceptance tests for a specific branch.

## Tests

- `setup_aiven_project.sh` — This test sets up an Aiven project for the acceptance tests to run against.
39 changes: 39 additions & 0 deletions .github/workflows/test/acceptance-tests/setup_aiven_project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

show_help() {
echo "Usage: $0 -t AIVEN_TOKEN -p AIVEN_PROJECT_NAME"
echo " -t AIVEN_TOKEN Set the Aiven API token."
echo " -p AIVEN_PROJECT_NAME Set the Aiven Project Name prefix."
echo " -h Show help."
}

while getopts 'ht:p:' flag; do
case "${flag}" in
t) AIVEN_TOKEN="${OPTARG}" ;;
p) AIVEN_PROJECT_NAME="${OPTARG}" ;;
h) show_help
exit 0 ;;
*) show_help
exit 1 ;;
esac
done

if [ -z "$AIVEN_TOKEN" ] || [ -z "$AIVEN_PROJECT_NAME" ]; then
echo "Error: Aiven API token and Project Name prefix are required."
show_help
exit 1
fi

response=$(curl -s -H "Authorization: Bearer $AIVEN_TOKEN" https://api.aiven.io/v1/project)

projects=$(echo "$response" | \
jq --arg prefix "$AIVEN_PROJECT_NAME" '[.projects[] | select(.project_name | startswith($prefix))]')

for project in $(echo "${projects}" | jq -r '.[].project_name'); do
services=$(curl -s -H "Authorization: Bearer $AIVEN_TOKEN" \
"https://api.aiven.io/v1/project/$project/service")
if [ "$(echo "$services" | jq '.services | length')" -eq 0 ]; then
echo "Selected project: $project"
break
fi
done
3 changes: 3 additions & 0 deletions .github/workflows/test/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

./acceptance-tests/setup_aiven_project.sh
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build build-dev debug test test-unit test-acc test-examples lint lint-go lint-test lint-docs fmt fmt-test fmt-imports clean clean-tools clean-examples sweep generate gen-go docs
.PHONY: build build-dev debug test test-unit test-acc test-examples test-workflows lint lint-go lint-test lint-docs fmt fmt-test fmt-imports clean clean-tools clean-examples sweep generate gen-go docs

#################################################
# Global
Expand Down Expand Up @@ -96,6 +96,10 @@ test-examples: build-dev clean-examples
AIVEN_PROVIDER_PATH=$(BUILD_DEV_DIR) $(GO) test --tags=examples ./examples_tests/... \
-v -count $(TEST_COUNT) -parallel $(ACC_TEST_PARALLELISM) $(RUNARGS) $(TESTARGS) -timeout $(ACC_TEST_TIMEOUT)


test-workflows:
cd .github/workflows/test && ./run_tests.sh

#################################################
# Lint
#################################################
Expand Down

0 comments on commit 43678bd

Please sign in to comment.