Commit 281644e9 authored by Steve Azzopardi's avatar Steve Azzopardi
Browse files

Merge branch '26642-test-more-executors-reg' into 'master'

Test more executors in TestAskRunnerOverrideDefaults

See merge request gitlab-org/gitlab-runner!2406
parents 1aa4b2fc a6fd118a
......@@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
......@@ -21,6 +22,9 @@ import (
clihelpers "gitlab.com/ayufan/golang-cli-helpers"
"gitlab.com/gitlab-org/gitlab-runner/common"
_ "gitlab.com/gitlab-org/gitlab-runner/executors/docker/machine"
_ "gitlab.com/gitlab-org/gitlab-runner/executors/kubernetes"
"gitlab.com/gitlab-org/gitlab-runner/helpers/ssh"
)
func setupDockerRegisterCommand(dockerConfig *common.DockerConfig) *RegisterCommand {
......@@ -205,29 +209,46 @@ func TestAccessLevelSetting(t *testing.T) {
}
}
func TestAskRunnerOverrideDefaults(t *testing.T) {
basicValidation := func(s *RegisterCommand) bool {
return s.URL == "http://gitlab.example.com/" &&
s.Token == "test-runner-token" &&
s.RunnerSettings.Executor == "docker" &&
s.RunnerSettings.Docker.Image == "busybox:latest"
func TestAskRunnerOverrideDefaultsForExecutors(t *testing.T) {
executors := []string{
"kubernetes",
"docker+machine",
"docker-ssh+machine",
"docker",
"docker-ssh",
"ssh",
"custom",
"parallels",
"virtualbox",
"shell",
}
if runtime.GOOS == osTypeWindows {
executors = append(executors, "docker-windows")
}
for _, executor := range executors {
t.Run(executor, func(t *testing.T) { testAskRunnerOverrideDefaultsForExecutor(t, executor) })
}
}
func testAskRunnerOverrideDefaultsForExecutor(t *testing.T, executor string) {
basicValidation := func(s *RegisterCommand) {
assertExecutorDefaultValues(t, executor, s)
}
tests := map[string]struct {
answers []string
arguments []string
validate func(s *RegisterCommand) bool
validate func(s *RegisterCommand)
expectedParams func(common.RegisterRunnerParameters) bool
}{
"basic answers": {
answers: []string{
answers: append([]string{
"http://gitlab.example.com/",
"test-registration-token",
"name",
"tag,list",
"docker",
"busybox:latest",
},
}, executorAnswers(t, executor)...),
validate: basicValidation,
expectedParams: func(p common.RegisterRunnerParameters) bool {
return p == common.RegisterRunnerParameters{
......@@ -239,17 +260,16 @@ func TestAskRunnerOverrideDefaults(t *testing.T) {
},
},
"basic arguments, accepting provided": {
answers: []string{"", "", "", "", "", ""},
arguments: []string{
answers: make([]string, 10),
arguments: append(
executorCmdLineArgs(t, executor),
"--url", "http://gitlab.example.com/",
"-r", "test-registration-token",
"--name", "name",
"--tag-list", "tag,list",
"--executor", "docker",
"--docker-image", "busybox:latest",
"--paused",
"--locked=false",
},
),
validate: basicValidation,
expectedParams: func(p common.RegisterRunnerParameters) bool {
return p == common.RegisterRunnerParameters{
......@@ -259,22 +279,18 @@ func TestAskRunnerOverrideDefaults(t *testing.T) {
},
},
"basic arguments override": {
answers: []string{"", "", "new-name", "", "", "nginx:latest"},
arguments: []string{
answers: append([]string{"", "", "new-name", "", ""}, executorOverrideAnswers(t, executor)...),
arguments: append(
executorCmdLineArgs(t, executor),
"--url", "http://gitlab.example.com/",
"-r", "test-registration-token",
"--name", "name",
"--tag-list", "tag,list",
"--executor", "docker",
"--docker-image", "ruby:2.6.6",
"--paused",
"--locked=false",
},
validate: func(s *RegisterCommand) bool {
return s.URL == "http://gitlab.example.com/" &&
s.Token == "test-runner-token" &&
s.RunnerSettings.Executor == "docker" &&
s.RunnerSettings.Docker.Image == "nginx:latest"
),
validate: func(s *RegisterCommand) {
assertExecutorOverridenValues(t, executor, s)
},
expectedParams: func(p common.RegisterRunnerParameters) bool {
return p == common.RegisterRunnerParameters{
......@@ -284,14 +300,12 @@ func TestAskRunnerOverrideDefaults(t *testing.T) {
},
},
"untagged implicit": {
answers: []string{
answers: append([]string{
"http://gitlab.example.com/",
"test-registration-token",
"name",
"",
"docker",
"busybox:latest",
},
}, executorAnswers(t, executor)...),
validate: basicValidation,
expectedParams: func(p common.RegisterRunnerParameters) bool {
return p == common.RegisterRunnerParameters{
......@@ -303,14 +317,12 @@ func TestAskRunnerOverrideDefaults(t *testing.T) {
},
},
"untagged explicit": {
answers: []string{
answers: append([]string{
"http://gitlab.example.com/",
"test-registration-token",
"name",
"",
"docker",
"busybox:latest",
},
}, executorAnswers(t, executor)...),
arguments: []string{"--run-untagged"},
validate: basicValidation,
expectedParams: func(p common.RegisterRunnerParameters) bool {
......@@ -323,14 +335,12 @@ func TestAskRunnerOverrideDefaults(t *testing.T) {
},
},
"untagged explicit with tags provided": {
answers: []string{
answers: append([]string{
"http://gitlab.example.com/",
"test-registration-token",
"name",
"tag,list",
"docker",
"busybox:latest",
},
}, executorAnswers(t, executor)...),
arguments: []string{"--run-untagged"},
validate: basicValidation,
expectedParams: func(p common.RegisterRunnerParameters) bool {
......@@ -373,13 +383,223 @@ func TestAskRunnerOverrideDefaults(t *testing.T) {
err := app.Run(append([]string{"runner", "register"}, tc.arguments...))
output := getLogrusOutput(t, hook)
assert.True(t, tc.validate(cmd))
assert.NoError(t, err)
tc.validate(cmd)
assert.Contains(t, output, "Runner registered successfully.")
})
}
}
func assertExecutorDefaultValues(t *testing.T, executor string, s *RegisterCommand) {
assert.Equal(t, "http://gitlab.example.com/", s.URL)
assert.Equal(t, "test-runner-token", s.Token)
assert.Equal(t, executor, s.RunnerSettings.Executor)
switch executor {
case "kubernetes":
assert.NotNil(t, s.RunnerSettings.Kubernetes)
case "custom":
assert.NotNil(t, s.RunnerSettings.Custom)
case "shell":
assert.NotNil(t, s.RunnerSettings.Shell)
if runtime.GOOS == osTypeWindows && s.RunnerConfig.Shell == "" {
assert.Equal(t, "powershell", s.RunnerSettings.Shell)
}
case "docker":
require.NotNil(t, s.RunnerSettings.Docker)
assert.Equal(t, "busybox:latest", s.RunnerSettings.Docker.Image)
case "docker-windows":
require.NotNil(t, s.RunnerSettings.Docker)
assert.Equal(t, "mcr.microsoft.com/windows/servercore:1809", s.RunnerSettings.Docker.Image)
case "docker+machine":
assert.NotNil(t, s.RunnerSettings.Machine)
require.NotNil(t, s.RunnerSettings.Docker)
assert.Equal(t, "busybox:latest", s.RunnerSettings.Docker.Image)
case "docker-ssh":
assertDefaultSSHLogin(t, s.RunnerSettings.SSH)
require.NotNil(t, s.RunnerSettings.Docker)
assert.Equal(t, "busybox:latest", s.RunnerSettings.Docker.Image)
case "docker-ssh+machine":
assert.NotNil(t, s.RunnerSettings.Machine)
assertDefaultSSHLogin(t, s.RunnerSettings.SSH)
require.NotNil(t, s.RunnerSettings.Docker)
assert.Equal(t, "busybox:latest", s.RunnerSettings.Docker.Image)
case "ssh":
assertDefaultSSHLogin(t, s.RunnerSettings.SSH)
assertDefaultSSHServer(t, s.RunnerSettings.SSH)
case "parallels":
assertDefaultSSHServer(t, s.RunnerSettings.SSH)
require.NotNil(t, s.RunnerSettings.Parallels)
assert.Equal(t, executor+"-vm-name", s.RunnerSettings.Parallels.BaseName)
case "virtualbox":
assertDefaultSSHLogin(t, s.RunnerSettings.SSH)
require.NotNil(t, s.RunnerSettings.VirtualBox)
assert.Equal(t, executor+"-vm-name", s.RunnerSettings.VirtualBox.BaseName)
default:
assert.FailNow(t, "no assertions found for executor", executor)
}
}
func assertDefaultSSHLogin(t *testing.T, sshCfg *ssh.Config) {
require.NotNil(t, sshCfg)
assert.Equal(t, "user", sshCfg.User)
assert.Equal(t, "password", sshCfg.Password)
assert.Equal(t, "/home/user/.ssh/id_rsa", sshCfg.IdentityFile)
}
func assertDefaultSSHServer(t *testing.T, sshCfg *ssh.Config) {
require.NotNil(t, sshCfg)
assert.Equal(t, "gitlab.example.com", sshCfg.Host)
assert.Equal(t, "22", sshCfg.Port)
}
func assertExecutorOverridenValues(t *testing.T, executor string, s *RegisterCommand) {
assert.Equal(t, "http://gitlab.example.com/", s.URL)
assert.Equal(t, "test-runner-token", s.Token)
assert.Equal(t, executor, s.RunnerSettings.Executor)
switch executor {
case "kubernetes":
assert.NotNil(t, s.RunnerSettings.Kubernetes)
case "custom":
assert.NotNil(t, s.RunnerSettings.Custom)
case "shell":
assert.NotNil(t, s.RunnerSettings.Shell)
if runtime.GOOS == osTypeWindows && s.RunnerConfig.Shell == "" {
assert.Equal(t, "powershell", s.RunnerSettings.Shell)
}
case "docker":
require.NotNil(t, s.RunnerSettings.Docker)
assert.Equal(t, "nginx:latest", s.RunnerSettings.Docker.Image)
case "docker-windows":
require.NotNil(t, s.RunnerSettings.Docker)
assert.Equal(t, "mcr.microsoft.com/windows/servercore:1903", s.RunnerSettings.Docker.Image)
case "docker+machine":
assert.NotNil(t, s.RunnerSettings.Machine)
require.NotNil(t, s.RunnerSettings.Docker)
assert.Equal(t, "nginx:latest", s.RunnerSettings.Docker.Image)
case "docker-ssh":
assertOverridenSSHLogin(t, s.RunnerSettings.SSH)
require.NotNil(t, s.RunnerSettings.Docker)
assert.Equal(t, "nginx:latest", s.RunnerSettings.Docker.Image)
case "docker-ssh+machine":
assert.NotNil(t, s.Machine)
assertOverridenSSHLogin(t, s.RunnerSettings.SSH)
require.NotNil(t, s.RunnerSettings.Docker)
assert.Equal(t, "nginx:latest", s.RunnerSettings.Docker.Image)
case "ssh":
assertOverridenSSHLogin(t, s.RunnerSettings.SSH)
assertOverridenSSHServer(t, s.RunnerSettings.SSH)
case "parallels":
assertOverridenSSHServer(t, s.RunnerSettings.SSH)
require.NotNil(t, s.RunnerSettings.Parallels)
assert.Equal(t, "override-"+executor+"-vm-name", s.RunnerSettings.Parallels.BaseName)
case "virtualbox":
assertOverridenSSHLogin(t, s.RunnerSettings.SSH)
require.NotNil(t, s.RunnerSettings.VirtualBox)
assert.Equal(t, "override-"+executor+"-vm-name", s.RunnerSettings.VirtualBox.BaseName)
default:
assert.FailNow(t, "no assertions found for executor", executor)
}
}
func assertOverridenSSHLogin(t *testing.T, sshCfg *ssh.Config) {
require.NotNil(t, sshCfg)
assert.Equal(t, "root", sshCfg.User)
assert.Equal(t, "admin", sshCfg.Password)
assert.Equal(t, "/root/.ssh/id_rsa", sshCfg.IdentityFile)
}
func assertOverridenSSHServer(t *testing.T, sshCfg *ssh.Config) {
require.NotNil(t, sshCfg)
assert.Equal(t, "ssh.gitlab.example.com", sshCfg.Host)
assert.Equal(t, "8822", sshCfg.Port)
}
func executorAnswers(t *testing.T, executor string) []string {
values := map[string][]string{
"kubernetes": {executor},
"custom": {executor},
"shell": {executor},
"docker": {executor, "busybox:latest"},
"docker-windows": {executor, "mcr.microsoft.com/windows/servercore:1809"},
"docker+machine": {executor, "busybox:latest"},
"docker-ssh": {executor, "busybox:latest", "user", "password", "/home/user/.ssh/id_rsa"},
"docker-ssh+machine": {executor, "busybox:latest", "user", "password", "/home/user/.ssh/id_rsa"},
"ssh": {executor, "gitlab.example.com", "22", "user", "password", "/home/user/.ssh/id_rsa"},
"parallels": {executor, "parallels-vm-name", "gitlab.example.com", "22"},
"virtualbox": {executor, "virtualbox-vm-name", "user", "password", "/home/user/.ssh/id_rsa"},
}
answers, ok := values[executor]
if !ok {
assert.FailNow(t, "No answers found for executor", executor)
}
return answers
}
func executorOverrideAnswers(t *testing.T, executor string) []string {
values := map[string][]string{
"kubernetes": {""},
"custom": {""},
"shell": {""},
"docker": {"nginx:latest"},
"docker-windows": {"mcr.microsoft.com/windows/servercore:1903"},
"docker+machine": {"nginx:latest"},
"docker-ssh": {"nginx:latest", "root", "admin", "/root/.ssh/id_rsa"},
"docker-ssh+machine": {"nginx:latest", "root", "admin", "/root/.ssh/id_rsa"},
"ssh": {"ssh.gitlab.example.com", "8822", "root", "admin", "/root/.ssh/id_rsa"},
"parallels": {"override-parallels-vm-name", "ssh.gitlab.example.com", "8822"},
"virtualbox": {"override-virtualbox-vm-name", "root", "admin", "/root/.ssh/id_rsa"},
}
answers, ok := values[executor]
if !ok {
assert.FailNow(t, "No override answers found for executor", executor)
}
return answers
}
func executorCmdLineArgs(t *testing.T, executor string) []string {
values := map[string][]string{
"kubernetes": {"--executor", executor},
"custom": {"--executor", executor},
"shell": {"--executor", executor},
"docker": {"--executor", executor, "--docker-image", "busybox:latest"},
"docker-windows": {"--executor", executor, "--docker-image", "mcr.microsoft.com/windows/servercore:1809"},
"docker+machine": {"--executor", executor, "--docker-image", "busybox:latest"},
"docker-ssh": {
"--executor", executor, "--docker-image", "busybox:latest", "--ssh-user", "user",
"--ssh-password", "password",
"--ssh-identity-file", "/home/user/.ssh/id_rsa",
},
"docker-ssh+machine": {
"--executor", executor, "--docker-image", "busybox:latest", "--ssh-user", "user",
"--ssh-password", "password",
"--ssh-identity-file", "/home/user/.ssh/id_rsa",
},
"ssh": {
"--executor", executor, "--ssh-host", "gitlab.example.com", "--ssh-port", "22", "--ssh-user", "user",
"--ssh-password", "password", "--ssh-identity-file", "/home/user/.ssh/id_rsa",
},
"parallels": {
"--executor", executor, "--ssh-host", "gitlab.example.com", "--ssh-port", "22",
"--parallels-base-name", "parallels-vm-name",
},
"virtualbox": {
"--executor", executor, "--ssh-host", "gitlab.example.com", "--ssh-user", "user",
"--ssh-password", "password", "--ssh-identity-file", "/home/user/.ssh/id_rsa",
"--virtualbox-base-name", "virtualbox-vm-name",
},
}
args, ok := values[executor]
if !ok {
assert.FailNow(t, "No command line args found for executor", executor)
}
return args
}
func TestConfigTemplate_Enabled(t *testing.T) {
tests := map[string]struct {
path string
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment