未验证 提交 2b5e4ca3 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Add support to clean invalid config item (#383)

* Add support to clean invalid config item

* Fix the package issues

* Fix some issues found by hound

* Fixes the test errors
上级 8c112e01
...@@ -3,6 +3,7 @@ package cmd ...@@ -3,6 +3,7 @@ package cmd
import ( import (
"bytes" "bytes"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/jenkins-zh/jenkins-cli/client"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"io/ioutil" "io/ioutil"
...@@ -66,7 +67,7 @@ var _ = Describe("center download command", func() { ...@@ -66,7 +67,7 @@ var _ = Describe("center download command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(ltsResponseBody)), Body: ioutil.NopCloser(bytes.NewBufferString(ltsResponseBody)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
rootCmd.SetArgs([]string{"center", "download", "--progress=false", "--output", targetFilePath}) rootCmd.SetArgs([]string{"center", "download", "--progress=false", "--output", targetFilePath})
_, err := rootCmd.ExecuteC() _, err := rootCmd.ExecuteC()
...@@ -88,7 +89,7 @@ var _ = Describe("center download command", func() { ...@@ -88,7 +89,7 @@ var _ = Describe("center download command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(weeklyResponseBody)), Body: ioutil.NopCloser(bytes.NewBufferString(weeklyResponseBody)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
rootCmd.SetArgs([]string{"center", "download", "--lts=false", "--progress=false", "--output", targetFilePath}) rootCmd.SetArgs([]string{"center", "download", "--lts=false", "--progress=false", "--output", targetFilePath})
_, err := rootCmd.ExecuteC() _, err := rootCmd.ExecuteC()
......
...@@ -2,6 +2,7 @@ package cmd ...@@ -2,6 +2,7 @@ package cmd
import ( import (
"bytes" "bytes"
"github.com/jenkins-zh/jenkins-cli/client"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
...@@ -54,7 +55,7 @@ var _ = Describe("center command", func() { ...@@ -54,7 +55,7 @@ var _ = Describe("center command", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(client.NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
request, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/updateCenter/api/json?pretty=false&depth=1", nil) request, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/updateCenter/api/json?pretty=false&depth=1", nil)
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
...@@ -67,7 +68,7 @@ var _ = Describe("center command", func() { ...@@ -67,7 +68,7 @@ var _ = Describe("center command", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
rootCmd.SetArgs([]string{"center"}) rootCmd.SetArgs([]string{"center"})
_, err = rootCmd.ExecuteC() _, err = rootCmd.ExecuteC()
......
...@@ -2,6 +2,7 @@ package cmd ...@@ -2,6 +2,7 @@ package cmd
import ( import (
"bytes" "bytes"
"github.com/jenkins-zh/jenkins-cli/client"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
...@@ -53,7 +54,7 @@ var _ = Describe("center upgrade command", func() { ...@@ -53,7 +54,7 @@ var _ = Describe("center upgrade command", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(client.NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
request, _ := http.NewRequest("POST", "http://localhost:8080/jenkins/updateCenter/upgrade", nil) request, _ := http.NewRequest("POST", "http://localhost:8080/jenkins/updateCenter/upgrade", nil)
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
...@@ -65,7 +66,7 @@ var _ = Describe("center upgrade command", func() { ...@@ -65,7 +66,7 @@ var _ = Describe("center upgrade command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
rootCmd.SetArgs([]string{"center", "upgrade"}) rootCmd.SetArgs([]string{"center", "upgrade"})
_, err = rootCmd.ExecuteC() _, err = rootCmd.ExecuteC()
......
...@@ -57,7 +57,7 @@ var _ = Describe("center watch command", func() { ...@@ -57,7 +57,7 @@ var _ = Describe("center watch command", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(client.NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
rootCmd.SetArgs([]string{"center", "watch"}) rootCmd.SetArgs([]string{"center", "watch"})
_, err = rootCmd.ExecuteC() _, err = rootCmd.ExecuteC()
......
...@@ -80,7 +80,7 @@ var _ = Describe("computer launch command", func() { ...@@ -80,7 +80,7 @@ var _ = Describe("computer launch command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(fakeJar)), Body: ioutil.NopCloser(bytes.NewBufferString(fakeJar)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
secret := "fake-secret" secret := "fake-secret"
client.PrepareForComputerAgentSecretRequest(roundTripper, client.PrepareForComputerAgentSecretRequest(roundTripper,
......
package cmd
import (
cfg "github.com/jenkins-zh/jenkins-cli/app/config"
"github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra"
"net"
"net/http"
"time"
)
// ConfigCleanOption option for config list command
type ConfigCleanOption struct {
Timeout int
CleanTimeout bool
Logger helper.Printer
}
var configCleanOption ConfigCleanOption
func init() {
configCmd.AddCommand(configCleanCmd)
configCleanCmd.Flags().IntVarP(&configCleanOption.Timeout, "timeout", "t", 5,
i18n.T("Timeout in second value when checking with the Jenkins URL"))
configCleanCmd.Flags().BoolVarP(&configCleanOption.CleanTimeout, "clean-timeout", "", false,
i18n.T("Clean the config items when timeout with API request"))
}
// CheckResult is the result of checking
type CheckResult struct {
Name string
StatusCode int
Timeout bool
}
var configCleanCmd = &cobra.Command{
Use: "clean",
Short: i18n.T("Clean up some unavailable config items"),
Long: i18n.T("Clean up some unavailable config items"),
RunE: configCleanOption.Run,
}
// Run is the main entry point of command config-clean
func (o *ConfigCleanOption) Run(cmd *cobra.Command, args []string) (err error) {
if config := getConfig(); config == nil {
cmd.Println("cannot found config file")
}
o.Logger = cmd
itemCount := len(config.JenkinsServers)
checkResult := make(chan CheckResult, itemCount)
for _, jenkins := range config.JenkinsServers {
go func(target cfg.JenkinsServer) {
checkResult <- o.Check(target)
}(jenkins)
}
checkResultList := make([]CheckResult, itemCount)
for i := range config.JenkinsServers {
checkResultList[i] = <-checkResult
}
// do the clean work
err = o.CleanByCondition(checkResultList)
cmd.Println()
return
}
// Check check the target JenkinsServer config
// make a request to a Jenkins API
func (o *ConfigCleanOption) Check(jenkins cfg.JenkinsServer) (result CheckResult) {
result.Name = jenkins.Name
jClient := &client.PluginManager{
JenkinsCore: client.JenkinsCore{
RoundTripper: pluginListOption.RoundTripper,
Timeout: time.Duration(o.Timeout),
},
}
jClient.URL = jenkins.URL
jClient.UserName = jenkins.UserName
jClient.Token = jenkins.Token
jClient.Proxy = jenkins.Proxy
jClient.ProxyAuth = jenkins.ProxyAuth
jClient.InsecureSkipVerify = jenkins.InsecureSkipVerify
var statusCode int
var err error
if statusCode, _, err = jClient.Request(http.MethodGet, "/api/json", nil, nil); err != nil {
if err, ok := err.(net.Error); ok {
result.Timeout = err.Timeout()
} else {
o.Logger.Println("check request failed, error is", err)
}
}
result.StatusCode = statusCode
return
}
// CleanByCondition do the clean work by conditions
func (o *ConfigCleanOption) CleanByCondition(resultList []CheckResult) (err error) {
if len(resultList) == 0 {
return
}
for _, result := range resultList {
if o.CleanTimeout && result.Timeout {
if err = removeJenkins(result.Name); err == nil {
o.Logger.Printf("removed invalid item %s due to timeout reason\n", result.Name)
}
} else {
o.Logger.Printf("status code of %s is %d\n", result.Name, result.StatusCode)
}
}
return
}
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/jenkins-zh/jenkins-cli/app/cmd/common" "github.com/jenkins-zh/jenkins-cli/app/cmd/common"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/mock/mhttp" "github.com/jenkins-zh/jenkins-cli/mock/mhttp"
"github.com/jenkins-zh/jenkins-cli/util" "github.com/jenkins-zh/jenkins-cli/util"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
...@@ -103,7 +104,7 @@ func prepareDownloadFileRequest(url, content string, roundTripper *mhttp.MockRou ...@@ -103,7 +104,7 @@ func prepareDownloadFileRequest(url, content string, roundTripper *mhttp.MockRou
Body: ioutil.NopCloser(bytes.NewBufferString(content)), Body: ioutil.NopCloser(bytes.NewBufferString(content)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
} }
func prepareMavenMetadataRequest(roundTripper *mhttp.MockRoundTripper) { func prepareMavenMetadataRequest(roundTripper *mhttp.MockRoundTripper) {
...@@ -114,7 +115,7 @@ func prepareMavenMetadataRequest(roundTripper *mhttp.MockRoundTripper) { ...@@ -114,7 +115,7 @@ func prepareMavenMetadataRequest(roundTripper *mhttp.MockRoundTripper) {
Body: ioutil.NopCloser(bytes.NewBufferString(getMavenMetadataSample())), Body: ioutil.NopCloser(bytes.NewBufferString(getMavenMetadataSample())),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
} }
func getMavenMetadataSample() string { func getMavenMetadataSample() string {
......
...@@ -74,7 +74,7 @@ var _ = Describe("job artifact download command", func() { ...@@ -74,7 +74,7 @@ var _ = Describe("job artifact download command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
rootCmd.SetOutput(buf) rootCmd.SetOutput(buf)
......
...@@ -62,7 +62,7 @@ var _ = Describe("job build command", func() { ...@@ -62,7 +62,7 @@ var _ = Describe("job build command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil) requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil)
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
...@@ -75,7 +75,7 @@ var _ = Describe("job build command", func() { ...@@ -75,7 +75,7 @@ var _ = Describe("job build command", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(client.NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
rootCmd.SetArgs([]string{"job", "build", jobName, "-b", "true"}) rootCmd.SetArgs([]string{"job", "build", jobName, "-b", "true"})
_, err = rootCmd.ExecuteC() _, err = rootCmd.ExecuteC()
...@@ -169,7 +169,7 @@ var _ = Describe("job build command", func() { ...@@ -169,7 +169,7 @@ var _ = Describe("job build command", func() {
// `)), // `)),
// } // }
// roundTripper.EXPECT(). // roundTripper.EXPECT().
// RoundTrip(request).Return(response, nil) // RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
// //
// client.PrepareForBuildWithParams(roundTripper, url, jobName, user, token) // client.PrepareForBuildWithParams(roundTripper, url, jobName, user, token)
// //
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/Netflix/go-expect" "github.com/Netflix/go-expect"
"github.com/jenkins-zh/jenkins-cli/app/cmd/common" "github.com/jenkins-zh/jenkins-cli/app/cmd/common"
"github.com/jenkins-zh/jenkins-cli/client"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
...@@ -67,7 +68,7 @@ var _ = Describe("job delete command", func() { ...@@ -67,7 +68,7 @@ var _ = Describe("job delete command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil) requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil)
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
...@@ -80,7 +81,7 @@ var _ = Describe("job delete command", func() { ...@@ -80,7 +81,7 @@ var _ = Describe("job delete command", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(client.NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
rootCmd.SetArgs([]string{"job", "delete", jobName, "-b", "true"}) rootCmd.SetArgs([]string{"job", "delete", jobName, "-b", "true"})
......
...@@ -107,7 +107,7 @@ var _ = Describe("job edit command", func() { ...@@ -107,7 +107,7 @@ var _ = Describe("job edit command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(script)), Body: ioutil.NopCloser(bytes.NewBufferString(script)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(remoteJenkinsfileReq).Return(remoteJenkinsfileResponse, nil) RoundTrip(client.NewRequestMatcher(remoteJenkinsfileReq)).Return(remoteJenkinsfileResponse, nil)
rootCmd.SetArgs([]string{"job", "edit", jobName, "--filename", "", "--script", "", "--url", remoteJenkinsfileURL}) rootCmd.SetArgs([]string{"job", "edit", jobName, "--filename", "", "--script", "", "--url", remoteJenkinsfileURL})
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/jenkins-zh/jenkins-cli/client"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"io/ioutil" "io/ioutil"
...@@ -57,7 +58,7 @@ var _ = Describe("job stop command", func() { ...@@ -57,7 +58,7 @@ var _ = Describe("job stop command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil) requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil)
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
...@@ -70,7 +71,7 @@ var _ = Describe("job stop command", func() { ...@@ -70,7 +71,7 @@ var _ = Describe("job stop command", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(client.NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
rootCmd.SetArgs([]string{"job", "stop", jobName, "1", "-b"}) rootCmd.SetArgs([]string{"job", "stop", jobName, "1", "-b"})
...@@ -99,7 +100,7 @@ var _ = Describe("job stop command", func() { ...@@ -99,7 +100,7 @@ var _ = Describe("job stop command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil) requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil)
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
...@@ -112,7 +113,7 @@ var _ = Describe("job stop command", func() { ...@@ -112,7 +113,7 @@ var _ = Describe("job stop command", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(client.NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
rootCmd.SetArgs([]string{"job", "stop", jobName, "-b"}) rootCmd.SetArgs([]string{"job", "stop", jobName, "-b"})
......
...@@ -54,7 +54,7 @@ var _ = Describe("job type command", func() { ...@@ -54,7 +54,7 @@ var _ = Describe("job type command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(`{"_class":"jenkins.model.item_category.Categories","categories":[{"description":"description","id":"standalone-projects","items":[{"displayName":"Freestyle project","iconFilePathPattern":"static/da605e5f/images/:size/freestyleproject.png","description":"description","iconClassName":"icon-freestyle-project","class":"hudson.model.FreeStyleProject","order":1}],"minToShow":1,"name":"Nested Projects","order":1}]}`)), Body: ioutil.NopCloser(bytes.NewBufferString(`{"_class":"jenkins.model.item_category.Categories","categories":[{"description":"description","id":"standalone-projects","items":[{"displayName":"Freestyle project","iconFilePathPattern":"static/da605e5f/images/:size/freestyleproject.png","description":"description","iconClassName":"icon-freestyle-project","class":"hudson.model.FreeStyleProject","order":1}],"minToShow":1,"name":"Nested Projects","order":1}]}`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
config = &Config{ config = &Config{
Current: "fake", Current: "fake",
...@@ -93,7 +93,7 @@ var _ = Describe("job type command", func() { ...@@ -93,7 +93,7 @@ var _ = Describe("job type command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("{}")), Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
rootCmd.SetArgs([]string{"job", "type"}) rootCmd.SetArgs([]string{"job", "type"})
...@@ -120,7 +120,7 @@ var _ = Describe("job type command", func() { ...@@ -120,7 +120,7 @@ var _ = Describe("job type command", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("{}")), Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
rootCmd.SetArgs([]string{"job", "type"}) rootCmd.SetArgs([]string{"job", "type"})
...@@ -165,7 +165,7 @@ var _ = Describe("job type command", func() { ...@@ -165,7 +165,7 @@ var _ = Describe("job type command", func() {
}`)), }`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
rootCmd.SetOutput(buf) rootCmd.SetOutput(buf)
......
...@@ -10,6 +10,8 @@ import ( ...@@ -10,6 +10,8 @@ import (
// Printer for print the info // Printer for print the info
type Printer interface { type Printer interface {
PrintErr(i ...interface{}) PrintErr(i ...interface{})
Println(i ...interface{})
Printf(format string, i ...interface{})
} }
// CheckErr print a friendly error message // CheckErr print a friendly error message
......
...@@ -86,6 +86,16 @@ func (p *MemoryPrinter) PrintErr(i ...interface{}) { ...@@ -86,6 +86,16 @@ func (p *MemoryPrinter) PrintErr(i ...interface{}) {
p.Buffer.WriteString(fmt.Sprintf("%v", i)) p.Buffer.WriteString(fmt.Sprintf("%v", i))
} }
// Println print the object
func (p *MemoryPrinter) Println(i ...interface{}) {
p.Buffer.WriteString(fmt.Sprintf("%v\n", i))
}
// Printf print against a format
func (p *MemoryPrinter) Printf(format string, i ...interface{}) {
p.Buffer.WriteString(fmt.Sprintf(format, i...))
}
var _ = Describe("CheckErr", func() { var _ = Describe("CheckErr", func() {
var ( var (
printer *MemoryPrinter printer *MemoryPrinter
......
...@@ -26,7 +26,7 @@ func PrepareGetArtifacts(roundTripper *mhttp.MockRoundTripper, rootURL, user, pa ...@@ -26,7 +26,7 @@ func PrepareGetArtifacts(roundTripper *mhttp.MockRoundTripper, rootURL, user, pa
Body: ioutil.NopCloser(bytes.NewBufferString(`[{"id":"n1","name":"a.log","path":"a.log","url":"/job/pipeline/1/artifact/a.log","size":0}]`)), Body: ioutil.NopCloser(bytes.NewBufferString(`[{"id":"n1","name":"a.log","path":"a.log","url":"/job/pipeline/1/artifact/a.log","size":0}]`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && passwd != "" { if user != "" && passwd != "" {
request.SetBasicAuth(user, passwd) request.SetBasicAuth(user, passwd)
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"time"
"github.com/jenkins-zh/jenkins-cli/app" "github.com/jenkins-zh/jenkins-cli/app"
"github.com/jenkins-zh/jenkins-cli/util" "github.com/jenkins-zh/jenkins-cli/util"
...@@ -26,6 +27,7 @@ func SetLanguage(lan string) { ...@@ -26,6 +27,7 @@ func SetLanguage(lan string) {
// JenkinsCore core information of Jenkins // JenkinsCore core information of Jenkins
type JenkinsCore struct { type JenkinsCore struct {
JenkinsCrumb JenkinsCrumb
Timeout time.Duration
URL string URL string
InsecureSkipVerify bool InsecureSkipVerify bool
UserName string UserName string
...@@ -58,7 +60,16 @@ func (j *JenkinsCore) GetClient() (client *http.Client) { ...@@ -58,7 +60,16 @@ func (j *JenkinsCore) GetClient() (client *http.Client) {
} }
roundTripper = tr roundTripper = tr
} }
client = &http.Client{Transport: roundTripper}
// make sure have a default timeout here
if j.Timeout <= 0 {
j.Timeout = 5
}
client = &http.Client{
Transport: roundTripper,
Timeout: j.Timeout * time.Second,
}
return return
} }
......
...@@ -55,7 +55,7 @@ var _ = Describe("common test", func() { ...@@ -55,7 +55,7 @@ var _ = Describe("common test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
statusCode, data, err := jenkinsCore.Request(method, api, headers, payload) statusCode, data, err := jenkinsCore.Request(method, api, headers, payload)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -75,7 +75,7 @@ var _ = Describe("common test", func() { ...@@ -75,7 +75,7 @@ var _ = Describe("common test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jenkinsCore.URL, "/crumbIssuer/api/json"), payload) requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jenkinsCore.URL, "/crumbIssuer/api/json"), payload)
responseCrumb := &http.Response{ responseCrumb := &http.Response{
...@@ -87,7 +87,7 @@ var _ = Describe("common test", func() { ...@@ -87,7 +87,7 @@ var _ = Describe("common test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
headers = make(map[string]string, 1) headers = make(map[string]string, 1)
headers["fake"] = "fake" headers["fake"] = "fake"
...@@ -108,7 +108,7 @@ var _ = Describe("common test", func() { ...@@ -108,7 +108,7 @@ var _ = Describe("common test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
_, err := jenkinsCore.GetCrumb() _, err := jenkinsCore.GetCrumb()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
...@@ -133,7 +133,7 @@ var _ = Describe("common test", func() { ...@@ -133,7 +133,7 @@ var _ = Describe("common test", func() {
// Body: ioutil.NopCloser(bytes.NewBufferString("")), // Body: ioutil.NopCloser(bytes.NewBufferString("")),
//} //}
//roundTripper.EXPECT(). //roundTripper.EXPECT().
// RoundTrip(requestCrumb).Return(responseCrumb, nil) // RoundTrip(NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
PrepareForGetIssuerWith500(roundTripper, jenkinsCore.URL, "", "") PrepareForGetIssuerWith500(roundTripper, jenkinsCore.URL, "", "")
_, err := jenkinsCore.GetCrumb() _, err := jenkinsCore.GetCrumb()
...@@ -159,7 +159,7 @@ var _ = Describe("common test", func() { ...@@ -159,7 +159,7 @@ var _ = Describe("common test", func() {
} }
request.Header.Set("Accept-Language", "zh-CN") request.Header.Set("Accept-Language", "zh-CN")
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
SetLanguage("zh-CN") SetLanguage("zh-CN")
statusCode, data, err := jenkinsCore.Request("GET", "/view/all/itemCategories?depth=3", nil, nil) statusCode, data, err := jenkinsCore.Request("GET", "/view/all/itemCategories?depth=3", nil, nil)
...@@ -199,7 +199,7 @@ var _ = Describe("common test", func() { ...@@ -199,7 +199,7 @@ var _ = Describe("common test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
err := jenkinsCore.CrumbHandle(requestCrumb) err := jenkinsCore.CrumbHandle(requestCrumb)
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("unexpected status code: 500")) Expect(err.Error()).To(Equal("unexpected status code: 500"))
......
...@@ -19,7 +19,7 @@ func PrepareForGetIssuer(roundTripper *mhttp.MockRoundTripper, rootURL, user, pa ...@@ -19,7 +19,7 @@ func PrepareForGetIssuer(roundTripper *mhttp.MockRoundTripper, rootURL, user, pa
Body: ioutil.NopCloser(bytes.NewBufferString(`{"CrumbRequestField":"CrumbRequestField","Crumb":"Crumb"}`)), Body: ioutil.NopCloser(bytes.NewBufferString(`{"CrumbRequestField":"CrumbRequestField","Crumb":"Crumb"}`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" { if user != "" && password != "" {
request.SetBasicAuth(user, password) request.SetBasicAuth(user, password)
} }
......
...@@ -21,7 +21,7 @@ func PrepareForComputerListRequest(roundTripper *mhttp.MockRoundTripper, rootURL ...@@ -21,7 +21,7 @@ func PrepareForComputerListRequest(roundTripper *mhttp.MockRoundTripper, rootURL
Body: ioutil.NopCloser(bytes.NewBufferString(PrepareForComputerList())), Body: ioutil.NopCloser(bytes.NewBufferString(PrepareForComputerList())),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" { if user != "" && password != "" {
request.SetBasicAuth(user, password) request.SetBasicAuth(user, password)
} }
...@@ -48,7 +48,7 @@ func PrepareForComputerLogRequestWithCode(roundTripper *mhttp.MockRoundTripper, ...@@ -48,7 +48,7 @@ func PrepareForComputerLogRequestWithCode(roundTripper *mhttp.MockRoundTripper,
Body: ioutil.NopCloser(bytes.NewBufferString(`fake-log`)), Body: ioutil.NopCloser(bytes.NewBufferString(`fake-log`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" { if user != "" && password != "" {
request.SetBasicAuth(user, password) request.SetBasicAuth(user, password)
} }
......
...@@ -34,7 +34,7 @@ func PrepareForGetIdentity(roundTripper *mhttp.MockRoundTripper, rootURL, user, ...@@ -34,7 +34,7 @@ func PrepareForGetIdentity(roundTripper *mhttp.MockRoundTripper, rootURL, user,
{"fingerprint":"fingerprint","publicKey":"publicKey","systemMessage":"systemMessage"}`)), {"fingerprint":"fingerprint","publicKey":"publicKey","systemMessage":"systemMessage"}`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" { if user != "" && password != "" {
request.SetBasicAuth(user, password) request.SetBasicAuth(user, password)
......
...@@ -23,7 +23,7 @@ func PrepareForGetCredentialList(roundTripper *mhttp.MockRoundTripper, rootURL, ...@@ -23,7 +23,7 @@ func PrepareForGetCredentialList(roundTripper *mhttp.MockRoundTripper, rootURL,
Body: ioutil.NopCloser(bytes.NewBufferString(PrepareForCredentialListJSON())), Body: ioutil.NopCloser(bytes.NewBufferString(PrepareForCredentialListJSON())),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" { if user != "" && password != "" {
request.SetBasicAuth(user, password) request.SetBasicAuth(user, password)
} }
......
...@@ -71,7 +71,7 @@ var _ = Describe("job test", func() { ...@@ -71,7 +71,7 @@ var _ = Describe("job test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil) requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil)
responseCrumb := &http.Response{ responseCrumb := &http.Response{
...@@ -83,7 +83,7 @@ var _ = Describe("job test", func() { ...@@ -83,7 +83,7 @@ var _ = Describe("job test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
err := jobClient.Build(jobName) err := jobClient.Build(jobName)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -100,7 +100,7 @@ var _ = Describe("job test", func() { ...@@ -100,7 +100,7 @@ var _ = Describe("job test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil) requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil)
responseCrumb := &http.Response{ responseCrumb := &http.Response{
...@@ -112,7 +112,7 @@ var _ = Describe("job test", func() { ...@@ -112,7 +112,7 @@ var _ = Describe("job test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
err := jobClient.Build(jobName) err := jobClient.Build(jobName)
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
...@@ -134,7 +134,7 @@ var _ = Describe("job test", func() { ...@@ -134,7 +134,7 @@ var _ = Describe("job test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
result, err := jobClient.GetBuild(jobName, buildID) result, err := jobClient.GetBuild(jobName, buildID)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -189,7 +189,7 @@ var _ = Describe("job test", func() { ...@@ -189,7 +189,7 @@ var _ = Describe("job test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil) requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil)
responseCrumb := &http.Response{ responseCrumb := &http.Response{
...@@ -201,7 +201,7 @@ var _ = Describe("job test", func() { ...@@ -201,7 +201,7 @@ var _ = Describe("job test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
err := jobClient.StopJob(jobName, buildID) err := jobClient.StopJob(jobName, buildID)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -219,7 +219,7 @@ var _ = Describe("job test", func() { ...@@ -219,7 +219,7 @@ var _ = Describe("job test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil) requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil)
responseCrumb := &http.Response{ responseCrumb := &http.Response{
...@@ -231,7 +231,7 @@ var _ = Describe("job test", func() { ...@@ -231,7 +231,7 @@ var _ = Describe("job test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
err := jobClient.StopJob(jobName, buildID) err := jobClient.StopJob(jobName, buildID)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -252,7 +252,7 @@ var _ = Describe("job test", func() { ...@@ -252,7 +252,7 @@ var _ = Describe("job test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
result, err := jobClient.GetJob(jobName) result, err := jobClient.GetJob(jobName)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -271,7 +271,7 @@ var _ = Describe("job test", func() { ...@@ -271,7 +271,7 @@ var _ = Describe("job test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("{}")), Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
_, err := jobClient.GetJobTypeCategories() _, err := jobClient.GetJobTypeCategories()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -335,7 +335,7 @@ var _ = Describe("job test", func() { ...@@ -335,7 +335,7 @@ var _ = Describe("job test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil) requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil)
responseCrumb := &http.Response{ responseCrumb := &http.Response{
...@@ -347,7 +347,7 @@ var _ = Describe("job test", func() { ...@@ -347,7 +347,7 @@ var _ = Describe("job test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
err := jobClient.Delete(jobName) err := jobClient.Delete(jobName)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
......
...@@ -25,7 +25,7 @@ func PrepareForGetJobInputActions(roundTripper *mhttp.MockRoundTripper, rootURL, ...@@ -25,7 +25,7 @@ func PrepareForGetJobInputActions(roundTripper *mhttp.MockRoundTripper, rootURL,
"abortUrl":"/job/test/5/input/Eff7d5dba32b4da32d9a67a519434d3f/abort","redirectApprovalUrl":"/job/test/5/input/"}]`)), "abortUrl":"/job/test/5/input/Eff7d5dba32b4da32d9a67a519434d3f/abort","redirectApprovalUrl":"/job/test/5/input/"}]`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" { if user != "" && password != "" {
request.SetBasicAuth(user, password) request.SetBasicAuth(user, password)
...@@ -95,7 +95,7 @@ func PrepareForGetJob(roundTripper *mhttp.MockRoundTripper, rootURL, jobName, us ...@@ -95,7 +95,7 @@ func PrepareForGetJob(roundTripper *mhttp.MockRoundTripper, rootURL, jobName, us
}`, jobName))), }`, jobName))),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" { if user != "" && password != "" {
request.SetBasicAuth(user, password) request.SetBasicAuth(user, password)
} }
...@@ -155,7 +155,7 @@ func PrepareForGetBuild(roundTripper *mhttp.MockRoundTripper, rootURL, jobName s ...@@ -155,7 +155,7 @@ func PrepareForGetBuild(roundTripper *mhttp.MockRoundTripper, rootURL, jobName s
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" { if user != "" && password != "" {
request.SetBasicAuth(user, password) request.SetBasicAuth(user, password)
} }
...@@ -180,7 +180,7 @@ func PrepareForJobLog(roundTripper *mhttp.MockRoundTripper, rootURL, jobName str ...@@ -180,7 +180,7 @@ func PrepareForJobLog(roundTripper *mhttp.MockRoundTripper, rootURL, jobName str
Body: ioutil.NopCloser(bytes.NewBufferString("fake log")), Body: ioutil.NopCloser(bytes.NewBufferString("fake log")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" { if user != "" && password != "" {
request.SetBasicAuth(user, password) request.SetBasicAuth(user, password)
} }
...@@ -196,7 +196,7 @@ func PrepareOneItem(roundTripper *mhttp.MockRoundTripper, rootURL, name, kind, u ...@@ -196,7 +196,7 @@ func PrepareOneItem(roundTripper *mhttp.MockRoundTripper, rootURL, name, kind, u
Body: ioutil.NopCloser(bytes.NewBufferString(`[{"name":"fake","displayName":"fake","description":null,"type":"WorkflowJob","shortURL":"job/fake/","url":"job/fake/"}]`)), Body: ioutil.NopCloser(bytes.NewBufferString(`[{"name":"fake","displayName":"fake","description":null,"type":"WorkflowJob","shortURL":"job/fake/","url":"job/fake/"}]`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && token != "" { if user != "" && token != "" {
request.SetBasicAuth(user, token) request.SetBasicAuth(user, token)
} }
...@@ -212,7 +212,7 @@ func PrepareEmptyItems(roundTripper *mhttp.MockRoundTripper, rootURL, name, kind ...@@ -212,7 +212,7 @@ func PrepareEmptyItems(roundTripper *mhttp.MockRoundTripper, rootURL, name, kind
Body: ioutil.NopCloser(bytes.NewBufferString(`[]`)), Body: ioutil.NopCloser(bytes.NewBufferString(`[]`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && token != "" { if user != "" && token != "" {
request.SetBasicAuth(user, token) request.SetBasicAuth(user, token)
} }
...@@ -228,7 +228,7 @@ func PrepareForDisableJob(roundTripper *mhttp.MockRoundTripper, rootURL, name, u ...@@ -228,7 +228,7 @@ func PrepareForDisableJob(roundTripper *mhttp.MockRoundTripper, rootURL, name, u
// Body: ioutil.NopCloser(bytes.NewBufferString(``)), // Body: ioutil.NopCloser(bytes.NewBufferString(``)),
//} //}
//roundTripper.EXPECT(). //roundTripper.EXPECT().
// RoundTrip(request).Return(response, nil) // RoundTrip(NewRequestMatcher(request)).Return(response, nil)
//if user != "" && token != "" { //if user != "" && token != "" {
// request.SetBasicAuth(user, token) // request.SetBasicAuth(user, token)
//} //}
...@@ -244,7 +244,7 @@ func PrepareForEnableJob(roundTripper *mhttp.MockRoundTripper, rootURL, name, us ...@@ -244,7 +244,7 @@ func PrepareForEnableJob(roundTripper *mhttp.MockRoundTripper, rootURL, name, us
// Body: ioutil.NopCloser(bytes.NewBufferString(``)), // Body: ioutil.NopCloser(bytes.NewBufferString(``)),
//} //}
//roundTripper.EXPECT(). //roundTripper.EXPECT().
// RoundTrip(request).Return(response, nil) // RoundTrip(NewRequestMatcher(request)).Return(response, nil)
//if user != "" && token != "" { //if user != "" && token != "" {
// request.SetBasicAuth(user, token) // request.SetBasicAuth(user, token)
//} //}
......
...@@ -22,7 +22,7 @@ func PrepareShowTrend(roundTripper *mhttp.MockRoundTripper, keyword string) ( ...@@ -22,7 +22,7 @@ func PrepareShowTrend(roundTripper *mhttp.MockRoundTripper, keyword string) (
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
return return
} }
...@@ -64,7 +64,7 @@ func PrepareDownloadPlugin(roundTripper *mhttp.MockRoundTripper) (response *http ...@@ -64,7 +64,7 @@ func PrepareDownloadPlugin(roundTripper *mhttp.MockRoundTripper) (response *http
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
return return
} }
...@@ -91,6 +91,6 @@ func PrepareShowPlugins(roundTripper *mhttp.MockRoundTripper, keyword string) ( ...@@ -91,6 +91,6 @@ func PrepareShowPlugins(roundTripper *mhttp.MockRoundTripper, keyword string) (
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
return return
} }
...@@ -90,7 +90,7 @@ var _ = Describe("PluginManager test", func() { ...@@ -90,7 +90,7 @@ var _ = Describe("PluginManager test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
_, err := pluginMgr.GetAvailablePlugins() _, err := pluginMgr.GetAvailablePlugins()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
......
...@@ -19,7 +19,7 @@ func PrepareGetStatus(roundTripper *mhttp.MockRoundTripper, rootURL, user, passw ...@@ -19,7 +19,7 @@ func PrepareGetStatus(roundTripper *mhttp.MockRoundTripper, rootURL, user, passw
} }
response.Header.Add("X-Jenkins", "version") response.Header.Add("X-Jenkins", "version")
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && password != "" { if user != "" && password != "" {
request.SetBasicAuth(user, password) request.SetBasicAuth(user, password)
......
...@@ -29,7 +29,7 @@ func PrepareForEmptyAvaiablePluginList(roundTripper *mhttp.MockRoundTripper, roo ...@@ -29,7 +29,7 @@ func PrepareForEmptyAvaiablePluginList(roundTripper *mhttp.MockRoundTripper, roo
}`)), }`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
return return
} }
...@@ -99,7 +99,7 @@ func PrepareForEmptyInstalledPluginList(roundTripper *mhttp.MockRoundTripper, ro ...@@ -99,7 +99,7 @@ func PrepareForEmptyInstalledPluginList(roundTripper *mhttp.MockRoundTripper, ro
}`)), }`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
return return
} }
...@@ -234,7 +234,7 @@ func PrepareCancelQueue(roundTripper *mhttp.MockRoundTripper, rootURL, user, pas ...@@ -234,7 +234,7 @@ func PrepareCancelQueue(roundTripper *mhttp.MockRoundTripper, rootURL, user, pas
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
PrepareForGetIssuer(roundTripper, rootURL, user, passwd) PrepareForGetIssuer(roundTripper, rootURL, user, passwd)
if user != "" && passwd != "" { if user != "" && passwd != "" {
...@@ -274,7 +274,7 @@ func PrepareGetQueue(roundTripper *mhttp.MockRoundTripper, rootURL, user, passwd ...@@ -274,7 +274,7 @@ func PrepareGetQueue(roundTripper *mhttp.MockRoundTripper, rootURL, user, passwd
}`)), }`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && passwd != "" { if user != "" && passwd != "" {
request.SetBasicAuth(user, passwd) request.SetBasicAuth(user, passwd)
...@@ -355,7 +355,7 @@ func PrepareForRequestUpdateCenter(roundTripper *mhttp.MockRoundTripper, rootURL ...@@ -355,7 +355,7 @@ func PrepareForRequestUpdateCenter(roundTripper *mhttp.MockRoundTripper, rootURL
} }
`)), `)),
} }
roundTripper.EXPECT().RoundTrip(requestCenter).Return(responseCenter, nil) roundTripper.EXPECT().RoundTrip(NewRequestMatcher(requestCenter)).Return(responseCenter, nil)
return return
} }
...@@ -381,7 +381,7 @@ func PrepareForNoAvailablePlugins(roundTripper *mhttp.MockRoundTripper, rootURL ...@@ -381,7 +381,7 @@ func PrepareForNoAvailablePlugins(roundTripper *mhttp.MockRoundTripper, rootURL
} }
`)), `)),
} }
roundTripper.EXPECT().RoundTrip(requestCenter).Return(responseCenter, nil) roundTripper.EXPECT().RoundTrip(NewRequestMatcher(requestCenter)).Return(responseCenter, nil)
return return
} }
...@@ -436,7 +436,7 @@ func PrepareForPipelineJob(roundTripper *mhttp.MockRoundTripper, rootURL, user, ...@@ -436,7 +436,7 @@ func PrepareForPipelineJob(roundTripper *mhttp.MockRoundTripper, rootURL, user,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"type":null,"displayName":null,"script":"script","sandbox":true}`)), Body: ioutil.NopCloser(bytes.NewBufferString(`{"type":null,"displayName":null,"script":"script","sandbox":true}`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && passwd != "" { if user != "" && passwd != "" {
request.SetBasicAuth(user, passwd) request.SetBasicAuth(user, passwd)
......
...@@ -51,7 +51,7 @@ var _ = Describe("update center test", func() { ...@@ -51,7 +51,7 @@ var _ = Describe("update center test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
err := manager.DownloadJenkins() err := manager.DownloadJenkins()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -79,7 +79,7 @@ var _ = Describe("update center test", func() { ...@@ -79,7 +79,7 @@ var _ = Describe("update center test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(NewRequestMatcher(requestCrumb)).Return(responseCrumb, nil)
request, _ := http.NewRequest("POST", "/updateCenter/upgrade", nil) request, _ := http.NewRequest("POST", "/updateCenter/upgrade", nil)
request.Header.Add("CrumbRequestField", "Crumb") request.Header.Add("CrumbRequestField", "Crumb")
...@@ -90,7 +90,7 @@ var _ = Describe("update center test", func() { ...@@ -90,7 +90,7 @@ var _ = Describe("update center test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString("")), Body: ioutil.NopCloser(bytes.NewBufferString("")),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
err := manager.Upgrade() err := manager.Upgrade()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -112,7 +112,7 @@ var _ = Describe("update center test", func() { ...@@ -112,7 +112,7 @@ var _ = Describe("update center test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
status, err := manager.Status() status, err := manager.Status()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -218,7 +218,7 @@ var _ = Describe("update center test", func() { ...@@ -218,7 +218,7 @@ var _ = Describe("update center test", func() {
`)), `)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCenter).Return(responseCenter, nil) RoundTrip(NewRequestMatcher(requestCenter)).Return(responseCenter, nil)
plugins, err := manager.GetSite() plugins, err := manager.GetSite()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
......
...@@ -21,7 +21,7 @@ func PrepareGetUser(roundTripper *mhttp.MockRoundTripper, rootURL, user, passwd ...@@ -21,7 +21,7 @@ func PrepareGetUser(roundTripper *mhttp.MockRoundTripper, rootURL, user, passwd
Body: ioutil.NopCloser(bytes.NewBufferString(`{"fullName":"admin","description":"fake-description"}`)), Body: ioutil.NopCloser(bytes.NewBufferString(`{"fullName":"admin","description":"fake-description"}`)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip(NewRequestMatcher(request)).Return(response, nil)
if user != "" && passwd != "" { if user != "" && passwd != "" {
request.SetBasicAuth(user, passwd) request.SetBasicAuth(user, passwd)
......
...@@ -66,7 +66,7 @@ var _ = Describe("http test", func() { ...@@ -66,7 +66,7 @@ var _ = Describe("http test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip((request)).Return(response, nil)
err := downloader.DownloadFile() err := downloader.DownloadFile()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -97,7 +97,7 @@ var _ = Describe("http test", func() { ...@@ -97,7 +97,7 @@ var _ = Describe("http test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip((request)).Return(response, nil)
err := downloader.DownloadFile() err := downloader.DownloadFile()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
...@@ -126,7 +126,7 @@ var _ = Describe("http test", func() { ...@@ -126,7 +126,7 @@ var _ = Describe("http test", func() {
request, _ := http.NewRequest("GET", "", nil) request, _ := http.NewRequest("GET", "", nil)
response := &http.Response{} response := &http.Response{}
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, fmt.Errorf("fake error")) RoundTrip((request)).Return(response, fmt.Errorf("fake error"))
err := downloader.DownloadFile() err := downloader.DownloadFile()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
...@@ -145,7 +145,7 @@ var _ = Describe("http test", func() { ...@@ -145,7 +145,7 @@ var _ = Describe("http test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip((request)).Return(response, nil)
err := downloader.DownloadFile() err := downloader.DownloadFile()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
...@@ -176,7 +176,7 @@ var _ = Describe("http test", func() { ...@@ -176,7 +176,7 @@ var _ = Describe("http test", func() {
Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)),
} }
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(request).Return(response, nil) RoundTrip((request)).Return(response, nil)
err := downloader.DownloadFile() err := downloader.DownloadFile()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
}) })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册