pluginManager_test.go 7.0 KB
Newer Older
LinuxSuRen's avatar
LinuxSuRen 已提交
1 2 3
package client

import (
4 5 6 7 8
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"

LinuxSuRen's avatar
LinuxSuRen 已提交
9
	"github.com/golang/mock/gomock"
10
	"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
LinuxSuRen's avatar
LinuxSuRen 已提交
11 12 13 14 15 16
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("PluginManager test", func() {
	var (
17 18 19
		ctrl         *gomock.Controller
		roundTripper *mhttp.MockRoundTripper
		pluginMgr    PluginManager
20
		updateMgr    UpdateCenterManager
LinuxSuRen's avatar
LinuxSuRen 已提交
21 22 23 24
	)

	BeforeEach(func() {
		ctrl = gomock.NewController(GinkgoT())
25 26 27 28
		roundTripper = mhttp.NewMockRoundTripper(ctrl)
		pluginMgr = PluginManager{}
		pluginMgr.RoundTripper = roundTripper
		pluginMgr.URL = "http://localhost"
29 30 31
		updateMgr = UpdateCenterManager{}
		updateMgr.RoundTripper = roundTripper
		updateMgr.URL = "http://localhost"
LinuxSuRen's avatar
LinuxSuRen 已提交
32 33 34 35 36 37 38 39 40
	})

	AfterEach(func() {
		ctrl.Finish()
	})

	Context("basic function test", func() {
		It("get install plugin query string", func() {
			names := make([]string, 0)
41
			Expect(pluginMgr.getPluginsInstallQuery(names)).To(Equal(""))
LinuxSuRen's avatar
LinuxSuRen 已提交
42 43

			names = append(names, "abc")
44
			Expect(pluginMgr.getPluginsInstallQuery(names)).To(Equal("plugin.abc="))
LinuxSuRen's avatar
LinuxSuRen 已提交
45 46

			names = append(names, "def")
47
			Expect(pluginMgr.getPluginsInstallQuery(names)).To(Equal("plugin.abc=&plugin.def="))
LinuxSuRen's avatar
LinuxSuRen 已提交
48 49

			names = append(names, "")
50
			Expect(pluginMgr.getPluginsInstallQuery(names)).To(Equal("plugin.abc=&plugin.def="))
LinuxSuRen's avatar
LinuxSuRen 已提交
51 52
		})
	})
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

	Context("GetAvailablePlugins", func() {
		It("no plugin in the list", func() {
			PrepareForEmptyAvaiablePluginList(roundTripper, pluginMgr.URL)

			pluginList, err := pluginMgr.GetAvailablePlugins()
			Expect(err).To(BeNil())
			Expect(pluginList).NotTo(BeNil())
			Expect(len(pluginList.Data)).To(Equal(0))
		})

		It("one plugin in the list", func() {
			PrepareForOneAvaiablePlugin(roundTripper, pluginMgr.URL)

			pluginList, err := pluginMgr.GetAvailablePlugins()
			Expect(err).To(BeNil())
			Expect(pluginList).NotTo(BeNil())
			Expect(len(pluginList.Data)).To(Equal(1))
			Expect(pluginList.Data[0].Name).To(Equal("fake"))
		})

74 75 76 77 78 79 80 81 82 83
		It("many plugins in the list", func() {
			PrepareForManyAvaiablePlugin(roundTripper, pluginMgr.URL)

			pluginList, err := pluginMgr.GetAvailablePlugins()
			Expect(err).To(BeNil())
			Expect(pluginList).NotTo(BeNil())
			Expect(len(pluginList.Data)).To(Equal(6))
			Expect(pluginList.Data[0].Name).To(Equal("fake-ocean"))
		})

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		It("response with 500", func() {
			request, _ := http.NewRequest("GET", fmt.Sprintf("%s/pluginManager/plugins", pluginMgr.URL), nil)
			response := &http.Response{
				StatusCode: 500,
				Proto:      "HTTP/1.1",
				Request:    request,
				Body:       ioutil.NopCloser(bytes.NewBufferString("")),
			}
			roundTripper.EXPECT().
				RoundTrip(request).Return(response, nil)

			_, err := pluginMgr.GetAvailablePlugins()
			Expect(err).To(HaveOccurred())
		})
	})

	Context("GetPlugins", func() {
		It("no plugin in the list", func() {
102
			PrepareForEmptyInstalledPluginList(roundTripper, pluginMgr.URL)
103 104 105 106 107 108 109 110

			pluginList, err := pluginMgr.GetPlugins()
			Expect(err).To(BeNil())
			Expect(pluginList).NotTo(BeNil())
			Expect(len(pluginList.Plugins)).To(Equal(0))
		})

		It("one plugin in the list", func() {
111
			PrepareForOneInstalledPlugin(roundTripper, pluginMgr.URL)
112 113 114 115 116 117 118 119 120

			pluginList, err := pluginMgr.GetPlugins()
			Expect(err).To(BeNil())
			Expect(pluginList).NotTo(BeNil())
			Expect(len(pluginList.Plugins)).To(Equal(1))
			Expect(pluginList.Plugins[0].ShortName).To(Equal("fake"))
		})

		It("response with 500", func() {
121
			PrepareFor500InstalledPluginList(roundTripper, pluginMgr.URL)
122 123 124 125 126 127

			_, err := pluginMgr.GetPlugins()
			Expect(err).To(HaveOccurred())
		})
	})

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	Context("InstallPlugin", func() {
		var (
			pluginName string
		)

		BeforeEach(func() {
			pluginName = "fake"
		})

		It("normal case, should success", func() {
			PrepareForInstallPlugin(roundTripper, pluginMgr.URL, pluginName, "", "")

			err := pluginMgr.InstallPlugin([]string{pluginName})
			Expect(err).To(BeNil())
		})
143

144 145 146 147 148 149 150
		It("normal case with version, should success", func() {
			PrepareForInstallPluginWithVersion(roundTripper, pluginMgr.URL, pluginName, "1.0", "", "")

			err := pluginMgr.InstallPlugin([]string{pluginName + "@" + "1.0"})
			Expect(err).To(BeNil())
		})

151 152 153 154 155 156 157
		It("with 400", func() {
			PrepareForInstallPluginWithCode(roundTripper, 400, pluginMgr.URL, pluginName, "", "")

			err := pluginMgr.InstallPlugin([]string{pluginName})
			Expect(err).NotTo(BeNil())
		})

158 159 160 161 162 163 164
		It("with 400 & version", func() {
			PrepareForInstallPluginWithCode(roundTripper, 400, pluginMgr.URL, pluginName + "@" + "1.0", "", "")

			err := pluginMgr.InstallPlugin([]string{pluginName + "@" + "1.0"})
			Expect(err).NotTo(BeNil())
		})

165 166 167 168 169 170 171 172 173
		It("with 400, error message", func() {
			response := PrepareForInstallPluginWithCode(roundTripper, 400, pluginMgr.URL, pluginName, "", "")
			response.Header = map[string][]string{
				"X-Error": []string{"X-Error"},
			}

			err := pluginMgr.InstallPlugin([]string{pluginName})
			Expect(err).To(Equal(fmt.Errorf("X-Error")))
		})
174 175
	})

176 177 178 179 180 181 182 183 184 185
	Context("UninstallPlugin", func() {
		var (
			pluginName string
		)

		BeforeEach(func() {
			pluginName = "fake"
		})

		It("normal case, should success", func() {
186
			PrepareForUninstallPlugin(roundTripper, pluginMgr.URL, pluginName)
187 188 189 190 191 192

			err := pluginMgr.UninstallPlugin(pluginName)
			Expect(err).To(BeNil())
		})

		It("response with 500", func() {
193
			PrepareForUninstallPluginWith500(roundTripper, pluginMgr.URL, pluginName)
194 195 196 197 198

			err := pluginMgr.UninstallPlugin(pluginName)
			Expect(err).To(HaveOccurred())
		})
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
199 200 201 202 203 204 205 206

	Context("Upload", func() {
		It("normal case, should success", func() {
			tmpfile, err := ioutil.TempFile("", "example")
			Expect(err).To(BeNil())

			PrepareForUploadPlugin(roundTripper, pluginMgr.URL)

LinuxSuRen's avatar
LinuxSuRen 已提交
207 208
			err = pluginMgr.Upload(tmpfile.Name())
			Expect(err).NotTo(HaveOccurred())
LinuxSuRen's avatar
LinuxSuRen 已提交
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

	Context("UpdateCenter", func() {
		It("normal case, should success", func() {
			PrepareForRequestUpdateCenter(roundTripper, pluginMgr.URL)

			site, err := updateMgr.GetSite()
			Expect(err).To(BeNil())
			Expect(site).NotTo(BeNil())
			Expect(site.ID).To(Equal("default"))
		})
	})

	Context("NullUpdateCenter", func() {
		It("normal case, should success", func() {
			PrepareForNoAvailablePlugins(roundTripper, pluginMgr.URL)

			site, err := updateMgr.GetSite()
			Expect(err).To(BeNil())
			Expect(site).NotTo(BeNil())
			Expect(site.ID).To(Equal("default"))
		})
	})

	Context("ManyInstalledPlugins", func() {
		It("normal case, should success", func() {
			PrepareForManyInstalledPlugins(roundTripper, pluginMgr.URL)

			pluginList, err := pluginMgr.GetPlugins()
			Expect(err).To(BeNil())
			Expect(pluginList).NotTo(BeNil())
			Expect(len(pluginList.Plugins)).To(Equal(4))
			Expect(pluginList.Plugins[0].ShortName).To(Equal("fake-ocean"))
		})
	})

	Context("500UpdateCenter", func() {
		It("normal case, should success", func() {
			PrepareForRequest500UpdateCenter(roundTripper, pluginMgr.URL)

			_, err := updateMgr.GetSite()
			Expect(err).To(HaveOccurred())
		})
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
254 255 256 257 258 259 260 261 262 263 264

	Context("CheckUpdate", func() {
		It("normal case, should success", func() {
			PrepareCheckUpdate(roundTripper, pluginMgr.URL, "", "")

			err := pluginMgr.CheckUpdate(func(_ *http.Response) {
				// do nothing
			})
			Expect(err).To(BeNil())
		})
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
265
})