Skip to content

Commit 390f04e

Browse files
committed
add unit tests
1 parent c181f37 commit 390f04e

10 files changed

+101
-16
lines changed

cmd/jsonschema_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"testing"
77

88
"github.com/linuxsuren/api-testing/cmd"
9+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
910
"github.com/stretchr/testify/assert"
1011
)
1112

1213
func TestJSONSchemaCmd(t *testing.T) {
13-
c := cmd.NewRootCmd()
14+
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
1415

1516
buf := new(bytes.Buffer)
1617
c.SetOut(buf)

cmd/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"os"
55

66
"github.com/linuxsuren/api-testing/pkg/version"
7+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
78
"github.com/spf13/cobra"
89
)
910

1011
// NewRootCmd creates the root command
11-
func NewRootCmd() (c *cobra.Command) {
12+
func NewRootCmd(execer fakeruntime.Execer) (c *cobra.Command) {
1213
c = &cobra.Command{
1314
Use: "atest",
1415
Short: "API testing tool",
@@ -18,6 +19,6 @@ func NewRootCmd() (c *cobra.Command) {
1819
c.AddCommand(createInitCommand(),
1920
createRunCommand(), createSampleCmd(),
2021
createServerCmd(), createJSONSchemaCmd(),
21-
createServiceCommand())
22+
createServiceCommand(execer))
2223
return
2324
}

cmd/run_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/h2non/gock"
1010
"github.com/linuxsuren/api-testing/pkg/limit"
11+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
1112
"github.com/spf13/cobra"
1213
"github.com/stretchr/testify/assert"
1314
)
@@ -103,7 +104,7 @@ func TestRunCommand(t *testing.T) {
103104
}
104105

105106
func TestRootCmd(t *testing.T) {
106-
c := NewRootCmd()
107+
c := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
107108
assert.NotNil(t, c)
108109
assert.Equal(t, "atest", c.Use)
109110
}

cmd/sample_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66

77
"github.com/linuxsuren/api-testing/cmd"
88
"github.com/linuxsuren/api-testing/sample"
9+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
910
"github.com/stretchr/testify/assert"
1011
)
1112

