root_test.go 4.1 KB
Newer Older
LinuxSuRen's avatar
LinuxSuRen 已提交
1 2 3
package cmd

import (
LinuxSuRen's avatar
LinuxSuRen 已提交
4
	"bytes"
LinuxSuRen's avatar
LinuxSuRen 已提交
5

LinuxSuRen's avatar
LinuxSuRen 已提交
6 7 8 9 10 11 12 13
	"github.com/golang/mock/gomock"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/spf13/cobra"
)

var _ = Describe("Root cmd test", func() {
	var (
LinuxSuRen's avatar
LinuxSuRen 已提交
14 15 16 17
		ctrl        *gomock.Controller
		fakeRootCmd *cobra.Command
		successCmd  string
		errorCmd    string
LinuxSuRen's avatar
LinuxSuRen 已提交
18 19 20 21
	)

	BeforeEach(func() {
		ctrl = gomock.NewController(GinkgoT())
LinuxSuRen's avatar
LinuxSuRen 已提交
22
		fakeRootCmd = &cobra.Command{Use: "root"}
LinuxSuRen's avatar
LinuxSuRen 已提交
23 24
		successCmd = "echo 1"
		errorCmd = "exit 1"
25
		config = nil
LinuxSuRen's avatar
LinuxSuRen 已提交
26 27 28
	})

	AfterEach(func() {
29
		config = nil
LinuxSuRen's avatar
LinuxSuRen 已提交
30 31 32
		ctrl.Finish()
	})

LinuxSuRen's avatar
LinuxSuRen 已提交
33 34 35 36 37 38 39 40 41 42 43 44
	Context("invalid logger level", func() {
		It("cause errors", func() {
			rootCmd.SetArgs([]string{"--logger-level", "fake"})
			_, err := rootCmd.ExecuteC()
			Expect(err).To(HaveOccurred())

			rootCmd.SetArgs([]string{"--logger-level", "warn"})
			_, err = rootCmd.ExecuteC()
			Expect(err).NotTo(HaveOccurred())
		})
	})

LinuxSuRen's avatar
LinuxSuRen 已提交
45 46
	Context("PreHook test", func() {
		It("only with root cmd", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
47
			path := getCmdPath(fakeRootCmd)
LinuxSuRen's avatar
LinuxSuRen 已提交
48 49 50 51 52 53 54
			Expect(path).To(Equal(""))
		})

		It("one sub cmd", func() {
			subCmd := &cobra.Command{
				Use: "sub",
			}
LinuxSuRen's avatar
LinuxSuRen 已提交
55
			fakeRootCmd.AddCommand(subCmd)
LinuxSuRen's avatar
LinuxSuRen 已提交
56 57 58 59 60 61 62 63 64 65 66 67

			path := getCmdPath(subCmd)
			Expect(path).To(Equal("sub"))
		})

		It("two sub cmds", func() {
			sub1Cmd := &cobra.Command{
				Use: "sub1",
			}
			sub2Cmd := &cobra.Command{
				Use: "sub2",
			}
LinuxSuRen's avatar
LinuxSuRen 已提交
68
			fakeRootCmd.AddCommand(sub1Cmd)
LinuxSuRen's avatar
LinuxSuRen 已提交
69 70 71 72 73 74
			sub1Cmd.AddCommand(sub2Cmd)

			path := getCmdPath(sub2Cmd)
			Expect(path).To(Equal("sub1.sub2"))
		})
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
75 76 77 78

	Context("execute cmd test", func() {
		It("basic command", func() {
			var buf bytes.Buffer
LinuxSuRen's avatar
LinuxSuRen 已提交
79
			err := execute(successCmd, &buf)
LinuxSuRen's avatar
LinuxSuRen 已提交
80 81 82 83 84 85 86

			Expect(buf.String()).To(Equal("1\n"))
			Expect(err).To(BeNil())
		})

		It("error command", func() {
			var buf bytes.Buffer
LinuxSuRen's avatar
LinuxSuRen 已提交
87
			err := execute(errorCmd, &buf)
LinuxSuRen's avatar
LinuxSuRen 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100

			Expect(err).To(HaveOccurred())
		})
	})

	Context("execute pre cmd", func() {
		It("should error", func() {
			err := executePreCmd(nil, nil, nil)
			Expect(err).To(HaveOccurred())
		})

		It("basic use case with one preHook, should success", func() {
			config = &Config{
LinuxSuRen's avatar
LinuxSuRen 已提交
101
				PreHooks: []CommndHook{CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
102
					Path:    "test",
LinuxSuRen's avatar
LinuxSuRen 已提交
103
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
				}},
			}

			rootCmd := &cobra.Command{}
			subCmd := &cobra.Command{
				Use: "test",
			}
			rootCmd.AddCommand(subCmd)

			var buf bytes.Buffer
			err := executePreCmd(subCmd, nil, &buf)
			Expect(err).To(BeNil())
			Expect(buf.String()).To(Equal("1\n"))
		})

		It("basic use case with many preHooks, should success", func() {
			config = &Config{
LinuxSuRen's avatar
LinuxSuRen 已提交
121
				PreHooks: []CommndHook{CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
122
					Path:    "test",
LinuxSuRen's avatar
LinuxSuRen 已提交
123
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
124
				}, CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
125 126
					Path:    "test",
					Command: "echo 2",
LinuxSuRen's avatar
LinuxSuRen 已提交
127
				}, CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
128
					Path:    "fake",
LinuxSuRen's avatar
LinuxSuRen 已提交
129
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
				}},
			}

			rootCmd := &cobra.Command{}
			subCmd := &cobra.Command{
				Use: "test",
			}
			rootCmd.AddCommand(subCmd)

			var buf bytes.Buffer
			err := executePreCmd(subCmd, nil, &buf)
			Expect(err).To(BeNil())
			Expect(buf.String()).To(Equal("1\n2\n"))
		})

		It("basic use case without preHooks, should success", func() {
			config = &Config{}

			rootCmd := &cobra.Command{}
			subCmd := &cobra.Command{
				Use: "test",
			}
			rootCmd.AddCommand(subCmd)

			var buf bytes.Buffer
			err := executePreCmd(subCmd, nil, &buf)
			Expect(err).To(BeNil())
			Expect(buf.String()).To(Equal(""))
		})
LinuxSuRen's avatar
LinuxSuRen 已提交
159 160 161

		It("basic use case with error command, should success", func() {
			config = &Config{
LinuxSuRen's avatar
LinuxSuRen 已提交
162
				PreHooks: []CommndHook{CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
163
					Path:    "test",
LinuxSuRen's avatar
LinuxSuRen 已提交
164
					Command: errorCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177
				}},
			}

			rootCmd := &cobra.Command{}
			subCmd := &cobra.Command{
				Use: "test",
			}
			rootCmd.AddCommand(subCmd)

			var buf bytes.Buffer
			err := executePreCmd(subCmd, nil, &buf)
			Expect(err).To(HaveOccurred())
		})
LinuxSuRen's avatar
LinuxSuRen 已提交
178
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
179
})
LinuxSuRen's avatar
LinuxSuRen 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

// FakeOpt only for test
type FakeOpt struct{}

// Check fake, only for test
func (o *FakeOpt) Check() error {
	return nil
}

var _ = Describe("RunDiagnose test", func() {
	It("should success", func() {
		opt := RootOptions{Doctor: true}

		rootCmd := &cobra.Command{
			Use: "fake",
		}
		healthCheckRegister.Register(getCmdPath(rootCmd), &FakeOpt{})
		err := opt.RunDiagnose(rootCmd)
		Expect(err).NotTo(HaveOccurred())
	})
})