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

[DRAFT] Pact consumer tests for TPS #1577

Draft
wants to merge 15 commits into
base: develop
Choose a base branch
from
Draft
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
149 changes: 149 additions & 0 deletions .github/workflows/consumer_contract_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
name: Consumer contract tests
# The purpose of this workflow is to validate the service level contract
# using the Pact framework.
#
# More details on Contract Testing can be found in our handbook
#
# https://broadworkbench.atlassian.net/wiki/spaces/IRT/pages/2660368406/Getting+Started+with+Pact+Contract+Testing
#
#
#
# NOTE: The publish-contracts workflow will use the latest commit of the branch that triggers this workflow to publish the unique consumer contract version to Pact Broker.
on:
pull_request:
branches: [ main, se/DR-3357-tps-consumer-tests ]
paths-ignore: [ '**.md' ]
push:
branches: [ main, se/DR-3357-tps-consumer-tests ]
paths-ignore: [ '**.md' ]
merge_group:
branches: [ main, se/DR-3357-tps-consumer-tests ]
paths-ignore: [ '**.md' ]

env:
PUBLISH_CONTRACTS_RUN_NAME: 'publish-contracts-${{ github.event.repository.name }}-${{ github.run_id }}-${{ github.run_attempt }}'
CAN_I_DEPLOY_RUN_NAME: 'can-i-deploy-${{ github.event.repository.name }}-${{ github.run_id }}-${{ github.run_attempt }}'

jobs:
bump-check:
runs-on: ubuntu-latest
outputs:
is-bump: ${{ steps.skiptest.outputs.is-bump }}
steps:
- uses: actions/checkout@v3
- name: Skip version bump merges
id: skiptest
uses: ./.github/actions/bump-skip
with:
event-name: ${{ github.event_name }}

# The primary objective of this section is to carefully control the dispatching of tags,
# ensuring it only occurs during the 'Tag, publish, deploy' workflow.
# However, a challenge arises with contract tests, as they require knowledge of the upcoming tag
# before the actual deployment. To address this, we leverage the dry run feature provided by bumper.
# This allows us to obtain the next tag for publishing contracts and verifying consumer pacts without
# triggering the tag dispatch. This approach sidesteps the need for orchestrating multiple workflows,
# simplifying our implementation.
#
# We regulate the tag job to meet the following requirements according to the trigger event type:
# 1. pull_request event (due to opening or updating of PR branch):
# dry-run flag is set to false
# this allows the new semver tag #major.#minor.#patch-#commit to be used to identity pacticipant version for development purpose
# PR has no effect on the value of the latest tag in settings.gradle on disk
# 2. PR merge to main, this triggers a push event on the main branch:
# dry-run flag is set to true
# this allows the new semver tag #major.#minor.#patch to be used to identity pacticipant version, and
# this action will not update the value of the latest tag in settings.gradle on disk
#
# Note: All workflows from the same PR merge should have the same copy of settings.gradle on disk,
# which should be the one from the HEAD of the main branch before the workflow starts running
regulated-tag-job:
needs: [ bump-check ]
if: ${{ needs.bump-check.outputs.is-bump == 'no' }}
uses: ./.github/workflows/tag.yml
with:
# The 'ref' parameter ensures that the consumer version is postfixed with the HEAD commit of the PR branch,
# facilitating cross-referencing of a pact between Pact Broker and GitHub.
ref: ${{ github.head_ref || '' }}
# The 'dry-run' parameter prevents the new tag from being dispatched.
dry-run: true
release-branches: main
secrets: inherit

init-github-context:
runs-on: ubuntu-latest
needs: [ bump-check ]
if: ${{ needs.bump-check.outputs.is-bump == 'no' }}
outputs:
repo-branch: ${{ steps.extract-branch.outputs.repo-branch }}
repo-version: ${{ steps.extract-branch.outputs.repo-version }}

