From 62a69727a2a5d7966f3589dc0436369567e62827 Mon Sep 17 00:00:00 2001 From: jgough Date: Tue, 12 Dec 2023 14:17:22 +0000 Subject: [PATCH 1/2] Add Gripmock for grpc mocking --- gripmock/Dockerfile | 67 +++++++++++++++++++++++++++++++++++++++++++++ gripmock/Readme.md | 42 ++++++++++++++++++++++++++++ gripmock/go.mod | 34 +++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 gripmock/Dockerfile create mode 100644 gripmock/Readme.md create mode 100644 gripmock/go.mod diff --git a/gripmock/Dockerfile b/gripmock/Dockerfile new file mode 100644 index 0000000..b9de9af --- /dev/null +++ b/gripmock/Dockerfile @@ -0,0 +1,67 @@ +# BASE_TAG *may* include VERSION, docker docs use TAG for the image being built +# https://docs.docker.com/engine/reference/commandline/image_tag/ +ARG BASE_TAG=latest + +FROM tkpd/gripmock:v1.12.1 + +# NOTE: the protoc stuff is copied from the builder image repo. + +# protoc command +ENV PROTOC_VERSION=24.3 +RUN PROTOC_ARCH=$(uname -m); \ + if test "$PROTOC_ARCH" = "aarch64"; then PROTOC_ARCH=aarch_64; fi; \ + wget https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-${PROTOC_ARCH}.zip \ + && unzip -q -o protoc-${PROTOC_VERSION}-linux-${PROTOC_ARCH}.zip -d /usr/local bin/protoc \ + && chmod +x /usr/local/bin/protoc \ + && unzip -q -o protoc-${PROTOC_VERSION}-linux-${PROTOC_ARCH}.zip -d /usr/local include/* \ + && chmod -R +rx /usr/local/include \ + && rm -rf protoc-${PROTOC_VERSION}-linux-${PROTOC_ARCH}.zip + +# Download common google api proto files. +ENV GOOGLE_COMMON_PROTOS_VERSION=1.50.0 +RUN wget https://github.com/googleapis/api-common-protos/archive/refs/tags/${GOOGLE_COMMON_PROTOS_VERSION}.tar.gz \ + && tar -C /usr/local -xvzf ${GOOGLE_COMMON_PROTOS_VERSION}.tar.gz \ + && rm -rf /usr/local/usr/local/api-common-protos-master \ + && mv /usr/local/api-common-protos-${GOOGLE_COMMON_PROTOS_VERSION} /usr/local/api-common-protos-master \ + && rm -f ${GOOGLE_COMMON_PROTOS_VERSION}.tar.gz + +# https://github.com/golang/protobuf/releases +# https://github.com/grpc-ecosystem/grpc-gateway/releases +# https://github.com/protocolbuffers/protobuf/releases +# +# TODO: it is not recommended to install pinned versions of grpc_gateway, +# protoc-gen-go and protoc-gen-grpc-gateway in a Dockerfile. Less +# prone to failure is to rely on the tools/tools.go file and control +# versions in go.mod. (Single source of truth) +# (From conversation with jbrandhorst on slack grpc-gateway channel) +# see ticket #756 +# Howver - for now - we must ensure that versions in this Dockerfile +# corerspond to versions in src/go.mod in the avid repo +# ensure that these values correspond to those in src/go.mod +# Hybrid tools - installs tools plus importable packages +ENV GRPC_GATEWAY_VERSION=2.18.0 +RUN go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v${GRPC_GATEWAY_VERSION} \ + && go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@v${GRPC_GATEWAY_VERSION} + +ENV PROTOC_GEN_GO_GRPC_VERSION=1.3.0 +RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v${PROTOC_GEN_GO_GRPC_VERSION} + +# Additional proto utilities +ENV PROTOC_GEN_VALIDATE_VERSION=v1.0.2 +ENV PROTOC_GEN_DOC_VERSION=v1.5.1 +RUN go install github.com/envoyproxy/protoc-gen-validate@${PROTOC_GEN_VALIDATE_VERSION} \ + && go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@${PROTOC_GEN_DOC_VERSION} + +# add go.mod to allow datrails api module +COPY go.mod /go/src/github.com/tokopedia/gripmock/go.mod + +# ensure we download the datatrails common api generated code +RUN go mod download github.com/datatrails/go-datatrails-common-api-gen +RUN go get github.com/datatrails/go-datatrails-common-api-gen/assets/v2/assets@v0.3.3 + +# due to us passing in the proto and also using the api gen as a dependency +# we have a namespace conflict, however they are both the same proto, so just turn +# the error into a warning. +ENV GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn + +ENTRYPOINT ["gripmock"] \ No newline at end of file diff --git a/gripmock/Readme.md b/gripmock/Readme.md new file mode 100644 index 0000000..2267c74 --- /dev/null +++ b/gripmock/Readme.md @@ -0,0 +1,42 @@ +## Gripmock is a mock for proto + +Got as far as including the proto in a command like this + +## Build the docker image + +``` +docker build . -t gripmock +``` + +## Run gRPC mock server + +``` +docker run -p 4770:4770 -p 4771:4771 -v ~/workspace/go-datatrails-common-api:/protobuf -v ~/workspace/go-datatrails-common-api:/proto gripmock --imports=/protobuf,/usr/local/api-common-protos-master,/usr/local/include,/go/pkg/mod/github.com/envoyproxy/protoc-gen-validate@v1.0.2,/go/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.18.0 /proto/datatrails-common-api/assets/v2/assets/service.proto +``` + +## Now the gRPC server is up and running, we can add expectations of the assets grpc service: + +``` +curl -X POST -d '{"service":"Assets", "method":"GetAsset","input":{"equals":{"uuid":"foo"}},"output":{"data":{"identity":"foo", "public":true}}}' localhost:4771/add +``` + +response: + +``` +Success add stub +``` + +## Finally we can use grpcurl or similar to check that the service works: + +``` +grpcurl -plaintext -d '{"uuid":"foo"}' localhost:4770 archivist.v2.Assets/GetAsset +``` + +response: + +``` +{ + "identity": "foo", + "public": true +} +``` \ No newline at end of file diff --git a/gripmock/go.mod b/gripmock/go.mod new file mode 100644 index 0000000..56d2ac4 --- /dev/null +++ b/gripmock/go.mod @@ -0,0 +1,34 @@ +module github.com/tokopedia/gripmock + +go 1.15 + +replace ( + // These pseudoversions are for: ConsenSys/quorum@v22.4.4 + github.com/coreos/etcd => github.com/Consensys/etcd v3.3.13-quorum197+incompatible + + // github.com/datatrails/go-datatrails-common-api => ../../go-datatrails-common-api/api + github.com/ethereum/go-ethereum => github.com/ConsenSys/quorum v0.0.0-20221208112643-d318a5aa973a + // github.com/ethereum/go-ethereum/crypto/secp256k1 => github.com/ConsenSys/goquorum-crypto-secp256k1 v0.0.2 + github.com/ethereum/go-ethereum/crypto/secp256k1 => github.com/ConsenSys/goquorum-crypto-secp256k1 v0.0.2 + +) + +require ( + github.com/datatrails/go-datatrails-common-api-gen v0.3.3 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-chi/chi v4.1.2+incompatible + github.com/golang/protobuf v1.5.3 + github.com/lithammer/fuzzysearch v1.1.1 + github.com/stretchr/testify v1.8.4 + github.com/tokopedia/gripmock/protogen v0.0.0 // indirect + github.com/tokopedia/gripmock/protogen/example v0.0.0 // indirect + google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4 + google.golang.org/grpc v1.59.0 + google.golang.org/protobuf v1.31.0 +) + +// this is for generated server to be able to run +replace github.com/tokopedia/gripmock/protogen/example v0.0.0 => ./protogen/example + +// this is for example client to be able to run +replace github.com/tokopedia/gripmock/protogen v0.0.0 => ./protogen \ No newline at end of file From b4e5edbd944765990442041473ca3be221cba65d Mon Sep 17 00:00:00 2001 From: jgough Date: Thu, 14 Dec 2023 09:41:34 +0000 Subject: [PATCH 2/2] fixup - use later gripmock docker image --- gripmock/Dockerfile | 5 +++-- gripmock/Readme.md | 2 +- gripmock/go-ext.mod | 14 ++++++++++++++ gripmock/go.mod | 34 ---------------------------------- 4 files changed, 18 insertions(+), 37 deletions(-) create mode 100644 gripmock/go-ext.mod delete mode 100644 gripmock/go.mod diff --git a/gripmock/Dockerfile b/gripmock/Dockerfile index b9de9af..47fad95 100644 --- a/gripmock/Dockerfile +++ b/gripmock/Dockerfile @@ -2,7 +2,7 @@ # https://docs.docker.com/engine/reference/commandline/image_tag/ ARG BASE_TAG=latest -FROM tkpd/gripmock:v1.12.1 +FROM bavix/gripmock:2.4.0 # NOTE: the protoc stuff is copied from the builder image repo. @@ -53,7 +53,8 @@ RUN go install github.com/envoyproxy/protoc-gen-validate@${PROTOC_GEN_VALIDATE_V && go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@${PROTOC_GEN_DOC_VERSION} # add go.mod to allow datrails api module -COPY go.mod /go/src/github.com/tokopedia/gripmock/go.mod +COPY go-ext.mod /go/src/github.com/bavix/gripmock/go-ext.mod +RUN cat /go/src/github.com/bavix/gripmock/go-ext.mod >> /go/src/github.com/bavix/gripmock/go.mod # ensure we download the datatrails common api generated code RUN go mod download github.com/datatrails/go-datatrails-common-api-gen diff --git a/gripmock/Readme.md b/gripmock/Readme.md index 2267c74..7cb344c 100644 --- a/gripmock/Readme.md +++ b/gripmock/Readme.md @@ -17,7 +17,7 @@ docker run -p 4770:4770 -p 4771:4771 -v ~/workspace/go-datatrails-common-api:/pr ## Now the gRPC server is up and running, we can add expectations of the assets grpc service: ``` -curl -X POST -d '{"service":"Assets", "method":"GetAsset","input":{"equals":{"uuid":"foo"}},"output":{"data":{"identity":"foo", "public":true}}}' localhost:4771/add +curl -X POST -d '{"service":"Assets", "method":"GetAsset","input":{"equals":{"uuid":"foo"}},"output":{"data":{"identity":"foo", "public":true}}}' localhost:4771/api/stubs ``` response: diff --git a/gripmock/go-ext.mod b/gripmock/go-ext.mod new file mode 100644 index 0000000..2a376b3 --- /dev/null +++ b/gripmock/go-ext.mod @@ -0,0 +1,14 @@ +replace ( + // These pseudoversions are for: ConsenSys/quorum@v22.4.4 + github.com/coreos/etcd => github.com/Consensys/etcd v3.3.13-quorum197+incompatible + + // github.com/datatrails/go-datatrails-common-api => ../../go-datatrails-common-api/api + github.com/ethereum/go-ethereum => github.com/ConsenSys/quorum v0.0.0-20221208112643-d318a5aa973a + // github.com/ethereum/go-ethereum/crypto/secp256k1 => github.com/ConsenSys/goquorum-crypto-secp256k1 v0.0.2 + github.com/ethereum/go-ethereum/crypto/secp256k1 => github.com/ConsenSys/goquorum-crypto-secp256k1 v0.0.2 + +) + +require ( + github.com/datatrails/go-datatrails-common-api-gen v0.3.3 // indirect +) \ No newline at end of file diff --git a/gripmock/go.mod b/gripmock/go.mod deleted file mode 100644 index 56d2ac4..0000000 --- a/gripmock/go.mod +++ /dev/null @@ -1,34 +0,0 @@ -module github.com/tokopedia/gripmock - -go 1.15 - -replace ( - // These pseudoversions are for: ConsenSys/quorum@v22.4.4 - github.com/coreos/etcd => github.com/Consensys/etcd v3.3.13-quorum197+incompatible - - // github.com/datatrails/go-datatrails-common-api => ../../go-datatrails-common-api/api - github.com/ethereum/go-ethereum => github.com/ConsenSys/quorum v0.0.0-20221208112643-d318a5aa973a - // github.com/ethereum/go-ethereum/crypto/secp256k1 => github.com/ConsenSys/goquorum-crypto-secp256k1 v0.0.2 - github.com/ethereum/go-ethereum/crypto/secp256k1 => github.com/ConsenSys/goquorum-crypto-secp256k1 v0.0.2 - -) - -require ( - github.com/datatrails/go-datatrails-common-api-gen v0.3.3 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/go-chi/chi v4.1.2+incompatible - github.com/golang/protobuf v1.5.3 - github.com/lithammer/fuzzysearch v1.1.1 - github.com/stretchr/testify v1.8.4 - github.com/tokopedia/gripmock/protogen v0.0.0 // indirect - github.com/tokopedia/gripmock/protogen/example v0.0.0 // indirect - google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4 - google.golang.org/grpc v1.59.0 - google.golang.org/protobuf v1.31.0 -) - -// this is for generated server to be able to run -replace github.com/tokopedia/gripmock/protogen/example v0.0.0 => ./protogen/example - -// this is for example client to be able to run -replace github.com/tokopedia/gripmock/protogen v0.0.0 => ./protogen \ No newline at end of file