Skip to content

Commit 286a413

Browse files
test(contract): refatorar o teste para permitir alteração de contrato
também foi feito ajuste na pipeline para utilizar o pact-broker via docker ao invés de baixar via curl
1 parent 6ab2188 commit 286a413

File tree

7 files changed

+90
-67
lines changed

7 files changed

+90
-67
lines changed

.github/workflows/continuous_delivery.yml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,6 @@ jobs:
117117
- name: Run contract test
118118
run: make test-contract
119119
env:
120-
PACT_BROKER_BASE_URL: https://paulogoncalves.pactflow.io
121-
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
122-
- name: Install pact broker
123-
run: |
124-
curl -LO https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v1.88.51/pact-1.88.51-linux-x86_64.tar.gz
125-
tar xzf pact-1.88.51-linux-x86_64.tar.gz
126-
- name: Verify that it will not break a contract in production
127-
run: |
128-
pact/bin/pact-broker can-i-deploy \
129-
--pacticipant 'ServeRest - API Rest' \
130-
--version ${{ github.sha }} \
131-
--to production
132-
env:
133-
PACT_BROKER_BASE_URL: https://paulogoncalves.pactflow.io
134120
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
135121

136122
release:
@@ -158,5 +144,4 @@ jobs:
158144
- name: Run contract test
159145
run: make test-contract
160146
env:
161-
PACT_BROKER_BASE_URL: https://paulogoncalves.pactflow.io
162147
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}

.github/workflows/continuous_integration.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ name: Continuous Integration
33
on:
44
pull_request:
55
workflow_dispatch:
6+
inputs:
7+
contract-test-verbose:
8+
description: 'Executar o teste de contrato em modo verboso?'
9+
required: false
10+
default: 'false'
611

712
jobs:
813
lint:
@@ -148,5 +153,5 @@ jobs:
148153
run: make test-contract
149154
env:
150155
GITHUB_BRANCH: ${{ env.GIT_BRANCH_NAME }}
151-
PACT_BROKER_BASE_URL: https://paulogoncalves.pactflow.io
152156
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
157+
VERBOSE: ${{ github.event.inputs.contract-test-verbose }}

.github/workflows/contract_test_by_dispatch.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ jobs:
1717
env:
1818
PACT_URL: ${{ github.event.client_payload.pactUrl }}
1919
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
20-
TRIGGERED_BY_PACT_CHANGE: true

.github/workflows/deploy-online-serverest.yml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,28 @@ on:
1616
schedule:
1717
- cron: "0 6 * * *"
1818

19+
env:
20+
PACT_BROKER_BASE_URL: https://paulogoncalves.pactflow.io
21+
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
22+
1923
jobs:
2024
can-i-deploy:
2125

2226
runs-on: ubuntu-18.04
2327

2428
steps:
2529
- uses: actions/checkout@v2
26-
- name: Install pact broker
27-
run: |
28-
curl -LO https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v1.88.51/pact-1.88.51-linux-x86_64.tar.gz
29-
tar xzf pact-1.88.51-linux-x86_64.tar.gz
30+
- run: docker pull pactfoundation/pact-cli:latest
3031
- name: Verify that it will not break a contract in production
3132
run: |
32-
pact/bin/pact-broker can-i-deploy \
33-
--pacticipant 'ServeRest - API Rest' \
34-
--version ${{ github.sha }} \
35-
--to production
36-
env:
37-
PACT_BROKER_BASE_URL: https://paulogoncalves.pactflow.io
38-
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
33+
docker run --rm \
34+
-e PACT_BROKER_BASE_URL \
35+
-e PACT_BROKER_TOKEN \
36+
pactfoundation/pact-cli \
37+
broker can-i-deploy \
38+
--pacticipant 'ServeRest - API Rest' \
39+
--version ${{ github.sha }} \
40+
--to production
3941
4042
deploy-on-serverest-dev:
4143
needs: can-i-deploy
@@ -95,16 +97,14 @@ jobs:
9597

9698
steps:
9799
- uses: actions/checkout@v2
98-
- name: Install pact broker
99-
run: |
100-
curl -LO https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v1.88.51/pact-1.88.51-linux-x86_64.tar.gz
101-
tar xzf pact-1.88.51-linux-x86_64.tar.gz
100+
- run: docker pull pactfoundation/pact-cli:latest
102101
- name: Tag the pact contract with 'production' tag
103102
run: |
104-
pact/bin/pact-broker create-version-tag \
105-
--pacticipant 'ServeRest - API Rest' \
106-
--version ${{ github.sha }} \
107-
--tag production
108-
env:
109-
PACT_BROKER_BASE_URL: https://paulogoncalves.pactflow.io
110-
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
103+
docker run --rm \
104+
-e PACT_BROKER_BASE_URL \
105+
-e PACT_BROKER_TOKEN \
106+
pactfoundation/pact-cli \
107+
broker create-version-tag \
108+
--pacticipant 'ServeRest - API Rest' \
109+
--version ${{ github.sha }} \
110+
--tag production

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ services:
1313
test-contract:
1414
extends: base
1515
environment:
16-
- CONSUMER_VERSION_TAG
1716
- PACT_BROKER_TOKEN
1817
- GITHUB_BRANCH
1918
- PACT_URL
2019
- TRIGGERED_BY_PACT_CHANGE
2120
- PACT_BROKER_BASE_URL
2221
- GITHUB_ACTIONS
22+
- VERBOSE
2323
command: npm run test:contract
2424

