From 4675156e2c763f7e8089465102b2f6db6f53dc69 Mon Sep 17 00:00:00 2001 From: Rick Date: Wed, 4 Sep 2019 10:24:02 +0800 Subject: [PATCH] Fix the issue that cannot uninstall pluing --- app/cmd/plugin_uninstall.go | 24 ++++++---- app/cmd/plugin_uninstall_test.go | 81 ++++++++++++++++++++++++++++++++ client/common_test.go | 15 ------ client/pluginManager_test.go | 30 +----------- client/pluginManger.go | 6 +-- client/test_methods.go | 44 +++++++++++++++++ sonar-project.properties | 2 +- 7 files changed, 145 insertions(+), 57 deletions(-) create mode 100644 app/cmd/plugin_uninstall_test.go diff --git a/app/cmd/plugin_uninstall.go b/app/cmd/plugin_uninstall.go index eb945fa..4d475b1 100644 --- a/app/cmd/plugin_uninstall.go +++ b/app/cmd/plugin_uninstall.go @@ -1,12 +1,19 @@ package cmd import ( - "log" + "net/http" "github.com/jenkins-zh/jenkins-cli/client" "github.com/spf13/cobra" ) +// PluginUninstallOption the option of uninstall a plugin +type PluginUninstallOption struct { + RoundTripper http.RoundTripper +} + +var pluginUninstallOption PluginUninstallOption + func init() { pluginCmd.AddCommand(pluginUninstallCmd) } @@ -24,16 +31,15 @@ var pluginUninstallCmd = &cobra.Command{ pluginName = args[0] - jenkins := getCurrentJenkinsFromOptionsOrDie() - jclient := &client.PluginManager{} - jclient.URL = jenkins.URL - jclient.UserName = jenkins.UserName - jclient.Token = jenkins.Token - jclient.Proxy = jenkins.Proxy - jclient.ProxyAuth = jenkins.ProxyAuth + jclient := &client.PluginManager{ + JenkinsCore: client.JenkinsCore{ + RoundTripper: pluginUninstallOption.RoundTripper, + }, + } + getCurrentJenkinsAndClient(&(jclient.JenkinsCore)) if err := jclient.UninstallPlugin(pluginName); err != nil { - log.Fatal(err) + cmd.PrintErr(err) } }, } diff --git a/app/cmd/plugin_uninstall_test.go b/app/cmd/plugin_uninstall_test.go new file mode 100644 index 0000000..7765d98 --- /dev/null +++ b/app/cmd/plugin_uninstall_test.go @@ -0,0 +1,81 @@ +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 uninstall command", func() { + var ( + ctrl *gomock.Controller + roundTripper *mhttp.MockRoundTripper + pluginName string + ) + + BeforeEach(func() { + ctrl = gomock.NewController(GinkgoT()) + roundTripper = mhttp.NewMockRoundTripper(ctrl) + pluginUninstallOption.RoundTripper = roundTripper + rootCmd.SetArgs([]string{}) + rootOptions.Jenkins = "" + rootOptions.ConfigFile = "test.yaml" + pluginName = "fake" + }) + + AfterEach(func() { + rootCmd.SetArgs([]string{}) + os.Remove(rootOptions.ConfigFile) + rootOptions.ConfigFile = "" + ctrl.Finish() + }) + + Context("basic cases", func() { + It("should success", func() { + data, err := generateSampleConfig() + Expect(err).To(BeNil()) + err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) + Expect(err).To(BeNil()) + + request, _, requestCrumb, _ := client.PrepareForUninstallPlugin(roundTripper, "http://localhost:8080/jenkins", pluginName) + request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") + requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") + + rootCmd.SetArgs([]string{"plugin", "uninstall", pluginName}) + + buf := new(bytes.Buffer) + rootCmd.SetOutput(buf) + _, err = rootCmd.ExecuteC() + Expect(err).To(BeNil()) + + Expect(buf.String()).To(Equal("")) + }) + + It("with error", func() { + data, err := generateSampleConfig() + Expect(err).To(BeNil()) + err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) + Expect(err).To(BeNil()) + + request, _, requestCrumb, _ := client.PrepareForUninstallPluginWith500(roundTripper, "http://localhost:8080/jenkins", pluginName) + request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") + requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") + + rootCmd.SetArgs([]string{"plugin", "uninstall", pluginName}) + + buf := new(bytes.Buffer) + rootCmd.SetOutput(buf) + _, err = rootCmd.ExecuteC() + Expect(err).To(BeNil()) + + Expect(buf.String()).To(Equal("unexpected status code: 500")) + }) + }) +}) diff --git a/client/common_test.go b/client/common_test.go index d02d79a..3f082a2 100644 --- a/client/common_test.go +++ b/client/common_test.go @@ -141,18 +141,3 @@ var _ = Describe("common test", func() { }) }) }) - -// RequestCrumb only for the test case -func RequestCrumb(roundTripper *mhttp.MockRoundTripper, rootURL string) { - requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", rootURL, "/crumbIssuer/api/json"), nil) - responseCrumb := &http.Response{ - StatusCode: 200, - Proto: "HTTP/1.1", - Request: requestCrumb, - Body: ioutil.NopCloser(bytes.NewBufferString(` - {"crumbRequestField":"CrumbRequestField","crumb":"Crumb"} - `)), - } - roundTripper.EXPECT(). - RoundTrip(requestCrumb).Return(responseCrumb, nil) -} diff --git a/client/pluginManager_test.go b/client/pluginManager_test.go index 8d25fa0..4285234 100644 --- a/client/pluginManager_test.go +++ b/client/pluginManager_test.go @@ -113,48 +113,22 @@ var _ = Describe("PluginManager test", func() { 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) + PrepareForUninstallPlugin(roundTripper, pluginMgr.URL, pluginName) 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) + PrepareForUninstallPluginWith500(roundTripper, pluginMgr.URL, pluginName) err := pluginMgr.UninstallPlugin(pluginName) Expect(err).To(HaveOccurred()) diff --git a/client/pluginManger.go b/client/pluginManger.go index 21f99cf..f641bf6 100644 --- a/client/pluginManger.go +++ b/client/pluginManger.go @@ -189,16 +189,14 @@ func (p *PluginManager) InstallPlugin(names []string) (err error) { // UninstallPlugin uninstall a plugin by name func (p *PluginManager) UninstallPlugin(name string) (err error) { - api := fmt.Sprintf("/pluginManager/plugin/%s/uninstall", name) + api := fmt.Sprintf("/pluginManager/plugin/%s/doUninstall", name) var ( statusCode int data []byte ) if statusCode, data, err = p.Request("POST", api, nil, nil); err == nil { - if statusCode == 200 { - fmt.Println("uninstall succeed.") - } else { + if statusCode != 200 { err = fmt.Errorf("unexpected status code: %d", statusCode) if p.Debug { ioutil.WriteFile("debug.html", data, 0664) diff --git a/client/test_methods.go b/client/test_methods.go index 6da0fea..cef57bb 100644 --- a/client/test_methods.go +++ b/client/test_methods.go @@ -88,3 +88,47 @@ func PrepareFor500InstalledPluginList(roundTripper *mhttp.MockRoundTripper, root response.StatusCode = 500 return } + +// PrepareForUninstallPlugin only for test +func PrepareForUninstallPlugin(roundTripper *mhttp.MockRoundTripper, rootURL, pluginName string) ( + request *http.Request, response *http.Response, requestCrumb *http.Request, responseCrumb *http.Response) { + request, _ = http.NewRequest("POST", fmt.Sprintf("%s/pluginManager/plugin/%s/doUninstall", rootURL, pluginName), 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, responseCrumb = RequestCrumb(roundTripper, rootURL) + return +} + +// PrepareForUninstallPluginWith500 only for test +func PrepareForUninstallPluginWith500(roundTripper *mhttp.MockRoundTripper, rootURL, pluginName string) ( + request *http.Request, response *http.Response, requestCrumb *http.Request, responseCrumb *http.Response) { + request, response, requestCrumb, responseCrumb = PrepareForUninstallPlugin(roundTripper, rootURL, pluginName) + response.StatusCode = 500 + return +} + +// RequestCrumb only for the test case +func RequestCrumb(roundTripper *mhttp.MockRoundTripper, rootURL string) ( + requestCrumb *http.Request, responseCrumb *http.Response) { + requestCrumb, _ = http.NewRequest("GET", fmt.Sprintf("%s%s", rootURL, "/crumbIssuer/api/json"), nil) + responseCrumb = &http.Response{ + StatusCode: 200, + Proto: "HTTP/1.1", + Request: requestCrumb, + Body: ioutil.NopCloser(bytes.NewBufferString(` + {"crumbRequestField":"CrumbRequestField","crumb":"Crumb"} + `)), + } + roundTripper.EXPECT(). + RoundTrip(requestCrumb).Return(responseCrumb, nil) + return +} diff --git a/sonar-project.properties b/sonar-project.properties index 568e70e..a6ea534 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -10,4 +10,4 @@ sonar.sources=. # Encoding of the source code. Default is default system encoding #sonar.sourceEncoding=UTF-8 -sonar.go.exclusions=**/vendor/**,**/**/*_test.go,mock/**/** +sonar.go.exclusions=**/vendor/**,**/**/*_test.go,client/test_methods.go -- GitLab