Skip to content

Commit

Permalink
Add support to download Jenkins (#129)
Browse files Browse the repository at this point in the history
* 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
LinuxSuRen committed Aug 29, 2019
1 parent 92ab7a6 commit 8c3052f
Show file tree
Hide file tree
Showing 22 changed files with 804 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -15,3 +15,5 @@ bin/
release/

*.xml

mock
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -17,7 +17,7 @@ addons:
script:
- make clean
# Execute some tests
- make test
- make init test
# And finally run the SonarQube analysis - read the "sonar-project.properties"
# file to see the specific configuration
- 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
Expand Down
9 changes: 9 additions & 0 deletions Jenkinsfile
Expand Up @@ -6,6 +6,15 @@ pipeline {
}

stages {
stage('Init') {
steps {
script {
entry.container_x('golang', 'go version'){
sh label: 'make init', script: 'make init'
}
}
}
}
stage('Build') {
parallel {
stage('MacOS') {
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Expand Up @@ -7,6 +7,13 @@ VERSION := dev-$(shell git describe --tags $(shell git rev-list --tags --max-cou
BUILDFLAGS = -ldflags "-X github.com/jenkins-zh/jenkins-cli/app.version=$(VERSION) -X github.com/jenkins-zh/jenkins-cli/app.commit=$(COMMIT)"
COVERED_MAIN_SRC_FILE=./main

gen-mock:
go get github.com/golang/mock/gomock
go install github.com/golang/mock/mockgen
mockgen -destination ./mock/mhttp/roundtripper.go -package mhttp net/http RoundTripper

init: gen-mock

darwin: ## Build for OSX
GO111MODULE=on CGO_ENABLED=$(CGO_ENABLED) GOOS=darwin GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o bin/darwin/$(NAME) $(MAIN_SRC_FILE)
chmod +x bin/darwin/$(NAME)
Expand Down
22 changes: 16 additions & 6 deletions app/cmd/center.go
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"log"
"net/http"

"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra"
Expand All @@ -11,6 +12,7 @@ import (
type CenterOption struct {
WatchOption

RoundTripper http.RoundTripper
CeneterStatus string
}

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

printUpdateCenter(jenkins)
printUpdateCenter(jenkins, centerOption.RoundTripper)
},
}

func printUpdateCenter(jenkins *JenkinsServer) {
jclient := &client.UpdateCenterManager{}
func printUpdateCenter(jenkins *JenkinsServer, roundTripper http.RoundTripper) {
jclient := &client.UpdateCenterManager{
JenkinsCore: client.JenkinsCore{
RoundTripper: roundTripper,
},
}
jclient.URL = jenkins.URL
jclient.UserName = jenkins.UserName
jclient.Token = jenkins.Token
Expand Down Expand Up @@ -63,8 +69,12 @@ func printUpdateCenter(jenkins *JenkinsServer) {
}
}

func printJenkinsStatus(jenkins *JenkinsServer) {
jclient := &client.JenkinsStatusClient{}
func printJenkinsStatus(jenkins *JenkinsServer, roundTripper http.RoundTripper) {
jclient := &client.JenkinsStatusClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: roundTripper,
},
}
jclient.URL = jenkins.URL
jclient.UserName = jenkins.UserName
jclient.Token = jenkins.Token
Expand Down
42 changes: 42 additions & 0 deletions app/cmd/center_download.go
@@ -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(&centerDownloadOption.LTS, "lts", "", true, "If you want to download Jenkins as LTS")
centerDownloadCmd.Flags().StringVarP(&centerDownloadOption.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)
}
},
}
65 changes: 65 additions & 0 deletions app/cmd/center_download_test.go
@@ -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))
})
})
})
77 changes: 77 additions & 0 deletions app/cmd/center_test.go
@@ -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())
})
})
})
14 changes: 13 additions & 1 deletion app/cmd/center_upgrade.go
Expand Up @@ -2,11 +2,19 @@ package cmd

import (
"log"
"net/http"

"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra"
)

// CenterUpgradeOption option for upgrade Jenkins
type CenterUpgradeOption struct {
RoundTripper http.RoundTripper
}

var centerUpgradeOption CenterUpgradeOption

func init() {
centerCmd.AddCommand(centerUpgradeCmd)
}
Expand All @@ -18,7 +26,11 @@ var centerUpgradeCmd = &cobra.Command{
Run: func(_ *cobra.Command, _ []string) {
jenkins := getCurrentJenkinsFromOptionsOrDie()

jclient := &client.UpdateCenterManager{}
jclient := &client.UpdateCenterManager{
JenkinsCore: client.JenkinsCore{
RoundTripper: centerUpgradeOption.RoundTripper,
},
}
jclient.URL = jenkins.URL
jclient.UserName = jenkins.UserName
jclient.Token = jenkins.Token
Expand Down
75 changes: 75 additions & 0 deletions app/cmd/center_upgrade_test.go
@@ -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())
})
})
})

0 comments on commit 8c3052f

Please sign in to comment.