github.com/secure-build/gitlab-runner@v12.5.0+incompatible/executors/docker/machine/executor_test.go (about) 1 package machine 2 3 import ( 4 "testing" 5 6 "github.com/prometheus/client_golang/prometheus" 7 "github.com/stretchr/testify/assert" 8 9 "gitlab.com/gitlab-org/gitlab-runner/common" 10 "gitlab.com/gitlab-org/gitlab-runner/helpers/docker" 11 ) 12 13 func getRunnerConfig() *common.RunnerConfig { 14 return &common.RunnerConfig{ 15 Name: "runner", 16 RunnerSettings: common.RunnerSettings{ 17 Executor: "docker+machine", 18 Docker: &common.DockerConfig{ 19 DockerCredentials: docker_helpers.DockerCredentials{}, 20 Image: "alpine", 21 }, 22 }, 23 } 24 } 25 26 func getRunnerConfigWithoutDockerConfig() *common.RunnerConfig { 27 return &common.RunnerConfig{ 28 Name: "runner", 29 RunnerSettings: common.RunnerSettings{ 30 Executor: "docker+machine", 31 }, 32 } 33 } 34 35 type machineCredentialsUsageFakeExecutor struct { 36 t *testing.T 37 38 expectedmachineCredentials docker_helpers.DockerCredentials 39 expectedRunnerConfig *common.RunnerConfig 40 } 41 42 func (e *machineCredentialsUsageFakeExecutor) assertRunnerConfiguration(runnerConfig *common.RunnerConfig) { 43 assert.Equal(e.t, e.expectedRunnerConfig.Name, runnerConfig.Name) 44 assert.Equal(e.t, e.expectedRunnerConfig.RunnerSettings.Executor, runnerConfig.RunnerSettings.Executor) 45 if e.expectedRunnerConfig.Docker != nil { 46 assert.Equal(e.t, e.expectedRunnerConfig.Docker.Image, runnerConfig.Docker.Image) 47 } 48 assert.Equal(e.t, e.expectedmachineCredentials, runnerConfig.Docker.DockerCredentials, "DockerCredentials should be filled with machine's credentials") 49 50 } 51 52 func (e *machineCredentialsUsageFakeExecutor) Prepare(options common.ExecutorPrepareOptions) error { 53 e.assertRunnerConfiguration(options.Config) 54 e.assertRunnerConfiguration(options.Build.Runner) 55 return nil 56 } 57 58 func (e *machineCredentialsUsageFakeExecutor) Shell() *common.ShellScriptInfo { return nil } 59 func (e *machineCredentialsUsageFakeExecutor) Run(cmd common.ExecutorCommand) error { return nil } 60 func (e *machineCredentialsUsageFakeExecutor) Finish(err error) {} 61 func (e *machineCredentialsUsageFakeExecutor) Cleanup() {} 62 func (e *machineCredentialsUsageFakeExecutor) SetCurrentStage(stage common.ExecutorStage) {} 63 func (e *machineCredentialsUsageFakeExecutor) GetCurrentStage() common.ExecutorStage { 64 return common.ExecutorStageCreated 65 } 66 67 func testMachineCredentialsUsage(t *testing.T, name string, runnerConfigSource func() *common.RunnerConfig) { 68 t.Run(name, func(t *testing.T) { 69 machineName := "expected-machine" 70 machineCredentials := docker_helpers.DockerCredentials{ 71 Host: "tcp://expected-host:1234", 72 } 73 74 runnerConfig := runnerConfigSource() 75 options := common.ExecutorPrepareOptions{ 76 Config: runnerConfig, 77 Build: &common.Build{ 78 Runner: runnerConfig, 79 ExecutorData: &machineDetails{ 80 Name: machineName, 81 State: machineStateAcquired, 82 }, 83 }, 84 } 85 86 machine := &docker_helpers.MockMachine{} 87 defer machine.AssertExpectations(t) 88 89 machine.On("CanConnect", machineName, true). 90 Return(true).Once() 91 machine.On("Credentials", machineName). 92 Return(machineCredentials, nil).Once() 93 94 executorProvider := &common.MockExecutorProvider{} 95 defer executorProvider.AssertExpectations(t) 96 97 fakeExecutor := &machineCredentialsUsageFakeExecutor{ 98 t: t, 99 expectedmachineCredentials: machineCredentials, 100 expectedRunnerConfig: runnerConfigSource(), 101 } 102 executorProvider.On("Create"). 103 Return(fakeExecutor).Once() 104 105 e := &machineExecutor{ 106 provider: &machineProvider{ 107 machine: machine, 108 provider: executorProvider, 109 totalActions: prometheus.NewCounterVec( 110 prometheus.CounterOpts{ 111 Name: "actions_total", 112 Help: "actions_total", 113 }, 114 []string{"action"}, 115 ), 116 }, 117 } 118 err := e.Prepare(options) 119 assert.NoError(t, err) 120 }) 121 } 122 123 func TestMachineCredentialsUsage(t *testing.T) { 124 testMachineCredentialsUsage(t, "config-with-docker-section", getRunnerConfig) 125 testMachineCredentialsUsage(t, "config-without-docker-section", getRunnerConfigWithoutDockerConfig) 126 }