Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
203 changes: 203 additions & 0 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# Mutation testing with PIT.
#
# A mutant is a small change to the library, such as `<` turned into `<=`.
# PIT records which tests run each line, then runs only those tests against each mutant.
# A mutant is killed when one of those tests fails.
# The mutation profile in pom.xml sets the PIT options.
#
# A pull request mutates the library classes it changes and keeps the mutants on its changed lines.
# The job fails if one of those mutants survives or sits on a line that no test runs.
# A push to master, or a pull request that changes this workflow, mutates the whole library and only reports.
# The job uploads mutation-results-esdk-java (results.json) for the
# Mutation testing page of the aws-crypto-tools-commons dashboard.

name: Mutation testing

on:
pull_request:
push:
branches:
- master
workflow_dispatch:
inputs:
commons-ref:
description: The aws-crypto-tools-commons ref whose mutation tools run. Empty means main.
type: string

jobs:
mutation:
name: esdk mutation testing
runs-on: ubuntu-latest
timeout-minutes: 240
permissions:
contents: read
id-token: write
# The resolve script reads this pull request's Test-Server-Override lines.
pull-requests: read
env:
TOOLS: ${{ github.workspace }}/commons/test-server-common/tools/mutation
RESULTS: ${{ github.workspace }}/results
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Check out the resolve script
uses: actions/checkout@v4
with:
repository: aws/aws-crypto-tools-commons
ref: ${{ inputs.commons-ref }}
token: ${{ secrets.COMMONS_REPO_PAT }}
sparse-checkout: .github/scripts
path: resolve

- name: Resolve the commons ref, Test-Server-Override lines included
id: refs
env:
REPOSITORY: ${{ github.repository }}
SHA: ${{ github.sha }}
PR_NUMBER: ${{ github.event.pull_request.number }}
HEAD_REF: ${{ github.head_ref }}
EVENT_BODY: ${{ github.event.pull_request.body }}
INPUT_COMMONS_REF: ${{ inputs.commons-ref }}
GH_TOKEN: ${{ github.token }}
REPO_PAT: ${{ secrets.COMMONS_REPO_PAT }}
run: resolve/.github/scripts/resolve-test-server-refs.sh resolve

- uses: actions/checkout@v4
with:
repository: aws/aws-crypto-tools-commons
ref: ${{ steps.refs.outputs.commons_ref }}
token: ${{ secrets.COMMONS_REPO_PAT }}
sparse-checkout: test-server-common/tools/mutation
path: commons

# A pull request that changes this workflow mutates the whole library, so the new setup gets a real run.
- name: Check whether this pull request changes this workflow
if: github.event_name == 'pull_request'
env:
BASE: ${{ github.event.pull_request.base.sha }}
run: |
git fetch --no-tags origin "$BASE"
if git diff --name-only "$BASE"...HEAD | grep -qx .github/workflows/mutation.yml; then
echo "WORKFLOW_CHANGED=true" >> "$GITHUB_ENV"
fi

- name: Mutate the whole library
if: github.event_name != 'pull_request' || env.WORKFLOW_CHANGED == 'true'
run: |
echo "SCOPE=full" >> "$GITHUB_ENV"
echo "MUTATE=true" >> "$GITHUB_ENV"
echo "TARGET_CLASSES=com.amazonaws.encryptionsdk.*" >> "$GITHUB_ENV"

- name: Mutate only the library classes this pull request changes
if: github.event_name == 'pull_request' && env.WORKFLOW_CHANGED != 'true'
env:
BASE: ${{ github.event.pull_request.base.sha }}
run: |
mkdir -p "$RUNNER_TEMP/mutation"
python3 "$TOOLS/results.py" changed-lines --repo . --base "$BASE" --out "$RUNNER_TEMP/mutation/lines.json" \
--glob 'src/main/java/*.java'
classes=$(python3 "$TOOLS/pit.py" target-classes "$RUNNER_TEMP/mutation/lines.json" --source-root src/main/java)
echo "SCOPE=changed" >> "$GITHUB_ENV"
echo "LINES=--lines $RUNNER_TEMP/mutation/lines.json" >> "$GITHUB_ENV"
echo "TARGET_CLASSES=$classes" >> "$GITHUB_ENV"
if [ -n "$classes" ]; then
echo "MUTATE=true" >> "$GITHUB_ENV"
else
echo "This pull request changes no esdk source line." | tee -a "$GITHUB_STEP_SUMMARY"
echo "MUTATE=false" >> "$GITHUB_ENV"
fi

- name: Setup Java 17
if: env.MUTATE == 'true'
uses: actions/setup-java@v4
with:
distribution: corretto
java-version: 17
cache: maven

# The tests need TestAwsCryptographicMaterialProviders, which Maven Central does not have.
# It is built from the MaterialProviders submodule, the same way codebuild/ci/vectors-ci.yml builds it.
- name: Read the MaterialProviders submodule commit
if: env.MUTATE == 'true'
id: mpl
run: echo "commit=$(git rev-parse HEAD:submodules/MaterialProviders)" >> "$GITHUB_OUTPUT"

- name: Restore the test vectors library
if: env.MUTATE == 'true'
id: vectors
uses: actions/cache/restore@v4
with:
path: ~/.m2/repository/software/amazon/cryptography
key: mpl-test-vectors-java-${{ steps.mpl.outputs.commit }}

