Skip to content

Commit

Permalink
Merge pull request #24 from bavix/docs
Browse files Browse the repository at this point in the history
[2.0] Docs
  • Loading branch information
rez1dent3 committed Sep 6, 2023
2 parents c1dc985 + fc2de1c commit 6b52cf6
Show file tree
Hide file tree
Showing 45 changed files with 4,858 additions and 292 deletions.
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/compile-assets.yml@0.0.5
secrets: inherit
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ protogen/*
!protogen/go.mod
!protogen/empty.go
!protogen/example/
temp
temp
node_modules/
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ WORKDIR /go/src/github.com/bavix/gripmock

EXPOSE 4770 4771

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

ENTRYPOINT ["gripmock"]
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ lint-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
51 changes: 30 additions & 21 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/bbb14393-2b71-4136-875f-e2e4d0b6bcb9)

# 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/bavix/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/bavix/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,9 +28,9 @@ 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/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 Down Expand Up @@ -104,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 @@ -119,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 @@ -186,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/)
249 changes: 249 additions & 0 deletions api/openapi/api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
openapi: 3.0.2
servers:
- url: /api
info:
version: 2.0.0
title: GripMock API Schema
tags:
- name: stubs
description: Stubs storage management
- name: healthcheck
description: Healthcheck
paths:
# healthcheck
/health/liveness:
get:
tags:
- healthcheck
summary: Liveness check
description: The test says that the service is alive and yet
operationId: liveness
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/MessageOK'
/health/readiness:
get:
tags:
- healthcheck
summary: Readiness check
description: The test indicates readiness to receive traffic
operationId: readiness
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/MessageOK'

# stubs
/stubs:
get:
tags:
- stubs
summary: Getting a list of stubs
description: The list of stubs is required to view all added stubs
operationId: listStubs
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/StubList'
post:
tags:
- stubs
summary: Add a new stub to the store
description: Add a new stub to the store
operationId: addStub
responses:
'200':
description: Successful operation
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/ListID'
requestBody:
description: Create a new pet in the store
required: true
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/StubList'
- $ref: '#/components/schemas/Stub'
delete:
tags:
- stubs
summary: Remove all stubs
description: Completely clears the stub storage
operationId: purgeStubs
responses:
'204':
description: Successful operation
'/stubs/{uuid}':
delete:
tags:
- stubs
summary: Deletes stub by ID
description: The method removes the stub by ID
operationId: deleteStubByID
parameters:
- name: uuid
in: path
description: ID of stub
required: true
schema:
$ref: '#/components/schemas/ID'
responses:
'204':
description: successful operation
'404':
description: Pet not found
'/stubs/search':
post:
tags:
- stubs
summary: Stub storage search
description: Performs a search for a stub by the given conditions
operationId: searchStubs
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/SearchResponse'
requestBody:
description: Description of filtering
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/SearchRequest'
components:
schemas:
# health
MessageOK:
type: object
required:
- message
- time
properties:
message:
type: string
time:
type: string
format: date-time
# stubs
ID:
type: string
format: uuid
example: 51c50050-ec27-4dae-a583-a32ca71a1dd5
ListID:
type: array
items:
$ref: '#/components/schemas/ID'
StubList:
type: array
items:
$ref: '#/components/schemas/Stub'
SearchRequest:
type: object
required:
- service
- method
- data
properties:
service:
type: string
example: Gripmock
method:
type: string
example: SayHello
data:
type: object
x-go-type: interface{}
additionalProperties: true
SearchResponse:
type: object
required:
- data
- error
properties:
data:
type: object
x-go-type: interface{}
additionalProperties: true
error:
type: string
example: Message not found
code:
type: integer
format: uint32
x-go-type: codes.Code
x-go-type-import:
name: codes
path: google.golang.org/grpc/codes
example: 3
Stub:
type: object
required:
- service
- method
- input
- output
properties:
id:
$ref: '#/components/schemas/ID'
service:
type: string
example: Gripmock
method:
type: string
example: SayHello
input:
$ref: '#/components/schemas/StubInput'
output:
$ref: '#/components/schemas/StubOutput'
StubInput:
type: object
properties:
equals:
type: object
additionalProperties: true
x-go-type-skip-optional-pointer: true
contains:
type: object
additionalProperties: true
x-go-type-skip-optional-pointer: true
matches:
type: object
additionalProperties: true
x-go-type-skip-optional-pointer: true
StubOutput:
type: object
required:
- data
- error
properties:
data:
type: object
additionalProperties: true
error:
type: string
example: Message not found
code:
type: integer
format: uint32
x-go-type: codes.Code
x-go-type-import:
name: codes
path: google.golang.org/grpc/codes
example: 3
Empty file added docs/.nojekyll
Empty file.
Loading

0 comments on commit 6b52cf6

Please sign in to comment.