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

Add integration tests (#301)

* Add a simple test for plugin

* Do int test in ubuntu

* Run the int test when pull-request

* Fix the build error caused by bindata

* Fix the wrong name of int test action

* Fix the path for build

* Add more int test cases

* Add more test cases which needs dependencies

* Add more test cases

* Add more test environments

* Fix the issues come from crumb cmd

* Fix the port conflict problem

* Ignore the result of restart jenkins

* Fix the panic of doc cmd

* Fix the doc cmd

* Given a specific version for gomock

* Add missing import

* Add slow testing

* Remove test package to e2e

* Rename package

* Test fixes

* Auto delete random web dir

* Fix the e2e tests
上级 f5a3ff88
name: IntTest-Darwin
on:
pull_request:
branches:
- master
jobs:
build:
name: Build
runs-on: macOS-latest
steps:
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Build
run: |
export PATH=$PATH:$GOPATH/bin:/home/runner/go/bin:${PWD}/bin:${PWD}/bin/darwin:/System/Volumes/Data/Users/runner/go/bin
make go-bindata-download-darwin tools init darwin
- name: Test in Jenkins 2.190.1
run: |
export PATH=$PATH:$GOPATH/bin:/home/runner/go/bin:${PWD}/bin:${PWD}/bin/darwin:/System/Volumes/Data/Users/runner/go/bin
export JENKINS_VERSION=2.190.1
make test-slow
- name: Test in Jenkins 2.190.2
run: |
export PATH=$PATH:$GOPATH/bin:/home/runner/go/bin:${PWD}/bin:${PWD}/bin/darwin:/System/Volumes/Data/Users/runner/go/bin
export JENKINS_VERSION=2.190.2
make test-slow
- name: Test in Jenkins 2.190.3
run: |
export PATH=$PATH:$GOPATH/bin:/home/runner/go/bin:${PWD}/bin:${PWD}/bin/darwin:/System/Volumes/Data/Users/runner/go/bin
export JENKINS_VERSION=2.190.2
make test-slow
- name: Test in Jenkins 2.204.1
run: |
export PATH=$PATH:$GOPATH/bin:/home/runner/go/bin:${PWD}/bin:${PWD}/bin/darwin:/System/Volumes/Data/Users/runner/go/bin
export JENKINS_VERSION=2.204.1
make test-slow
\ No newline at end of file
name: IntTest-Ubuntu
on:
pull_request:
branches:
- master
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Build
run: |
export PATH=$PATH:$GOPATH/bin:/home/runner/go/bin:${PWD}/bin:${PWD}/bin/linux:/System/Volumes/Data/Users/runner/go/bin
make go-bindata-download-linux tools init linux
- name: Test in Jenkins 2.190.1
run: |
export PATH=$PATH:$GOPATH/bin:/home/runner/go/bin:${PWD}/bin:${PWD}/bin/linux:/System/Volumes/Data/Users/runner/go/bin
export JENKINS_VERSION=2.190.1
make test-slow
- name: Test in Jenkins 2.190.2
run: |
export PATH=$PATH:$GOPATH/bin:/home/runner/go/bin:${PWD}/bin:${PWD}/bin/linux:/System/Volumes/Data/Users/runner/go/bin
export JENKINS_VERSION=2.190.2
make test-slow
- name: Test in Jenkins 2.190.3
run: |
export PATH=$PATH:$GOPATH/bin:/home/runner/go/bin:${PWD}/bin:${PWD}/bin/linux:/System/Volumes/Data/Users/runner/go/bin
export JENKINS_VERSION=2.190.2
make test-slow
- name: Test in Jenkins 2.204.1
run: |
export PATH=$PATH:$GOPATH/bin:/home/runner/go/bin:${PWD}/bin:${PWD}/bin/linux:/System/Volumes/Data/Users/runner/go/bin
export JENKINS_VERSION=2.204.1
make test-slow
\ No newline at end of file
......@@ -95,6 +95,25 @@ fmt:
go fmt ./app/...
gofmt -s -w .
test-slow:
# JENKINS_VERSION=2.190.3 go test ./e2e/... -v -count=1 -parallel 1
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestBashCompletion$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestZshCompletion$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestPowerShellCompletion$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestListComputers$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestConfigList$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestConfigGenerate$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestConfigList$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestShowCurrentConfig$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestCrumb$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestDoc$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestSearchPlugins$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestListPlugins$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestCheckUpdateCenter$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestInstallPlugin$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestDownloadPlugin$
JENKINS_VERSION=2.190.3 go test github.com/jenkins-zh/jenkins-cli/e2e -v -test.run ^TestListQueue$
test:
mkdir -p bin
go test ./util -v -count=1
......
......@@ -32,7 +32,7 @@ var cascApplyCmd = &cobra.Command{
RoundTripper: cascApplyOption.RoundTripper,
},
}
getCurrentJenkinsAndClientOrDie(&(jClient.JenkinsCore))
getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
return jClient.Apply()
},
Annotations: map[string]string{
......
......@@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
"github.com/jenkins-zh/jenkins-cli/app/helper"
"go.uber.org/zap"
"os"
"path/filepath"
......@@ -36,10 +37,11 @@ type CenterStartOption struct {
Environments []string
System []string
Download bool
Version string
LTS bool
Formula string
Download bool
Version string
LTS bool
Formula string
RandomWebDir bool
DryRun bool
}
......@@ -82,6 +84,8 @@ func init() {
centerStartCmd.Flags().IntVarP(&centerStartOption.ConcurrentIndexing, "concurrent-indexing", "", -1,
i18n.T("Concurrent indexing limit, take this value only it is bigger than -1"))
centerStartCmd.Flags().BoolVarP(&centerStartOption.RandomWebDir, "random-web-dir", "", false,
i18n.T("If start jenkins.war in a random web dir"))
centerStartCmd.Flags().BoolVarP(&centerStartOption.DryRun, "dry-run", "", false,
i18n.T("Don't run jenkins.war really"))
......@@ -145,7 +149,19 @@ var centerStartCmd = &cobra.Command{
binary, err = util.LookPath("java", centerStartOption.LookPathContext)
if err == nil {
env := os.Environ()
env = append(env, fmt.Sprintf("JENKINS_HOME=%s/.jenkins-cli/cache/%s/web", userHome, centerStartOption.Version))
if centerStartOption.RandomWebDir {
randomWebDir := fmt.Sprintf("JENKINS_HOME=%s/.jenkins-cli/cache/%s/web", os.TempDir(), centerStartOption.Version)
defer func(logger helper.Printer, randomWebDir string) {
if err := os.RemoveAll(randomWebDir); err != nil {
logger.PrintErr(fmt.Sprintf("remove random web dir [%s] of Jenkins failed, %#v", randomWebDir, err))
}
}(cmd, randomWebDir)
env = append(env, randomWebDir)
} else {
env = append(env, fmt.Sprintf("JENKINS_HOME=%s/.jenkins-cli/cache/%s/web", userHome, centerStartOption.Version))
}
if centerStartOption.Environments != nil {
for _, item := range centerStartOption.Environments {
......@@ -157,7 +173,8 @@ var centerStartCmd = &cobra.Command{
jenkinsWarArgs = centerStartOption.setSystemProperty(jenkinsWarArgs)
jenkinsWarArgs = append(jenkinsWarArgs, "-jar", jenkinsWar)
jenkinsWarArgs = append(jenkinsWarArgs, fmt.Sprintf("--httpPort=%d", centerStartOption.Port))
jenkinsWarArgs = append(jenkinsWarArgs, "--argumentsRealm.passwd.admin=admin --argumentsRealm.roles.admin=admin")
jenkinsWarArgs = append(jenkinsWarArgs, "--argumentsRealm.passwd.admin=admin")
jenkinsWarArgs = append(jenkinsWarArgs, "--argumentsRealm.roles.admin=admin")
jenkinsWarArgs = append(jenkinsWarArgs, fmt.Sprintf("--prefix=%s", centerStartOption.Context))
if centerStartOption.HTTPSEnable {
......
......@@ -31,21 +31,26 @@ var configListCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, _ []string) (err error) {
configListOption.Writer = cmd.OutOrStdout()
config := getConfig()
if config == nil {
return fmt.Errorf("no config file found")
}
switch configListOption.Config {
case "JenkinsServers":
err = configListOption.OutputV2(getConfig().JenkinsServers)
err = configListOption.OutputV2(config.JenkinsServers)
case "PreHooks":
configListOption.Columns = "Path,Command"
err = configListOption.OutputV2(getConfig().PreHooks)
err = configListOption.OutputV2(config.PreHooks)
case "PostHooks":
configListOption.Columns = "Path,Command"
err = configListOption.OutputV2(getConfig().PostHooks)
err = configListOption.OutputV2(config.PostHooks)
case "Mirrors":
configListOption.Columns = "Name,URL"
err = configListOption.OutputV2(getConfig().Mirrors)
err = configListOption.OutputV2(config.Mirrors)
case "PluginSuites":
configListOption.Columns = "Name,Description"
err = configListOption.OutputV2(getConfig().PluginSuites)
err = configListOption.OutputV2(config.PluginSuites)
default:
err = fmt.Errorf("unknow config %s", configListOption.Config)
}
......
......@@ -50,7 +50,7 @@ func init() {
}
var credentialCreateCmd = &cobra.Command{
Use: "create [store] [id]",
Use: "create",
Short: i18n.T("Create a credential from Jenkins"),
Long: i18n.T("Create a credential from Jenkins"),
PreRunE: func(cmd *cobra.Command, args []string) (err error) {
......
......@@ -3,8 +3,6 @@ package cmd
import (
"net/http"
"github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra"
......@@ -25,14 +23,14 @@ var crumbIssuerCmd = &cobra.Command{
Use: "crumb",
Short: "Print crumbIssuer of Jenkins",
Long: `Print crumbIssuer of Jenkins`,
Run: func(cmd *cobra.Command, _ []string) {
RunE: func(cmd *cobra.Command, _ []string) (err error) {
jenkinsCore := &client.JenkinsCore{RoundTripper: crumbIssuerOptions.RoundTripper}
getCurrentJenkinsAndClientOrDie(jenkinsCore)
getCurrentJenkinsAndClient(jenkinsCore)
crumb, err := jenkinsCore.GetCrumb()
if err == nil {
var crumb *client.JenkinsCrumb
if crumb, err = jenkinsCore.GetCrumb(); err == nil {
cmd.Printf("%s=%s\n", crumb.CrumbRequestField, crumb.Crumb)
}
helper.CheckErr(cmd, err)
return
},
}
package cmd
import (
"net/http"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra"
"net/http"
)
// PluginDownloadOption is the option for plugin download command
......@@ -13,6 +14,7 @@ type PluginDownloadOption struct {
SkipOptional bool
UseMirror bool
ShowProgress bool
DownloadDir string
RoundTripper http.RoundTripper
}
......@@ -29,22 +31,26 @@ func init() {
i18n.T("If you want to download plugin from a mirror site"))
pluginDownloadCmd.Flags().BoolVarP(&pluginDownloadOption.ShowProgress, "show-progress", "", true,
i18n.T("If you want to show the progress of download a plugin"))
pluginDownloadCmd.Flags().StringVarP(&pluginDownloadOption.DownloadDir, "download-dir", "", "",
i18n.T("The directory which you want to download to"))
}
var pluginDownloadCmd = &cobra.Command{
Use: "download <keyword>",
Short: i18n.T("Download the plugins"),
Long: i18n.T(`Download the plugins which contain the target plugin and its dependencies`),
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Use: "download",
Short: i18n.T("Download the plugins"),
Long: i18n.T(`Download the plugins which contain the target plugin and its dependencies`),
Args: cobra.MinimumNArgs(1),
Example: "download localization-zh-cn",
RunE: func(cmd *cobra.Command, args []string) (err error) {
jClient := &client.PluginAPI{
SkipDependency: pluginDownloadOption.SkipDependency,
SkipOptional: pluginDownloadOption.SkipOptional,
UseMirror: pluginDownloadOption.UseMirror,
ShowProgress: pluginDownloadOption.ShowProgress,
MirrorURL: getDefaultMirror(),
DownloadDir: pluginDownloadOption.DownloadDir,
RoundTripper: pluginDownloadOption.RoundTripper,
}
jClient.DownloadPlugins(args)
return jClient.DownloadPlugins(args)
},
}
......@@ -34,7 +34,7 @@ var queueListCmd = &cobra.Command{
Debug: rootOptions.Debug,
},
}
getCurrentJenkinsAndClientOrDie(&(jClient.JenkinsCore))
getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
var jobQueue *client.JobQueue
if jobQueue, err = jClient.Get(); err == nil {
......
......@@ -26,6 +26,7 @@ var logger *zap.Logger
// RootOptions is a global option for whole cli
type RootOptions struct {
ConfigFile string
ConfigLoad bool
Jenkins string
Debug bool
......@@ -35,6 +36,7 @@ type RootOptions struct {
InsecureSkipVerify bool
Proxy string
ProxyAuth string
ProxyDisable bool
Doctor bool
......@@ -59,7 +61,8 @@ We'd love to hear your feedback at https://github.com/jenkins-zh/jenkins-cli/iss
return
}
if needReadConfig(cmd) {
rootOptions.ConfigLoad = !("false" == os.Getenv("JCLI_CONFIG_LOAD"))
if rootOptions.ConfigLoad && needReadConfig(cmd) {
if rootOptions.ConfigFile == "" {
rootOptions.ConfigFile = os.Getenv("JCLI_CONFIG")
}
......@@ -144,6 +147,8 @@ var rootOptions RootOptions
func init() {
rootCmd.PersistentFlags().StringVarP(&rootOptions.ConfigFile, "configFile", "", "",
i18n.T("An alternative config file"))
rootCmd.PersistentFlags().BoolVarP(&rootOptions.ConfigLoad, "config-load", "", true,
i18n.T("If load a default config file"))
rootCmd.PersistentFlags().StringVarP(&rootOptions.Jenkins, "jenkins", "j", "",
i18n.T("Select a Jenkins server for this time"))
rootCmd.PersistentFlags().BoolVarP(&rootOptions.Debug, "debug", "", false, "Print the output into debug.html")
......@@ -164,6 +169,8 @@ func init() {
i18n.T("The proxy of connection to Jenkins"))
rootCmd.PersistentFlags().StringVarP(&rootOptions.ProxyAuth, "proxy-auth", "", "",
i18n.T("The auth of proxy of connection to Jenkins"))
rootCmd.PersistentFlags().BoolVarP(&rootOptions.ProxyDisable, "proxy-disable", "", false,
i18n.T("Disable proxy setting"))
rootCmd.SetOut(os.Stdout)
......@@ -196,6 +203,19 @@ func getCurrentJenkinsFromOptions() (jenkinsServer *JenkinsServer) {
if rootOptions.Token != "" {
jenkinsServer.Token = rootOptions.Token
}
if rootOptions.Proxy != "" {
jenkinsServer.Proxy = rootOptions.Proxy
}
if rootOptions.ProxyAuth != "" {
jenkinsServer.ProxyAuth = rootOptions.ProxyAuth
}
if rootOptions.ProxyDisable {
jenkinsServer.Proxy = ""
jenkinsServer.ProxyAuth = ""
}
}
return
}
......
......@@ -66,6 +66,7 @@ func (j *JenkinsCore) GetClient() (client *http.Client) {
func (j *JenkinsCore) ProxyHandle(request *http.Request) {
if j.ProxyAuth != "" {
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(j.ProxyAuth))
logger.Debug("setting proxy for HTTP request", zap.String("header", basicAuth))
request.Header.Add("Proxy-Authorization", basicAuth)
}
}
......@@ -116,11 +117,11 @@ func (j *JenkinsCore) GetCrumb() (crumbIssuer *JenkinsCrumb, err error) {
err = json.Unmarshal(data, &crumbIssuer)
} else if statusCode == 404 {
// return 404 if Jenkins does no have crumb
err = fmt.Errorf("crumb is disabled")
} else {
err = fmt.Errorf("unexpected status code: %d", statusCode)
}
}
return
}
......@@ -222,10 +223,14 @@ func (j *JenkinsCore) RequestWithResponse(method, api string, headers map[string
func (j *JenkinsCore) Request(method, api string, headers map[string]string, payload io.Reader) (
statusCode int, data []byte, err error) {
var (
req *http.Request
response *http.Response
req *http.Request
response *http.Response
requestURL string
)
if req, err = http.NewRequest(method, fmt.Sprintf("%s%s", j.URL, api), payload); err != nil {
requestURL = fmt.Sprintf("%s%s", j.URL, api)
logger.Debug("send HTTP request", zap.String("URL", requestURL), zap.String("method", method))
if req, err = http.NewRequest(method, requestURL, payload); err != nil {
return
}
if language != "" {
......
......@@ -110,9 +110,8 @@ var _ = Describe("common test", func() {
roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil)
crumb, err := jenkinsCore.GetCrumb()
Expect(crumb).To(BeNil())
Expect(err).To(BeNil())
_, err := jenkinsCore.GetCrumb()
Expect(err).To(HaveOccurred())
})
It("with crumb setting", func() {
......
......@@ -7,6 +7,7 @@ import (
"io/ioutil"
"log"
"net/http"
"path"
"strings"
"github.com/jenkins-zh/jenkins-cli/util"
......@@ -22,6 +23,7 @@ type PluginAPI struct {
UseMirror bool
ShowProgress bool
MirrorURL string
DownloadDir string
RoundTripper http.RoundTripper
}
......@@ -118,7 +120,7 @@ func (d *PluginAPI) ShowTrend(name string) (trend string, err error) {
}
// DownloadPlugins will download those plugins from update center
func (d *PluginAPI) DownloadPlugins(names []string) {
func (d *PluginAPI) DownloadPlugins(names []string) (err error) {
d.dependencyMap = make(map[string]string)
logger.Info("start to collect plugin dependencies...")
plugins := make([]PluginInfo, 0)
......@@ -128,7 +130,6 @@ func (d *PluginAPI) DownloadPlugins(names []string) {
}
logger.Info("ready to download plugins", zap.Int("total", len(plugins)))
var err error
for i, plugin := range plugins {
logger.Info("start to download plugin",
zap.String("name", plugin.Name),
......@@ -138,8 +139,10 @@ func (d *PluginAPI) DownloadPlugins(names []string) {
if err = d.download(plugin.URL, plugin.Name); err != nil {
logger.Error("download plugin error", zap.String("name", plugin.Name), zap.Error(err))
break
}
}
return
}
func (d *PluginAPI) getMirrorURL(url string) (mirror string) {
......@@ -157,7 +160,7 @@ func (d *PluginAPI) download(url string, name string) (err error) {
downloader := util.HTTPDownloader{
RoundTripper: d.RoundTripper,
TargetFilePath: fmt.Sprintf("%s.hpi", name),
TargetFilePath: path.Join(d.DownloadDir, fmt.Sprintf("%s.hpi", name)),
URL: url,
ShowProgress: d.ShowProgress,
}
......
package e2e
import (
"fmt"
"io"
"os/exec"
"strings"
)
// ExecuteCmd execute a jcli command
func ExecuteCmd(args ...string) {
if err := exec.Command("jcli", args...).Run(); err != nil {
panic(err)
}
}
// InstallPlugin install a plugin by jcli
func InstallPlugin(name, jenkins string, wait bool) {
ExecuteCmd("plugin", "install", name, "--url", jenkins)
fmt.Printf("install %s done\n", name)
if wait {
ExecuteCmd("center", "watch", "--util-install-complete", "--url", jenkins)
}
}
// RestartAndWait restart Jenkins then wait it
func RestartAndWait(jenkins string, outputReader io.ReadCloser) {
buf := make([]byte, 1024, 1024)
// should assert the error of restart
_ = exec.Command("jcli", "restart", "-b", "--url", jenkins).Run()
for {
if strNum, err := outputReader.Read(buf); err != nil || strings.Contains(string(buf[:strNum]), "Jenkins is fully up and running") {
break
} else {
fmt.Print(string(buf[:strNum]))
}
}
}
// WaitRunningUp wait until Jenkins running up
func WaitRunningUp(outputReader io.ReadCloser) {
buf := make([]byte, 1024, 1024)
for {
if strNum, err := outputReader.Read(buf); err != nil || strings.Contains(string(buf[:strNum]), "Jenkins is fully up and running") {
fmt.Print(string(buf[:strNum]))
break
} else {
fmt.Print(string(buf[:strNum]))
}
}
}
package e2e
import (
"github.com/stretchr/testify/assert"
"os/exec"
"testing"
)
func TestBashCompletion(t *testing.T) {
cmd := exec.Command("jcli", "completion")
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
assert.Contains(t, string(data), "bash completion for jcli")
// with options
cmd = exec.Command("jcli", "completion", "--type", "bash")
data, err = cmd.CombinedOutput()
assert.Nil(t, err)
assert.Contains(t, string(data), "bash completion for jcli")
}
func TestZshCompletion(t *testing.T) {
cmd := exec.Command("jcli", "completion", "--type", "zsh")
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
assert.Contains(t, string(data), "#compdef _jcli jcli")
}
func TestPowerShellCompletion(t *testing.T) {
cmd := exec.Command("jcli", "completion", "--type", "powerShell")
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
assert.Contains(t, string(data), "using namespace System.Management.Automation")
}
package e2e
import (
"fmt"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestListComputers(t *testing.T) {
cmd := exec.Command("jcli", "computer", "list", "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
fmt.Println(string(data))
}
package e2e
import (
"github.com/stretchr/testify/assert"
"os/exec"
"testing"
)
func TestConfigList(t *testing.T) {
cmd := exec.Command("jcli", "config", "list")
_, err := cmd.CombinedOutput()
assert.NotNil(t, err)
}
func TestConfigGenerate(t *testing.T) {
cmd := exec.Command("jcli", "config", "generate", "-i=false")
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
assert.Contains(t, string(data), "jenkins_servers")
}
func TestShowCurrentConfig(t *testing.T) {
cmd := exec.Command("jcli", "config")
data, err := cmd.CombinedOutput()
assert.NotNil(t, err)
assert.Contains(t, string(data), "Error: no config file found or no current setting")
}
package e2e
import (
"github.com/stretchr/testify/assert"
"os/exec"
"testing"
)
func TestCrumb(t *testing.T) {
cmd := exec.Command("jcli", "crumb", "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.NotNil(t, err)
assert.Contains(t, string(data), "Error: crumb is disabled")
}
package e2e
import (
"os"
"os/exec"
"path"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDoc(t *testing.T) {
tempDir := os.TempDir()
defer os.RemoveAll(tempDir)
cmd := exec.Command("jcli", "doc", tempDir)
_, err := cmd.CombinedOutput()
assert.Nil(t, err)
_, err = os.Stat(path.Join(tempDir, "jcli.md"))
assert.Nil(t, err)
}
package e2e
import (
"fmt"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestListJobType(t *testing.T) {
cmd := exec.Command("jcli", "job", "type", "--url", GetJenkinsURL())
fmt.Println(cmd.String())
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
fmt.Println(string(data))
}
package e2e
import (
"fmt"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestListPlugins(t *testing.T) {
cmd := exec.Command("jcli", "plugin", "list", "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
fmt.Println(string(data))
}
func TestSearchPlugins(t *testing.T) {
cmd := exec.Command("jcli", "plugin", "search", "localization-zh-cn", "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
fmt.Println(string(data))
}
func TestCheckUpdateCenter(t *testing.T) {
cmd := exec.Command("jcli", "plugin", "check", "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
fmt.Println(string(data))
}
func TestInstallPlugin(t *testing.T) {
cmd := exec.Command("jcli", "plugin", "install", "localization-zh-cn", "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
fmt.Println(string(data))
}
func TestDownloadPlugin(t *testing.T) {
tempDir := os.TempDir()
defer os.Remove(tempDir)
cmd := exec.Command("jcli", "plugin", "download", "localization-zh-cn",
"--download-dir", tempDir, "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
fmt.Println(string(data))
}
package e2e
import (
"fmt"
"github.com/stretchr/testify/assert"
"os/exec"
"testing"
)
func TestListQueue(t *testing.T) {
cmd := exec.Command("jcli", "queue", "list", "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.Nil(t, err, fmt.Sprintf("failed in cmd queue list, output is %s", string(data)))
assert.Contains(t, string(data), "ID Why URL")
}
package e2e
import (
"github.com/stretchr/testify/assert"
"os/exec"
"testing"
)
func TestRoot(t *testing.T) {
cmd := exec.Command("jcli")
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
assert.Contains(t, string(data), "Jenkins CLI (jcli) manage your Jenkins")
}
package e2e
import (
"fmt"
"github.com/phayes/freeport"
"io"
"os"
"os/exec"
"testing"
)
var jenkinsURL string
func GetJenkinsURL() string {
return jenkinsURL
}
func TestMain(m *testing.M) {
var err error
version := os.Getenv("JENKINS_VERSION")
os.Setenv("PATH", ".:"+os.Getenv("PATH"))
javaHome := os.Getenv("JCLI_JAVA_HOME")
if javaHome != "" {
os.Setenv("PATH", javaHome+"/bin:"+os.Getenv("PATH"))
}
if err = os.Setenv("JCLI_CONFIG_LOAD", "false"); err != nil {
panic(err)
}
if version == "" {
return
}
var port int
if port, err = freeport.GetFreePort(); err != nil {
fmt.Println("get free port error", err)
panic(err)
}
jenkinsURL = fmt.Sprintf("http://localhost:%d", port)
cmd := exec.Command("jcli", "center", "start", "--random-web-dir", "--setup-wizard=false", "--port", fmt.Sprintf("%d", port), "--version", version)
fmt.Println(cmd.String())
cmdStderrPipe, _ := cmd.StderrPipe()
if err = cmd.Start(); err != nil {
panic(err)
}
go func(reader io.ReadCloser, cmd *exec.Cmd) {
WaitRunningUp(reader)
m.Run()
if err = cmd.Process.Kill(); err != nil {
panic(err)
}
}(cmdStderrPipe, cmd)
err = cmd.Wait()
}
package withdependencies
import (
"github.com/stretchr/testify/assert"
"os/exec"
"testing"
)
func TestCascExport(t *testing.T) {
cmd := exec.Command("jcli", "casc", "export", "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
assert.Contains(t, string(data), "adminAddress")
}
package withdependencies
import (
"fmt"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestListCredentials(t *testing.T) {
cmd := exec.Command("jcli", "credential", "list", "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
fmt.Println(string(data))
}
package withdependencies
import (
"fmt"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSearchJobs(t *testing.T) {
cmd := exec.Command("jcli", "job", "search", "--url", GetJenkinsURL())
data, err := cmd.CombinedOutput()
assert.Nil(t, err)
fmt.Println(string(data))
}
func TestCreateJob(t *testing.T) {
cmd := exec.Command("jcli", "job", "create", "fake",
"--type", "com.cloudbees.hudson.plugins.folder.Folder", "--url", GetJenkinsURL())
_, err := cmd.CombinedOutput()
assert.Nil(t, err)
}
package withdependencies
import (
"fmt"
"github.com/jenkins-zh/jenkins-cli/e2e"
"github.com/phayes/freeport"
"io"
"os"
"os/exec"
"testing"
)
var jenkinsURL string
func GetJenkinsURL() string {
return jenkinsURL
}
func TestMain(m *testing.M) {
var err error
version := os.Getenv("JENKINS_VERSION")
os.Setenv("PATH", ".:"+os.Getenv("PATH"))
javaHome := os.Getenv("JCLI_JAVA_HOME")
if javaHome != "" {
os.Setenv("PATH", javaHome+"/bin:"+os.Getenv("PATH"))
}
if err = os.Setenv("JCLI_CONFIG_LOAD", "false"); err != nil {
panic(err)
}
if version == "" {
return
}
var port int
if port, err = freeport.GetFreePort(); err != nil {
fmt.Println("get free port error", err)
panic(err)
}
jenkinsURL = fmt.Sprintf("http://localhost:%d", port)
cmd := exec.Command("jcli", "center", "start", "--random-web-dir", "--setup-wizard=false", "--port", fmt.Sprintf("%d", port), "--version", version)
fmt.Println(cmd.String())
cmdStderrPipe, _ := cmd.StderrPipe()
err = cmd.Start()
if err != nil {
panic(err)
}
go func(reader io.ReadCloser, cmd *exec.Cmd) {
e2e.WaitRunningUp(reader)
e2e.InstallPlugin("localization-zh-cn", GetJenkinsURL(), true)
e2e.RestartAndWait(GetJenkinsURL(), reader)
e2e.ExecuteCmd("center", "mirror", "--url", GetJenkinsURL())
e2e.ExecuteCmd("plugin", "check", "--url", GetJenkinsURL())
e2e.InstallPlugin("configuration-as-code", GetJenkinsURL(), true)
e2e.InstallPlugin("pipeline-restful-api", GetJenkinsURL(), true)
e2e.RestartAndWait(GetJenkinsURL(), reader)
m.Run()
if err = cmd.Process.Kill(); err != nil {
panic(err)
}
}(cmdStderrPipe, cmd)
err = cmd.Wait()
}
......@@ -20,6 +20,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.9.0
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.6.3 // indirect
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册