|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/golang/mock/gomock" |
| 10 | + . "github.com/onsi/ginkgo" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + |
| 13 | + "github.com/jenkins-zh/jenkins-cli/mock/mhttp" |
| 14 | +) |
| 15 | + |
| 16 | +var _ = Describe("center upgrade command", func() { |
| 17 | + var ( |
| 18 | + ctrl *gomock.Controller |
| 19 | + roundTripper *mhttp.MockRoundTripper |
| 20 | + ) |
| 21 | + |
| 22 | + BeforeEach(func() { |
| 23 | + ctrl = gomock.NewController(GinkgoT()) |
| 24 | + roundTripper = mhttp.NewMockRoundTripper(ctrl) |
| 25 | + centerUpgradeOption.RoundTripper = roundTripper |
| 26 | + rootCmd.SetArgs([]string{}) |
| 27 | + rootOptions.Jenkins = "" |
| 28 | + rootOptions.ConfigFile = "test.yaml" |
| 29 | + }) |
| 30 | + |
| 31 | + AfterEach(func() { |
| 32 | + rootCmd.SetArgs([]string{}) |
| 33 | + os.Remove(rootOptions.ConfigFile) |
| 34 | + rootOptions.ConfigFile = "" |
| 35 | + ctrl.Finish() |
| 36 | + }) |
| 37 | + |
| 38 | + Context("basic cases", func() { |
| 39 | + It("should success", func() { |
| 40 | + data, err := generateSampleConfig() |
| 41 | + Expect(err).To(BeNil()) |
| 42 | + err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) |
| 43 | + Expect(err).To(BeNil()) |
| 44 | + |
| 45 | + requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil) |
| 46 | + requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") |
| 47 | + responseCrumb := &http.Response{ |
| 48 | + StatusCode: 200, |
| 49 | + Proto: "HTTP/1.1", |
| 50 | + Request: requestCrumb, |
| 51 | + Body: ioutil.NopCloser(bytes.NewBufferString(` |
| 52 | + {"crumbRequestField":"CrumbRequestField","crumb":"Crumb"} |
| 53 | + `)), |
| 54 | + } |
| 55 | + roundTripper.EXPECT(). |
| 56 | + RoundTrip(requestCrumb).Return(responseCrumb, nil) |
| 57 | + |
| 58 | + request, _ := http.NewRequest("POST", "http://localhost:8080/jenkins/updateCenter/upgrade", nil) |
| 59 | + request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") |
| 60 | + request.Header.Add("CrumbRequestField", "Crumb") |
| 61 | + response := &http.Response{ |
| 62 | + StatusCode: 200, |
| 63 | + Proto: "HTTP/1.1", |
| 64 | + Request: request, |
| 65 | + Body: ioutil.NopCloser(bytes.NewBufferString("")), |
| 66 | + } |
| 67 | + roundTripper.EXPECT(). |
| 68 | + RoundTrip(request).Return(response, nil) |
| 69 | + |
| 70 | + rootCmd.SetArgs([]string{"center", "upgrade"}) |
| 71 | + _, err = rootCmd.ExecuteC() |
| 72 | + Expect(err).To(BeNil()) |
| 73 | + }) |
| 74 | + }) |
| 75 | +}) |
0 commit comments