pact-foundation/pact_broker

Multiple pacts in a multi-module project

spaniard81 opened this issue · 10 comments

Hi, I have a java project setup where my main service (a consumer in PACT world) comprises of sub-modules (A, B and C) which all rely on the same provider ServiceProvider. Since A,B and C have PACTs with ServiceProvider integration testing on the consumer end (MainService) generates indivdual pacts in the respective target folder of the sub-modules as illustrated below:

ConsumerName: MainService
ProviderName: ServiceProvider

image

When publishing the pacts to the broker (mvn publish:pact) I observe that the sub-module successively overwrite the pacts published by the other modules. As a result only the latest pact i.e. from SubModule-C are published with the pact-broker.

I would like the pacts for all the three sub-modules A, B and C to be published to the pact-broker. What is a possible solution in the current framework? It is not possible in my current design to make A,B and C as individual services (and deploy them). MainService is the endpoint for all of the submodules.

So, are you saying that each of these submodules belongs to the same consumer, and you effectively want to merge the 3 pacts together?

If you transfer all the pact files into the same directory and publish them using the pact-cli (rather than the maven pact:publish), then it will merge pacts with the same consumer/provider together before publishing. The docs for the pact publish cli are here.

yes, the three pacts belong to the same consumer and therefore the three jsons in respective target/pacts dir have the same property value for:

`"consumer": {
"name": "Mainservice"
},

and

"provider": {
"name": "ServiceProvider"
}`

Also, the json files also have the same filename. I am following the CLI approach you have suggested. First, i had to rename the files (as they all have the same name) from the sub-modules. However I getting the following error on CLI:

$ pact-1.88.83-win32/pact/bin/pact-broker.bat publish pacts -b myURL -u myUSER -p myPASS -a myVer Merging pacts/mainservice-serviceprovider-1.json, pacts/mainservice-serviceprovider-2.json C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/merge_pacts.rb:21:in merge': undefined method each' for nil:NilClass (NoMethodError) from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/merge_pacts.rb:13:in block in call'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/merge_pacts.rb:13:in each' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/merge_pacts.rb:13:in reduce'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/merge_pacts.rb:13:in call' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/publish_pacts.rb:132:in merge_contents'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/publish_pacts.rb:119:in block in contracts_for' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/publish_pacts.rb:117:in collect'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/publish_pacts.rb:117:in contracts_for' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/publish_pacts.rb:54:in request_body_for'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/publish_pacts.rb:60:in block in publish_pacts' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/rake-13.0.6/lib/rake/file_list.rb:70:in collect'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/rake-13.0.6/lib/rake/file_list.rb:70:in collect' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/publish_pacts.rb:59:in publish_pacts'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/publish_pacts.rb:32:in call' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/publish_pacts.rb:14:in call'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/cli/broker.rb:157:in publish_pacts' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/cli/broker.rb:42:in publish'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/thor-1.2.1/lib/thor/command.rb:27:in run' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in invoke_command'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/thor-1.2.1/lib/thor.rb:392:in dispatch' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/thor-1.2.1/lib/thor/base.rb:485:in start'
from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/vendor/ruby/2.2.0/gems/pact_broker-client-1.59.0/lib/pact_broker/client/cli/custom_thor.rb:15:in start' from C:/myGitProjs/dev/pact-broker-cli/pact-1.88.83-win32/pact/lib/app/pact-broker.rb:34:in

'
`

One more piece of information I wanted to add: the pacts I am referring to are message pacts for communication over Kafka.

The error "merge_pacts.rb:21:in merge': undefined method each' for nil:NilClass (NoMethodError)" and the code at line 21
additional[:interactions].each do |new_interaction|
suggests that the merge is only possible for HTTP Pacts and not for Message Pacts (as these contains messages and not interactions).

Resolved this by making each sub-module as a separate consumer. This way, we have now 3 consumers in a project/service rather than just one main consumer.

I've updated the client so that it can merge message pacts now. Please update and try again. If you have 3 different consumer names, you won't be able to use can-i-deploy properly.

Hi. I am experiencing this very same issue.
I have two pact files one for HTTP and another for a Message and the HTTP pact is always overwriting the Message pact. I am using the --merge option.

the version of pact-broker is 1.64.0.

This is not a Pact Broker feature, it's a client feature. What version of the pact broker client are you using? Please update to the latest version and let me know if you have the same issue.

It is the pact-broker client that I am referring to. The version is 1.64.0

% pact-broker version
1.64.0

I installed the client from pact-1.88.90-osx.tar.gz.

This is the command I am running

cd .pacts    # directory with a HTTP pact and message Pact
pact-broker publish . -a d07059e-2022061618061655399065 -b https://<company>.pactflow.io --tag-with-git-branch --auto-detect-version-properties -k <my_token> -v --merge

Ah, right. The feature was released in 1.60.0 of the pact broker client, so you should have it. https://github.com/pact-foundation/pact_broker-client/blob/master/CHANGELOG.md#features-4

Can you please open an issue in the pact_broker-client project and provide the exact steps to reproduce the issue. Please attach json files that demonstrate the merge issue.

Closing due to inactivity.