pluginManager_test.go 4.3 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
LinuxSuRen's avatar
LinuxSuRen 已提交
20 21 22 23
	)

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

	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="))
		})
	})
49 50 51 52 53 54 55 56 57 58 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 87

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

		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() {
88
			PrepareForEmptyInstalledPluginList(roundTripper, pluginMgr.URL)
89 90 91 92 93 94 95 96

			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() {
97
			PrepareForOneInstalledPlugin(roundTripper, pluginMgr.URL)
98 99 100 101 102 103 104 105 106

			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() {
107
			PrepareFor500InstalledPluginList(roundTripper, pluginMgr.URL)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

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

	Context("UninstallPlugin", func() {
		var (
			api        string
			pluginName string
		)

		BeforeEach(func() {
			pluginName = "fake"
			api = fmt.Sprintf("%s/pluginManager/plugin/%s/uninstall", pluginMgr.URL, pluginName)
		})

		It("normal case, should success", func() {
			request, _ := http.NewRequest("POST", api, nil)
			request.Header.Add("CrumbRequestField", "Crumb")
			response := &http.Response{
				StatusCode: 200,
				Proto:      "HTTP/1.1",
				Request:    request,
				Body:       ioutil.NopCloser(bytes.NewBufferString("")),
			}
			roundTripper.EXPECT().
				RoundTrip(request).Return(response, nil)

			// common crumb request
			RequestCrumb(roundTripper, pluginMgr.URL)

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

		It("response with 500", func() {
			request, _ := http.NewRequest("POST", api, nil)
			request.Header.Add("CrumbRequestField", "Crumb")
			response := &http.Response{
				StatusCode: 500,
				Proto:      "HTTP/1.1",
				Request:    request,
				Body:       ioutil.NopCloser(bytes.NewBufferString("")),
			}
			roundTripper.EXPECT().
				RoundTrip(request).Return(response, nil)

			// common crumb request
			RequestCrumb(roundTripper, pluginMgr.URL)

			err := pluginMgr.UninstallPlugin(pluginName)
			Expect(err).To(HaveOccurred())
		})
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
163
})