Refactor Build.SharedEnv as FeaturesInfo

parent a77eace3
Pipeline #12253194 passed with stages
in 26 minutes and 19 seconds
......@@ -84,6 +84,7 @@ func mockingExecutionStack(t *testing.T, executorName string, maxBuilds int, job
//ExecutorProvider
p.On("Create").Return(&e).Times(maxBuilds)
p.On("GetFeatures", mock.Anything).Times(maxBuilds)
p.On("Acquire", mock.Anything).Return(&common.MockExecutorData{}, nil).Times(maxBuilds)
p.On("Release", mock.Anything, mock.Anything).Return(nil).Times(maxBuilds)
......
......@@ -60,13 +60,14 @@ const (
type Build struct {
JobResponse `yaml:",inline"`
SystemInterrupt chan os.Signal `json:"-" yaml:"-"`
RootDir string `json:"-" yaml:"-"`
BuildDir string `json:"-" yaml:"-"`
CacheDir string `json:"-" yaml:"-"`
Hostname string `json:"-" yaml:"-"`
Runner *RunnerConfig `json:"runner"`
ExecutorData ExecutorData
SystemInterrupt chan os.Signal `json:"-" yaml:"-"`
RootDir string `json:"-" yaml:"-"`
BuildDir string `json:"-" yaml:"-"`
CacheDir string `json:"-" yaml:"-"`
Hostname string `json:"-" yaml:"-"`
Runner *RunnerConfig `json:"runner"`
ExecutorData ExecutorData
ExecutorFeatures FeaturesInfo `json:"-" yaml:"-"`
// Unique ID for all running builds on this runner
RunnerID int `json:"runner_id"`
......@@ -74,8 +75,6 @@ type Build struct {
// Unique ID for all running builds on this runner and this project
ProjectRunnerID int `json:"project_runner_id"`
SharedEnv bool
CurrentStage BuildStage
CurrentState BuildRuntimeState
......@@ -385,6 +384,8 @@ func (b *Build) Run(globalConfig *Config, trace JobTrace) (err error) {
return errors.New("executor not found")
}
provider.GetFeatures(&b.ExecutorFeatures)
executor, err = b.retryCreateExecutor(options, provider, logger)
if err == nil {
err = b.run(context, executor)
......@@ -430,6 +431,10 @@ func (b *Build) GetGitTLSVariables() JobVariables {
return variables
}
func (b *Build) IsSharedEnv() bool {
return b.ExecutorFeatures.Shared
}
func (b *Build) GetAllVariables() JobVariables {
if b.allVariables != nil {
return b.allVariables
......@@ -443,7 +448,7 @@ func (b *Build) GetAllVariables() JobVariables {
variables = append(variables, b.GetCITLSVariables()...)
variables = append(variables, b.Variables...)
if b.SharedEnv {
if b.IsSharedEnv() {
variables = append(variables, JobVariable{Key: "CI_SHARED_ENVIRONMENT", Value: "true", Public: true, Internal: true, File: false})
} else {
variables = append(variables, JobVariable{Key: "CI_DISPOSABLE_ENVIRONMENT", Value: "true", Public: true, Internal: true, File: false})
......
......@@ -27,6 +27,7 @@ func TestBuildRun(t *testing.T) {
// Create executor only once
p.On("Create").Return(&e).Once()
p.On("GetFeatures", mock.Anything).Once()
// We run everything once
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
......@@ -64,6 +65,7 @@ func TestRetryPrepare(t *testing.T) {
// Create executor
p.On("Create").Return(&e).Times(3)
p.On("GetFeatures", mock.Anything).Once()
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
......@@ -104,6 +106,7 @@ func TestPrepareFailure(t *testing.T) {
// Create executor
p.On("Create").Return(&e).Times(3)
p.On("GetFeatures", mock.Anything).Once()
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
......@@ -135,6 +138,7 @@ func TestPrepareFailureOnBuildError(t *testing.T) {
// Create executor
p.On("Create").Return(&e).Times(1)
p.On("GetFeatures", mock.Anything).Once()
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
......@@ -166,6 +170,7 @@ func TestRunFailure(t *testing.T) {
// Create executor
p.On("Create").Return(&e).Once()
p.On("GetFeatures", mock.Anything).Once()
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
......@@ -201,6 +206,7 @@ func TestGetSourcesRunFailure(t *testing.T) {
// Create executor
p.On("Create").Return(&e).Once()
p.On("GetFeatures", mock.Anything).Once()
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
......@@ -239,6 +245,7 @@ func TestArtifactDownloadRunFailure(t *testing.T) {
// Create executor
p.On("Create").Return(&e).Once()
p.On("GetFeatures", mock.Anything).Once()
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
......@@ -277,6 +284,7 @@ func TestArtifactUploadRunFailure(t *testing.T) {
// Create executor
p.On("Create").Return(&e).Once()
p.On("GetFeatures", mock.Anything).Once()
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
......@@ -322,6 +330,7 @@ func TestRestoreCacheRunFailure(t *testing.T) {
// Create executor
p.On("Create").Return(&e).Once()
p.On("GetFeatures", mock.Anything).Once()
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
......@@ -359,6 +368,7 @@ func TestRunWrongAttempts(t *testing.T) {
// Create executor
p.On("Create").Return(&e)
p.On("GetFeatures", mock.Anything).Once()
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
......@@ -394,6 +404,7 @@ func TestRunSuccessOnSecondAttempt(t *testing.T) {
// Create executor only once
p.On("Create").Return(&e).Once()
p.On("GetFeatures", mock.Anything).Once()
// We run everything once
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
......@@ -452,7 +463,9 @@ func TestSharedEnvVariables(t *testing.T) {
for _, shared := range [...]bool{true, false} {
t.Run(fmt.Sprintf("Value:%v", shared), func(t *testing.T) {
assert := assert.New(t)
build := Build{SharedEnv: shared}
build := Build{
ExecutorFeatures: FeaturesInfo{Shared: shared},
}
vars := build.GetAllVariables().StringList()
assert.NotNil(vars)
......
......@@ -48,6 +48,7 @@ type FeaturesInfo struct {
Services bool `json:"services"`
Artifacts bool `json:"features"`
Cache bool `json:"cache"`
Shared bool `json:"shared"`
}
type RegisterRunnerRequest struct {
......
......@@ -769,7 +769,7 @@ func TestPrepare(t *testing.T) {
err := e.Prepare(prepareOptions)
if err != nil {
assert.False(t, test.Build.SharedEnv)
assert.False(t, test.Build.IsSharedEnv())
if test.Error {
assert.Error(t, err)
} else {
......
......@@ -49,7 +49,6 @@ func (s *executor) Prepare(options common.ExecutorPrepareOptions) error {
if err != nil {
return err
}
options.Build.SharedEnv = true
s.Println("Using Shell executor...")
return nil
......@@ -155,6 +154,7 @@ func init() {
featuresUpdater := func(features *common.FeaturesInfo) {
features.Variables = true
features.Shared = true
}
common.RegisterExecutor("shell", executors.DefaultExecutorProvider{
......
......@@ -52,7 +52,7 @@ func runBuildWithTrace(t *testing.T, build *common.Build, trace *common.Trace) e
func runBuild(t *testing.T, build *common.Build) error {
err := runBuildWithTrace(t, build, &common.Trace{Writer: os.Stdout})
assert.True(t, build.SharedEnv)
assert.True(t, build.IsSharedEnv())
return err
}
......
......@@ -18,7 +18,6 @@ func (s *executor) Prepare(options common.ExecutorPrepareOptions) error {
if err != nil {
return err
}
options.Build.SharedEnv = true
s.Println("Using SSH executor...")
if s.BuildShell.PassFile {
......@@ -85,6 +84,7 @@ func init() {
featuresUpdater := func(features *common.FeaturesInfo) {
features.Variables = true
features.Shared = true
}
common.RegisterExecutor("ssh", executors.DefaultExecutorProvider{
......
......@@ -26,7 +26,7 @@ var (
const SSH_SERVER_PRIVATE_KEY = "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIJCgNhsvCiKATDBXmRYHQfXIatKKOXGrmBLEVtGZtVv7oAoGCCqGSM49\nAwEHoUQDQgAE+36GvpVV34STGaV+YU4HHXCtJjburfo8IQDVTgLRwAkoLqLIl1cO\nduKDmdmeG/n66BNH1rJUkXFfEr4OYbZH5g==\n-----END EC PRIVATE KEY-----"
func TestPrepareSharedEnv(t *testing.T) {
func TestPrepare(t *testing.T) {
runnerConfig := &common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Executor: "ssh",
......@@ -64,8 +64,14 @@ func TestPrepareSharedEnv(t *testing.T) {
Context: context.TODO(),
}
assert.False(t, build.SharedEnv)
err = e.Prepare(prepareOptions)
assert.NoError(t, err)
assert.True(t, build.SharedEnv)
}
func TestSharedEnv(t *testing.T) {
provider := common.GetExecutor("ssh")
features := &common.FeaturesInfo{}
provider.GetFeatures(features)
assert.True(t, features.Shared)
}
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