Skip to content

nicklaros/pact-contract-test-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Service Dependencies Map

                                   --> 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.

Prerequisite

  1. Make sure to run command sudo pact-go -l DEBUG install to download and install the required libraries.

Steps For Explaining The Demo

  1. 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 in fe/main.go
    • productServiceBaseURL variable in gateway/main.go
    • inventoryServiceBaseURL variable in gateway/main.go
  2. In consumer driven contract testing, contracts is defined by consumer. Review consumer test files written on fe and gateway services, eg:

    • Consumer test on fe service: fe/consumer_test.go
    • Consumer test on gateway service: gateway/consumer_test.go
  3. 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 and gateway service: fe/pacts/fe_service-gateway_service.json
    • Contract between gateway service and product service: gateway/pacts/gateway_service-product_service.json
    • Contract between gateway service and inventory service: gateway/pacts/gateway_service-inventory_service.json
  4. 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 and inventory 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
  5. 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
    
  6. Up to this point, to successfully verify gateway service, you must first run both product service and inventory service, otherwise it will fails because gateway service need to fetch product name from product service and available stock from inventory 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?

  7. 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 and inventory services with stub service using pact contract files generated by consumer tests on gateway service.

    Kill product service and inventory 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages