Skip to content

Commit

Permalink
Merge pull request #46 from YOU54F/workshops
Browse files Browse the repository at this point in the history
gRPC AreaCalculator Examples - Updates
  • Loading branch information
rholshausen authored Oct 17, 2023
2 parents 1220875 + cc3614c commit 44c8bfd
Show file tree
Hide file tree
Showing 1,437 changed files with 8,827 additions and 673,516 deletions.
1 change: 1 addition & 0 deletions examples/csv/csv-consumer-jvm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin
1 change: 1 addition & 0 deletions examples/csv/csv-provider-jvm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/server/bin
1 change: 1 addition & 0 deletions examples/gRPC/area_calculator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/server/bin
62 changes: 62 additions & 0 deletions examples/gRPC/area_calculator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@


# Verifier CLI

verifier_cli_install_cargo:
cargo install pact_verifier_cli

# GoLang
consumer_build_go:
cd consumer-go && \
go mod tidy && \
go mod vendor
provider_build_go:
cd provider-go && \
go mod tidy && \
go mod vendor
consumer_test_go:
cd consumer-go && go test
provider_run_go:
cd provider-go && go run provider.go
provider_test_go:
cd provider-go && go test provider_test.go
provider_test_go_cli:
cd provider-go && \
pact_verifier_cli -f ../consumer-go/pacts/grpc-consumer-go-area-calculator-provider.json \
--transport grpc -p $$PORT

## Rust

consumer_test_rust:
cd consumer-rust && \
cargo test

## JVM - Gradle

consumer_test_jvm:
cd consumer-jvm && \
./gradlew clean test

provider_run_jvm:
cd provider-jvm && \
./gradlew run
provider_test_jvm:
cd provider-jvm && \
./gradlew clean test
provider_test_jvm_cli:
cd provider-jvm && \
pact_verifier_cli -f ../consumer-jvm/build/pacts/grpc-consumer-jvm-area-calculator-provider.json \
--transport grpc -p $$PORT

## JVM - Maven

consumer_test_maven:
cd consumer-maven && \
mvn test
pacts_move_maven:
cd provider-maven && \
mkdir -p src/test/resources/pacts && \
cp ../consumer-maven/target/pacts/*.json src/test/resources/pacts
provider_test_maven:
cd provider-maven && \
mvn clean test
2 changes: 2 additions & 0 deletions examples/gRPC/area_calculator/consumer-go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
consumer
vendor
197 changes: 196 additions & 1 deletion examples/gRPC/area_calculator/consumer-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ It tests the following interaction from the proto file:
rpc calculate (ShapeMessage) returns (AreaResponse) {}
```

You need to [install the Pact Go library](https://github.com/pact-foundation/pact-go/tree/2.x.x#installation).
You need to [install the Pact Go library](https://github.com/pact-foundation/pact-go/tree/2.x.x#installation).

For more information about gRPC in Go - Check out the [Go gRPC quick start](https://grpc.io/docs/languages/go/quickstart/)

## gRPC plugin

Expand All @@ -18,8 +20,201 @@ To run the test in this project, it requires the gRPC plugin to be installed. Se
To generate the Go code for the proto file, you need to install the Protobuf compiler (protoc), and install the Go
protobuf and grpc protoc plugins, and then run `protoc --go_out=. --go-grpc_out=. --proto_path ../proto ../proto/area_calculator.proto`.

### Protoc Compiler

- https://pkg.go.dev/google.golang.org/protobuf/cmd/protoc-gen-go
- https://pkg.go.dev/google.golang.org/grpc/cmd/protoc-gen-go-grpc

```sh
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
export PATH="$PATH:$(go env GOPATH)/bin"
protoc --go_out=. --go-grpc_out=. --proto_path ../proto ../proto/area_calculator.proto
```

## Test method

The test method [TestCalculateClient](consumer_test.go) first sets up the interaction using the Pact DSL, then sets up a
gRPC mock server to use. The generated stub structs are then used to send the `ShapeMessage` to the mock server,
and an `AreaResponse` message is received back. This is then validated.

## Run the test

```sh
go test
```

A pact file, will be generated in the `./pacts` folder.

### Pact File

You can see it contains the Protobuf file used in the interaction, as well as the consumers request and response expectations.

These will be used to replay against the provider to verify the contract can be honoured, in isolation.

```json
{
"consumer": {
"name": "grpc-consumer-go"
},
"interactions": [
{
"description": "calculate rectangle area request",
"interactionMarkup": {
"markup": "```protobuf\nmessage AreaResponse {\n repeated float value = 1;\n}\n```\n",
"markupType": "COMMON_MARK"
},
"pending": false,
"pluginConfiguration": {
"protobuf": {
"descriptorKey": "a85dff8f82655a9681aad113575dcfbb",
"service": "Calculator/calculateMulti"
}
},
"request": {
"contents": {
"content": "CgwSCg0AAEBAFQAAgEAKBwoFDQAAQEA=",
"contentType": "application/protobuf;message=AreaRequest",
"contentTypeHint": "BINARY",
"encoded": "base64"
},
"matchingRules": {
"body": {
"$.shapes[0].rectangle.length": {
"combine": "AND",
"matchers": [
{
"match": "number"
}
]
},
"$.shapes[0].rectangle.width": {
"combine": "AND",
"matchers": [
{
"match": "number"
}
]
},
"$.shapes[1].square.edge_length": {
"combine": "AND",
"matchers": [
{
"match": "number"
}
]
}
}
},
"metadata": {
"contentType": "application/protobuf;message=AreaRequest"
}
},
"response": [
{
"contents": {
"content": "CggAAEBBAAAQQQ==",
"contentType": "application/protobuf;message=AreaResponse",
"contentTypeHint": "BINARY",
"encoded": "base64"
},
"matchingRules": {
"body": {
"$.value[0].*": {
"combine": "AND",
"matchers": [
{
"match": "number"
}
]
},
"$.value[1].*": {
"combine": "AND",
"matchers": [
{
"match": "number"
}
]
}
}
},
"metadata": {
"contentType": "application/protobuf;message=AreaResponse"
}
}
],
"transport": "grpc",
"type": "Synchronous/Messages"
}
],
"metadata": {
"pactRust": {
"ffi": "0.4.5",
"mockserver": "1.1.1",
"models": "1.1.2"
},
"pactSpecification": {
"version": "4.0"
},
"plugins": [
{
"configuration": {
"a85dff8f82655a9681aad113575dcfbb": {
"protoDescriptors": "CsoHChVhcmVhX2NhbGN1bGF0b3IucHJvdG8SD2FyZWFfY2FsY3VsYXRvciK6AgoMU2hhcGVNZXNzYWdlEjEKBnNxdWFyZRgBIAEoCzIXLmFyZWFfY2FsY3VsYXRvci5TcXVhcmVIAFIGc3F1YXJlEjoKCXJlY3RhbmdsZRgCIAEoCzIaLmFyZWFfY2FsY3VsYXRvci5SZWN0YW5nbGVIAF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BCHFoXaW8ucGFjdC9hcmVhX2NhbGN1bGF0b3LQAgFiBnByb3RvMw==",
"protoFile": "syntax = \"proto3\";\n\npackage area_calculator;\n\noption php_generic_services = true;\noption go_package = \"io.pact/area_calculator\";\n\nservice Calculator {\n rpc calculateOne (ShapeMessage) returns (AreaResponse) {}\n rpc calculateMulti (AreaRequest) returns (AreaResponse) {}\n}\n\nmessage ShapeMessage {\n oneof shape {\n Square square = 1;\n Rectangle rectangle = 2;\n Circle circle = 3;\n Triangle triangle = 4;\n Parallelogram parallelogram = 5;\n }\n}\n\nmessage Square {\n float edge_length = 1;\n}\n\nmessage Rectangle {\n float length = 1;\n float width = 2;\n}\n\nmessage Circle {\n float radius = 1;\n}\n\nmessage Triangle {\n float edge_a = 1;\n float edge_b = 2;\n float edge_c = 3;\n}\n\nmessage Parallelogram {\n float base_length = 1;\n float height = 2;\n}\n\nmessage AreaRequest {\n repeated ShapeMessage shapes = 1;\n}\n\nmessage AreaResponse {\n repeated float value = 1;\n}\n"
}
},
"name": "protobuf",
"version": "0.3.6"
}
]
},
"provider": {
"name": "area-calculator-provider"
}
}
```

## Run the consumer & provider

In terminal 1: Start the provider

```sh
cd ../provider-go
go run provider.go
```

You should see an address

```console
go run provider.go
2023/10/16 21:39:17 Server started 127.0.0.1:58132
```

In terminal 2: Run the consumer

Use the address, shown by the provider, with the `--addr` flag

```sh
cd consumer-go
go run consumer.go --addr localhost:58132
```

Outgoing request

```console
2023/10/16 21:40:39 Sending calculate rectangle and square request
```

You should see the provider receive the request

```console
2023/10/16 21:40:39 Calculating the area for multiple values shapes:<rectangle:<length:3 width:4 > > shapes:<square:<edge_length:3 > >
```

And the consumer showing the returned response

```console
2023/10/16 21:40:39 Sending calculate rectangle and square request
Areas: [12.000000 9.000000]
```
53 changes: 13 additions & 40 deletions examples/gRPC/area_calculator/consumer-go/go.mod
Original file line number Diff line number Diff line change
@@ -1,53 +1,26 @@
module area_calculator/consumer

go 1.18
go 1.21

require (
github.com/golang/protobuf v1.5.2
github.com/pact-foundation/pact-go/v2 v2.0.0-beta.22
google.golang.org/grpc v1.46.2
github.com/golang/protobuf v1.5.3 // indirect
github.com/pact-foundation/pact-go/v2 v2.0.1
google.golang.org/grpc v1.58.3
)

require (
cloud.google.com/go v0.101.1 // indirect
cloud.google.com/go/compute v1.6.1 // indirect
cloud.google.com/go/iam v0.3.0 // indirect
cloud.google.com/go/storage v1.22.1 // indirect
github.com/aws/aws-sdk-go v1.44.20 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/googleapis/go-type-adapters v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.6.1 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.5.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.15.4 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cobra v1.1.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
google.golang.org/api v0.80.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220524023933-508584e28198 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require github.com/stretchr/testify v1.8.0
require (
github.com/stretchr/testify v1.8.3
google.golang.org/protobuf v1.31.0
)
Loading

0 comments on commit 44c8bfd

Please sign in to comment.