pluginManager_test.go 6.2 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 41 42 43 44 45 46 47 48 49 50 51 52
	})

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

	Context("basic function test", func() {
		It("get install plugin query string", func() {
			names := make([]string, 0)
			Expect(getPluginsInstallQuery(names)).To(Equal(""))

			names = append(names, "abc")
			Expect(getPluginsInstallQuery(names)).To(Equal("plugin.abc="))

			names = append(names, "def")
			Expect(getPluginsInstallQuery(names)).To(Equal("plugin.abc=&plugin.def="))

			names = append(names, "")
			Expect(getPluginsInstallQuery(names)).To(Equal("plugin.abc=&plugin.def="))
		})
	})
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 151 152 153 154 155 156 157 158 159

		It("with 400", func() {
			PrepareForInstallPluginWithCode(roundTripper, 400, pluginMgr.URL, pluginName, "", "")

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

		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")))
		})
160 161
	})

162 163 164 165 166 167 168 169 170 171
	Context("UninstallPlugin", func() {
		var (
			pluginName string
		)

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

		It("normal case, should success", func() {
172
			PrepareForUninstallPlugin(roundTripper, pluginMgr.URL, pluginName)
173 174 175 176 177 178

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

		It("response with 500", func() {
179
			PrepareForUninstallPluginWith500(roundTripper, pluginMgr.URL, pluginName)
180 181 182 183 184

			err := pluginMgr.UninstallPlugin(pluginName)
			Expect(err).To(HaveOccurred())
		})
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
185 186 187 188 189 190 191 192 193 194 195

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

			PrepareForUploadPlugin(roundTripper, pluginMgr.URL)

			pluginMgr.Upload(tmpfile.Name())
		})
	})
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

	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 已提交
239
})