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

import (
LinuxSuRen's avatar
LinuxSuRen 已提交
4
	"bytes"
LinuxSuRen's avatar
LinuxSuRen 已提交
5 6 7 8
	"github.com/golang/mock/gomock"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/spf13/cobra"
9 10
	"io/ioutil"
	"os"
LinuxSuRen's avatar
LinuxSuRen 已提交
11 12 13 14
)

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

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

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

LinuxSuRen's avatar
LinuxSuRen 已提交
34 35 36 37 38 39 40 41 42 43 44 45
	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 已提交
46 47
	Context("PreHook test", func() {
		It("only with root cmd", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
48
			path := getCmdPath(fakeRootCmd)
LinuxSuRen's avatar
LinuxSuRen 已提交
49 50 51 52 53 54 55
			Expect(path).To(Equal(""))
		})

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

			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 已提交
69
			fakeRootCmd.AddCommand(sub1Cmd)
LinuxSuRen's avatar
LinuxSuRen 已提交
70 71 72 73 74 75
			sub1Cmd.AddCommand(sub2Cmd)

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

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

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

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

			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{
102
				PreHooks: []CommandHook{CommandHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
103
					Path:    "test",
LinuxSuRen's avatar
LinuxSuRen 已提交
104
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
				}},
			}

			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{
122
				PreHooks: []CommandHook{CommandHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
123
					Path:    "test",
LinuxSuRen's avatar
LinuxSuRen 已提交
124
					Command: successCmd,
125
				}, CommandHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
126 127
					Path:    "test",
					Command: "echo 2",
128
				}, CommandHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
129
					Path:    "fake",
LinuxSuRen's avatar
LinuxSuRen 已提交
130
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
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 159
				}},
			}

			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 已提交
160 161 162

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

			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 已提交
179
	})
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

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

		It("basic use case with one postHook, should success", func() {
			config = &Config{
				PostHooks: []CommandHook{CommandHook{
					Path:    "test",
					Command: successCmd,
				}},
			}

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

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

		It("basic use case with many postHooks, should success", func() {
			config = &Config{
				PostHooks: []CommandHook{CommandHook{
					Path:    "test",
					Command: successCmd,
				}, CommandHook{
					Path:    "test",
					Command: "echo 2",
				}, CommandHook{
					Path:    "fake",
					Command: successCmd,
				}},
			}

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

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

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

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

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

		It("basic use case with error command, should success", func() {
			config = &Config{
				PostHooks: []CommandHook{CommandHook{
					Path:    "test",
					Command: errorCmd,
				}},
			}

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

			var buf bytes.Buffer
			err := executePostCmd(subCmd, nil, &buf)
			Expect(err).To(HaveOccurred())
		})
	})

	Context("basic root command test", func() {
		var (
			buf *bytes.Buffer
		)

		BeforeEach(func() {
			rootOptions = RootOptions{}
			buf = new(bytes.Buffer)
			rootCmd.SetOut(buf)
		})

		It("should contain substring Version:", func() {
			rootCmd.SetArgs([]string{"--version"})
			_, err := rootCmd.ExecuteC()
			Expect(err).NotTo(HaveOccurred())
			Expect(buf.String()).To(ContainSubstring("Version:"))
		})

		It("with a fake jenkins as option", func() {
			rootCmd.SetArgs([]string{"--jenkins", "fake"})
			_, err := rootCmd.ExecuteC()
			Expect(err).To(HaveOccurred())
			Expect(buf.String()).To(ContainSubstring("cannot found the configuration:"))
		})

		It("with an exists jenkins as option", func() {
			configFile, err := ioutil.TempFile("/tmp", ".yaml")
			Expect(err).NotTo(HaveOccurred())

			defer os.Remove(configFile.Name())

			data, err := generateSampleConfig()
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(configFile.Name(), data, 0664)
			Expect(err).To(BeNil())

			rootCmd.SetArgs([]string{"--jenkins", "yourServer", "--configFile", configFile.Name()})
			_, err = rootCmd.ExecuteC()
			Expect(err).NotTo(HaveOccurred())
			Expect(buf.String()).To(ContainSubstring("Current Jenkins is:"))
		})
	})

	Context("use connection from options", func() {
		var (
			err error
		)

		BeforeEach(func() {
			rootOptions = RootOptions{}
		})

		AfterEach(func() {
			rootOptions = RootOptions{}
		})

		It("fake jenkins, but with URL from option", func() {
			rootCmd.SetArgs([]string{"--configFile", "fake", "--url", "fake-url",
				"--username", "fake-user", "--token", "fake-token"})
			_, err = rootCmd.ExecuteC()
			Expect(err).NotTo(HaveOccurred())

			jenkins := getCurrentJenkinsFromOptions()
			Expect(jenkins.URL).To(Equal("fake-url"))
			Expect(jenkins.UserName).To(Equal("fake-user"))
			Expect(jenkins.Token).To(Equal("fake-token"))
		})
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
336
})
LinuxSuRen's avatar
LinuxSuRen 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

// 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())
	})
})