1213
func TestSampleCmd(t *testing.T) {
13-
c := cmd.NewRootCmd()
14+
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
1415

1516
buf := new(bytes.Buffer)
1617
c.SetOut(buf)

cmd/server_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"testing"
77

8+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -30,7 +31,7 @@ func TestPrintProto(t *testing.T) {
3031
for _, tt := range tests {
3132
t.Run(tt.name, func(t *testing.T) {
3233
buf := new(bytes.Buffer)
33-
root := NewRootCmd()
34+
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
3435
root.SetOut(buf)
3536
root.SetArgs(tt.args)
3637
err := root.Execute()

cmd/service.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package cmd
33
import (
44
"fmt"
55
"os"
6-
"runtime"
76

7+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
88
"github.com/spf13/cobra"
99
)
1010

11-
func createServiceCommand() (c *cobra.Command) {
12-
opt := &serviceOption{}
11+
func createServiceCommand(execer fakeruntime.Execer) (c *cobra.Command) {
12+
opt := &serviceOption{
13+
Execer: execer,
14+
}
1315
c = &cobra.Command{
1416
Use: "service",
1517
Aliases: []string{"s"},
@@ -18,28 +20,47 @@ func createServiceCommand() (c *cobra.Command) {
1820
RunE: opt.runE,
1921
}
2022
flags := c.Flags()
21-
flags.StringVarP(&opt.action, "action", "a", "", "The action of service, support actions: install")
23+
flags.StringVarP(&opt.action, "action", "a", "", "The action of service, support actions: install, start, stop, restart, status")
24+
flags.StringVarP(&opt.scriptPath, "script-path", "", "/lib/systemd/system/atest.service", "The service script file path")
2225
return
2326
}
2427

2528
type serviceOption struct {
26-
action string
29+
action string
30+
scriptPath string
31+
fakeruntime.Execer
2732
}
2833

2934
func (o *serviceOption) preRunE(c *cobra.Command, args []string) (err error) {
30-
if runtime.GOOS != "linux" {
35+
if o.Execer.OS() != "linux" {
3136
err = fmt.Errorf("only support on Linux")
3237
}
38+
if o.action == "" && len(args) > 0 {
39+
o.action = args[0]
40+
}
3341
return
3442
}
3543

3644
func (o *serviceOption) runE(c *cobra.Command, args []string) (err error) {
45+
var output string
3746
switch o.action {
3847
case "install", "i":
39-
err = os.WriteFile("/lib/systemd/system/atest.service", []byte(script), os.ModeAppend)
48+
err = os.WriteFile(o.scriptPath, []byte(script), os.ModeAppend)
49+
case "start":
50+
output, err = o.Execer.RunCommandAndReturn("systemctl", "", "start", "atest")
51+
case "stop":
52+
output, err = o.Execer.RunCommandAndReturn("systemctl", "", "stop", "atest")
53+
case "restart":
54+
output, err = o.Execer.RunCommandAndReturn("systemctl", "", "restart", "atest")
55+
case "status":
56+
output, err = o.Execer.RunCommandAndReturn("systemctl", "", "status", "atest")
4057
default:
4158
err = fmt.Errorf("not support action: '%s'", o.action)
4259
}
60+
61+
if output != "" {
62+
c.Println(output)
63+
}
4364
return
4465
}
4566

cmd/service_test.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,69 @@
11
package cmd
22

33
import (
4+
"bytes"
5+
"os"
46
"testing"
57

8+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
69
"github.com/stretchr/testify/assert"
710
)
811

912
func TestService(t *testing.T) {
10-
root := NewRootCmd()
11-
root.SetArgs([]string{"service", "--action", "fake"})
13+
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
14+
root.SetArgs([]string{"service", "fake"})
1215
err := root.Execute()
1316
assert.NotNil(t, err)
17+
18+
notLinux := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "fake"})
19+
notLinux.SetArgs([]string{"service", "--action", "install"})
20+
err = notLinux.Execute()
21+
assert.NotNil(t, err)
22+
23+
tmpFile, err := os.CreateTemp(os.TempDir(), "service")
24+
assert.Nil(t, err)
25+
defer func() {
26+
os.RemoveAll(tmpFile.Name())
27+
}()
28+
29+
targetScript := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"})
30+
targetScript.SetArgs([]string{"service", "--action", "install", "--script-path", tmpFile.Name()})
31+
err = targetScript.Execute()
32+
assert.Nil(t, err)
33+
data, err := os.ReadFile(tmpFile.Name())
34+
assert.Nil(t, err)
35+
assert.Equal(t, script, string(data))
36+
37+
tests := []struct {
38+
name string
39+
action string
40+
expectOutput string
41+
}{{
42+
name: "action: start",
43+
action: "start",
44+
expectOutput: "output1",
45+
}, {
46+
name: "action: stop",
47+
action: "stop",
48+
expectOutput: "output2",
49+
}, {
50+
name: "action: restart",
51+
action: "restart",
52+
expectOutput: "output3",
53+
}, {
54+
name: "action: status",
55+
action: "status",
56+
expectOutput: "output4",
57+
}}
58+
for _, tt := range tests {
59+
t.Run(tt.name, func(t *testing.T) {
60+
buf := new(bytes.Buffer)
61+
normalRoot := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux", ExpectOutput: tt.expectOutput})
62+
normalRoot.SetOut(buf)
63+
normalRoot.SetArgs([]string{"service", "--action", tt.action})
64+
err = normalRoot.Execute()
65+
assert.Nil(t, err)
66+
assert.Equal(t, tt.expectOutput+"\n", buf.String())
67+
})
68+
}
1469
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
github.com/imdario/mergo v0.3.11 // indirect
2929
github.com/inconshreveable/mousetrap v1.0.1 // indirect
3030
github.com/invopop/jsonschema v0.7.0 // indirect
31+
github.com/linuxsuren/go-fake-runtime v0.0.0-20230413085645-15e77ab55dbd // indirect
3132
github.com/mitchellh/copystructure v1.0.0 // indirect
3233
github.com/mitchellh/reflectwalk v1.0.0 // indirect
3334
github.com/pmezard/go-difflib v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
563563
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
564564
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
565565
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
566+
github.com/linuxsuren/go-fake-runtime v0.0.0-20230413085645-15e77ab55dbd h1:2Avir30WOgcDqG3sA4hlW4bC4c/tgseAUntPhf5JQ6E=
567+
github.com/linuxsuren/go-fake-runtime v0.0.0-20230413085645-15e77ab55dbd/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ=
566568
github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursDSE+j00iU=
567569
github.com/linuxsuren/unstructured v0.0.1/go.mod h1:KH6aTj+FegzGBzc1vS6mzZx3/duhTUTEVyW5sO7p4as=
568570
github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"os"
55

66
"github.com/linuxsuren/api-testing/cmd"
7+
exec "github.com/linuxsuren/go-fake-runtime"
78
)
89

910
func main() {
10-
c := cmd.NewRootCmd()
11+
c := cmd.NewRootCmd(exec.DefaultExecer{})
1112
if err := c.Execute(); err != nil {
1213
os.Exit(1)
1314
}

0 commit comments

Comments
 (0)