restart_test.go 2.1 KB
Newer Older
LinuxSuRen's avatar
LinuxSuRen 已提交
1 2 3 4 5 6 7 8
package cmd

import (
	"bytes"
	"github.com/jenkins-zh/jenkins-cli/client"
	"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
	"io/ioutil"
	"os"
9
	"path"
LinuxSuRen's avatar
LinuxSuRen 已提交
10 11 12 13 14 15 16 17

	"github.com/golang/mock/gomock"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("restart command", func() {
	var (
18
		ctrl         *gomock.Controller
LinuxSuRen's avatar
LinuxSuRen 已提交
19
		roundTripper *mhttp.MockRoundTripper
20
		err          error
LinuxSuRen's avatar
LinuxSuRen 已提交
21 22 23 24 25 26 27 28
	)

	BeforeEach(func() {
		ctrl = gomock.NewController(GinkgoT())
		roundTripper = mhttp.NewMockRoundTripper(ctrl)
		restartOption.RoundTripper = roundTripper
		rootCmd.SetArgs([]string{})
		rootOptions.Jenkins = ""
29 30 31
		rootOptions.ConfigFile = path.Join(os.TempDir(), "fake.yaml")

		var data []byte
LinuxSuRen's avatar
LinuxSuRen 已提交
32
		data, err = GenerateSampleConfig()
33 34 35
		Expect(err).To(BeNil())
		err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
		Expect(err).To(BeNil())
LinuxSuRen's avatar
LinuxSuRen 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	})

	AfterEach(func() {
		rootCmd.SetArgs([]string{})
		os.Remove(rootOptions.ConfigFile)
		rootOptions.ConfigFile = ""
		ctrl.Finish()
	})

	Context("with batch mode", func() {
		It("should success", func() {
			client.PrepareRestart(roundTripper, "http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9", 503)

			rootCmd.SetArgs([]string{"restart", "-b"})

			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)
			_, err = rootCmd.ExecuteC()
			Expect(err).To(BeNil())

			Expect(buf.String()).To(Equal("Please wait while Jenkins is restarting\n"))
		})

		It("with error code, 400", func() {
			client.PrepareRestart(roundTripper, "http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9", 400)

			rootCmd.SetArgs([]string{"restart", "-b"})

			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)
			_, err = rootCmd.ExecuteC()
67 68
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(ContainSubstring("bad request, code 400"))
LinuxSuRen's avatar
LinuxSuRen 已提交
69
		})
70 71 72 73 74 75 76 77 78 79 80

		It("restart directly", func() {
			client.PrepareRestartDirectly(roundTripper, "http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9", 503)

			rootCmd.SetArgs([]string{"restart", "-b", "--safe=false"})

			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)
			_, err = rootCmd.ExecuteC()
			Expect(err).NotTo(HaveOccurred())
		})
LinuxSuRen's avatar
LinuxSuRen 已提交
81 82
	})
})