Skip to content

Commit e2340a2

Browse files
authored
feat: add grpc server support (#22)
* feat: add grpc server support * publish image --------- Co-authored-by: Rick <[email protected]>
1 parent 136e82d commit e2340a2

28 files changed

+1808
-35
lines changed

.github/workflows/build.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ jobs:
3636
github_token: ${{ secrets.GH_PUBLISH_SECRETS }}
3737
version: v1.14.0
3838
args: release --skip-publish --rm-dist --snapshot
39+
- name: Image
40+
run: make build-image

.github/workflows/release.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ on:
55
tags:
66
- '*'
77

8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository }}
11+
812
jobs:
913
goreleaser:
1014
runs-on: ubuntu-20.04
@@ -24,3 +28,33 @@ jobs:
2428
args: release --rm-dist
2529
env:
2630
GITHUB_TOKEN: ${{ secrets.GH_PUBLISH_SECRETS }}
31+
32+
image:
33+
runs-on: ubuntu-20.04
34+
steps:
35+
- name: Checkout
36+
uses: actions/[email protected]
37+
- name: Setup Docker buildx
38+
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf
39+
- name: Log into registry ${{ env.REGISTRY }}
40+
if: github.event_name != 'pull_request'
41+
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
42+
with:
43+
registry: ${{ env.REGISTRY }}
44+
username: ${{ github.actor }}
45+
password: ${{ secrets.GH_PUBLISH_SECRETS }}
46+
- name: Extract Docker metadata
47+
id: meta
48+
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
49+
with:
50+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
51+
- name: Build and push Docker image
52+
id: build-and-push
53+
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
54+
with:
55+
context: .
56+
push: ${{ github.event_name != 'pull_request' }}
57+
tags: ${{ steps.meta.outputs.tags }}
58+
labels: ${{ steps.meta.outputs.labels }}
59+
cache-from: type=gha
60+
cache-to: type=gha,mode=max

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
bin/
22
.idea/
33
coverage.out
4+
dist/

.goreleaser.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ builds:
1212
- linux
1313
- windows
1414
- darwin
15+
ldflags:
16+
- -w
17+
- -s
18+
- -X github.com/linuxsuren/api-testing/cmd.version={{.Version}}
1519
archives:
1620
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
1721
format_overrides:

Dockerfile

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@ LABEL "maintainer"="Rick <[email protected]>"
2020

2121
LABEL "Name"="API testing"
2222

23-
ENV LC_ALL C.UTF-8
24-
ENV LANG en_US.UTF-8
25-
ENV LANGUAGE en_US.UTF-8
26-
27-
RUN apk add --no-cache \
28-
git \
29-
openssh-client \
30-
libc6-compat \
31-
libstdc++
32-
33-
COPY entrypoint.sh /entrypoint.sh
3423
COPY --from=builder /workspace/atest /usr/bin/atest
35-
COPY --from=hd /usr/local/bin/hd /usr/local/bin/hd
36-
RUN hd i kubernetes-sigs/kubectl && \
37-
hd i k3d
3824

39-
ENTRYPOINT ["/entrypoint.sh"]
25+
ENTRYPOINT [atest, server]

Makefile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,26 @@ build:
22
mkdir -p bin
33
rm -rf bin/atest
44
go build -o bin/atest main.go
5-
5+
goreleaser:
6+
goreleaser build --rm-dist --snapshot
7+
build-image:
8+
docker build -t ghcr.io/linuxsuren/api-testing:dev .
69
copy: build
710
sudo cp bin/atest /usr/local/bin/
811
test:
912
go test ./... -cover -v -coverprofile=coverage.out
1013
go tool cover -func=coverage.out
14+
grpc:
15+
protoc --go_out=. --go_opt=paths=source_relative \
16+
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
17+
pkg/server/server.proto
18+
grpc-js:
19+
protoc -I=pkg/server server.proto \
20+
--js_out=import_style=commonjs:bin \
21+
--grpc-web_out=import_style=commonjs,mode=grpcwebtext:bin
22+
grpc-java:
23+
protoc --plugin=protoc-gen-grpc-java=/usr/local/bin/protoc-gen-grpc-java \
24+
--grpc-java_out=bin --proto_path=pkg/server server.proto
25+
install-tool:
26+
go install google.golang.org/protobuf/cmd/[email protected]
27+
go install google.golang.org/grpc/cmd/[email protected]

cmd/root.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package cmd
22

33
import "github.com/spf13/cobra"
44

5+
// should be injected during the build process
6+
var version string
7+
58
// NewRootCmd creates the root command
69
func NewRootCmd() (c *cobra.Command) {
710
c = &cobra.Command{
811
Use: "atest",
912
Short: "API testing tool",
1013
}
14+
c.Version = version
1115
c.AddCommand(createInitCommand(),
12-
createRunCommand(), createSampleCmd())
16+
createRunCommand(), createSampleCmd(),
17+
createServerCmd())
1318
return
1419
}

cmd/root_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ func TestCreateRunCommand(t *testing.T) {
4545

4646
init := createInitCommand()
4747
assert.Equal(t, "init", init.Use)
48+
49+
server := createServerCmd()
50+
assert.NotNil(t, server)
51+
assert.Equal(t, "server", server.Use)
4852
}