steps:
- uses: actions/checkout@v3
- id: extract-branch
run: |
GITHUB_EVENT_NAME=${{ github.event_name }}
if [[ "$GITHUB_EVENT_NAME" == "push" ]]; then
GITHUB_REF=${{ github.ref }}
GITHUB_SHA=${{ github.sha }}
elif [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
GITHUB_REF=refs/heads/${{ github.head_ref }}
GITHUB_SHA=${{ github.event.pull_request.head.sha }}
elif [[ "$GITHUB_EVENT_NAME" == "merge_group" ]]; then
GITHUB_REF=refs/heads/${{ github.head_ref }}
else
echo "Failed to extract branch information"
exit 1
fi
echo "repo-branch=${GITHUB_REF/refs\/heads\//""}" >> $GITHUB_OUTPUT
echo "repo-version=${GITHUB_SHA}" >> $GITHUB_OUTPUT
- name: Echo repo and branch information
run: |
echo "repo-owner=${{ github.repository_owner }}"
echo "repo-name=${{ github.event.repository.name }}"
echo "repo-branch=${{ steps.extract-branch.outputs.repo-branch }}"
echo "repo-version=${{ steps.extract-branch.outputs.repo-version }}"

tdr-consumer-contract-tests:
runs-on: ubuntu-latest
needs: [ bump-check, init-github-context ]
if: ${{ needs.bump-check.outputs.is-bump == 'no' }}
outputs:
pact-b64: ${{ steps.encode-pact.outputs.pact-b64 }}

steps:
- uses: actions/checkout@v3
- name: Set up JDK
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'temurin'
- name: Run consumer tests
run: ./gradlew pactTests
- name: Output consumer contract as non-breaking base64 string
id: encode-pact
run: |
NON_BREAKING_B64=$(cat build/pacts/datarepo-tps.json | base64 -w 0)
echo "pact-b64=${NON_BREAKING_B64}" >> $GITHUB_OUTPUT

publish-contracts:
runs-on: ubuntu-latest
needs: [ bump-check, init-github-context, tdr-consumer-contract-tests, regulated-tag-job ]
if: ${{ needs.bump-check.outputs.is-bump == 'no' }}
steps:
- name: Dispatch to terra-github-workflows
uses: broadinstitute/[email protected]
with:
run-name: "${{ env.PUBLISH_CONTRACTS_RUN_NAME }}"
workflow: .github/workflows/publish-contracts.yaml
repo: broadinstitute/terra-github-workflows
ref: refs/heads/main
token: ${{ secrets.BROADBOT_TOKEN }} # github token for access to kick off a job in the private repo
inputs: '{
"run-name": "${{ env.PUBLISH_CONTRACTS_RUN_NAME }}",
"pact-b64": "${{ needs.tdr-consumer-contract-tests.outputs.pact-b64 }}",
"repo-owner": "${{ github.repository_owner }}",
"repo-name": "${{ github.event.repository.name }}",
"repo-branch": "${{ needs.init-github-context.outputs.repo-branch }}",
"release-tag": "${{ needs.regulated-tag-job.outputs.new-tag }}"
}'
17 changes: 14 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,16 @@ dependencies {
exclude group: 'com.sun.jersey', module: 'jersey-server'
}

testImplementation 'au.com.dius.pact.provider:junit5:4.3.19'
testImplementation 'au.com.dius.pact.provider:junit5spring:4.3.19'
testImplementation 'au.com.dius.pact.provider:junit5:4.6.1'
testImplementation 'au.com.dius.pact.provider:junit5spring:4.6.1'
testImplementation 'au.com.dius.pact.consumer:junit5:4.6.1'

antlr 'org.antlr:antlr4:4.8'
spotbugs 'com.github.spotbugs:spotbugs:4.2.3'

// Need groovy on the class path for the logback config. Could use XML and skip this dependency,
// but the groovy config is... well... groovy.
runtimeOnly 'org.codehaus.groovy:groovy:3.0.7'
runtimeOnly 'org.apache.groovy:groovy:4.0.11'

// Findbugs annotations, so we can selectively suppress findbugs findings
compileOnly 'com.google.code.findbugs:annotations:3.0.1'
Expand Down Expand Up @@ -567,6 +568,16 @@ task testAll(type: Test) {
outputs.upToDateWhen { false }
}

// PACT

task pactTests(type: Test) {
useJUnitPlatform {
includeTags "pact-test"
}
environment.put('pact.rootDir', "$buildDir/pacts")
environment.put('pact.provider.version', "$project.version")
}

task verifyPacts(type: Test) {
useJUnitPlatform {
includeTags 'bio.terra.common.category.Pact'
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading
Loading