center_watch_test.go 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package cmd

import (
	"bytes"
	"io/ioutil"
	"net/http"
	"os"

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

13
	"github.com/jenkins-zh/jenkins-cli/client"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
	"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
)

var _ = Describe("center watch command", func() {
	var (
		ctrl         *gomock.Controller
		roundTripper *mhttp.MockRoundTripper
	)

	BeforeEach(func() {
		ctrl = gomock.NewController(GinkgoT())
		roundTripper = mhttp.NewMockRoundTripper(ctrl)
		rootCmd.SetArgs([]string{})
		rootOptions.Jenkins = ""
		rootOptions.ConfigFile = "test.yaml"

		centerWatchOption.WatchOption.Count = -1
		centerWatchOption.RoundTripper = roundTripper
		centerOption.RoundTripper = roundTripper
	})

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

	Context("basic cases", func() {
43
		It("should success, center watch command", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
44
			data, err := GenerateSampleConfig()
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
			Expect(err).To(BeNil())

			requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/api/json", nil)
			requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
			responseCrumb := &http.Response{
				StatusCode: 200,
				Proto:      "HTTP/1.1",
				Request:    requestCrumb,
				Body: ioutil.NopCloser(bytes.NewBufferString(`
				{"version":"0"}
				`)),
			}
			roundTripper.EXPECT().
60
				RoundTrip(client.NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
61 62 63 64 65

			rootCmd.SetArgs([]string{"center", "watch"})
			_, err = rootCmd.ExecuteC()
			Expect(err).To(BeNil())
		})
66 67 68 69 70 71 72 73

		It("allPluginsCompleted", func() {
			Expect(allPluginsCompleted(nil)).To(Equal(false))

			status := &client.UpdateCenter{}
			Expect(allPluginsCompleted(status)).To(Equal(false))

			// all install job is completed
74
			status.Jobs = []client.InstallationJob{{
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
				UpdateCenterJob: client.UpdateCenterJob{Type: "InstallationJob"},
				Status: client.InstallationJobStatus{
					Success: true,
				},
			}}
			Expect(allPluginsCompleted(status)).To(Equal(true))

			// there's one install job is not completed
			status.Jobs = append(status.Jobs, client.InstallationJob{
				UpdateCenterJob: client.UpdateCenterJob{Type: "InstallationJob"},
				Status: client.InstallationJobStatus{
					Success: false,
				},
			})
			Expect(allPluginsCompleted(status)).To(Equal(false))
		})
91 92
	})
})