Skip to content

Commit

Permalink
add support to conduct api test for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
jxr98 committed Sep 10, 2021
1 parent b591e86 commit 538433e
Show file tree
Hide file tree
Showing 27 changed files with 523 additions and 1,103 deletions.
35 changes: 35 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.187.0/containers/docker-existing-dockerfile
{
"name": "Existing Dockerfile",

// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",

// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerFile": "../.gitpod.Dockerfile",

// Set *default* container specific settings.json values on container create.
"settings": {},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"zenor.makefile-creator",
"golang.go"
]

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Uncomment the next line to run commands after the container is created - for example installing curl.
// "postCreateCommand": "apt-get update && apt-get install -y curl",

// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],

// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],

// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
5 changes: 5 additions & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ exclude-labels:
- skip-changelog
- invalid
change-template: '* $TITLE (#$NUMBER) @$AUTHOR'
replacers:
- search: '/(?:and )?@dependabot-preview(?:\[bot\])?,?/g'
replace: ''
template: |
## What’s Changed
$CHANGES
Thanks again to $CONTRIBUTORS! 🎉
2 changes: 1 addition & 1 deletion .github/workflows/backup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
run: |
make test
- name: Upload coverage to Codecov
uses: codecov/[email protected].2
uses: codecov/[email protected].3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.out
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ jobs:
run: |
make test
- name: Upload coverage to Codecov
uses: codecov/[email protected].2
uses: codecov/[email protected].3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.out
Expand Down
68 changes: 45 additions & 23 deletions app/cmd/center_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package cmd

import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"

"github.com/jedib0t/go-pretty/v6/table"
Expand All @@ -26,8 +28,8 @@ const ASCIIOfSpace = 32

//CenterListOption as options for Jenkins RSS
type CenterListOption struct {
Channel Channel `xml:"channel"`
RoundTripper http.RoundTripper
Channel Channel `xml:"channel"`
// RoundTripper http.RoundTripper
}

//Channel as part of CenterListOption
Expand All @@ -43,40 +45,59 @@ type Item struct {
PubDate string `xml:"pubDate"`
}

var centerListOption CenterListOption
var numberOfLines int
var coreVersion string

func init() {
centerCmd.AddCommand(centerListCmd)
centerListCmd.Flags().IntVarP(&numberOfLines, "lines", "", 10,
i18n.T("the number of lines to be printed in description column"))
centerListCmd.Flags().IntVar(&numberOfLines, "amount", 10,
i18n.T("the amount of information to be printed in description column"))
centerListCmd.Flags().StringVarP(&coreVersion, "version", "v", "",
i18n.T("current jenkins version"))
}

var centerListCmd = &cobra.Command{
Use: "list",
Short: i18n.T("Print the information of recent-released Jenkins"),
Long: i18n.T("Print the information of recent-released Jenkins"),
Long: i18n.T("Print the information of recent-released Jenkins which are newer than the installed one."),
Example: `jcli center list --version 2.345.1
jcli center list`,
PreRunE: checkConnectionWithJenkins,
RunE: func(cmd *cobra.Command, _ []string) (err error) {
jenkins := getCurrentJenkinsFromOptionsOrDie()
jclient := &client.JenkinsStatusClient{
var jenkinsVersion string
if coreVersion != "" {
jenkinsVersion = coreVersion
changeLog, err := getChangelog(LtsURL, jenkinsVersion, numberOfLines, getVersionData)
cmd.Println(changeLog)
return err
}
return err
},
}

func checkConnectionWithJenkins(cmd *cobra.Command, args []string) (err error) {
if coreVersion != "" {
matched, _ := regexp.MatchString("^[0-9]*\\.[0-9]*\\.[0-9]*", coreVersion)
if matched {
return nil
}
panic("Format of the jenkins version is not correct. The format should be sth like 2.235.2")
} else if coreVersion == "" {
jCoreClient := &client.JenkinsStatusClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: centerOption.RoundTripper,
RoundTripper: pluginFormulaOption.RoundTripper,
},
}
jclient.URL = jenkins.URL
jclient.UserName = jenkins.UserName
jclient.Token = jenkins.Token
jclient.Proxy = jenkins.Proxy
jclient.ProxyAuth = jenkins.ProxyAuth
status, error := jclient.Get()
if error != nil {
return error
getCurrentJenkinsAndClient(&(jCoreClient.JenkinsCore))
var status *client.JenkinsStatus
status, err := jCoreClient.Get()
if status.Version == "" {
err = fmt.Errorf("cannot get the version of current Jenkins, error is %v. Please check current status of your jenkins and your .jenkins-cli.yaml", err)
panic(err)
}
jenkinsVersion := status.Version
changeLog, err := getChangelog(LtsURL, jenkinsVersion, numberOfLines, getVersionData)
cmd.Println(changeLog)
return err
},
coreVersion = status.Version
}
return err
}

