Skip to content

🧪 Add input validation for --run-as flag #624

🧪 Add input validation for --run-as flag

🧪 Add input validation for --run-as flag #624

Workflow file for this run

name: E2E Tests PR Tester
on:
pull_request:
branches:
- main
concurrency:
group: e2e-pr-tester-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
detect-changes:
permissions:
contents: read
runs-on: ubuntu-22.04
outputs:
run_tests: ${{ steps.changed_tests.outputs.run_tests }}
run_mode: ${{ steps.changed_tests.outputs.run_mode }}
test_paths: ${{ steps.changed_tests.outputs.test_paths }}
focus_file_regex: ${{ steps.changed_tests.outputs.focus_file_regex }}
full_reason: ${{ steps.changed_tests.outputs.full_reason }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Detect touched e2e test files
id: changed_tests
run: |
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
# Three-dot diff (merge-base..HEAD): only files changed on the PR branch.
# Two-dot BASE..HEAD compares full trees and wrongly includes all of main
# when the branch has not merged the latest base (shows outside_e2e for everything).
ALL_CHANGED="$(git diff --name-only "$BASE_SHA"..."$HEAD_SHA")"
# Full E2E for "outside e2e-tests" ignores CI/docs-only paths and root README.
has_outside_e2e=false
while IFS= read -r file; do
[ -z "$file" ] && continue
case "$file" in
e2e-tests/*|.github/*|docs/*|README.md) ;;
*) has_outside_e2e=true; break ;;
esac
done <<EOF
$ALL_CHANGED
EOF
if [ "$has_outside_e2e" = "true" ]; then
echo "run_tests=true" >> "$GITHUB_OUTPUT"
echo "run_mode=all" >> "$GITHUB_OUTPUT"
echo "full_reason=outside_e2e" >> "$GITHUB_OUTPUT"
echo "test_paths=" >> "$GITHUB_OUTPUT"
echo "focus_file_regex=" >> "$GITHUB_OUTPUT"
exit 0
fi
CHANGED_E2E_FILES="$(git diff --name-only "$BASE_SHA"..."$HEAD_SHA" -- e2e-tests/)"
if [ -z "$CHANGED_E2E_FILES" ]; then
echo "run_tests=false" >> "$GITHUB_OUTPUT"
echo "run_mode=none" >> "$GITHUB_OUTPUT"
echo "full_reason=skip" >> "$GITHUB_OUTPUT"
echo "test_paths=" >> "$GITHUB_OUTPUT"
echo "focus_file_regex=" >> "$GITHUB_OUTPUT"
exit 0
fi
has_tests=false
has_impactful_non_test=false
suite_file_changed=false
test_files=""
while IFS= read -r file; do
[ -z "$file" ] && continue
case "$file" in
e2e-tests/tests/tier0/*_test.go|e2e-tests/tests/tier1/*_test.go)
has_tests=true
test_files="${test_files}${file}"$'\n'
if [ "$file" = "e2e-tests/tests/tier0/e2e_suite_test.go" ] || [ "$file" = "e2e-tests/tests/tier1/e2e_suite_test.go" ]; then
suite_file_changed=true
fi
;;
e2e-tests/framework/*|e2e-tests/config/*|e2e-tests/utils/*|e2e-tests/testdata/*|e2e-tests/tests/*)
has_impactful_non_test=true
;;
esac
done <<EOF
$CHANGED_E2E_FILES
EOF
# README/docs-only edits under e2e-tests should not trigger E2E execution.
if [ "$has_tests" = "false" ] && [ "$has_impactful_non_test" = "false" ]; then
echo "run_tests=false" >> "$GITHUB_OUTPUT"
echo "run_mode=none" >> "$GITHUB_OUTPUT"
echo "full_reason=skip" >> "$GITHUB_OUTPUT"
echo "test_paths=" >> "$GITHUB_OUTPUT"
echo "focus_file_regex=" >> "$GITHUB_OUTPUT"
exit 0
fi
TEST_PATHS="$(printf '%s' "$test_files" | sort -u | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
FOCUS_FILE_REGEX="$(printf '%s' "$test_files" | sort -u | sed -E 's/[][(){}.^$*+?|\\]/\\&/g' | paste -sd'|' -)"
if [ "$has_impactful_non_test" = "true" ] || [ "$suite_file_changed" = "true" ] || [ "$has_tests" = "false" ]; then
echo "run_mode=all" >> "$GITHUB_OUTPUT"
echo "full_reason=e2e_impact" >> "$GITHUB_OUTPUT"
else
echo "run_mode=focused" >> "$GITHUB_OUTPUT"
echo "full_reason=focused" >> "$GITHUB_OUTPUT"
fi
echo "run_tests=true" >> "$GITHUB_OUTPUT"
echo "test_paths=$TEST_PATHS" >> "$GITHUB_OUTPUT"
echo "focus_file_regex=$FOCUS_FILE_REGEX" >> "$GITHUB_OUTPUT"
- name: Print execution plan
run: |
echo "Run mode: ${{ steps.changed_tests.outputs.run_mode }}"
echo "Full reason: ${{ steps.changed_tests.outputs.full_reason }}"
case "${{ steps.changed_tests.outputs.full_reason }}" in
outside_e2e)
echo "Running full suite: e2e-tests/tests"
echo "Reason: changes outside e2e-tests/ (crane or repo paths)"
;;
e2e_impact)
echo "Running full suite: e2e-tests/tests"
echo "Reason: impactful non-test file(s) or suite bootstrap changed under e2e-tests/"
;;
focused)
echo "Running touched tests: ${{ steps.changed_tests.outputs.test_paths }}"
echo "Using focus-file regex: ${{ steps.changed_tests.outputs.focus_file_regex }}"
;;
*)
echo "Skipping E2E run (no applicable changes)."
;;
esac
run-changed-e2e-tests:
needs: detect-changes
if: needs.detect-changes.outputs.run_tests == 'true'
permissions:
contents: read
runs-on: ubuntu-22.04
timeout-minutes: 60
env:
CRANE_BIN: ${{ github.workspace }}/crane
E2E_SUITE_DIR: e2e-tests/tests
GINKGO_COMMON_ARGS: >-
--k8sdeploy-bin=k8sdeploy
--crane-bin=${{ github.workspace }}/crane
--source-context=src
--target-context=tgt
--source-nonadmin-context=src-dev
--target-nonadmin-context=tgt-dev
--verbose-logs
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup minikube clusters
uses: ./.github/actions/setup-minikube-clusters
with:
cpus: max
memory: "8192"
create_users: "true"
src_user: "dev"
tgt_user: "dev"
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Clone k8s-apps-deployer
run: git clone https://github.com/stillalearner/k8s-apps-deployer.git
- name: Create venv and install package
run: |
cd k8s-apps-deployer
python3 -m venv venv
echo "$PWD/venv/bin" >> $GITHUB_PATH
./venv/bin/pip install --upgrade pip
./venv/bin/pip install -e .
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Install Ginkgo CLI
run: |
GINKGO_VERSION="$(go list -m -f '{{.Version}}' github.com/onsi/ginkgo/v2)"
go install "github.com/onsi/ginkgo/v2/ginkgo@$GINKGO_VERSION"
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
ginkgo version
- name: Build crane CLI
run: go build -o "$CRANE_BIN" .
- name: Run touched crane e2e tests
run: |
if [ "${{ needs.detect-changes.outputs.run_mode }}" = "all" ]; then
ginkgo run -v -r "$E2E_SUITE_DIR" -- $GINKGO_COMMON_ARGS
else
ginkgo run -v -r --focus-file="${{ needs.detect-changes.outputs.focus_file_regex }}" "$E2E_SUITE_DIR" -- $GINKGO_COMMON_ARGS
fi