Skip to content

Commit 82f2fdf

Browse files
authored
feat: support to run a specific test case via grpc (#28)
Co-authored-by: Rick <[email protected]>
1 parent 3720aca commit 82f2fdf

14 files changed

+498
-95
lines changed

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ builds:
1515
ldflags:
1616
- -w
1717
- -s
18-
- -X github.com/linuxsuren/api-testing/cmd.version={{.Version}}
18+
- -X github.com/linuxsuren/api-testing/pkg/version.version={{.Version}}
1919
archives:
2020
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
2121
format_overrides:

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This is a API testing tool.
88
* Response Body fields equation check
99
* Response Body [eval](https://expr.medv.io/)
1010
* Output reference between TestCase
11+
* Run in server mode, and provide the gRPC endpoint
12+
* [VS Code extension](https://github.com/LinuxSuRen/vscode-api-testing) support
1113

1214
## Template
1315
The following fields are templated with [sprig](http://masterminds.github.io/sprig/):

cmd/root.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package cmd
22

3-
import "github.com/spf13/cobra"
4-
5-
// should be injected during the build process
6-
var version string
3+
import (
4+
"github.com/linuxsuren/api-testing/pkg/version"
5+
"github.com/spf13/cobra"
6+
)
77

88
// NewRootCmd creates the root command
99
func NewRootCmd() (c *cobra.Command) {
1010
c = &cobra.Command{
1111
Use: "atest",
1212
Short: "API testing tool",
1313
}
14-
c.Version = version
14+
c.Version = version.GetVersion()
1515
c.AddCommand(createInitCommand(),
1616
createRunCommand(), createSampleCmd(),
1717
createServerCmd())

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +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
109
github.com/h2non/gock v1.2.0
1110
github.com/linuxsuren/unstructured v0.0.1
1211
github.com/spf13/cobra v1.6.1
1312
github.com/stretchr/testify v1.8.2
1413
golang.org/x/sync v0.1.0
1514
google.golang.org/grpc v1.54.0
15+
google.golang.org/protobuf v1.30.0
1616
gopkg.in/yaml.v2 v2.4.0
1717
)
1818

1919
require (
2020
github.com/Masterminds/goutils v1.1.1 // indirect
2121
github.com/Masterminds/semver/v3 v3.2.0 // indirect
2222
github.com/davecgh/go-spew v1.1.1 // indirect
23+
github.com/golang/protobuf v1.5.2 // indirect
2324
github.com/google/uuid v1.3.0 // indirect
2425
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
2526
github.com/huandu/xstrings v1.3.3 // indirect
@@ -37,6 +38,5 @@ require (
3738
golang.org/x/sys v0.6.0 // indirect
3839
golang.org/x/text v0.8.0 // indirect
3940
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
40-
google.golang.org/protobuf v1.30.0 // indirect
4141
gopkg.in/yaml.v3 v3.0.1 // indirect
4242
)

pkg/server/remote_server.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/linuxsuren/api-testing/pkg/render"
1111
"github.com/linuxsuren/api-testing/pkg/runner"
1212
"github.com/linuxsuren/api-testing/pkg/testing"
13+
"github.com/linuxsuren/api-testing/pkg/version"
1314
)
1415

1516
type server struct {
@@ -41,6 +42,28 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
4142
suite = &testing.TestSuite{
4243
Items: []testing.TestCase{*testCase},
4344
}
45+
case "testcaseInSuite":
46+
if suite, err = testing.ParseFromData([]byte(task.Data)); err != nil {
47+
return
48+
} else if suite == nil || suite.Items == nil {
49+
err = fmt.Errorf("no test suite found")
50+
return
51+
}
52+
53+
var targetTestcase *testing.TestCase
54+
for _, item := range suite.Items {
55+
if item.Name == task.CaseName {
56+
targetTestcase = &item
57+
break
58+
}
59+
}
60+
61+
if targetTestcase != nil {
62+
suite.Items = []testing.TestCase{*targetTestcase}
63+
} else {
64+
err = fmt.Errorf("cannot found testcase %s", task.CaseName)
65+
return
66+
}
4467
default:
4568
err = fmt.Errorf("not support '%s'", task.Kind)
4669
return
@@ -77,3 +100,9 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
77100
reply = &HelloReply{Message: buf.String()}
78101
return
79102
}
103+
104+
// GetVersion returns the version
105+
func (s *server) GetVersion(ctx context.Context, in *Empty) (reply *HelloReply, err error) {
106+
reply = &HelloReply{Message: version.GetVersion()}
107+
return
108+
}

pkg/server/remote_server_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestRemoteServer(t *testing.T) {
1818
})
1919
assert.NotNil(t, err)
2020

21+
gock.New("http://foo").Get("/").Reply(http.StatusOK).JSON(&server)
2122
gock.New("http://foo").Get("/").Reply(http.StatusOK).JSON(&server)
2223
_, err = server.Run(context.TODO(), &TestTask{
2324
Kind: "suite",
@@ -31,6 +32,27 @@ func TestRemoteServer(t *testing.T) {
3132
Data: simpleTestCase,
3233
})
3334
assert.Nil(t, err)
35+
36+
gock.New("http://foo").Get("/").Reply(http.StatusOK).JSON(&server)
37+
_, err = server.Run(context.TODO(), &TestTask{
38+
Kind: "testcaseInSuite",
39+
Data: simpleSuite,
40+
CaseName: "get",
41+
})
42+
assert.Nil(t, err)
43+
44+
gock.New("http://foo").Get("/").Reply(http.StatusOK).JSON(&server)
45+
_, err = server.Run(context.TODO(), &TestTask{
46+
Kind: "testcaseInSuite",
47+
Data: simpleSuite,
48+
CaseName: "fake",
49+
})
50+
assert.NotNil(t, err)
51+
52+
var ver *HelloReply
53+
ver, err = server.GetVersion(context.TODO(), &Empty{})
54+
assert.Empty(t, ver.Message)
55+
assert.Nil(t, err)
3456
}
3557

3658
//go:embed testdata/simple.yaml

0 commit comments

Comments
 (0)