Skip to content

Commit 8c3052f

Browse files
authored
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
1 parent 92ab7a6 commit 8c3052f

22 files changed

+804
-41
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ bin/
1515
release/
1616

1717
*.xml
18+
19+
mock

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ addons:
1717
script:
1818
- make clean
1919
# Execute some tests
20-
- make test
20+
- make init test
2121
# And finally run the SonarQube analysis - read the "sonar-project.properties"
2222
# file to see the specific configuration
2323
- curl -LsS https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.0.0.1744-linux.zip > sonar-scanner-cli-4.0.0.1744-linux.zip

Jenkinsfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ pipeline {
66
}
77

88
stages {
9+
stage('Init') {
10+
steps {
11+
script {
12+
entry.container_x('golang', 'go version'){
13+
sh label: 'make init', script: 'make init'
14+
}
15+
}
16+
}
17+
}
918
stage('Build') {
1019
parallel {
1120
stage('MacOS') {

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ VERSION := dev-$(shell git describe --tags $(shell git rev-list --tags --max-cou
77
BUILDFLAGS = -ldflags "-X github.com/jenkins-zh/jenkins-cli/app.version=$(VERSION) -X github.com/jenkins-zh/jenkins-cli/app.commit=$(COMMIT)"
88
COVERED_MAIN_SRC_FILE=./main
99

10+
gen-mock:
11+
go get github.com/golang/mock/gomock
12+
go install github.com/golang/mock/mockgen
13+
mockgen -destination ./mock/mhttp/roundtripper.go -package mhttp net/http RoundTripper
14+
15+
init: gen-mock
16+
1017
darwin: ## Build for OSX
1118
GO111MODULE=on CGO_ENABLED=$(CGO_ENABLED) GOOS=darwin GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o bin/darwin/$(NAME) $(MAIN_SRC_FILE)
1219
chmod +x bin/darwin/$(NAME)

app/cmd/center.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"log"
6+
"net/http"
67

78
"github.com/jenkins-zh/jenkins-cli/client"
89
"github.com/spf13/cobra"
@@ -11,6 +12,7 @@ import (
1112
type CenterOption struct {
1213
WatchOption
1314

15+
RoundTripper http.RoundTripper
1416
CeneterStatus string
1517
}
1618

@@ -26,14 +28,18 @@ var centerCmd = &cobra.Command{
2628
Long: `Manage your update center`,
2729
Run: func(_ *cobra.Command, _ []string) {
2830
jenkins := getCurrentJenkinsFromOptionsOrDie()
29-
printJenkinsStatus(jenkins)
31+
printJenkinsStatus(jenkins, centerOption.RoundTripper)
3032

31-
printUpdateCenter(jenkins)
33+
printUpdateCenter(jenkins, centerOption.RoundTripper)
3234
},
3335
}
3436

35-
func printUpdateCenter(jenkins *JenkinsServer) {
36-
jclient := &client.UpdateCenterManager{}
37+
func printUpdateCenter(jenkins *JenkinsServer, roundTripper http.RoundTripper) {
38+
jclient := &client.UpdateCenterManager{
39+
JenkinsCore: client.JenkinsCore{
40+
RoundTripper: roundTripper,
41+
},
42+
}
3743
jclient.URL = jenkins.URL
3844
jclient.UserName = jenkins.UserName
3945
jclient.Token = jenkins.Token
@@ -63,8 +69,12 @@ func printUpdateCenter(jenkins *JenkinsServer) {
6369
}
6470
}
6571

66-
func printJenkinsStatus(jenkins *JenkinsServer) {
67-
jclient := &client.JenkinsStatusClient{}
72+
func printJenkinsStatus(jenkins *JenkinsServer, roundTripper http.RoundTripper) {
73+
jclient := &client.JenkinsStatusClient{
74+
JenkinsCore: client.JenkinsCore{
75+
RoundTripper: roundTripper,
76+
},
77+
}
6878
jclient.URL = jenkins.URL
6979
jclient.UserName = jenkins.UserName
7080
jclient.Token = jenkins.Token

app/cmd/center_download.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cmd
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/jenkins-zh/jenkins-cli/client"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// CenterDownloadOption as the options of download command
12+
type CenterDownloadOption struct {
13+
LTS bool
14+
Output string
15+
16+
RoundTripper http.RoundTripper
17+
}
18+
19+
var centerDownloadOption CenterDownloadOption
20+
21+
func init() {
22+
centerCmd.AddCommand(centerDownloadCmd)
23+
centerDownloadCmd.Flags().BoolVarP(&centerDownloadOption.LTS, "lts", "", true, "If you want to download Jenkins as LTS")
24+
centerDownloadCmd.Flags().StringVarP(&centerDownloadOption.Output, "output", "o", "jenkins.war", "The file of output")
25+
}
26+
27+
var centerDownloadCmd = &cobra.Command{
28+
Use: "download",
29+
Short: "Download Jenkins",
30+
Long: `Download Jenkins`,
31+
Run: func(_ *cobra.Command, _ []string) {
32+
jclient := &client.UpdateCenterManager{
33+
JenkinsCore: client.JenkinsCore{
34+
RoundTripper: centerDownloadOption.RoundTripper,
35+
},
36+
}
37+
38+
if err := jclient.DownloadJenkins(centerDownloadOption.LTS, centerDownloadOption.Output); err != nil {
39+
log.Fatal(err)
40+
}
41+
},
42+
}

app/cmd/center_download_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 download command", func() {
17+
var (
18+
ctrl *gomock.Controller
19+
roundTripper *mhttp.MockRoundTripper
20+
targetFilePath string
21+
responseBody string
22+
)
23+
24+
BeforeEach(func() {
25+
ctrl = gomock.NewController(GinkgoT())
26+
roundTripper = mhttp.NewMockRoundTripper(ctrl)
27+
targetFilePath = "jenkins.war"
28+
responseBody = "fake response"
29+
})
30+
31+
AfterEach(func() {
32+
rootCmd.SetArgs([]string{})
33+
os.Remove(targetFilePath)
34+
ctrl.Finish()
35+
})
36+
37+
Context("basic cases", func() {
38+
It("should success", func() {
39+
centerDownloadOption.RoundTripper = roundTripper
40+
centerDownloadOption.LTS = false
41+
42+
request, _ := http.NewRequest("GET", "http://mirrors.jenkins.io/war/latest/jenkins.war", nil)
43+
response := &http.Response{
44+
StatusCode: 200,
45+
Proto: "HTTP/1.1",
46+
Header: http.Header{},
47+
Request: request,
48+
Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)),
49+
}
50+
roundTripper.EXPECT().
51+
RoundTrip(request).Return(response, nil)
52+
53+
rootCmd.SetArgs([]string{"center", "download"})
54+
_, err := rootCmd.ExecuteC()
55+
Expect(err).To(BeNil())
56+
57+
_, err = os.Stat(targetFilePath)
58+
Expect(err).To(BeNil())
59+
60+
content, readErr := ioutil.ReadFile(targetFilePath)
61+
Expect(readErr).To(BeNil())
62+
Expect(string(content)).To(Equal(responseBody))
63+
})
64+
})
65+
})

app/cmd/center_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 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+
rootCmd.SetArgs([]string{})
26+
rootOptions.Jenkins = ""
27+
rootOptions.ConfigFile = "test.yaml"
28+
29+
centerOption.RoundTripper = roundTripper
30+
})
31+
32+
AfterEach(func() {
33+
rootCmd.SetArgs([]string{})
34+
os.Remove(rootOptions.ConfigFile)
35+
rootOptions.ConfigFile = ""
36+
ctrl.Finish()
37+
})
38+
39+
Context("basic cases", func() {
40+
It("should success", func() {
41+
data, err := generateSampleConfig()
42+
Expect(err).To(BeNil())
43+
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
44+
Expect(err).To(BeNil())
45+
46+
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/api/json", nil)
47+
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
48+
responseCrumb := &http.Response{
49+
StatusCode: 200,
50+
Proto: "HTTP/1.1",
51+
Request: requestCrumb,
52+
Body: ioutil.NopCloser(bytes.NewBufferString(`
53+
{"version":"0"}
54+
`)),
55+
}
56+
roundTripper.EXPECT().
57+
RoundTrip(requestCrumb).Return(responseCrumb, nil)
58+
59+
request, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/updateCenter/api/json?pretty=false&depth=1", nil)
60+
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
61+
response := &http.Response{
62+
StatusCode: 200,
63+
Proto: "HTTP/1.1",
64+
Request: request,
65+
Body: ioutil.NopCloser(bytes.NewBufferString(`
66+
{"RestartRequiredForCompletion":true}
67+
`)),
68+
}
69+
roundTripper.EXPECT().
70+
RoundTrip(request).Return(response, nil)
71+
72+
rootCmd.SetArgs([]string{"center"})
73+
_, err = rootCmd.ExecuteC()
74+
Expect(err).To(BeNil())
75+
})
76+
})
77+
})

app/cmd/center_upgrade.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ package cmd
22

33
import (
44
"log"
5+
"net/http"
56

67
"github.com/jenkins-zh/jenkins-cli/client"
78
"github.com/spf13/cobra"
89
)
910

11+
// CenterUpgradeOption option for upgrade Jenkins
12+
type CenterUpgradeOption struct {
13+
RoundTripper http.RoundTripper
14+
}
15+
16+
var centerUpgradeOption CenterUpgradeOption
17+
1018
func init() {
1119
centerCmd.AddCommand(centerUpgradeCmd)
1220
}
@@ -18,7 +26,11 @@ var centerUpgradeCmd = &cobra.Command{
1826
Run: func(_ *cobra.Command, _ []string) {
1927
jenkins := getCurrentJenkinsFromOptionsOrDie()
2028

21-
jclient := &client.UpdateCenterManager{}
29+
jclient := &client.UpdateCenterManager{
30+
JenkinsCore: client.JenkinsCore{
31+
RoundTripper: centerUpgradeOption.RoundTripper,
32+
},
33+
}
2234
jclient.URL = jenkins.URL
2335
jclient.UserName = jenkins.UserName
2436
jclient.Token = jenkins.Token

app/cmd/center_upgrade_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)