Skip to content

feat: use matrix strategy in tests #862

feat: use matrix strategy in tests

feat: use matrix strategy in tests #862

name: Integration Test K8s-snap
on:
push:
branches: ["master"]
pull_request:
schedule:
- cron: "0 0 * * *" # Runs every midnight
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
prepare:
name: Prepare Environment
runs-on: ubuntu-latest
outputs:
test-tags: ${{ steps.get-e2e-tags.outputs.test-tags }}
tests: ${{ steps.collect-tests.outputs.tests }}
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install tox
run: sudo apt-get install -y tox
- name: Get e2e test tags
id: get-e2e-tags
run: |
set -x
tags="up_to_weekly"
has_cnfc_label=${{ contains(github.event.pull_request.labels.*.name, 'cncf-conformance') }}
if [[ ${{ github.event_name }} == "schedule" || "$has_cnfc_label" == "true" ]]; then
tags+=" conformance_tests"
fi
echo "test-tags=$tags" >> "$GITHUB_OUTPUT"
- name: Collect tests
id: collect-tests
run: |
git clone https://github.com/canonical/k8s-snap.git
cd k8s-snap/tests/integration
# Split the input tags into an array and pass them as individual args
IFS=' ' read -ra TAGS <<< "${{ steps.get-e2e-tags.outputs.test-tags }}"
TAGS_ARGS=""
for tag in "${TAGS[@]}"; do
TAGS_ARGS="$TAGS_ARGS --tags $tag"
done
# Collect test names and convert to JSON array for GitHub Actions
# There is no easy way to get the test names from pytest, so we use the --collect-only flag
# and parse the output to extract the test names.
TEST_FILES=$(tox -e integration -- --collect-only $TAGS_ARGS --quiet --no-header tests/ | grep ::)
# Convert to JSON array for GitHub Actions
JSON_ARRAY=$(echo "$TEST_FILES" | jq -R -s -c 'split("\n") | map(select(length > 0))')
echo "tests=$JSON_ARRAY" >> $GITHUB_OUTPUT
echo "Found test files: $TEST_FILES"
build:
name: ${{ matrix.test }}
needs: prepare
strategy:
fail-fast: false
matrix:
test: ${{ fromJson(needs.prepare.outputs.tests) }}
runs-on: self-hosted-linux-amd64-jammy-large
steps:
- name: Checking out repo
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install tox
run: pip install tox
- name: Install Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Install lxd
uses: canonical/[email protected]
with:
bridges: "lxdbr0,dualstack-br0,ipv6-br0"
- name: Patch k8s snap
uses: ./.github/actions/patch-k8s-snap
- name: Generate names for step and artifacts
run: |
FULL_TEST_NODE_ID="${{ matrix.test }}"
# Extract test name (part after '::') for the step display name
TEST_NAME="${FULL_TEST_NODE_ID##*::}"
echo "test_name=${TEST_NAME}" >> $GITHUB_ENV
- name: Running ${{ env.test_name }}
env:
TEST_SNAP: ${{ github.workspace }}/k8s-updated.snap
TEST_SUBSTRATE: lxd
TEST_LXD_IMAGE: ubuntu:22.04
TEST_INSPECTION_REPORTS_DIR: ${{ github.workspace }}/inspection-reports/${{ env.test_name }}
run: |
git clone https://github.com/canonical/k8s-snap.git
cd k8s-snap/tests/integration && sg lxd -c "tox -e integration -- ${{ matrix.test }}"
- name: Prepare inspection reports
if: failure()
run: |
mkdir -p inspection-report-archives
tar -czvf inspection-report-archives/${{ env.test_name }}.tar.gz -C ${{ github.workspace }} inspection-reports/${{ env.test_name }}
- name: Upload inspection report artifact
if: failure()
uses: actions/upload-artifact@v4
with:
name: inspection-reports-${{ env.test_name }}
path: ${{ github.workspace }}/inspection-report-archives/${{ env.test_name }}.tar.gz
- name: Upload CNCF conformance report artifact
if: ${{ failure() && contains(needs.prepare.outputs.test-tags, 'conformance_tests') }}
uses: actions/upload-artifact@v4
with:
name: sonobuoy-e2e-${{ env.test_name }}
path: k8s-snap/tests/integration/sonobuoy_e2e.tar.gz