Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workflow to trigger OCP tests #12542

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions .github/workflows/ocp-test-profiles.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
name: Trigger OCP Tests When Relevant
on:
pull_request:
branches: [ master, 'stabilization*' ]
concurrency:
group: ${{ github.workflow }}-${{ github.event.number || github.run_id }}
cancel-in-progress: true
jobs:
check-and-trigger-ocp-prow-tests:
name: Identify rules changed in PR and test them in OCP Prow
runs-on: ubuntu-latest
container:
image: fedora:latest
steps:
- name: Install Deps
run: dnf install -y cmake make openscap-utils python3-pyyaml python3-jinja2 git python3-deepdiff python3-requests jq python3-pip nodejs
- name: Install deps python
run: pip install gitpython xmldiff
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
with:
fetch-depth: 0
- name: Checkout (CTF)
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
with:
repository: ComplianceAsCode/content-test-filtering
path: ctf
# https://github.com/actions/checkout/issues/766
- name: Set git safe directory
run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
- name: Find forking point
env:
BASE_BRANCH: ${{ github.base_ref }}
run: echo "FORK_POINT=$(git merge-base origin/$BASE_BRANCH ${{ github.event.pull_request.head.sha }})" >> $GITHUB_OUTPUT
id: fork_point
- name: Detect content changes in the PR
run: python3 ./ctf/content_test_filtering.py pr --base ${{ steps.fork_point.outputs.FORK_POINT }} --remote_repo ${{ github.server_url }}/${{ github.repository }} --verbose --rule --output json ${{ github.event.pull_request.number }} > ctf-output.json
- name: Test if there are no content changes
run: echo "CTF_OUTPUT_SIZE=$(stat --printf="%s" ctf-output.json)" >> $GITHUB_OUTPUT
id: ctf
- uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4
if: ${{ steps.ctf.outputs.CTF_OUTPUT_SIZE != '0' }}
with:
name: ctf-output
path: ctf-output.json
- name: Print changes to content detected if any
if: ${{ steps.ctf.outputs.CTF_OUTPUT_SIZE != '0' }}
run: cat ctf-output.json
- name: Get product attribute
if: ${{ steps.ctf.outputs.CTF_OUTPUT_SIZE != '0' }}
id: product
uses: notiz-dev/github-action-json-property@a5a9c668b16513c737c3e1f8956772c99c73f6e8 # v0.2.0
with:
path: 'ctf-output.json'
prop_path: 'product'

- name: Build product OCP and RHCOS content
if: ${{ steps.ctf.outputs.CTF_OUTPUT_SIZE != '0' && (contains(steps.product.outputs.prop, 'ocp4') || contains(steps.product.outputs.prop, 'rhcos4')) }}
run: ./build_product -d ocp4 rhcos4

- name: Process list of rules into a list of product-profiles to test
if: ${{ steps.ctf.outputs.CTF_OUTPUT_SIZE != '0' && (contains(steps.product.outputs.prop, 'ocp4') || contains(steps.product.outputs.prop, 'rhcos4')) }}
id: profiles_to_test
run: |
OCP_VERSIONS=(4.17 4.16)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to manage this in addition to any OCP version changes in openshift/releases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.
Another approach is to trigger the version-less tests, like e2e-aws-ocp4-high-node.

Sidenote: currently the version-less tests are running on 4.16, not the latest 4.17, 🙈

I considered running on two OCP versions, just to have more default coverage.
But it can be wasteful. I'm also okay with running only the version-less tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhmdnd Let's stick with the version-less test jobs for now

RULES=$(cat ctf-output.json | jq -r '.rules[]')

# Let's grab one profile for each changed rule
PROFILES=()
ALL_PROFILES=()

