Skip to content

Commit 49158c5

Browse files
authored
Introduce consumer-driven contract testing using Pact (#352)
* Implement first basic contract test using pact * Fix lint errors * Integrate CDCT into GitHub Actions Workflow This approach uses the local file system for contract exchange * Fix error in CDCT-related docker files The contract files are now properly shared between providers and consumers. Previously, the provider did not use the newly generated contract files. * Delete all contract files This commit serves as a test to see if GitHub Actions commits the auto-generated contract files * Update auto-generated contracts * Create separate GitHub Actions Workflow for CDCT * Update auto-generated contracts * Improve mocking in pipeline provider test Now environment variables are properly mocked Also the main method is invoked to start the provider This removes the duplicate code in the provider test * Change pactfile write mode to overwrite Now existing pact files are entirely overwritten when consumer test execution brings changes to the files Co-authored-by: felix-oq <[email protected]>
1 parent 57e8070 commit 49158c5

15 files changed

+3705
-89
lines changed

.github/workflows/cdct.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Consumer-Driven Contract Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
workflow_dispatch:
11+
12+
jobs:
13+
cdct:
14+
name: Consumer- and Provider-Side Testing
15+
runs-on: ubuntu-18.04
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
20+
- name: Build Consumer-Driven Contract Test
21+
run: |
22+
docker-compose -f docker-compose.pact.yml build
23+
24+
- name: Run Consumer-Driven Contract Test
25+
run: |
26+
docker-compose -f docker-compose.pact.yml up
27+
28+
- name: Commit latest contracts
29+
uses: stefanzweifel/git-auto-commit-action@v4
30+
with:
31+
commit_message: Update auto-generated contracts
32+
file_pattern: pacts/*.json

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.idea/
22
*.iml
3+
4+
pacts/logs

docker-compose.pact.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: "3.7"
2+
3+
services:
4+
# ----------------- UI Consumer --------------------
5+
ui-consumer:
6+
image: open-data-service/ods-main/ui-consumer
7+
build:
8+
context: ./ui
9+
dockerfile: Dockerfile.consumer
10+
volumes:
11+
- ./pacts:/app/pacts
12+
13+
# ----------------- PIPELINE SERVICE Provider --------------------
14+
pipeline-provider:
15+
image: open-data-service/ods-main/pipeline-provider
16+
build:
17+
context: .
18+
dockerfile: ./pipeline/Dockerfile.provider
19+
depends_on:
20+
ui-consumer:
21+
condition: service_completed_successfully
22+
volumes:
23+
- ./pacts:/app/pacts
24+

pacts/ui-pipeline.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"consumer": {
3+
"name": "UI"
4+
},
5+
"provider": {
6+
"name": "Pipeline"
7+
},
8+
"interactions": [
9+
{
10+
"description": "a request for getting all pipelines",
11+
"providerState": "no pipelines registered",
12+
"request": {
13+
"method": "GET",
14+
"path": "/configs/"
15+
},
16+
"response": {
17+
"status": 200,
18+
"headers": {
19+
},
20+
"body": [
21+
22+
]
23+
}
24+
}
25+
],
26+
"metadata": {
27+
"pactSpecification": {
28+
"version": "2.0.0"
29+
}
30+
}
31+
}

pipeline/Dockerfile.provider

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM node:15-alpine
2+
3+
# install dependencies for pact (https://docs.pact.io/docker/)
4+
RUN apk add --no-cache --virtual build-dependencies build-base
5+
RUN apk --no-cache add ca-certificates wget bash \
6+
&& wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
7+
&& wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.29-r0/glibc-2.29-r0.apk \
8+
&& apk add glibc-2.29-r0.apk
9+
10+
WORKDIR /app/pipeline
11+
12+
# Copy package*.json files first in order to make best use of docker layer caching
13+
COPY ./pipeline/package*.json ./
14+
15+
# npm clean slate install to get reproducible builds and quicker installs
16+
RUN npm ci
17+
18+
# copy rest of the files
19+
COPY ./pipeline/src ./src
20+
COPY ./pipeline/*.js ./
21+
COPY ./pipeline/*.json ./
22+
23+
# run provider tests
24+
CMD ["npm", "run", "test:pact-provider"]

0 commit comments

Comments
 (0)