- name: Check out the MaterialProviders submodule
if: env.MUTATE == 'true' && steps.vectors.outputs.cache-hit != 'true'
run: |
git submodule update --init submodules/MaterialProviders
git -C submodules/MaterialProviders submodule update --init libraries smithy-dafny

- name: Setup Dafny
if: env.MUTATE == 'true' && steps.vectors.outputs.cache-hit != 'true'
uses: dafny-lang/setup-dafny-action@v1.7.0
with:
dafny-version: 4.8.0

- name: Setup Gradle
if: env.MUTATE == 'true' && steps.vectors.outputs.cache-hit != 'true'
uses: gradle/actions/setup-gradle@v4
with:
gradle-version: "7.6"

- name: Build the test vectors library
if: env.MUTATE == 'true' && steps.vectors.outputs.cache-hit != 'true'
working-directory: submodules/MaterialProviders/TestVectorsAwsCryptographicMaterialProviders
run: make build_java CORES=4 && make mvn_local_deploy

# Saved here, because a pull request with a surviving mutant fails the job before a post-job save.
- name: Save the test vectors library
if: env.MUTATE == 'true' && steps.vectors.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: ~/.m2/repository/software/amazon/cryptography
key: mpl-test-vectors-java-${{ steps.mpl.outputs.commit }}

- name: Configure AWS credentials for the KMS tests
if: env.MUTATE == 'true'
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: us-west-2
role-to-assume: arn:aws:iam::370957321024:role/GitHub-CI-Public-ESDK-Java-Role-us-west-2
role-session-name: JavaMutationTesting

- name: Run the mutants
if: env.MUTATE == 'true'
run: |
start=$SECONDS
mvn -B -ntp -Pmutation -Djacoco.skip=true test-compile org.pitest:pitest-maven:mutationCoverage \
"-DtargetClasses=$TARGET_CLASSES" -Dthreads=4
echo "MUTATION_SECONDS=$((SECONDS - start))" >> "$GITHUB_ENV"

- name: Write results.json
run: |
mkdir -p "$RESULTS"
python3 "$TOOLS/pit.py" convert target/pit-reports/mutations.xml --source-root src/main/java \
--out "$RESULTS/results.json" --repository "$GITHUB_REPOSITORY" --commit "$GITHUB_SHA" \
--language java --library esdk --scope "$SCOPE" --seconds "${MUTATION_SECONDS:-0}" $LINES

- name: Report the survivors
if: env.SCOPE == 'full'
run: python3 "$TOOLS/results.py" check "$RESULTS/results.json"

- name: Fail on a survivor or a changed line that no test runs
if: env.SCOPE == 'changed'
run: python3 "$TOOLS/results.py" check "$RESULTS/results.json" --fail

- name: Upload the mutation results
if: always()
uses: actions/upload-artifact@v4
with:
name: mutation-results-esdk-java
path: results/results.json
if-no-files-found: error
49 changes: 49 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,55 @@
</plugins>
</build>
</profile>

<!-- Mutation testing with PIT: mvn -Pmutation test-compile org.pitest:pitest-maven:mutationCoverage -->
<profile>
<id>mutation</id>
<build>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.30.2</version>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<configuration>
<!-- PIT runs each test class on its own, so this leaves out what FastTestsOnlySuite
leaves out. Running the suite instead rebuilds every parameter set for each mutant. -->
<jvmArgs>
<param>-DfastTestsOnly=true</param>
</jvmArgs>
<excludedGroups>
<param>com.amazonaws.encryptionsdk.SlowTestCategory</param>
</excludedGroups>
<excludedTestClasses>
<param>*Suite</param>
<!-- Runs for hours. -->
<param>com.amazonaws.encryptionsdk.internal.FrameEncryptionHandlerVeryLongTest</param>
<!-- IntegrationTestSuite runs these. -->
<param>com.amazonaws.encryptionsdk.XCompatKmsDecryptTest</param>
<param>com.amazonaws.encryptionsdk.KMSProviderBuilderIntegrationTests</param>
<param>com.amazonaws.encryptionsdk.MaxEncryptedDataKeysIntegrationTest</param>
<param>com.amazonaws.encryptionsdk.kms.*IntegrationTest*</param>
<param>com.amazonaws.encryptionsdk.kms.XCompatKmsDecryptTest</param>
</excludedTestClasses>
<outputFormats>
<param>XML</param>
</outputFormats>
<timestampedReports>false</timestampedReports>
<!-- A test that fails on the unmutated library (for example one that needs AWS
credentials the run lacks) drops out instead of stopping the run. -->
<skipFailingTests>true</skipFailingTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ protected Boolean initialValue() {
// when the fast tests
// are selected
public static boolean isFastTestSuiteActive() {
return IS_FAST_TEST_SUITE_ACTIVE.get();
// Mutation testing runs test classes one by one, outside this suite, so it sets this property.
return IS_FAST_TEST_SUITE_ACTIVE.get() || Boolean.getBoolean("fastTestsOnly");
}

// Require that this fast suite completes relatively quickly. If you're seeing this timeout get
Expand Down
Loading