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

proto uploads and multi projects proto #54

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ jobs:
uses: ./
with:
entrypoint: example/multi-files/entrypoint.sh
- name: Run upload Example
uses: ./
with:
entrypoint: example/upload/entrypoint.sh
- name: Run multi project Example
uses: ./
with:
entrypoint: example/multi-project/entrypoint.sh


3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
vendor
Gopkg.lock
gripmock

# IDEs
.vscode
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ RUN apk -U --no-cache add git protobuf
RUN go get -u -v github.com/golang/protobuf/protoc-gen-go \
google.golang.org/grpc \
google.golang.org/grpc/reflection \
golang.org/x/net/context \
github.com/go-chi/chi \
github.com/lithammer/fuzzysearch/fuzzy \
golang.org/x/tools/imports
Expand Down Expand Up @@ -45,6 +44,6 @@ WORKDIR /go/src/github.com/tokopedia/gripmock
# install gripmock
RUN go install -v

EXPOSE 4770 4771
EXPOSE 4770 4771 4772

ENTRYPOINT ["gripmock"]
58 changes: 0 additions & 58 deletions Gopkg.toml

This file was deleted.

95 changes: 95 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,98 @@ Input matching has 3 rules to match an input. which is **equals**,**contains** a
}
```

## After Fork
The above is still valid.

The reason I created a fork was because I did a lot of changes and ended up drifting a bit from the original project.

## New features

This fork adds the following features:
* Uploading proto files
* When uploading protos, immediate sub directories can represent different imports, if tthe flag `--isd` is set
* Packages at the same Level - proto files defined at their own folders and none of them is at the min package (top level)

Changes:
* modules
* generating proto into GOPATH
* Argument list now considers directories. These directories are added to the import list and all the proto files inside are also added to the list of compiled protos.

Breaking changes:
* `GOPATH` needs to be defined


### Packages at the same Level
Now we can address the scenario where we have proto files packages where none is at the main package.
Imagine that we have two projects. One is `foo` and the other is `bar`. `foo` has a dependency on `bar`.
We should do the following.
Create a folder that will have all proto files (eg: proto) and then underneath the folders for the proto files for `foo` and another for `bar`.

We will end up with:

```
proto
├── bar
│   └── bar.proto
└── foo
├── foo.proto
└── hello.proto
```

Unfortunately just copying doesn't work for all projects, since some projects have their own particularities and a generic tool would have a hard time trying to cope with all of them.
I am thinking about when some projects define the packages in the `protoc` command like `--go_out=Mbar/bar.proto=this/is/a/package:.`.
What we have to do is the opposite. Change the proto files used for the mocks so that all have the option `go_package` and that the imports reflect the current structure.

If sub dirs import flag is set `-isb`, all immediate sub dirs from the uploaded protos.
Consider a zip file with the following tree dir.

```
proto
├── prj-bar
│   └── bar
│   ├── bar.pb.go
│   └── bar.proto
└── prj-foo
└── foo
├── foo.pb.go
├── foo.proto
├── hello.pb.go
└── hello.prot
```

`prj-foo` and `prj-bar` inside `/proto`, will be imported by the `protoc` with the compile option `-I` allowing us to have different packages in different sub directories.

Please make you proto well behaved:
* all proto files have the `option go_package`.
* eg: `option go_package = "github.com/quintans/foo";`
* all the imports must be relative to the current file structure
* eg: `import "github.com/quintans/bar/bar.proto";` ==> `import "bar/bar.proto";`
* do not use `.` in the package name.
* eg: `svc.foo` => `foo`. It is best to make it consistent with the folder name.

Finally start gripmock specifying the top level proto folder
```sh
gripmock -o ./grpc ./proto
```

or without an initial proto folder

```sh
gripmock -o ./grpc
```

Docker
```sh
docker run -p 4770:4770 -p 4771:4771 -p 4772:4772 -v /mypath:/proto -v /mystubs:/stub tkpd/gripmock --stub=/stub /proto
```

or without an initial proto folder or stubs

```sh
docker run -p 4770:4770 -p 4771:4771 -p 4772:4772 tkpd/gripmock
```

### Uploading proto files
Proto files can be uploaded by zipping the proto folder and upload it to `http://localhost:4772/upload`. There is also an utility tool at the `tool` package.

This will add up to the existing proto files.
4 changes: 2 additions & 2 deletions example/multi-files/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

# this file is used by .github/workflows/integration-test.yml

gripmock --stub=example/multi-files/stub example/multi-files/file1.proto example/multi-files/file2.proto &
gripmock --stub=example/multi-files/stub example/multi-files &

go run example/multi-files/client/*.go
go run example/multi-files/client/*.go
2 changes: 1 addition & 1 deletion example/multi-files/stub/greet2.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"output": {
"data": {
"message": "Hello Tokopedia 2",
"return_code": 1
"return_code": 2
}
}
}
4 changes: 2 additions & 2 deletions example/multi-package/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

# we need to add example/multi-package ar as included import path
# without it protoc could not find the bar/bar.proto
gripmock --stub=example/multi-package/stub --imports=example/multi-package/ example/multi-package/foo.proto example/multi-package/hello.proto &
gripmock --stub=example/multi-package/stub example/multi-package &

go run example/multi-package/client/*.go
go run example/multi-package/client/*.go
38 changes: 38 additions & 0 deletions example/multi-project/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"log"
"os"
"time"

"github.com/tokopedia/gripmock/example/multi-project/proto/prj-bar/bar"
"github.com/tokopedia/gripmock/example/multi-project/proto/prj-foo/foo"
"google.golang.org/grpc"
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

// Set up a connection to the server.
conn, err := grpc.DialContext(ctx, "localhost:4770", grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()

c := foo.NewGreeterClient(conn)

// Contact the server and print out its response.
name := "tokopedia"
if len(os.Args) > 1 {
name = os.Args[1]
}
r, err := c.Greet(context.Background(), &bar.Request{Name: name})
if err != nil {
log.Fatalf("error from grpc: %v", err)
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)

}
7 changes: 7 additions & 0 deletions example/multi-project/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env sh

# this file is used by .github/workflows/integration-test.yml

gripmock --stub=example/multi-project/stub --imports=example/multi-project/proto/prj-bar,example/multi-project/proto/prj-foo example/multi-project/proto &

go run example/multi-project/client/*.go
Loading