2525
test:

test/contract/contract.test.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,65 @@ const isCI = require('is-ci')
33
const { Verifier } = require('@pact-foundation/pact')
44

55
const app = require('../../src/app')
6+
const {
7+
dateOneMonthAgo,
8+
currentGitBranch,
9+
currentGitHash,
10+
isDefaultBranch
11+
} = require('./util')
612

713
describe('ServeRest - Verificação do contrato', () => {
814
const SERVER_URL = 'http://localhost:3001'
915
const server = http.createServer(app)
1016

11-
const gitHash = require('child_process')
12-
.execSync('git rev-parse HEAD')
13-
.toString()
14-
.trim()
15-
16-
const gitBranch = require('child_process')
17-
.execSync('git branch --show-current')
18-
.toString()
19-
.trim()
20-
2117
before(() => {
22-
server.listen(3001, () => {
23-
console.log(`Clients Service listening on ${SERVER_URL}`)
24-
})
18+
server.listen(3001, () => console.log(`Server listening on ${SERVER_URL}`))
2519
})
2620

2721
after(() => {
2822
server.close()
2923
})
3024

3125
it('Validates the expectations of ServeRest', () => {
32-
const options = {
26+
const baseOptions = {
3327
provider: 'ServeRest - API Rest',
3428
logLevel: 'INFO',
3529
pactBrokerToken: process.env.PACT_BROKER_TOKEN,
3630
providerBaseUrl: SERVER_URL,
37-
providerVersionTags: process.env.GITHUB_BRANCH || gitBranch,
38-
providerVersion: gitHash,
39-
publishVerificationResult: isCI
31+
providerVersionTags: currentGitBranch,
32+
providerVersion: currentGitHash,
33+
publishVerificationResult: isCI,
34+
verbose: process.env.VERBOSE === 'true'
4035
}
4136

42-
// https://docs.pact.io/provider/recommended_configuration/#verification-triggered-by-pact-change
43-
if (process.env.TRIGGERED_BY_PACT_CHANGE) {
44-
options.pactUrls = [process.env.PACT_URL]
45-
} else {
46-
const consumerVersionTags = process.env.CONSUMER_VERSION_TAG
47-
? ['production', 'main', process.env.CONSUMER_VERSION_TAG]
48-
: ['production', 'main']
37+
// Para builds que foram 'trigados' por webhook de 'mudança de conteúdo de contrato' é preciso verificar apenas o contrato (pact) alterado.
38+
// A URL (env PACT_URL) será passada pelo webhook para o job de CI.
39+
// https://docs.pact.io/provider/recommended_configurtion/#verification-triggered-by-pact-change
40+
const pactChangedOptions = {
41+
pactUrls: [process.env.PACT_URL]
42+
}
4943

50-
options.consumerVersionTags = consumerVersionTags
51-
options.pactBrokerUrl = process.env.PACT_BROKER_BASE_URL
44+
const fetchPactsDynamicallyOptions = {
45+
pactBrokerUrl: 'https://paulogoncalves.pactflow.io',
46+
consumerVersionSelectors: [
47+
{
48+
tag: currentGitBranch,
49+
fallbackTag: 'main',
50+
latest: true
51+
},
52+
{
53+
tag: 'production',
54+
latest: true
55+
}
56+
],
57+
includeWipPactsSince: isDefaultBranch ? dateOneMonthAgo() : undefined,
58+
enablePending: true
5259
}
5360

54-
return new Verifier(options)
61+
return new Verifier({
62+
...baseOptions,
63+
...(process.env.PACT_URL ? pactChangedOptions : fetchPactsDynamicallyOptions)
64+
})
5565
.verifyProvider()
5666
.then((output) => {
5767
console.log('Pact Verification Complete!')

test/contract/util/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const dateOneMonthAgo = () => {
2+
const date = new Date()
3+
date.setMonth(date.getMonth() - 1)
4+
return date.toISOString()
5+
}
6+
7+
const currentGitHash = require('child_process')
8+
.execSync('git rev-parse HEAD')
9+
.toString()
10+
.trim()
11+
12+
const currentGitBranch = process.env.GITHUB_BRANCH || require('child_process')
13+
.execSync('git branch --show-current')
14+
.toString()
15+
.trim()
16+
17+
const isDefaultBranch = currentGitBranch === 'trunk'
18+
19+
module.exports = {
20+
dateOneMonthAgo,
21+
currentGitBranch,
22+
currentGitHash,
23+
isDefaultBranch
24+
}

0 commit comments

Comments
 (0)