--> product service
/
fe service --> gateway service --
\
--> inventory service
The fe
service ask product information to gateway
service, which then fetch name of product from product
service, and available stock from inventory
service.
- Make sure to run command
sudo pact-go -l DEBUG install
to download and install the required libraries.
-
Run all services using this commands.
// Run fe service PORT=8080 go run fe/main.go // Run gateway service PORT=8081 go run gateway/main.go // Run inventory service PORT=8082 go run inventory/main.go // Run product service PORT=8083 go run product/main.go
Then, open
fe
service on http://localhost:8080, from there you can walk through the dependencies service via link provided in the response body.If you use other port than above, make sure you adjust:
gatewayServiceBaseURL
variable infe/main.go
productServiceBaseURL
variable ingateway/main.go
inventoryServiceBaseURL
variable ingateway/main.go
-
In consumer driven contract testing, contracts is defined by consumer. Review consumer test files written on
fe
andgateway
services, eg:- Consumer test on
fe
service:fe/consumer_test.go
- Consumer test on
gateway
service:gateway/consumer_test.go
- Consumer test on
-
Run all of them to get the sense of what they do.
// Run consumer test on fe service go test -run Consumer$ -count 1 ./fe // Run consumer test on gateway service go test -run Consumer$ -count 1 ./gateway
Note that it will generate pact contract files that define API contract between consumer and provider, eg:
- Contract between
fe
service andgateway
service:fe/pacts/fe_service-gateway_service.json
- Contract between
gateway
service andproduct
service:gateway/pacts/gateway_service-product_service.json
- Contract between
gateway
service andinventory
service:gateway/pacts/gateway_service-inventory_service.json
- Contract between
-
Those generated contracts need to be verified by provider so that we can be sure that the contract is valid.
Review verify provider test files written on
gateway
,product
andinventory
services, eg:- Verify provider test on
gateway
service:gateway/provider_test.go
- Verify provider test on
product
service:product/provider_test.go
- Verify provider test on
inventory
service:inventory/provider_test.go
- Verify provider test on
-
Run all of them to get the sense of what they do.
// Run verify provider test on gateway service go test -run ^TestPactProvider$ -count 1 ./gateway // Run verify provider test on product service go test -run ^TestPactProvider$ -count 1 ./product // Run verify provider test on inventory service go test -run ^TestPactProvider$ -count 1 ./inventory
-
Up to this point, to successfully verify
gateway
service, you must first run bothproduct
service andinventory
service, otherwise it will fails becausegateway
service need to fetch product name fromproduct
service and available stock frominventory
service.Lets kill inventory service to make the test fails.
- Kill inventory service
- Run verify gateway service
go test -run ^TestPactProvider$ -count 1 ./gateway
We can see the test is failing, which is expected. So we must run all dependency services to make it succeed. But it comes with a great cost, eg:
- Complicating test data setup and teardown, hence hurt maintainability.
- Hard to manage tightly coupled end-to-end test environments.
- Bigger resource to run our test, hence costly.
So how we tackle this issue?
-
This is where Pact can help us to run our contract test easily and efficiently. For above use case, we can use pact-stub-service to replace
product
andinventory
services with stub service using pact contract files generated by consumer tests ongateway
service.Kill
product
service andinventory
service, then run stub services using this commands:// Run stub product service pact-stub-service -p 8082 gateway/pacts/gateway_service-product_service.json // Run stub inventory service pact-stub-service -p 8083 gateway/pacts/gateway_service-inventory_service.json
After that, all we have to do is run our verify provider test again.
go test -run ^TestPactProvider$ -count 1 ./gateway
Now our test will succeed without having to run the world. It also good to know that our stub services are guaranteed to represent behaviour of the real service, so you can test with confidence.