func getVersionData(rss string) ([]Item, string, error) {
Expand Down Expand Up @@ -110,7 +131,8 @@ func getChangelog(rss string, version string, lines int, getData func(rss string
break
}
isTheLatestVersion = 0
temp = trimXMLSymbols(item.Description)
temp = strings.Replace(item.Description, "\n", "", -1)
temp = trimXMLSymbols(temp)
temp = regulateWidthAndLines(temp, WidthOfDescription, lines)
t.AppendRow([]interface{}{index + 1, item.Title, temp, item.PubDate[:17]})
t.AppendSeparator()
Expand Down
9 changes: 9 additions & 0 deletions app/cmd/center_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
+-----------------------------------------------------------------------------------------------------------------------------------------+
| JENKINS LTS CHANGELOG |
+-------------------------+-------------------------+-----------------------------------------------------------+-------------------------+
| Index | Title | Description | PubDate |
| 1 | Jenkins 2.289.2 | Security: Important security fixes. | Wed, 30 Jun 2021 |
| | | RFE: Winstone 5.18: Update Jetty from 9.4.39.v20210325 to | |
| | | 9.4.41.v20210516 for bug fixes and enhancements. | |
| | | | |
+-------------------------+-------------------------+-----------------------------------------------------------+-------------------------+
9 changes: 4 additions & 5 deletions app/cmd/center_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"

"github.com/golang/mock/gomock"
"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
Expand All @@ -19,14 +18,14 @@ var resultOneVersionData string

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

BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
roundTripper = mhttp.NewMockRoundTripper(ctrl)
centerListOption.RoundTripper = roundTripper
// roundTripper = mhttp.NewMockRoundTripper(ctrl)
// centerListOption.RoundTripper = roundTripper
rootCmd.SetArgs([]string{})
rootOptions.Jenkins = ""
rootOptions.ConfigFile = "test.yaml"
Expand Down
11 changes: 6 additions & 5 deletions app/cmd/computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
appCfg "github.com/jenkins-zh/jenkins-cli/app/config"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
"github.com/jenkins-zh/jenkins-client/pkg/core"
"github.com/spf13/cobra"
"strings"
)
Expand All @@ -21,13 +22,13 @@ var computerCmd = &cobra.Command{
}

// GetComputerClient returns the client of computer
func GetComputerClient(option common.Option) (*client.ComputerClient, *appCfg.JenkinsServer) {
jClient := &client.ComputerClient{
JenkinsCore: client.JenkinsCore{
func GetComputerClient(option common.Option) (*computer.Client, *appCfg.JenkinsServer) {
jClient := &computer.Client{
JenkinsCore: core.JenkinsCore{
RoundTripper: option.RoundTripper,
},
}
return jClient, getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
return jClient, getCurrentJenkinsAndClientV2(&(jClient.JenkinsCore))
}

// ValidAgentNames autocomplete with agent names
Expand Down
7 changes: 3 additions & 4 deletions app/cmd/computer_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package cmd

import (
"bytes"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
"io"
"io/ioutil"
"os"

"github.com/jenkins-zh/jenkins-cli/client"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

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

var _ = Describe("create list command", func() {
Expand Down Expand Up @@ -57,7 +56,7 @@ var _ = Describe("create list command", func() {
It("should success", func() {
name := "fake-name"

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

rootCmd.SetArgs([]string{"computer", "create", name})
Expand Down
7 changes: 3 additions & 4 deletions app/cmd/computer_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package cmd

import (
"bytes"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
"io"
"io/ioutil"
"os"

"github.com/jenkins-zh/jenkins-cli/client"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

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

var _ = Describe("create delete command", func() {
Expand Down Expand Up @@ -57,7 +56,7 @@ var _ = Describe("create delete command", func() {
It("should success", func() {
name := "fake-name"

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

rootCmd.SetArgs([]string{"computer", "delete", name})
Expand Down
6 changes: 3 additions & 3 deletions app/cmd/computer_launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
appCfg "github.com/jenkins-zh/jenkins-cli/app/config"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/util"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
httpdownloader "github.com/linuxsuren/http-downloader/pkg"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand All @@ -25,7 +25,7 @@ type ComputerLaunchOption struct {
ShowProgress bool

/** share info between inner functions */
ComputerClient *client.ComputerClient
ComputerClient *computer.Client
CurrentJenkins *appCfg.JenkinsServer
Output string

Expand Down Expand Up @@ -270,7 +270,7 @@ func (o *ComputerLaunchOption) LaunchJnlp(name string) (err error) {
env := os.Environ()
agentArgs := []string{"java", "-jar", computerLaunchOption.Output,
"-jnlpUrl", fmt.Sprintf("%s/computer/%s/slave-agent.jnlp", o.ComputerClient.URL, name),
"-secret", secret, "-workDir", client.GetDefaultAgentWorkDir()}
"-secret", secret, "-workDir", computer.GetDefaultAgentWorkDir()}

if o.CurrentJenkins.ProxyAuth != "" {
proxyURL, _ := url.Parse(o.CurrentJenkins.Proxy)
Expand Down
10 changes: 5 additions & 5 deletions app/cmd/computer_launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package cmd

import (
"bytes"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/util"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
"io"
"io/ioutil"
"net/http"
"os"

"github.com/jenkins-zh/jenkins-cli/client"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

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

var _ = Describe("computer launch command", func() {
Expand Down Expand Up @@ -55,7 +55,7 @@ var _ = Describe("computer launch command", func() {
Context("launch a default type of agent", func() {
It("should success", func() {

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

rootCmd.SetArgs([]string{"computer", "launch", name})
Expand Down Expand Up @@ -83,7 +83,7 @@ var _ = Describe("computer launch command", func() {
RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)

secret := "fake-secret"
client.PrepareForComputerAgentSecretRequest(roundTripper,
computer.PrepareForComputerAgentSecretRequest(roundTripper,
"http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9", name, secret)
})

Expand Down
4 changes: 2 additions & 2 deletions app/cmd/computer_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
cobra_ext "github.com/linuxsuren/cobra-extension"

"github.com/jenkins-zh/jenkins-cli/app/i18n"
Expand Down Expand Up @@ -35,7 +35,7 @@ var computerListCmd = &cobra.Command{
return
}

var computers client.ComputerList
var computers computer.List
if computers, err = jClient.List(); err == nil {
computerListOption.Writer = cmd.OutOrStdout()
computerListOption.CellRenderMap = map[string]cobra_ext.RenderCell{
Expand Down
Loading

0 comments on commit 538433e

Please sign in to comment.