sbmt-pact
is a powerful Ruby gem designed to simplify and streamline Pact testing between microservices. This gem provides a robust set of RSpec utilities to help developers implement consumer-driven contract testing in microservice architectures.
In the world of microservices, ensuring seamless communication between services is crucial. This gem leverages the Pact to enable effective contract testing, allowing you to catch integration issues early in the development cycle. Contract testing is a way to check if different parts of a system work well together. It's like making sure two puzzle pieces fit perfectly before assembling the whole puzzle. Each part is tested on its own to see if it follows a set of rules (the "contract") that everyone agreed on. This helps catch problems early and makes sure all parts of the system speak the same language.
Currently, the existing version of the pact-ruby gem only supports Pact Specification versions v1 and v2, which doesn't allow for:
The pact-ruby
gem is in maintenance mode, as there has been a transition to rust-core, which is intended to be used through FFI in non-Rust stacks.
This gem eliminates the above-mentioned limitations and implements support for the latest versions of Pact specifications:
For each type of interaction (due to their specific features), a separate version of DSL has been implemented. However, the general principles remain the same for each type of interaction.
# Declaration of a consumer test, always include the :pact tag
# This is used in CI/CD pipelines to separate Pact tests from other RSpec tests
# Pact tests are not run as part of the general RSpec pipeline
RSpec.describe "SomePactConsumerTestForAnyTransport", :pact do
# declaration of the type of interaction - here we determine which consumer and provider interact on which transport
has_http_pact_between "CONSUMER-NAME", "PROVIDER-NAME"
# or
has_grpc_pact_between "CONSUMER-NAME", "PROVIDER-NAME"
# or
has_message_pact_between "CONSUMER-NAME", "PROVIDER-NAME"
# the context for one of the interactions, for example GET /api/v2/stores
context "with GET /api/v2/stores" do
let(:interaction) do
# creating a new interaction - within which we describe the contract
new_interaction
# if you need to save any metadata for subsequent use by the test provider,
# for example, specify the entity ID that will need to be moved to the database in the test provider
# we use the provider states, see more at https://docs.pact.io/getting_started/provider_states
.given("UNIQUE PROVIDER STATE", key1: value1, key2: value2)
# the description of the interaction, used for identification inside the package binding,
# is optional in some cases, but it is recommended to always specify
.upon_receiving("UNIQUE INTERACTION DESCRIPTION")
# the description of the request using the matchers
# the name and parameters of the method differ for different transports
.with_request(...)
# the description of the response using the matchers
# the name and parameters of the method differ for different transports
.with_response(...)
# further, there are differences for different types of transports,
# for more information, see the relevant sections of the documentation
end
it "executes the pact test without errors" do
interaction.execute do
# here our client is called for the API being tested
# in this context, the client can be: http client, grpc client, kafka consumer
expect(make_request).to be_success
end
end
end
end
Common DSL Methods:
new_interaction
- initializes a new interactiongiven
- allows specifying a provider state with or without parameters, for more details see https://docs.pact.io/getting_started/provider_statesupon_receiving
- allows specifying the name of the interactionSpecific DSL methods:
with_request(HTTP_METHOD, QUERY_PATH, {headers: kv_hash, body: kv_hash})
- request definitionwith_response(HTTP_STATUS, {headers: kv_hash, body: kv_hash})
- response definitionMore at http_client_spec.rb
Specific DSL methods:
with_service(PROTO_PATH, RPC_SERVICE_AND_ACTION)
- specifies the contract used, PROTO_PATH is relative from the app rootwith_request(request_kv_hash)
- request definitionwith_response(response_kv_hash)
- response definitionMore at grpc_client_spec.rb
Specific DSL methods:
with_headers(kv_hash)
- message-headers definition; you can use matcherswith_metadata(kv_hash)
- message-metadata definition (special keys are key
and topic
, where, respectively, you can specify the matchers for the partitioning key and the topicNext, the specifics are one of two options for describing the format:
JSON (to describe a message in a JSON representation):
with_json_contents(kv_hash)
- message format definitionPROTO (to describe the message in the protobuf view):
with_proto_class(PROTO_PATH, PROTO_MESSAGE_NAME)
- specifies the contract used, PROTO_PATH is relative to the root, PROTO_MESSAGE_NAME is the name of the message used from the proto filewith_proto_contents(kv_hash)
- message format definitionMore at kafka_spec.rb
Matchers are special helper methods that allow you to define rules for matching request/response parameters at the level of the pact manifest. The matchers are described in the Pact specifications. In this gem, the matchers are implemented as RSpec helpers.
For details of the implementation, see matchers.rb
match_exactly(sample)
- match the exact value specified in the samplematch_type_of(sample)
- match the data type (integer, string, boolean) specified in the samplematch_include(sample)
- match a substringmatch_any_string(sample)
- match any string, because of the peculiarities, null and empty strings will also be matched herematch_any_integer(sample)
- match any integermatch_any_decimal(sample)
- match any float/doublematch_any_number(sample)
- match any integer/float/doublematch_any_boolean(sample)
- match any true/falsematch_uuid(sample)
- match any UUID (match_regex
is used under the hood)match_regex(regex, sample)
- match by regexpmatch_datetime(format, sample)
- match any datetimematch_iso8601(sample)
- match datetime in ISO8601 (the matcher does not fully comply with ISO8601, matches only the most common variants, match_regex
is used under the hood)match_date(format, sample)
- match any date (rust datetime)match_time(format, sample)
- match any time (rust datetime)match_each(template)
- match all the elements of the array according to the specified template, you can use it for nested elementsmatch_each_regex(regex, sample)
- match all array elements by regex, used for arrays with string elementsmatch_each_key(template, key_matchers)
- match each hash key according to the specified templatematch_each_value(template)
- match each hash value according to the specified template, can be used for nested elementsmatch_each_kv(template, key_matchers)
- match all the keys/values of Hash according to the specified template and key_matchers, can be used for nested elementsSee the different uses of the matchers in matchers_spec.rb
Install Dip
dip provision
dip rspec
The Pact tests are not run within the general rspec pipeline, they need to be run separately, see below
dip pact consumer
# or
bundle exec rspec -t pact spec/pact/providers/**/*_spec.rb
NOTE If you have never run it, you need to run it at least once to generate json-pact manifests that will be used in provider tests (below)
dip pact provider
# or
bundle exec rspec -t pact spec/pact/consumers/*_spec.rb
If you need to check the ability to work with a package broker locally, you need to do the following:
$ dip up pact-broker
spec/internal/pacts
based on the results of successful completion of consumer tests)$ dip pact consumer
$ dip pact-cli publish
Make sure that the manifest is published by opening it in a browser http://localhost:9292 (don't forget to forward port 9292 to localhost, see docker-compose.yml
)
run provider tests using a pact broker
$ dip pact provider-with-local-broker