From 5f6b5cccc0ac49ffbf776b0ce73a79700042c9f7 Mon Sep 17 00:00:00 2001 From: Pete Wall Date: Wed, 16 Apr 2025 09:32:54 -0500 Subject: [PATCH 1/3] WIP: test only what changes Signed-off-by: Pete Wall --- .github/workflows/integration-test.yml | 44 ++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index dcc06351d8..a6b276e66e 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -5,12 +5,22 @@ on: push: branches: ["main"] paths: - - 'charts/**' - - '!charts/k8s-monitoring-v1/**' +# - '.github/workflows/integration-test.yml' + - 'charts/k8s-monitoring/**' + - '!charts/k8s-monitoring/Makefile' # Skip chart README file + - '!charts/k8s-monitoring/README.md*' # Skip chart README file + - '!charts/k8s-monitoring/docs/**' # Skip documentation files + - '!charts/k8s-monitoring/tests/*.yaml' # Skip unit test files + - 'charts/k8s-monitoring-test/**' pull_request: paths: - - 'charts/**' - - '!charts/k8s-monitoring-v1/**' +# - '.github/workflows/integration-test.yml' + - 'charts/k8s-monitoring/**' + - '!charts/k8s-monitoring/Makefile' # Skip chart README file + - '!charts/k8s-monitoring/README.md*' # Skip chart README file + - '!charts/k8s-monitoring/docs/**' # Skip documentation files + - '!charts/k8s-monitoring/tests/*.yaml' # Skip unit test files + - 'charts/k8s-monitoring-test/**' # Allows you to run this workflow manually from the Actions tab workflow_dispatch: @@ -29,7 +39,7 @@ jobs: id: list_tests working-directory: charts/k8s-monitoring/tests/integration run: | - # if "integration-test-skip" is set, return an empty list + # If "integration-test-skip" is set, return an empty list # All labels on this PR labels='${{ toJson(github.event.pull_request.labels.*.name) }}' if echo "${labels}" | jq --exit-status '. | any(. == "integration-test-skip")' > /dev/null; then @@ -37,9 +47,29 @@ jobs: echo "tests=[]" >> "${GITHUB_OUTPUT}" exit 0 fi - + tests=$(find . -name values.yaml -exec dirname {} \;) - echo "Tests: ${tests}" + # If manually triggered, run all tests + + # If the only changes are within a test directory, only run those tests + if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then + echo "Running all tests." + + # If pull request, check if the only changes are within a test directory + elif [ "${{ github.event_name }}" == "pull_request" ]; then + # Check if the only changes are within a test directory + changed_files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }}) + if echo "${changed_files}" | grep -qv '^charts/k8s-monitoring/tests/integration/'; then + echo "Changes outside of test directories detected, running all tests." + else + echo "Only changes within test directories detected, running those tests." + tests=$(echo "${tests}" | grep -E "$(echo "${changed_files}" | sed 's|^charts/k8s-monitoring/tests/integration/||' | sed 's|/values.yaml$||' | tr '\n' '|')" | sed 's/|$//') + fi + + elif [ "${{ github.event_name }}" == "push" ]; then + echo "Running all tests." + fi + echo "tests=$(echo "${tests}" | jq --raw-input --slurp --compact-output 'split("\n") | map(select(. != ""))')" >> "${GITHUB_OUTPUT}" run-tests: From c96924696191e404861164c711e89823ccd5bdd0 Mon Sep 17 00:00:00 2001 From: Pete Wall Date: Wed, 16 Apr 2025 09:34:55 -0500 Subject: [PATCH 2/3] Add a change to a single integration test Signed-off-by: Pete Wall --- .../annotation-autodiscovery/deployments/cert-manager.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/charts/k8s-monitoring/tests/integration/annotation-autodiscovery/deployments/cert-manager.yaml b/charts/k8s-monitoring/tests/integration/annotation-autodiscovery/deployments/cert-manager.yaml index d73d1ec1a3..6c67611eaa 100644 --- a/charts/k8s-monitoring/tests/integration/annotation-autodiscovery/deployments/cert-manager.yaml +++ b/charts/k8s-monitoring/tests/integration/annotation-autodiscovery/deployments/cert-manager.yaml @@ -34,3 +34,4 @@ spec: k8s.grafana.com/scrape: "true" k8s.grafana.com/job: "integrations/cert-manager" k8s.grafana.com/metrics.portNumber: "9402" + another-annotation: yep From e70e100aa4d2afeeb3384a8b6eecc1f83dcc01d3 Mon Sep 17 00:00:00 2001 From: Pete Wall Date: Wed, 16 Apr 2025 10:26:24 -0500 Subject: [PATCH 3/3] Update test checking algorithm Signed-off-by: Pete Wall --- .github/workflows/integration-test.yml | 39 ++++++++++++++++---------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index a6b276e66e..1ef85737de 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -48,26 +48,35 @@ jobs: exit 0 fi - tests=$(find . -name values.yaml -exec dirname {} \;) - # If manually triggered, run all tests + all_tests=$(find . -name values.yaml -exec dirname {} \; | sort) + + # Default: run all tests + tests="${all_tests}" - # If the only changes are within a test directory, only run those tests - if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then - echo "Running all tests." + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then + echo "Manual triggered - running all tests." + + elif [ "${{ github.event_name }}" = "push" ]; then + echo "Push to main - running all tests." - # If pull request, check if the only changes are within a test directory elif [ "${{ github.event_name }}" == "pull_request" ]; then - # Check if the only changes are within a test directory - changed_files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }}) - if echo "${changed_files}" | grep -qv '^charts/k8s-monitoring/tests/integration/'; then - echo "Changes outside of test directories detected, running all tests." + # Fetch base branch so we can diff against it + git fetch origin ${{ github.base_ref }} + + changed_files=$(git diff --name-only origin/${{ github.base_ref }}...HEAD) + + # Check if any changed files are outside of the integration test directory + if echo "$changed_files" | grep -qv '^charts/k8s-monitoring/tests/integration/'; then + echo "PR contains changes outside of integration test directory - running all tests." else - echo "Only changes within test directories detected, running those tests." - tests=$(echo "${tests}" | grep -E "$(echo "${changed_files}" | sed 's|^charts/k8s-monitoring/tests/integration/||' | sed 's|/values.yaml$||' | tr '\n' '|')" | sed 's/|$//') + echo "PR only contains changes inside integration test directory - running only changed tests." + # Extract test dirs from changed values.yaml + tests=$(echo "$changed_files" \ + | grep '^charts/k8s-monitoring/tests/integration/' \ + | sed 's|^charts/k8s-monitoring/tests/integration/||' \ + | awk -F/ '{print $1}' \ + | sort -u) fi - - elif [ "${{ github.event_name }}" == "push" ]; then - echo "Running all tests." fi echo "tests=$(echo "${tests}" | jq --raw-input --slurp --compact-output 'split("\n") | map(select(. != ""))')" >> "${GITHUB_OUTPUT}"