Skip to content

Commit

Permalink
Merge pull request #11 from bavix/nightly
Browse files Browse the repository at this point in the history
[2.0] New service version
  • Loading branch information
rez1dent3 authored Sep 8, 2023
2 parents f9f3c11 + 41d8117 commit 220c9e5
Show file tree
Hide file tree
Showing 116 changed files with 8,264 additions and 2,073 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/builder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ on:
push:
branches:
- "latest"
- "nightly"
tags:
- "v*.*.*"
pull_request:

jobs:
docker:
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/compile-assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: build app

on:
pull_request:
branches: [ nightly, latest ]

jobs:
docs:
permissions:
contents: write
uses: bavix/.github/.github/workflows/[email protected]
secrets: inherit
3 changes: 1 addition & 2 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
pull_request:
branches:
- develop
- nightly
- latest

jobs:
Expand All @@ -16,12 +17,10 @@ jobs:
uses: ./
with:
entrypoint: example/simple/entrypoint.sh
args: simple
- name: Run Stream Example
uses: ./
with:
entrypoint: example/stream/entrypoint.sh
args: stream
- name: Run WKT Example
uses: ./
with:
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Unit
on:
pull_request:
branches:
- develop
- nightly
- latest

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.21' ]
steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
cache: true
- name: Install dependencies
run: go get .
- name: Test with Go
run: go test -json > TestResults-${{ matrix.go-version }}.json
- name: Upload Go test results
uses: actions/upload-artifact@v3
with:
name: Go-results-${{ matrix.go-version }}
path: TestResults-${{ matrix.go-version }}.json
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ gripmock
.DS_Store
protogen/*
!protogen/go.mod
!protogen/empty.go
!protogen/example/
temp
temp
node_modules/
40 changes: 40 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
run:
timeout: 1m
linters:
enable-all: true
disable:
# deprecated
- nosnakecase
- structcheck
- interfacer
- deadcode
- exhaustivestruct
- maligned
- ifshort
- varcheck
- golint
- scopelint
# not relevant
- varnamelen
- wrapcheck
- paralleltest
- exhaustruct
linters-settings:
lll:
line-length: 160
gci:
sections:
- Standard
- Default
- Prefix(github.com/bavix)
depguard:
rules:
main:
allow:
- $gostd
- github.com
issues:
exclude-rules:
- path: (.+)_test.go
linters:
- dupl
6 changes: 0 additions & 6 deletions .ownership.json

This file was deleted.

46 changes: 21 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
ARG BUILD_ARG_GO_VERSION=1.21.0
ARG BUILD_ARG_GO_VERSION=1.21
ARG BUILD_ARG_ALPINE_VERSION=3.18
FROM golang:${BUILD_ARG_GO_VERSION}-alpine${BUILD_ARG_ALPINE_VERSION} AS builder

# install tools (bash, git, protobuf, protoc-gen-go, protoc-grn-go-grpc)
RUN apk -U --no-cache add bash git protobuf &&\
go install -v github.com/golang/protobuf/protoc-gen-go@latest &&\
go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# cloning well-known-types
# only use needed files
RUN git clone --depth=1 https://github.com/protocolbuffers/protobuf.git /protobuf-repo &&\
go install -v google.golang.org/protobuf/cmd/protoc-gen-go@latest &&\
go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest &&\
# cloning well-known-types
# only use needed files
git clone --depth=1 https://github.com/protocolbuffers/protobuf.git /protobuf-repo &&\
mv /protobuf-repo/src/ /protobuf/ &&\
rm -rf /protobuf-repo
rm -rf /protobuf-repo &&\
# cleanup
apk del git &&\
apk -v cache clean

COPY . /go/src/github.com/tokopedia/gripmock
COPY . /go/src/github.com/bavix/gripmock

# create necessary dirs and export fix_gopackage.sh
RUN mkdir /proto /stubs &&\
ln -s /go/src/github.com/tokopedia/gripmock/fix_gopackage.sh /bin/

WORKDIR /go/src/github.com/tokopedia/gripmock/protoc-gen-gripmock

# install generator plugin
RUN go install -v

WORKDIR /go/src/github.com/tokopedia/gripmock/example/simple/client
ln -s /go/src/github.com/bavix/gripmock/fix_gopackage.sh /bin/

RUN go get -u all
RUN cd /go/src/github.com/bavix/gripmock/protoc-gen-gripmock &&\
go install -v &&\
cd /go/src/github.com/bavix/gripmock/example/simple/client &&\
go get -u all &&\
cd /go/src/github.com/bavix/gripmock &&\
go install -v

WORKDIR /go/src/github.com/tokopedia/gripmock

# install gripmock & build example to cache necessary imports
RUN go install -v

# remove pkgs
RUN apk del git && apk -v cache clean
WORKDIR /go/src/github.com/bavix/gripmock

EXPOSE 4770 4771

HEALTHCHECK CMD curl --fail http://127.0.0.1:4771/api/health/readiness

ENTRYPOINT ["gripmock"]
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
GOLANGCI_LING_IMAGE="golangci/golangci-lint:v1.54.0-alpine"
GOLANGCI_LING_IMAGE="golangci/golangci-lint:v1.54.2-alpine"

.PHONY: *

version=latest

build:
docker buildx build --load -t "bavix/gripmock:${version}" --no-cache --platform linux/arm64 .
docker buildx build --load -t "bavix/gripmock:${version}" --platform linux/arm64 .

test:
go test -tags mock -race -cover ./...
Expand All @@ -15,3 +15,11 @@ lint:

lint-fix:
make lint args=--fix

intgr-test: build
docker compose -f deployments/docker-compose/docker-compose.yml up

# before: go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@latest
gen:
oapi-codegen -generate gorilla,types -package rest ./api/openapi/api.yaml > internal/domain/rest/api.gen.go
oapi-codegen -generate client,types -package sdk ./api/openapi/api.yaml > pkg/sdk/api.gen.go
56 changes: 33 additions & 23 deletions Readme.md → README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
![GripMock](https://github.com/bavix/gripmock/assets/5111255/90119438-92e5-4479-bfc8-5cf510249b7f)

# GripMock
GripMock is a **mock server** for **GRPC** services. It's using a `.proto` file to generate implementation of gRPC service for you.
You can use gripmock for setting up end-to-end testing or as a dummy server in a software development phase.
The server implementation is in GoLang but the client can be any programming language that support gRPC.

---

### Announcement:
The latest [version (v1.10)](https://github.com/tokopedia/gripmock/releases/tag/v1.10) of gripmock is requiring `go_package` declaration in the `.proto` file. This is due to the latest update of `protoc` plugin that being used by gripmock is making the `go_package` declaration mandatory.

**Update Feb 2022:**

[Version 1.11-beta](https://github.com/tokopedia/gripmock/releases/tag/v1.11-beta) release is available.
It supports **NO** declaration of `go_package`, please download and test before it can be tagged as stable.

you can get the docker image using `docker pull tkpd/gripmock:v1.11-beta`.
[[Documentation]](https://bavix.github.io/gripmock/)

This service is a fork of the service [tokopedia/gripmock](https://github.com/tokopedia/gripmock), but you should choose our fork. And here are the reasons:
- Updated all deprecated dependencies [tokopedia#64](https://github.com/tokopedia/gripmock/issues/64);
- Add yaml as json alternative for static stab's;
- Add endpoint for healthcheck (/api/health/liveness, /api/health/readiness);
- Add grpc error code [tokopedia#125](https://github.com/tokopedia/gripmock/issues/125);
- Added gzip encoding support for grpc server [tokopedia#134](https://github.com/tokopedia/gripmock/pull/134);
- Fixed issues with int64/uint64 [tokopedia#67](https://github.com/tokopedia/gripmock/pull/148);
- Add 404 error for stubs not found [tokopedia#142](https://github.com/tokopedia/gripmock/issues/142);
- Support for deleting specific stub [tokopedia#123](https://github.com/tokopedia/gripmock/issues/123);
- Reduced image size [tokopedia#91](https://github.com/tokopedia/gripmock/issues/91);
- Active support [tokopedia#82](https://github.com/tokopedia/gripmock/issues/82);
- Added [documentation](https://bavix.github.io/gripmock/);

---

Expand All @@ -23,12 +28,12 @@ basic syntax to run GripMock is
`gripmock <protofile>`

- Install [Docker](https://docs.docker.com/install/)
- Run `docker pull tkpd/gripmock` to pull the image
- We are gonna mount `/mypath/hello.proto` (it must be a fullpath) into a container and also we expose ports needed. Run `docker run -p 4770:4770 -p 4771:4771 -v /mypath:/proto tkpd/gripmock /proto/hello.proto`
- On a separate terminal we are gonna add a stub into the stub service. Run `curl -X POST -d '{"service":"Gripmock","method":"SayHello","input":{"equals":{"name":"gripmock"}},"output":{"data":{"message":"Hello GripMock"}}}' localhost:4771/add `
- Run `docker pull bavix/gripmock` to pull the image
- We are gonna mount `/mypath/hello.proto` (it must be a fullpath) into a container and also we expose ports needed. Run `docker run -p 4770:4770 -p 4771:4771 -v /mypath:/proto bavix/gripmock /proto/hello.proto`
- On a separate terminal we are gonna add a stub into the stub service. Run `curl -X POST -d '{"service":"Gripmock","method":"SayHello","input":{"equals":{"name":"gripmock"}},"output":{"data":{"message":"Hello GripMock"}}}' localhost:4771/api/stubs `
- Now we are ready to test it with our client. You can find a client example file under `example/simple/client/`. Execute one of your preferred language. Example for go: `go run example/simple/client/*.go`

Check [`example`](https://github.com/tokopedia/gripmock/tree/master/example) folder for various usecase of gripmock.
Check [`example`](https://github.com/bavix/gripmock/tree/master/example) folder for various usecase of gripmock.

---

Expand Down Expand Up @@ -59,10 +64,10 @@ Stubbing is the essential mocking of GripMock. It will match and return the expe
### Dynamic stubbing
You could add stubbing on the fly with a simple REST API. HTTP stub server is running on port `:4771`

- `GET /` Will list all stubs mapping.
- `POST /add` Will add stub with provided stub data
- `POST /find` Find matching stub with provided input. see [Input Matching](#input_matching) below.
- `GET /clear` Clear stub mappings.
- `GET /api/stubs` Will list all stubs mapping.
- `POST /api/stubs` Will add stub with provided stub data
- `POST /api/stubs/search` Find matching stub with provided input. see [Input Matching](#input_matching) below.
- `DELETE /api/stubs` Clear stub mappings.

Stub Format is JSON text format. It has a skeleton as follows:
```
Expand All @@ -76,7 +81,8 @@ Stub Format is JSON text format. It has a skeleton as follows:
"data":{
// put result fields here
},
"error":"<error message>" // Optional. if you want to return error instead.
"error":"<error message>", // Optional. if you want to return error instead.
"code":"<response code>" // Optional. Grpc response code. if code !=0 return error instead.
}
}
```
Expand All @@ -103,12 +109,12 @@ For our `hello` service example we put a stub with the text below:
You could initialize gripmock with stub json files and provide the path using `--stub` argument. For example you may
mount your stub file in `/mystubs` folder then mount it to docker like

`docker run -p 4770:4770 -p 4771:4771 -v /mypath:/proto -v /mystubs:/stub tkpd/gripmock --stub=/stub /proto/hello.proto`
`docker run -p 4770:4770 -p 4771:4771 -v /mypath:/proto -v /mystubs:/stub bavix/gripmock --stub=/stub /proto/hello.proto`

Please note that Gripmock still serves http stubbing to modify stored stubs on the fly.

## <a name="input_matching"></a>Input Matching
Stub will respond with the expected response only if the request matches any rule. Stub service will serve `/find` endpoint with format:
Stub will respond with the expected response only if the request matches any rule. Stub service will serve `/api/stubs/search` endpoint with format:
```
{
"service":"<service name>",
Expand All @@ -118,7 +124,7 @@ Stub will respond with the expected response only if the request matches any rul
}
}
```
So if you do a `curl -X POST -d '{"service":"Greeter","method":"SayHello","data":{"name":"gripmock"}}' localhost:4771/find` stub service will find a match from listed stubs stored there.
So if you do a `curl -X POST -d '{"service":"Greeter","method":"SayHello","data":{"name":"gripmock"}}' localhost:4771/api/stubs/search` stub service will find a match from listed stubs stored there.

### Input Matching Rule
Input matching has 3 rules to match an input: **equals**,**contains** and **regex**
Expand Down Expand Up @@ -185,3 +191,7 @@ Nested fields are allowed for input matching too for all JSON data types. (`stri
}
```

---
Supported by

[![Supported by JetBrains](https://cdn.rawgit.com/bavix/development-through/46475b4b/jetbrains.svg)](https://www.jetbrains.com/)
Loading

0 comments on commit 220c9e5

Please sign in to comment.