Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gripmock for grpc mocking #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions gripmock/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are present in the proto-include.tar.gz included on every release. the runes to install both the google headers and the grpc onese are just

rm -rf proto-include
curl -fsSOL https://github.com/datatrails/go-datatrails-common-api/releases/download/{{.DATATRAILS_COMMON_API_VERSION}}/proto-include.tar.gz
tar -zxf proto-include.tar.gz
rm proto-include.tar.gz

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The avid taskfiles/Taskfile_apis.yml bootstrap step is a good reference

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/[email protected]

# 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"]
42 changes: 42 additions & 0 deletions gripmock/Readme.md
Original file line number Diff line number Diff line change
@@ -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/[email protected],/go/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/[email protected] /proto/datatrails-common-api/assets/v2/assets/service.proto
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you use the proto-includes.tar these include paths get a lot simpler

```

## 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
}
```
34 changes: 34 additions & 0 deletions gripmock/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module github.com/tokopedia/gripmock

go 1.15
honourfish marked this conversation as resolved.
Show resolved Hide resolved

replace (
// These pseudoversions are for: ConsenSys/[email protected]
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
Loading