Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to download Jenkins (#129)
* Add support to download Jenkins * Add test cases for http downloader * Exclued mock files * Exclued mock files * Add test cases for center download * Remove mock files * Fix gen-mock * Add init stage for building * Add test case for jenkins upgrade cmd * Add test casee for center watch cmd * Add test case for center cmd
- Loading branch information
1 parent
92ab7a6
commit 8c3052f
Showing
22 changed files
with
804 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ bin/ | |
release/ | ||
|
||
*.xml | ||
|
||
mock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/jenkins-zh/jenkins-cli/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// CenterDownloadOption as the options of download command | ||
type CenterDownloadOption struct { | ||
LTS bool | ||
Output string | ||
|
||
RoundTripper http.RoundTripper | ||
} | ||
|
||
var centerDownloadOption CenterDownloadOption | ||
|
||
func init() { | ||
centerCmd.AddCommand(centerDownloadCmd) | ||
centerDownloadCmd.Flags().BoolVarP(¢erDownloadOption.LTS, "lts", "", true, "If you want to download Jenkins as LTS") | ||
centerDownloadCmd.Flags().StringVarP(¢erDownloadOption.Output, "output", "o", "jenkins.war", "The file of output") | ||
} | ||
|
||
var centerDownloadCmd = &cobra.Command{ | ||
Use: "download", | ||
Short: "Download Jenkins", | ||
Long: `Download Jenkins`, | ||
Run: func(_ *cobra.Command, _ []string) { | ||
jclient := &client.UpdateCenterManager{ | ||
JenkinsCore: client.JenkinsCore{ | ||
RoundTripper: centerDownloadOption.RoundTripper, | ||
}, | ||
} | ||
|
||
if err := jclient.DownloadJenkins(centerDownloadOption.LTS, centerDownloadOption.Output); err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/jenkins-zh/jenkins-cli/mock/mhttp" | ||
) | ||
|
||
var _ = Describe("center download command", func() { | ||
var ( | ||
ctrl *gomock.Controller | ||
roundTripper *mhttp.MockRoundTripper | ||
targetFilePath string | ||
responseBody string | ||
) | ||
|
||
BeforeEach(func() { | ||
ctrl = gomock.NewController(GinkgoT()) | ||
roundTripper = mhttp.NewMockRoundTripper(ctrl) | ||
targetFilePath = "jenkins.war" | ||
responseBody = "fake response" | ||
}) | ||
|
||
AfterEach(func() { | ||
rootCmd.SetArgs([]string{}) | ||
os.Remove(targetFilePath) | ||
ctrl.Finish() | ||
}) | ||
|
||
Context("basic cases", func() { | ||
It("should success", func() { | ||
centerDownloadOption.RoundTripper = roundTripper | ||
centerDownloadOption.LTS = false | ||
|
||
request, _ := http.NewRequest("GET", "http://mirrors.jenkins.io/war/latest/jenkins.war", nil) | ||
response := &http.Response{ | ||
StatusCode: 200, | ||
Proto: "HTTP/1.1", | ||
Header: http.Header{}, | ||
Request: request, | ||
Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), | ||
} | ||
roundTripper.EXPECT(). | ||
RoundTrip(request).Return(response, nil) | ||
|
||
rootCmd.SetArgs([]string{"center", "download"}) | ||
_, err := rootCmd.ExecuteC() | ||
Expect(err).To(BeNil()) | ||
|
||
_, err = os.Stat(targetFilePath) | ||
Expect(err).To(BeNil()) | ||
|
||
content, readErr := ioutil.ReadFile(targetFilePath) | ||
Expect(readErr).To(BeNil()) | ||
Expect(string(content)).To(Equal(responseBody)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/jenkins-zh/jenkins-cli/mock/mhttp" | ||
) | ||
|
||
var _ = Describe("center command", func() { | ||
var ( | ||
ctrl *gomock.Controller | ||
roundTripper *mhttp.MockRoundTripper | ||
) | ||
|
||
BeforeEach(func() { | ||
ctrl = gomock.NewController(GinkgoT()) | ||
roundTripper = mhttp.NewMockRoundTripper(ctrl) | ||
rootCmd.SetArgs([]string{}) | ||
rootOptions.Jenkins = "" | ||
rootOptions.ConfigFile = "test.yaml" | ||
|
||
centerOption.RoundTripper = roundTripper | ||
}) | ||
|
||
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()) | ||
|
||
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/api/json", nil) | ||
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") | ||
responseCrumb := &http.Response{ | ||
StatusCode: 200, | ||
Proto: "HTTP/1.1", | ||
Request: requestCrumb, | ||
Body: ioutil.NopCloser(bytes.NewBufferString(` | ||
{"version":"0"} | ||
`)), | ||
} | ||
roundTripper.EXPECT(). | ||
RoundTrip(requestCrumb).Return(responseCrumb, nil) | ||
|
||
request, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/updateCenter/api/json?pretty=false&depth=1", nil) | ||
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") | ||
response := &http.Response{ | ||
StatusCode: 200, | ||
Proto: "HTTP/1.1", | ||
Request: request, | ||
Body: ioutil.NopCloser(bytes.NewBufferString(` | ||
{"RestartRequiredForCompletion":true} | ||
`)), | ||
} | ||
roundTripper.EXPECT(). | ||
RoundTrip(request).Return(response, nil) | ||
|
||
rootCmd.SetArgs([]string{"center"}) | ||
_, err = rootCmd.ExecuteC() | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/jenkins-zh/jenkins-cli/mock/mhttp" | ||
) | ||
|
||
var _ = Describe("center upgrade command", func() { | ||
var ( | ||
ctrl *gomock.Controller | ||
roundTripper *mhttp.MockRoundTripper | ||
) | ||
|
||
BeforeEach(func() { | ||
ctrl = gomock.NewController(GinkgoT()) | ||
roundTripper = mhttp.NewMockRoundTripper(ctrl) | ||
centerUpgradeOption.RoundTripper = roundTripper | ||
rootCmd.SetArgs([]string{}) | ||
rootOptions.Jenkins = "" | ||
rootOptions.ConfigFile = "test.yaml" | ||
}) | ||
|
||
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()) | ||
|
||
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil) | ||
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") | ||
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) | ||
|
||
request, _ := http.NewRequest("POST", "http://localhost:8080/jenkins/updateCenter/upgrade", nil) | ||
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") | ||
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) | ||
|
||
rootCmd.SetArgs([]string{"center", "upgrade"}) | ||
_, err = rootCmd.ExecuteC() | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.