cmd/run.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type runOption struct {
3232
reporter runner.TestReporter
3333
reportWriter runner.ReportResultWriter
3434
report string
35+
reportIgnore bool
3536
}
3637

3738
func newDefaultRunOption() *runOption {
@@ -71,14 +72,22 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
7172
flags.Int64VarP(&opt.thread, "thread", "", 1, "Threads of the execution")
7273
flags.Int32VarP(&opt.qps, "qps", "", 5, "QPS")
7374
flags.Int32VarP(&opt.burst, "burst", "", 5, "burst")
74-
flags.StringVarP(&opt.report, "report", "", "", "The type of target report")
75+
flags.StringVarP(&opt.report, "report", "", "", "The type of target report. Supported: markdown, md, discard, std")
7576
return
7677
}
7778

7879
func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
80+
writer := cmd.OutOrStdout()
81+
7982
switch o.report {
8083
case "markdown", "md":
81-
o.reportWriter = runner.NewMarkdownResultWriter(cmd.OutOrStdout())
84+
o.reportWriter = runner.NewMarkdownResultWriter(writer)
85+
case "discard":
86+
o.reportWriter = runner.NewDiscardResultWriter()
87+
case "", "std":
88+
o.reportWriter = runner.NewResultWriter(writer)
89+
default:
90+
err = fmt.Errorf("not supported report type: '%s'", o.report)
8291
}
8392
return
8493
}
@@ -97,17 +106,18 @@ func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
97106
for i := range files {
98107
item := files[i]
99108
if err = o.runSuiteWithDuration(item); err != nil {
100-
return
109+
break
101110
}
102111
}
103112
}
104113

105114
// print the report
106-
if err == nil {
107-
var results []runner.ReportResult
108-
if results, err = o.reporter.ExportAllReportResults(); err == nil {
109-
err = o.reportWriter.Output(results)
115+
if results, reportErr := o.reporter.ExportAllReportResults(); reportErr == nil {
116+
if reportErr = o.reportWriter.Output(results); reportErr != nil {
117+
cmd.Println("failed to Output all reports", reportErr)
110118
}
119+
} else {
120+
cmd.Println("failed to export all reports", reportErr)
111121
}
112122
return
113123
}
@@ -205,6 +215,7 @@ func (o *runOption) runSuite(suite string, dataContext map[string]interface{}, c
205215
simpleRunner := runner.NewSimpleTestCaseRunner()
206216
simpleRunner.WithTestReporter(o.reporter)
207217
if output, err = simpleRunner.RunTestCase(&testCase, dataContext, ctxWithTimeout); err != nil && !o.requestIgnoreError {
218+
err = fmt.Errorf("failed to run '%s', %v", testCase.Name, err)
208219
return
209220
} else {
210221
err = nil

cmd/run_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,72 @@ func TestRootCmd(t *testing.T) {
107107
assert.NotNil(t, c)
108108
assert.Equal(t, "atest", c.Use)
109109
}
110+
111+
func TestPreRunE(t *testing.T) {
112+
tests := []struct {
113+
name string
114+
opt *runOption
115+
verify func(*testing.T, *runOption, error)
116+
}{{
117+
name: "markdown report",
118+
opt: &runOption{
119+
report: "markdown",
120+
},
121+
verify: func(t *testing.T, ro *runOption, err error) {
122+
assert.Nil(t, err)
123+
assert.NotNil(t, ro.reportWriter)
124+
},
125+
}, {
126+
name: "md report",
127+
opt: &runOption{
128+
report: "md",
129+
},
130+
verify: func(t *testing.T, ro *runOption, err error) {
131+
assert.Nil(t, err)
132+
assert.NotNil(t, ro.reportWriter)
133+
},
134+
}, {
135+
name: "discard report",
136+
opt: &runOption{
137+
report: "discard",
138+
},
139+
verify: func(t *testing.T, ro *runOption, err error) {
140+
assert.Nil(t, err)
141+
assert.NotNil(t, ro.reportWriter)
142+
},
143+
}, {
144+
name: "std report",
145+
opt: &runOption{
146+
report: "std",
147+
},
148+
verify: func(t *testing.T, ro *runOption, err error) {
149+
assert.Nil(t, err)
150+
assert.NotNil(t, ro.reportWriter)
151+
},
152+
}, {
153+
name: "empty report",
154+
opt: &runOption{
155+
report: "",
156+
},
157+
verify: func(t *testing.T, ro *runOption, err error) {
158+
assert.Nil(t, err)
159+
assert.NotNil(t, ro.reportWriter)
160+
},
161+
}, {
162+
name: "invalid report",
163+
opt: &runOption{
164+
report: "fake",
165+
},
166+
verify: func(t *testing.T, ro *runOption, err error) {
167+
assert.NotNil(t, err)
168+
assert.Nil(t, ro.reportWriter)
169+
},
170+
}}
171+
for _, tt := range tests {
172+
t.Run(tt.name, func(t *testing.T) {
173+
c := &cobra.Command{}
174+
err := tt.opt.preRunE(c, nil)
175+
tt.verify(t, tt.opt, err)
176+
})
177+
}
178+
}

0 commit comments

Comments
 (0)