Step 13 - Using a PactFlow Broker
Move to step 13
git checkout step13
- Javascript
npm install
Learning Objectives
Step | Title | Concept Covered | Learning objectives | Further Reading |
---|---|---|---|---|
step 13 | Integrate with a PactFlow authenticated broker | Automation |
|
In step 11 we've been publishing our pacts from the consumer and provider projects to our locally hosted open source Pact broker.
We can use a managed Pact Broker from PactFlow to do this instead.
Using a hosted pact broker with pactflow, will allow you to concentrate on testing your application without having to worry about managing infrastructure, along with a number of other useful features.
Creating a pactflow account
Create a new PactFlow account and signup to the free Starter Plan. You will be emailed a set of credentials to access your account, these credentials are only for accessing the UI.
Grab your API Token(Click on settings -> API Tokens -> Read/write token -> COPY ENV VARS) and set the environment variables in your terminal as follows:
export PACT_BROKER_BASE_URL=https://<your_broker_name>.pactflow.io
export PACT_BROKER_TOKEN=exampleToken
Update your scripts to use the pact broker token based authentication method
First, in the consumer project we need to tell Pact about our broker.
In consumer/publish.pact.js
:
const pact = require('@pact-foundation/pact-node');
if (!process.env.CI && !process.env.PUBLISH_PACT) {
console.log("skipping Pact publish...");
process.exit(0)
}
const pactBrokerUrl = process.env.PACT_BROKER_BASE_URL || 'https://<your_broker_name>.pactflow.io';
const pactBrokerToken = process.env.PACT_BROKER_TOKEN || 'pact_workshop';
const gitHash = require('child_process')
.execSync('git rev-parse --short HEAD')
.toString().trim();
const opts = {
pactFilesOrDirs: ['./pacts/'],
pactBroker: pactBrokerUrl,
pactBrokerToken: pactBrokerToken,
tags: ['prod', 'test'],
consumerVersion: gitHash
};
pact
.publishPacts(opts)
.then(() => {
console.log('Pact contract publishing complete!');
console.log('');
console.log(`Head over to ${pactBrokerUrl}`);
console.log('to see your published contracts.')
})
.catch(e => {
console.log('Pact contract publishing failed: ', e)
});
Now run
❯ npm run test:pact --prefix consumer
> consumer@0.1.0 test:pact /Users/you54f/dev/saf/dev/pact-workshop-clone/consumer
> react-scripts test --testTimeout 30000 pact.spec.js
PASS src/api.pact.spec.js
API Pact test
getting all products
✓ products exists (19ms)
✓ no products exists (10ms)
✓ no auth token (10ms)
getting one product
✓ ID 10 exists (10ms)
✓ product does not exist (8ms)
✓ no auth token (12ms)
Then publish your pacts:
❯ npm run pact:publish --prefix consumer
> pact-broker publish ./pacts --consumer-app-version="1.0.0" --auto-detect-version-properties
Updated FrontendWebsite version 71c1b7-step12+71c1b7.SNAPSHOT.SB-AS-G7GM9F7 with branch step12
Pact successfully published for FrontendWebsite version 71c1b7-step12+71c1b7.SNAPSHOT.SB-AS-G7GM9F7 and provider ProductService.
View the published pact at https://testdemo.pactflow.io/pacts/provider/ProductService/consumer/FrontendWebsite/version/71c1b7-step12%2B71c1b7.SNAPSHOT.SB-AS-G7GM9F7
Events detected: contract_published, contract_requiring_verification_published, contract_content_changed (first time untagged pact published)
Webhook "Automatically trigger pact verification on contract change." triggered for event contract_requiring_verification_published.
View logs at https://testdemo.pactflow.io/triggered-webhooks/fa8d571e-8b61-41f8-9955-79a6fa9481fd/logs
Have a browse around your pactflow broker and see your newly published contract
Verify contracts on Provider
All we need to do for the provider is update where it finds its pacts, from local broker, to one from a hosted pactflow broker
In provider/product/product.pact.test.js
:
//replace
pactBrokerUrl: process.env.PACT_BROKER_BASE_URL || "http://127.0.0.1:8000",
pactBrokerUsername: process.env.PACT_BROKER_USERNAME || "pact_workshop",
pactBrokerPassword: process.env.PACT_BROKER_PASSWORD || "pact_workshop",
// with
pactBrokerUrl :process.env.PACT_BROKER_BASE_URL || 'https://<your_broker_name>.pactflow.io',
pactBrokerToken: process.env.PACT_BROKER_TOKEN || 'pact_workshop',
Let's run the provider verification one last time after this change:
❯ npm run test:pact --prefix provider
> product-service@1.0.0 test:pact /Users/you54f/dev/saf/dev/pact-workshop-clone/provider
> npx jest --testTimeout 30000 --testMatch "**/*.pact.test.js"
INFO: pact@9.11.1/84537 on safmac.local: Verifying provider
INFO: pact-node@10.10.1/84537 on safmac.local: Verifying Pacts.
INFO: pact-node@10.10.1/84537 on safmac.local: Verifying Pact Files
PASS product/product.pact.test.js (6.786s)
Pact Verification
✓ validates the expectations of ProductService (6006ms)
INFO: Verification results published to https://you54f.pactflow.io/pacts/provider/ProductService/consumer/FrontendWebsite/pact-version/c4b62aae734255d00eba62ced76594343a148e29/verification-results/256
Can I deploy?
As per step 11, we can use the can-i-deploy
command to gate releases.
You can run the pact-broker can-i-deploy
checks as follows:
❯ cd consumer
❯ npx pact-broker can-i-deploy \
--pacticipant FrontendWebsite \
--latest
Computer says yes \o/
CONSUMER | C.VERSION | PROVIDER | P.VERSION | SUCCESS?
----------------|-----------|----------------|-----------|---------
FrontendWebsite | fe0b6a3 | ProductService | 1.0.0 | true
All required verification results are published and successful
----------------------------
❯ cd provider
❯ npx pact-broker can-i-deploy \
--pacticipant ProductService \
--latest
Computer says yes \o/
CONSUMER | C.VERSION | PROVIDER | P.VERSION | SUCCESS?
----------------|-----------|----------------|-----------|---------
FrontendWebsite | fe0b6a3 | ProductService | 1.0.0 | true
All required verification results are published and successful
NOTE: Because we have exported the PACT_*
environment variables, we can omit the necessary flags on the command.
That's it - you're now a Pact pro. Go build 🔨