feat: support to print a sample YAML (#20)

Co-authored-by: Rick <linuxsuren@users.noreply.github.com>
This commit is contained in:
Rick 2023-04-02 11:50:43 +08:00 committed by GitHub
parent 5ebb0e9cd9
commit e57743633b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 88 additions and 28 deletions

View File

@ -11,13 +11,14 @@ type initOption struct {
waitResource string
}
// CreateInitCommand returns the init command
func CreateInitCommand() (cmd *cobra.Command) {
// createInitCommand returns the init command
func createInitCommand() (cmd *cobra.Command) {
opt := &initOption{}
cmd = &cobra.Command{
Use: "init",
Long: "Support to init Kubernetes cluster with kustomization, and wait it with command: kubectl wait",
RunE: opt.runE,
Use: "init",
Long: "Support to init Kubernetes cluster with kustomization, and wait it with command: kubectl wait",
Hidden: true,
RunE: opt.runE,
}
flags := cmd.Flags()

14
cmd/root.go Normal file
View File

@ -0,0 +1,14 @@
package cmd
import "github.com/spf13/cobra"
// NewRootCmd creates the root command
func NewRootCmd() (c *cobra.Command) {
c = &cobra.Command{
Use: "atest",
Short: "API testing tool",
}
c.AddCommand(createInitCommand(),
createRunCommand(), createSampleCmd())
return
}

View File

@ -40,9 +40,9 @@ func Test_setRelativeDir(t *testing.T) {
}
func TestCreateRunCommand(t *testing.T) {
cmd := CreateRunCommand()
cmd := createRunCommand()
assert.Equal(t, "run", cmd.Use)
init := CreateInitCommand()
init := createInitCommand()
assert.Equal(t, "init", init.Use)
}

View File

@ -48,8 +48,8 @@ func newDiskCardRunOption() *runOption {
}
}
// CreateRunCommand returns the run command
func CreateRunCommand() (cmd *cobra.Command) {
// createRunCommand returns the run command
func createRunCommand() (cmd *cobra.Command) {
opt := newDefaultRunOption()
cmd = &cobra.Command{
Use: "run",

View File

@ -92,7 +92,7 @@ func TestRunCommand(t *testing.T) {
tt.prepare()
root := &cobra.Command{Use: "root"}
root.AddCommand(CreateRunCommand())
root.AddCommand(createRunCommand())
root.SetArgs(append([]string{"run"}, tt.args...))
@ -101,3 +101,9 @@ func TestRunCommand(t *testing.T) {
})
}
}
func TestRootCmd(t *testing.T) {
c := NewRootCmd()
assert.NotNil(t, c)
assert.Equal(t, "atest", c.Use)
}

18
cmd/sample.go Normal file
View File

@ -0,0 +1,18 @@
package cmd
import (
"github.com/linuxsuren/api-testing/sample"
"github.com/spf13/cobra"
)
func createSampleCmd() (c *cobra.Command) {
c = &cobra.Command{
Use: "sample",
Short: "Generate a sample test case YAML file",
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.Println(sample.TestSuiteGitLab)
return
},
}
return
}

22
cmd/sample_test.go Normal file
View File

@ -0,0 +1,22 @@
package cmd_test
import (
"bytes"
"testing"
"github.com/linuxsuren/api-testing/cmd"
"github.com/linuxsuren/api-testing/sample"
"github.com/stretchr/testify/assert"
)
func TestSampleCmd(t *testing.T) {
c := cmd.NewRootCmd()
buf := new(bytes.Buffer)
c.SetOut(buf)
c.SetArgs([]string{"sample"})
err := c.Execute()
assert.Nil(t, err)
assert.Equal(t, sample.TestSuiteGitLab+"\n", buf.String())
}

6
go.mod
View File

@ -8,8 +8,9 @@ require (
github.com/antonmedv/expr v1.12.1
github.com/h2non/gock v1.2.0
github.com/linuxsuren/unstructured v0.0.1
github.com/spf13/cobra v1.4.0
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.2
golang.org/x/sync v0.1.0
gopkg.in/yaml.v2 v2.4.0
)
@ -21,7 +22,7 @@ require (
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
@ -30,6 +31,5 @@ require (
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/sync v0.1.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

10
go.sum
View File

@ -8,7 +8,7 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNg
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/antonmedv/expr v1.12.1 h1:GTGrGN1kxxb+le0uQKaFRK8By4cvq1sleUCGE/U6hHg=
github.com/antonmedv/expr v1.12.1/go.mod h1:FPC8iWArxls7axbVLsW+kpg1mz29A1b2M6jt+hZfDkU=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -22,8 +22,8 @@ github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@ -46,8 +46,8 @@ github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXY
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

13
main.go
View File

@ -3,19 +3,12 @@ package main
import (
"os"
c "github.com/linuxsuren/api-testing/cmd"
"github.com/spf13/cobra"
"github.com/linuxsuren/api-testing/cmd"
)
func main() {
cmd := &cobra.Command{
Use: "atest",
Short: "API testing tool",
}
cmd.AddCommand(c.CreateInitCommand(), c.CreateRunCommand())
// run command
if err := cmd.Execute(); err != nil {
c := cmd.NewRootCmd()
if err := c.Execute(); err != nil {
os.Exit(1)
}
}

6
sample/constants.go Normal file
View File

@ -0,0 +1,6 @@
package sample
import _ "embed"
//go:embed testsuite-gitlab.yaml
var TestSuiteGitLab string