# Let's consistently grab a random profile for each rule, in order to do that we use the
# PR number as the seed
RANDOM=${{ github.event.pull_request.number }}
for rule in $RULES; do
readarray -t TEMP <<< $(grep -lr -e "- ${rule}\$" build/*/profiles | sort)

# Let's ilter out profiles for which we don't have a CI job configured
yuumasato marked this conversation as resolved.
Show resolved Hide resolved
# Here is an example of how to quicly update this variable in the future
# TESTED_PROFILES=$(grep -r PROFILE= ./ComplianceAsCode-content-master__4.16.yaml | sort -u | sed 's/.*export PROFILE=\(.*\)/\1/')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be run from the openshift/releases repository, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a good work around for this.
We could clone the repo and parse the files, or issue a /test comment and parse the comment from the ci bot.

I added it because we don't have tests for all profile ids.
You can see in previous comments that pci-dss-3-2 was chosen for tests, but we don't have CI wired up for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed that to actually curl the job config file and grep it.
So unless the job format or the test command changes, it should keep it self in sync with the tested profiles.

# echo -n TESTED_PROFILES=\(${TESTED_PROFILES[@]}\)
# Copy and paste the profiles here
TESTED_PROFILES=(bsi bsi-node cis cis-node e8 high high-node moderate moderate-node pci-dss pci-dss-4-0 pci-dss-node pci-dss-node-4-0 stig stig-node)

ELIGIBLE_PROFILES=()
for index in "${!TEMP[@]}"; do
for tp in ${TESTED_PROFILES[@]}; do
if [[ ${TEMP[$index]} =~ build\/.*\/profiles\/${tp}\.profile ]]; then
ELIGIBLE_PROFILES+=(${TEMP[$index]});
fi
done
done

ALL_PROFILES+=(${ELIGIBLE_PROFILES[@]})
PROFILES+=(${ELIGIBLE_PROFILES[$(($RANDOM%(${#ELIGIBLE_PROFILES[@]})))]})
done

# Sort and ensure that the profiles are unique
readarray -t UNIQUE_PROFILES <<< $(echo ${PROFILES[@]} | tr ' ' '\n' | sort -u | tr '\n' ' ')
readarray -t ALL_UNIQUE_PROFILES <<< $(echo ${ALL_PROFILES[@]} | tr ' ' '\n' | sort -u | tr '\n' ' ')

# Craft a command to trigger tests
COMMAND=$(for profile in ${UNIQUE_PROFILES[@]}; do
for OCP_V in "${OCP_VERSIONS[@]}"; do
echo ${profile} | sed 's/build\/\(.*\)\/profiles\/\(.*\)\.profile/\/test '"${OCP_V}"'-e2e-aws-\1-\2/'
done
done)

# COMMAND is a multiline string, so we need to set it this way
{
echo 'TEST_PROFILES_COMMAND<<EOF'
echo "${COMMAND}"
echo EOF
} >> $GITHUB_OUTPUT

# Format all identified profiles for display
ALL_PROFILES_FORMATTED=$(for profile in ${ALL_UNIQUE_PROFILES[@]}; do
echo ${profile} | sed 's/build\/\(.*\)\/profiles\/\(.*\)\.profile/- `<OCP_VERSION>-e2e-aws-\1-\2`/'
done)
{
echo 'ALL_PROFILES_COMMENT<<EOF'
echo "${ALL_PROFILES_FORMATTED}"
echo EOF
} >> $GITHUB_OUTPUT
- uses: thollander/actions-comment-pull-request@e2c37e53a7d2227b61585343765f73a9ca57eda9 # v2
if: ${{ steps.profiles_to_test.outputs.TEST_PROFILES_COMMAND != '' }}
with:
message: |
:robot: Trigger prow tests based on changed rules

${{ steps.profiles_to_test.outputs.TEST_PROFILES_COMMAND }}

Note: if a test is not started it could be that a CI Job is not configure for that particular profile or product.

<details>
<summary>Click here to see all the relevant profiles</summary>

${{ steps.profiles_to_test.outputs.ALL_PROFILES_COMMENT}}

</details>
comment-tag: kubernetes_start_prow_tests
pr-number: ${{ github.event.pull_request.number }}
mode: recreate
Loading