Skip to content

Commit f6d271b

Browse files
authored
feat: support verify against Kubernetes resources (#41)
* feat: support check the response againt k8s * feat: support all the CRDs * feat: support to verify fields * add more unit tests
1 parent e08c204 commit f6d271b

19 files changed

+456
-129
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ run-image:
1010
docker run ghcr.io/linuxsuren/api-testing:dev
1111
copy: build
1212
sudo cp bin/atest /usr/local/bin/
13+
copy-restart:
14+
atest service stop
15+
make copy
16+
atest service restart
1317
test:
1418
go test ./... -cover -v -coverprofile=coverage.out
1519
go tool cover -func=coverage.out

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This is a API testing tool.
88

99
* Response Body fields equation check
1010
* Response Body [eval](https://expr.medv.io/)
11+
* Verify the Kubernetes resources
1112
* Validate the response body with [JSON schema](https://json-schema.org/)
1213
* Output reference between TestCase
1314
* Run in server mode, and provide the gRPC endpoint
@@ -62,6 +63,15 @@ The following fields are templated with [sprig](http://masterminds.github.io/spr
6263
* Request Body
6364
* Request Header
6465
66+
## Verify against Kubernetes
67+
68+
It could verify any kinds of Kubernetes resources. Please set the environment variables before using it:
69+
70+
* `KUBERNETES_SERVER`
71+
* `KUBERNETES_TOKEN`
72+
73+
See also the [example](sample/kubernetes.yaml).
74+
6575
## TODO
6676
6777
* Reduce the size of context

cmd/jsonschema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestJSONSchemaCmd(t *testing.T) {
14-
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
14+
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, cmd.NewFakeGRPCServer())
1515

1616
buf := new(bytes.Buffer)
1717
c.SetOut(buf)

cmd/root.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ import (
66
"github.com/linuxsuren/api-testing/pkg/version"
77
fakeruntime "github.com/linuxsuren/go-fake-runtime"
88
"github.com/spf13/cobra"
9-
"google.golang.org/grpc"
109
)
1110

1211
// NewRootCmd creates the root command
13-
func NewRootCmd(execer fakeruntime.Execer) (c *cobra.Command) {
12+
func NewRootCmd(execer fakeruntime.Execer, gRPCServer gRPCServer) (c *cobra.Command) {
1413
c = &cobra.Command{
1514
Use: "atest",
1615
Short: "API testing tool",
1716
}
1817
c.SetOut(os.Stdout)
1918
c.Version = version.GetVersion()
20-
gRPCServer := grpc.NewServer()
2119
c.AddCommand(createInitCommand(execer),
2220
createRunCommand(), createSampleCmd(),
2321
createServerCmd(gRPCServer), createJSONSchemaCmd(),

cmd/root_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestCreateRunCommand(t *testing.T) {
5151
assert.NotNil(t, server)
5252
assert.Equal(t, "server", server.Use)
5353

54-
root := NewRootCmd(exec.FakeExecer{})
54+
root := NewRootCmd(exec.FakeExecer{}, NewFakeGRPCServer())
5555
root.SetArgs([]string{"init", "-k=demo.yaml", "--wait-namespace", "demo", "--wait-resource", "demo"})
5656
err := root.Execute()
5757
assert.Nil(t, err)

cmd/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestRunCommand(t *testing.T) {
104104
}
105105

106106
func TestRootCmd(t *testing.T) {
107-
c := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
107+
c := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, NewFakeGRPCServer())
108108
assert.NotNil(t, c)
109109
assert.Equal(t, "atest", c.Use)
110110
}

cmd/sample_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestSampleCmd(t *testing.T) {
14-
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
14+
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, cmd.NewFakeGRPCServer())
1515

1616
buf := new(bytes.Buffer)
1717
c.SetOut(buf)

cmd/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ type gRPCServer interface {
5959
type fakeGRPCServer struct {
6060
}
6161

62+
// NewFakeGRPCServer creates a fake gRPC server
63+
func NewFakeGRPCServer() gRPCServer {
64+
return &fakeGRPCServer{}
65+
}
66+
6267
// Serve is a fake method
6368
func (s *fakeGRPCServer) Serve(net.Listener) error {
6469
return nil

cmd/server_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ func TestPrintProto(t *testing.T) {
2727
verify: func(t *testing.T, buf *bytes.Buffer, err error) {
2828
assert.NotNil(t, err)
2929
},
30+
}, {
31+
name: "random port",
32+
args: []string{"server", "-p=0"},
33+
verify: func(t *testing.T, buf *bytes.Buffer, err error) {
34+
assert.Nil(t, err)
35+
},
3036
}}
3137
for _, tt := range tests {
3238
t.Run(tt.name, func(t *testing.T) {
3339
buf := new(bytes.Buffer)
34-
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
40+
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, &fakeGRPCServer{})
3541
root.SetOut(buf)
3642
root.SetArgs(tt.args)
3743
err := root.Execute()
3844
tt.verify(t, buf, err)
3945
})
4046
}
41-
42-
server := createServerCmd(&fakeGRPCServer{})
43-
err := server.Execute()
44-
assert.Nil(t, err)
4547
}

cmd/service_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
)
1111

1212
func TestService(t *testing.T) {
13-
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
13+
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, NewFakeGRPCServer())
1414
root.SetArgs([]string{"service", "fake"})
1515
err := root.Execute()
1616
assert.NotNil(t, err)
1717

18-
notLinux := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "fake"})
18+
notLinux := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "fake"}, NewFakeGRPCServer())
1919
notLinux.SetArgs([]string{"service", "--action", "install"})
2020
err = notLinux.Execute()
2121
assert.NotNil(t, err)
@@ -26,7 +26,7 @@ func TestService(t *testing.T) {
2626
os.RemoveAll(tmpFile.Name())
2727
}()
2828

29-
targetScript := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
29+
targetScript := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}, NewFakeGRPCServer())
3030
targetScript.SetArgs([]string{"service", "--action", "install", "--script-path", tmpFile.Name()})
3131
err = targetScript.Execute()
3232
assert.Nil(t, err)
@@ -58,7 +58,7 @@ func TestService(t *testing.T) {
5858
for _, tt := range tests {
5959
t.Run(tt.name, func(t *testing.T) {
6060
buf := new(bytes.Buffer)
61-
normalRoot := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux", ExpectOutput: tt.expectOutput})
61+
normalRoot := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux", ExpectOutput: tt.expectOutput}, NewFakeGRPCServer())
6262
normalRoot.SetOut(buf)
6363
normalRoot.SetArgs([]string{"service", "--action", tt.action})
6464
err = normalRoot.Execute()

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55

66
"github.com/linuxsuren/api-testing/cmd"
77
exec "github.com/linuxsuren/go-fake-runtime"
8+
"google.golang.org/grpc"
89
)
910

1011
func main() {
11-
c := cmd.NewRootCmd(exec.DefaultExecer{})
12+
gRPCServer := grpc.NewServer()
13+
c := cmd.NewRootCmd(exec.DefaultExecer{}, gRPCServer)
1214
if err := c.Execute(); err != nil {
1315
os.Exit(1)
1416
}

pkg/exec/command.go

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)