plugin_search_test.go 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 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
package cmd

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

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

	"github.com/jenkins-zh/jenkins-cli/client"
	"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
)

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

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

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

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

			request, _ := client.PrepareForEmptyAvaiablePluginList(roundTripper, "http://localhost:8080/jenkins")
			request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
			rootCmd.SetArgs([]string{"plugin", "search", "fake"})

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

			Expect(buf.String()).To(Equal(""))
		})

57
		It("many plugins in the list", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
58
			data, err := GenerateSampleConfig()
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
			Expect(err).To(BeNil())

			request, _ := client.PrepareForManyAvaiablePlugin(roundTripper, "http://localhost:8080/jenkins")
			request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
			request, _ = client.PrepareForRequestUpdateCenter(roundTripper, "http://localhost:8080/jenkins")
			request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
			request, _ = client.PrepareForOneInstalledPlugin(roundTripper, "http://localhost:8080/jenkins")
			request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")

			rootCmd.SetArgs([]string{"plugin", "search", "fake"})
			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)
			_, err = rootCmd.ExecuteC()
			Expect(err).To(BeNil())

			Expect(buf.String()).To(Equal(`number name       installed version  installedVersion title
0      fake-ocean true      1.19.011 1.18.111         fake-ocean
1      fake-ln    true      1.19.011 1.18.1           fake-ln
2      fake-is    true      1.19.1   1.18.111         fake-is
3      fake-oa    false     1.13.011                  fake-oa
4      fake-open  false     1.13.0                    fake-open
5      fake       true               1.0              fake
`))
		})

		It("should success, empty updateCenter list", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
87
			data, err := GenerateSampleConfig()
88 89 90 91
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
			Expect(err).To(BeNil())

92
			request, _ := client.PrepareForManyAvaiablePlugin(roundTripper, "http://localhost:8080/jenkins")
93
			request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
94 95
			request, _ = client.PrepareForNoAvailablePlugins(roundTripper, "http://localhost:8080/jenkins")
			request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
96
			request, _ = client.PrepareForManyInstalledPlugins(roundTripper, "http://localhost:8080/jenkins", 1)
97 98 99 100 101 102 103 104 105 106
			request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
			rootCmd.SetArgs([]string{"plugin", "search", "fake"})

			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)
			_, err = rootCmd.ExecuteC()
			Expect(err).To(BeNil())
			Expect(buf.String()).To(Equal(`number name       installed version installedVersion title
0      fake-ocean true              1.18.111         fake-ocean
1      fake-ln    true              1.18.1           fake-ln
107
2      fake-is    true              1.18.131-2.0     fake-is
108 109 110 111 112 113 114
3      fake-oa    false                              fake-oa
4      fake-open  false                              fake-open
5      fake       true              1.0              fake
`))
		})

		It("should success, null updateCenter and 500 installed list", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
115
			data, err := GenerateSampleConfig()
116 117 118 119 120 121 122 123
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
			Expect(err).To(BeNil())

			request, _ := client.PrepareForManyAvaiablePlugin(roundTripper, "http://localhost:8080/jenkins")
			request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
			request, _ = client.PrepareForRequest500UpdateCenter(roundTripper, "http://localhost:8080/jenkins")
			request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
124
			request, _ = client.PrepareFor500InstalledPluginList(roundTripper, "http://localhost:8080/jenkins", 1)
125
			request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
126 127 128 129 130 131 132
			rootCmd.SetArgs([]string{"plugin", "search", "fake"})

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

133 134 135 136 137 138 139
			Expect(buf.String()).To(Equal(`number name       installed version installedVersion title
0      fake-ocean false                              fake-ocean
1      fake-ln    false                              fake-ln
2      fake-is    false                              fake-is
3      fake-oa    false                              fake-oa
4      fake-open  false                              fake-open
5      fake       false                              fake
140 141 142 143
`))
		})
	})
})