Skip to content

Commit 0a52667

Browse files
committed
feat: add grpc server support
1 parent 136e82d commit 0a52667

File tree

17 files changed

+1604
-13
lines changed

17 files changed

+1604
-13
lines changed

.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:

Makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,24 @@ 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
67
copy: build
78
sudo cp bin/atest /usr/local/bin/
89
test:
910
go test ./... -cover -v -coverprofile=coverage.out
1011
go tool cover -func=coverage.out
12+
grpc:
13+
protoc --go_out=. --go_opt=paths=source_relative \
14+
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
15+
pkg/server/server.proto
16+
grpc-js:
17+
protoc -I=pkg/server server.proto \
18+
--js_out=import_style=commonjs:bin \
19+
--grpc-web_out=import_style=commonjs,mode=grpcwebtext:bin
20+
grpc-java:
21+
protoc --plugin=protoc-gen-grpc-java=/usr/local/bin/protoc-gen-grpc-java \
22+
--grpc-java_out=bin --proto_path=pkg/server server.proto
23+
install-tool:
24+
go install google.golang.org/protobuf/cmd/[email protected]
25+
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+
}

cmd/server.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Package cmd provides all the commands
2+
package cmd
3+
4+
import (
5+
"fmt"
6+
"log"
7+
"net"
8+
9+
"github.com/linuxsuren/api-testing/pkg/server"
10+
"github.com/spf13/cobra"
11+
"google.golang.org/grpc"
12+
)
13+
14+
func createServerCmd() (c *cobra.Command) {
15+
opt := &serverOption{}
16+
c = &cobra.Command{
17+
Use: "server",
18+
Short: "Run as a server mode",
19+
RunE: opt.runE,
20+
}
21+
flags := c.Flags()
22+
flags.IntVarP(&opt.port, "port", "p", 9090, "The RPC server port")
23+
return
24+
}
25+
26+
type serverOption struct {
27+
port int
28+
}
29+
30+
func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
31+
var lis net.Listener
32+
33+
lis, err = net.Listen("tcp", fmt.Sprintf(":%d", o.port))
34+
if err != nil {
35+
return
36+
}
37+
38+
s := grpc.NewServer()
39+
server.RegisterRunnerServer(s, server.NewRemoteServer())
40+
log.Printf("server listening at %v", lis.Addr())
41+
s.Serve(lis)
42+
return
43+
}

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ require (
66
github.com/Masterminds/sprig/v3 v3.2.3
77
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
88
github.com/antonmedv/expr v1.12.1
9+
github.com/golang/protobuf v1.5.2
910
github.com/h2non/gock v1.2.0
1011
github.com/linuxsuren/unstructured v0.0.1
1112
github.com/spf13/cobra v1.6.1
1213
github.com/stretchr/testify v1.8.2
1314
golang.org/x/sync v0.1.0
15+
google.golang.org/grpc v1.54.0
1416
gopkg.in/yaml.v2 v2.4.0
1517
)
1618

1719
require (
1820
github.com/Masterminds/goutils v1.1.1 // indirect
1921
github.com/Masterminds/semver/v3 v3.2.0 // indirect
2022
github.com/davecgh/go-spew v1.1.1 // indirect
21-
github.com/google/uuid v1.1.1 // indirect
23+
github.com/google/uuid v1.3.0 // indirect
2224
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
2325
github.com/huandu/xstrings v1.3.3 // indirect
2426
github.com/imdario/mergo v0.3.11 // indirect
@@ -31,5 +33,10 @@ require (
3133
github.com/spf13/cast v1.3.1 // indirect
3234
github.com/spf13/pflag v1.0.5 // indirect
3335
golang.org/x/crypto v0.3.0 // indirect
36+
golang.org/x/net v0.8.0 // indirect
37+
golang.org/x/sys v0.6.0 // indirect
38+
golang.org/x/text v0.8.0 // indirect
39+
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
40+
google.golang.org/protobuf v1.30.0 // indirect
3441
gopkg.in/yaml.v3 v3.0.1 // indirect
3542
)

0 commit comments

Comments
 (0)