Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support get the identity of Jenkins #292

Merged
merged 2 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### For the bug fixes or features only

- [ ] [Quality Gate Passed](https://sonarcloud.io/dashboard?id=jenkins-zh_jenkins-cli). Change this URL to your PR.
- [ ] The coverage is xxx on the new lines
- [ ] I've tested it locally

<!--
Expand Down
43 changes: 43 additions & 0 deletions app/cmd/center_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"encoding/json"
"github.com/jenkins-zh/jenkins-cli/app/i18n"

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

// CenterIdentityOption option for upgrade Jenkins
type CenterIdentityOption struct {
CommonOption
}

var centerIdentityOption CenterIdentityOption

func init() {
centerCmd.AddCommand(centerIdentityCmd)
}

var centerIdentityCmd = &cobra.Command{
Use: "identity",
Short: i18n.T("Print the identity of current Jenkins"),
Long: i18n.T("Print the identity of current Jenkins"),
RunE: func(cmd *cobra.Command, _ []string) (err error) {
jClient := &client.CoreClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: centerIdentityOption.RoundTripper,
},
}
getCurrentJenkinsAndClient(&(jClient.JenkinsCore))

var identity client.JenkinsIdentity
var data []byte
if identity, err = jClient.GetIdentity(); err == nil {
if data, err = json.MarshalIndent(identity, "", " "); err == nil {
cmd.Println(string(data))
}
}
return
},
}
64 changes: 64 additions & 0 deletions app/cmd/center_identity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"bytes"
"github.com/golang/mock/gomock"
"github.com/jenkins-zh/jenkins-cli/client"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"os"

"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
)

var _ = Describe("center identity command", func() {
var (
ctrl *gomock.Controller
roundTripper *mhttp.MockRoundTripper
targetFilePath string

err error
)

BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
roundTripper = mhttp.NewMockRoundTripper(ctrl)
centerIdentityOption.RoundTripper = roundTripper
targetFilePath = "jenkins.war"

rootOptions.Jenkins = ""
rootOptions.ConfigFile = "test.yaml"
})

AfterEach(func() {
rootCmd.SetArgs([]string{})
err = os.Remove(targetFilePath)
ctrl.Finish()
})

Context("basic cases", func() {
It("should not error", func() {
var data []byte
data, err = generateSampleConfig()
Expect(err).To(BeNil())
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
Expect(err).To(BeNil())

client.PrepareForGetIdentity(roundTripper, "http://localhost:8080/jenkins",
"admin", "111e3a2f0231198855dceaff96f20540a9")

buf := new(bytes.Buffer)
rootCmd.SetOut(buf)
rootCmd.SetArgs([]string{"center", "identity"})
_, err = rootCmd.ExecuteC()
Expect(err).NotTo(HaveOccurred())
Expect(buf.String()).To(Equal(`{
"Fingerprint": "fingerprint",
"PublicKey": "publicKey",
"SystemMessage": "systemMessage"
}
`))
})
})
})
12 changes: 2 additions & 10 deletions app/cmd/center_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,12 @@ var centerUpgradeCmd = &cobra.Command{
Short: i18n.T("Upgrade your Jenkins"),
Long: i18n.T("Upgrade your Jenkins"),
RunE: func(cmd *cobra.Command, _ []string) (err error) {
jenkins := getCurrentJenkinsFromOptionsOrDie()

jclient := &client.UpdateCenterManager{
JenkinsCore: client.JenkinsCore{
RoundTripper: centerUpgradeOption.RoundTripper,
},
}
jclient.URL = jenkins.URL
jclient.UserName = jenkins.UserName
jclient.Token = jenkins.Token
jclient.Proxy = jenkins.Proxy
jclient.ProxyAuth = jenkins.ProxyAuth

err = jclient.Upgrade()
return
getCurrentJenkinsAndClient(&(jclient.JenkinsCore))
return jclient.Upgrade()
},
}
13 changes: 13 additions & 0 deletions client/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ func (q *CoreClient) Restart() (err error) {
_, err = q.RequestWithoutData("POST", "/safeRestart", nil, nil, 503)
return
}

// JenkinsIdentity belongs to a Jenkins
type JenkinsIdentity struct {
Fingerprint string
PublicKey string
SystemMessage string
}

// GetIdentity returns the identity of a Jenkins
func (q *CoreClient) GetIdentity() (identity JenkinsIdentity, err error) {
err = q.RequestWithData("GET", "/instance", nil, nil, 200, &identity)
return
}
21 changes: 15 additions & 6 deletions client/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var _ = Describe("core test", func() {

username = "admin"
password = "token"

coreClient.UserName = username
coreClient.Token = password
})

AfterEach(func() {
Expand All @@ -34,23 +37,29 @@ var _ = Describe("core test", func() {

Context("Get", func() {
It("should success", func() {
coreClient.UserName = username
coreClient.Token = password

PrepareRestart(roundTripper, coreClient.URL, username, password, 503)

err := coreClient.Restart()
Expect(err).To(BeNil())
})

It("should error, 400", func() {
coreClient.UserName = username
coreClient.Token = password

PrepareRestart(roundTripper, coreClient.URL, username, password, 400)

err := coreClient.Restart()
Expect(err).To(HaveOccurred())
})

It("GetIdentity", func() {
PrepareForGetIdentity(roundTripper, coreClient.URL, username, password)

identity, err := coreClient.GetIdentity()
Expect(err).NotTo(HaveOccurred())
Expect(identity).To(Equal(JenkinsIdentity{
Fingerprint: "fingerprint",
PublicKey: "publicKey",
SystemMessage: "systemMessage",
}))
})
})
})
21 changes: 20 additions & 1 deletion client/core_test_common.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package client

import (
"bytes"
"fmt"
"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
"io/ioutil"
"net/http"
)

//PrepareRestart only for test
// PrepareRestart only for test
func PrepareRestart(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string, statusCode int) {
request, _ := http.NewRequest("POST", fmt.Sprintf("%s/safeRestart", rootURL), nil)
response := PrepareCommonPost(request, "", roundTripper, user, password, rootURL)
response.StatusCode = statusCode
return
}

// PrepareForGetIdentity only for test
func PrepareForGetIdentity(roundTripper *mhttp.MockRoundTripper, rootURL, user, password string) {
request, _ := http.NewRequest("GET", fmt.Sprintf("%s/instance", rootURL), nil)
response := &http.Response{
StatusCode: 200,
Request: request,
Body: ioutil.NopCloser(bytes.NewBufferString(`
{"fingerprint":"fingerprint","publicKey":"publicKey","systemMessage":"systemMessage"}`)),
}
roundTripper.EXPECT().
RoundTrip(request).Return(response, nil)

if user != "" && password != "" {
request.SetBasicAuth(user, password)
}
}