Skip to content

Commit 8ed8394

Browse files
committed
add more unit tests
1 parent 390f04e commit 8ed8394

File tree

8 files changed

+85
-27
lines changed

8 files changed

+85
-27
lines changed

cmd/init.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package cmd
22

33
import (
4-
"github.com/linuxsuren/api-testing/pkg/exec"
4+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
55
"github.com/spf13/cobra"
66
)
77

88
type initOption struct {
9+
execer fakeruntime.Execer
910
kustomization string
1011
waitNamespace string
1112
waitResource string
1213
}
1314

1415
// createInitCommand returns the init command
15-
func createInitCommand() (cmd *cobra.Command) {
16-
opt := &initOption{}
16+
func createInitCommand(execer fakeruntime.Execer) (cmd *cobra.Command) {
17+
opt := &initOption{execer: execer}
1718
cmd = &cobra.Command{
1819
Use: "init",
1920
Long: "Support to init Kubernetes cluster with kustomization, and wait it with command: kubectl wait",
@@ -30,13 +31,13 @@ func createInitCommand() (cmd *cobra.Command) {
3031

3132
func (o *initOption) runE(cmd *cobra.Command, args []string) (err error) {
3233
if o.kustomization != "" {
33-
if err = exec.RunCommand("kubectl", "apply", "-k", o.kustomization, "--wait=true"); err != nil {
34+
if err = o.execer.RunCommand("kubectl", "apply", "-k", o.kustomization, "--wait=true"); err != nil {
3435
return
3536
}
3637
}
3738

3839
if o.waitNamespace != "" && o.waitResource != "" {
39-
if err = exec.RunCommand("kubectl", "wait", "-n", o.waitNamespace, o.waitResource, "--for", "condition=Available=True", "--timeout=900s"); err != nil {
40+
if err = o.execer.RunCommand("kubectl", "wait", "-n", o.waitNamespace, o.waitResource, "--for", "condition=Available=True", "--timeout=900s"); err != nil {
4041
return
4142
}
4243
}

cmd/root.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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"
910
)
1011

1112
// NewRootCmd creates the root command
@@ -16,9 +17,10 @@ func NewRootCmd(execer fakeruntime.Execer) (c *cobra.Command) {
1617
}
1718
c.SetOut(os.Stdout)
1819
c.Version = version.GetVersion()
19-
c.AddCommand(createInitCommand(),
20+
gRPCServer := grpc.NewServer()
21+
c.AddCommand(createInitCommand(execer),
2022
createRunCommand(), createSampleCmd(),
21-
createServerCmd(), createJSONSchemaCmd(),
23+
createServerCmd(gRPCServer), createJSONSchemaCmd(),
2224
createServiceCommand(execer))
2325
return
2426
}

cmd/root_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/stretchr/testify/assert"
77

88
atesting "github.com/linuxsuren/api-testing/pkg/testing"
9+
exec "github.com/linuxsuren/go-fake-runtime"
910
)
1011

1112
func Test_setRelativeDir(t *testing.T) {
@@ -43,10 +44,15 @@ func TestCreateRunCommand(t *testing.T) {
4344
cmd := createRunCommand()
4445
assert.Equal(t, "run", cmd.Use)
4546

46-
init := createInitCommand()
47+
init := createInitCommand(exec.FakeExecer{})
4748
assert.Equal(t, "init", init.Use)
4849

49-
server := createServerCmd()
50+
server := createServerCmd(&fakeGRPCServer{})
5051
assert.NotNil(t, server)
5152
assert.Equal(t, "server", server.Use)
53+
54+
root := NewRootCmd(exec.FakeExecer{})
55+
root.SetArgs([]string{"init", "-k=demo.yaml", "--wait-namespace", "demo", "--wait-resource", "demo"})
56+
err := root.Execute()
57+
assert.Nil(t, err)
5258
}

cmd/server.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"google.golang.org/grpc"
1212
)
1313

14-
func createServerCmd() (c *cobra.Command) {
15-
opt := &serverOption{}
14+
func createServerCmd(gRPCServer gRPCServer) (c *cobra.Command) {
15+
opt := &serverOption{gRPCServer: gRPCServer}
1616
c = &cobra.Command{
1717
Use: "server",
1818
Short: "Run as a server mode",
@@ -25,6 +25,7 @@ func createServerCmd() (c *cobra.Command) {
2525
}
2626

2727
type serverOption struct {
28+
gRPCServer gRPCServer
2829
port int
2930
printProto bool
3031
}
@@ -43,9 +44,26 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
4344
return
4445
}
4546

46-
s := grpc.NewServer()
47+
s := o.gRPCServer
4748
server.RegisterRunnerServer(s, server.NewRemoteServer())
4849
log.Printf("server listening at %v", lis.Addr())
4950
s.Serve(lis)
5051
return
5152
}
53+
54+
type gRPCServer interface {
55+
Serve(lis net.Listener) error
56+
grpc.ServiceRegistrar
57+
}
58+
59+
type fakeGRPCServer struct {
60+
}
61+
62+
// Serve is a fake method
63+
func (s *fakeGRPCServer) Serve(net.Listener) error {
64+
return nil
65+
}
66+
67+
// RegisterService is a fake method
68+
func (s *fakeGRPCServer) RegisterService(desc *grpc.ServiceDesc, impl interface{}) {
69+
}

cmd/server_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ func TestPrintProto(t *testing.T) {
3838
tt.verify(t, buf, err)
3939
})
4040
}
41+
42+
server := createServerCmd(&fakeGRPCServer{})
43+
err := server.Execute()
44+
assert.Nil(t, err)
4145
}

cmd/service.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package cmd provides a service command
12
package cmd
23

34
import (

pkg/runner/simple.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"github.com/andreyvit/diff"
1818
"github.com/antonmedv/expr"
1919
"github.com/antonmedv/expr/vm"
20-
"github.com/linuxsuren/api-testing/pkg/exec"
2120
"github.com/linuxsuren/api-testing/pkg/testing"
21+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
2222
unstructured "github.com/linuxsuren/unstructured/pkg"
2323
"github.com/xeipuuv/gojsonschema"
2424
)
@@ -78,6 +78,7 @@ type TestCaseRunner interface {
7878
WithOutputWriter(io.Writer) TestCaseRunner
7979
WithWriteLevel(level string) TestCaseRunner
8080
WithTestReporter(TestReporter) TestCaseRunner
81+
WithExecer(fakeruntime.Execer) TestCaseRunner
8182
}
8283

8384
// ReportRecord represents the raw data of a HTTP request
@@ -157,14 +158,16 @@ type simpleTestCaseRunner struct {
157158
testReporter TestReporter
158159
writer io.Writer
159160
log LevelWriter
161+
execer fakeruntime.Execer
160162
}
161163

162164
// NewSimpleTestCaseRunner creates the instance of the simple test case runner
163165
func NewSimpleTestCaseRunner() TestCaseRunner {
164166
runner := &simpleTestCaseRunner{}
165167
return runner.WithOutputWriter(io.Discard).
166168
WithWriteLevel("info").
167-
WithTestReporter(NewDiscardTestReporter())
169+
WithTestReporter(NewDiscardTestReporter()).
170+
WithExecer(fakeruntime.DefaultExecer{})
168171
}
169172

170173
// RunTestCase is the main entry point of a test case
@@ -179,16 +182,14 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
179182
r.testReporter.PutRecord(rr)
180183
}(record)
181184

182-
if err = doPrepare(testcase); err != nil {
185+
if err = r.doPrepare(testcase); err != nil {
183186
err = fmt.Errorf("failed to prepare, error: %v", err)
184187
return
185188
}
186189

187190
defer func() {
188191
if testcase.Clean.CleanPrepare {
189-
if err = doCleanPrepare(testcase); err != nil {
190-
return
191-
}
192+
err = r.doCleanPrepare(testcase)
192193
}
193194
}()
194195

@@ -363,29 +364,29 @@ func (r *simpleTestCaseRunner) WithTestReporter(reporter TestReporter) TestCaseR
363364
return r
364365
}
365366

366-
// Deprecated
367-
// RunTestCase runs the test case.
368-
func RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx context.Context) (output interface{}, err error) {
369-
return NewSimpleTestCaseRunner().WithOutputWriter(os.Stdout).RunTestCase(testcase, dataContext, ctx)
367+
// WithExecer sets the execer
368+
func (r *simpleTestCaseRunner) WithExecer(execer fakeruntime.Execer) TestCaseRunner {
369+
r.execer = execer
370+
return r
370371
}
371372

372-
func doPrepare(testcase *testing.TestCase) (err error) {
373+
func (r *simpleTestCaseRunner) doPrepare(testcase *testing.TestCase) (err error) {
373374
for i := range testcase.Prepare.Kubernetes {
374375
item := testcase.Prepare.Kubernetes[i]
375376

376-
if err = exec.RunCommand("kubectl", "apply", "-f", item); err != nil {
377+
if err = r.execer.RunCommand("kubectl", "apply", "-f", item); err != nil {
377378
return
378379
}
379380
}
380381
return
381382
}
382383

383-
func doCleanPrepare(testcase *testing.TestCase) (err error) {
384+
func (r *simpleTestCaseRunner) doCleanPrepare(testcase *testing.TestCase) (err error) {
384385
count := len(testcase.Prepare.Kubernetes)
385386
for i := count - 1; i >= 0; i-- {
386387
item := testcase.Prepare.Kubernetes[i]
387388

388-
if err = exec.RunCommand("kubectl", "delete", "-f", item); err != nil {
389+
if err = r.execer.RunCommand("kubectl", "delete", "-f", item); err != nil {
389390
return
390391
}
391392
}

pkg/runner/simple_test.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,37 @@ import (
55
"context"
66
"errors"
77
"net/http"
8+
"os"
89
"testing"
910

1011
_ "embed"
1112

1213
"github.com/h2non/gock"
1314
atest "github.com/linuxsuren/api-testing/pkg/testing"
15+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
1416
"github.com/stretchr/testify/assert"
1517
)
1618

1719
func TestTestCase(t *testing.T) {
1820
tests := []struct {
1921
name string
22+
execer fakeruntime.Execer
2023
testCase *atest.TestCase
2124
ctx interface{}
2225
prepare func()
2326
verify func(t *testing.T, output interface{}, err error)
2427
}{{
28+
name: "failed during the prepare stage",
29+
testCase: &atest.TestCase{
30+
Prepare: atest.Prepare{
31+
Kubernetes: []string{"demo.yaml"},
32+
},
33+
},
34+
execer: fakeruntime.FakeExecer{ExpectError: errors.New("fake")},
35+
verify: func(t *testing.T, output interface{}, err error) {
36+
assert.NotNil(t, err)
37+
},
38+
}, {
2539
name: "normal, response is map",
2640
testCase: &atest.TestCase{
2741
Request: atest.Request{
@@ -44,7 +58,14 @@ func TestTestCase(t *testing.T) {
4458
`data.name == "linuxsuren"`,
4559
},
4660
},
61+
Prepare: atest.Prepare{
62+
Kubernetes: []string{"demo.yaml"},
63+
},
64+
Clean: atest.Clean{
65+
CleanPrepare: true,
66+
},
4767
},
68+
execer: fakeruntime.FakeExecer{},
4869
prepare: func() {
4970
gock.New("http://localhost").
5071
Get("/foo").
@@ -370,7 +391,11 @@ func TestTestCase(t *testing.T) {
370391
if tt.prepare != nil {
371392
tt.prepare()
372393
}
373-
output, err := RunTestCase(tt.testCase, tt.ctx, context.TODO())
394+
runner := NewSimpleTestCaseRunner().WithOutputWriter(os.Stdout)
395+
if tt.execer != nil {
396+
runner.WithExecer(tt.execer)
397+
}
398+
output, err := runner.RunTestCase(tt.testCase, tt.ctx, context.TODO())
374399
tt.verify(t, output, err)
375400
})
376401
}

0 commit comments

Comments
 (0)