Skip to content

Commit

Permalink
feat: support to print a sample YAML (#20)
Browse files Browse the repository at this point in the history
Co-authored-by: Rick <linuxsuren@users.noreply.github.com>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Apr 2, 2023
1 parent 5ebb0e9 commit e577436
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 28 deletions.
11 changes: 6 additions & 5 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...))

Expand All @@ -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 changes: 18 additions & 0 deletions cmd/sample.go
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions cmd/sample_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand All @@ -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 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand Down
13 changes: 3 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 6 additions & 0 deletions sample/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sample

import _ "embed"

//go:embed testsuite-gitlab.yaml
var TestSuiteGitLab string

0 comments on commit e577436

Please sign in to comment.