Template project to help bootstrap a new Pact Plugin for the Pact framework.
Features:
- Stubbed gRPC methods ready to implement
- Automated release procedure
- Support for recommended common platform/targets
- Levelled logging for observability
TODO
- Support Matchers and Generators (requires FFI package support)
βββ go.mod # Go module (β
fill me in!)
βββ main.go # Entrypoint for the application
βββ plugin.go # Stub gRPC methods for you to implement (β
fill me in!)
βββ configuration.go # Type definitions for your plugin's DSL (β
fill me in!)
βββ Makefile # Build configuration (β
fill me in!)
βββ io_pact_plugin/ # Location of protobuf and gRPC definitions for Plugin Framework
βββ log.go # Logging utility
βββ pact-plugin.json # Plugin configuration file
βββ pact.go # Pact type definitions
βββ server.go # The gRPC server implementation
βββ RELEASING.md # Instructions on how to release π
The protoc compiler must be installed for this plugin
- Clone this repository
- Create a new repository in GitHub. The name of the plugin should be
pact-<PROJECT>-plugin
e.g.pact-protobuf-plugin
- Push this code to your new repository
Replace github.com/pact-foundation/pact-plugin-template-golang
in go.mod
with your github URL (without the protocol prefix) to identify the package uniquely.
Similarly, correct the import at the top of plugin.go
and server.go
.
Or simply do a workspace wide search and replace from github.com/pact-foundation/pact-plugin-template-golang
to your go module name.
Run:
go mod tidy
go mod vendor
To ensure the dependencies and vendoring are correct.
In the top of the Makefile
set PROJECT
to your plugin's name.
PROJECT
should map to <PROJECT>
in your GitHub repository.
NOTE: It's important that the name of your go module and the PROJECT
variable must align, to create artifacts discoverable to the CLI tooling, such as the Plugin CLI.
This is how the users of your plugin will write the plugin specific interaction details.
For example, take the following HTTP interaction:
await pact
.addInteraction()
.given('the Matt protocol exists')
.uponReceiving('an HTTP request to /matt')
.usingPlugin({
plugin: 'matt',
version: '0.0.4',
})
.withRequest('POST', '/matt', (builder) => {
builder.pluginContents('application/matt', mattRequest); // <- request
})
.willRespondWith(200, (builder) => {
builder.pluginContents('application/matt', mattResponse); // <- response
})
.executeTest((mockserver) => {
...
The user needs to specify the request and response body portion of the request.
Because the use cases for plugins are so wide and varied, the framework does not impose limits on this data structure and is something you need to design.
This being said, most plugins have opted to use a JSON structure.
This structure should be represented in configuration.go
Think about how you would like your user to specify the interaction details for the various interaction types.
Here is an example for a TCP plugin with a custom text protocol:
Set the expected response from the API:
mattMessage := `{"response": {"body": "hellotcp"}}`
Set the request/response all in one go:
mattMessage := `{"request": {"body": "hellotcp"}, "response":{"body":"tcpworld"}}`
Separate out the body on the request/response part of the interaction:
mattRequest := `{"request": {"body": "hello"}}`
mattResponse := `{"response":{"body":"world"}}`
Open plugin.go
and update the relevant RPC functions.
Depending on your use case, some of the RPC calls won't be required, each method is well signposted to help you along.
You should log regularly. Debugging gRPC calls from the framework can be challenging, as the plugin is started asynchronously by the Plugin Driver behind the scenes.
There are two ways to log:
- Stdout - all stdout (e.g.
fmt.Print*
) is pulled into the general Pact logs for the framework you're running - To file. All calls to
log.Print*
will be written to file
The log setup has three main features:
- It works with the native Go
log
package - It logs to a file relative to plugin execution in
log/plugin.log
- It is levelled, at the direction of the plugin driver (that is, the log level will pass in from the driver which will restrict the levels logged in this plugin)
To write something to the log file, you simply use the log
package, with the level prefixed as per below:
log.Println("[TRACE] ...")
log.Println("[DEBUG] ...")
log.Println("[INFO] ...")
log.Println("[WARN] ...")
log.Println("[ERROR] ...")
Follow the steps in Releasing to publish a new version of your Plugin.
The following command will build the plugin, and install into the correct plugin directory for local development:
make install_local
You can then reference your plugin in local tests to try it out.
If a new protobuf definition is required (e.g. to support a new feature), copy into the io_pact_plugin
folder and run the following Make task:
make proto
It will update the definitions in the io_pact_plugin
package. Note this may result in a breaking change, depending on the version. So upgrade carefully.
This code base should automatically create artifacts for the following OS/Architecture combiations:
OS | Architecture | Supported |
---|---|---|
OSX | x86_64 | β |
OSX | arm | β |
Linux | x86_64 | β |
Linux | arm | β |
Windows | x86_64 | β |
Windows | arm | β |