build_test.go 22.2 KB
Newer Older
Kamil Trzciński's avatar
Kamil Trzciński committed
1 2 3
package common

import (
4
	"errors"
5
	"fmt"
Kamil Trzciński's avatar
Kamil Trzciński committed
6
	"os"
7
	"path/filepath"
Kamil Trzciński's avatar
Kamil Trzciński committed
8
	"testing"
Shinya Maeda's avatar
Shinya Maeda committed
9
	"time"
Kamil Trzciński's avatar
Kamil Trzciński committed
10

11
	"github.com/sirupsen/logrus/hooks/test"
Kamil Trzciński's avatar
Kamil Trzciński committed
12 13
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
14
	"github.com/stretchr/testify/require"
Kamil Trzciński's avatar
Kamil Trzciński committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
)

func init() {
	s := MockShell{}
	s.On("GetName").Return("script-shell")
	s.On("GenerateScript", mock.Anything, mock.Anything).Return("script", nil)
	RegisterShell(&s)
}

func TestBuildRun(t *testing.T) {
	e := MockExecutor{}
	defer e.AssertExpectations(t)

	p := MockExecutorProvider{}
	defer p.AssertExpectations(t)

	// Create executor only once
Kamil Trzciński's avatar
Kamil Trzciński committed
32 33 34 35
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

Kamil Trzciński's avatar
Kamil Trzciński committed
36 37 38 39 40 41 42 43 44
	p.On("Create").Return(&e).Once()

	// We run everything once
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
	e.On("Finish", nil).Return().Once()
	e.On("Cleanup").Return().Once()

	// Run script successfully
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
Kamil Trzciński's avatar
Kamil Trzciński committed
45 46 47 48 49 50 51 52
	e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageDownloadArtifacts)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageUserScript)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageAfterScript)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageArchiveCache)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageUploadOnSuccessArtifacts)).Return(nil).Once()
Kamil Trzciński's avatar
Kamil Trzciński committed
53 54 55

	RegisterExecutor("build-run-test", &p)

56 57
	successfulBuild, err := GetSuccessfulBuild()
	assert.NoError(t, err)
Kamil Trzciński's avatar
Kamil Trzciński committed
58
	build := &Build{
59
		JobResponse: successfulBuild,
Kamil Trzciński's avatar
Kamil Trzciński committed
60 61 62 63 64 65
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-run-test",
			},
		},
	}
66
	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
Kamil Trzciński's avatar
Kamil Trzciński committed
67 68 69 70 71 72 73 74 75 76 77 78 79
	assert.NoError(t, err)
}

func TestRetryPrepare(t *testing.T) {
	PreparationRetryInterval = 0

	e := MockExecutor{}
	defer e.AssertExpectations(t)

	p := MockExecutorProvider{}
	defer p.AssertExpectations(t)

	// Create executor
Kamil Trzciński's avatar
Kamil Trzciński committed
80 81 82 83
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

Kamil Trzciński's avatar
Kamil Trzciński committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	p.On("Create").Return(&e).Times(3)

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
		Return(errors.New("prepare failed")).Twice()
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
		Return(nil).Once()
	e.On("Cleanup").Return().Times(3)

	// Succeed a build script
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
	e.On("Run", mock.Anything).Return(nil)
	e.On("Finish", nil).Return().Once()

	RegisterExecutor("build-run-retry-prepare", &p)

100 101
	successfulBuild, err := GetSuccessfulBuild()
	assert.NoError(t, err)
Kamil Trzciński's avatar
Kamil Trzciński committed
102
	build := &Build{
103
		JobResponse: successfulBuild,
Kamil Trzciński's avatar
Kamil Trzciński committed
104 105 106 107 108 109
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-run-retry-prepare",
			},
		},
	}
110
	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
Kamil Trzciński's avatar
Kamil Trzciński committed
111 112 113 114 115 116 117 118 119 120 121 122 123
	assert.NoError(t, err)
}

func TestPrepareFailure(t *testing.T) {
	PreparationRetryInterval = 0

	e := MockExecutor{}
	defer e.AssertExpectations(t)

	p := MockExecutorProvider{}
	defer p.AssertExpectations(t)

	// Create executor
Kamil Trzciński's avatar
Kamil Trzciński committed
124 125 126 127
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

Kamil Trzciński's avatar
Kamil Trzciński committed
128 129 130 131 132 133 134 135 136
	p.On("Create").Return(&e).Times(3)

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
		Return(errors.New("prepare failed")).Times(3)
	e.On("Cleanup").Return().Times(3)

	RegisterExecutor("build-run-prepare-failure", &p)

137 138
	successfulBuild, err := GetSuccessfulBuild()
	assert.NoError(t, err)
Kamil Trzciński's avatar
Kamil Trzciński committed
139
	build := &Build{
140
		JobResponse: successfulBuild,
Kamil Trzciński's avatar
Kamil Trzciński committed
141 142 143 144 145 146
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-run-prepare-failure",
			},
		},
	}
147
	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
Kamil Trzciński's avatar
Kamil Trzciński committed
148 149 150
	assert.EqualError(t, err, "prepare failed")
}

151 152 153 154 155 156 157 158
func TestPrepareFailureOnBuildError(t *testing.T) {
	e := MockExecutor{}
	defer e.AssertExpectations(t)

	p := MockExecutorProvider{}
	defer p.AssertExpectations(t)

	// Create executor
Kamil Trzciński's avatar
Kamil Trzciński committed
159 160 161 162
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

163 164 165 166 167 168 169 170 171
	p.On("Create").Return(&e).Times(1)

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
		Return(&BuildError{}).Times(1)
	e.On("Cleanup").Return().Times(1)

	RegisterExecutor("build-run-prepare-failure-on-build-error", &p)

Shinya Maeda's avatar
Shinya Maeda committed
172
	successfulBuild, err := GetSuccessfulBuild()
173
	assert.NoError(t, err)
174
	build := &Build{
Shinya Maeda's avatar
Shinya Maeda committed
175
		JobResponse: successfulBuild,
176 177 178 179 180 181
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-run-prepare-failure-on-build-error",
			},
		},
	}
182
	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
183
	assert.IsType(t, err, &BuildError{})
Shinya Maeda's avatar
Shinya Maeda committed
184 185
}

Shinya Maeda's avatar
Shinya Maeda committed
186
func TestJobFailure(t *testing.T) {
187
	e := new(MockExecutor)
Shinya Maeda's avatar
Shinya Maeda committed
188 189
	defer e.AssertExpectations(t)

190
	p := new(MockExecutorProvider)
Shinya Maeda's avatar
Shinya Maeda committed
191 192 193 194 195 196 197
	defer p.AssertExpectations(t)

	// Create executor
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

198
	p.On("Create").Return(e).Times(1)
Shinya Maeda's avatar
Shinya Maeda committed
199 200 201

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
Shinya Maeda's avatar
Shinya Maeda committed
202
		Return(nil).Times(1)
Shinya Maeda's avatar
Shinya Maeda committed
203 204
	e.On("Cleanup").Return().Times(1)

Shinya Maeda's avatar
Shinya Maeda committed
205 206 207 208 209 210
	// Succeed a build script
	thrownErr := &BuildError{Inner: errors.New("test error")}
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
	e.On("Run", mock.Anything).Return(thrownErr)
	e.On("Finish", thrownErr).Return().Once()

211
	RegisterExecutor("build-run-job-failure", p)
Shinya Maeda's avatar
Shinya Maeda committed
212

Shinya Maeda's avatar
Shinya Maeda committed
213
	failedBuild, err := GetFailedBuild()
Shinya Maeda's avatar
Shinya Maeda committed
214 215
	assert.NoError(t, err)
	build := &Build{
Shinya Maeda's avatar
Shinya Maeda committed
216
		JobResponse: failedBuild,
Shinya Maeda's avatar
Shinya Maeda committed
217 218
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
Shinya Maeda's avatar
Shinya Maeda committed
219
				Executor: "build-run-job-failure",
Shinya Maeda's avatar
Shinya Maeda committed
220 221 222 223
			},
		},
	}

Shinya Maeda's avatar
Shinya Maeda committed
224 225 226 227 228 229 230 231 232 233 234 235
	trace := new(MockJobTrace)
	defer trace.AssertExpectations(t)
	trace.On("Write", mock.Anything).Return(0, nil)
	trace.On("IsStdout").Return(true)
	trace.On("SetCancelFunc", mock.Anything).Once()
	trace.On("Fail", thrownErr, ScriptFailure).Once()

	err = build.Run(&Config{}, trace)
	require.IsType(t, &BuildError{}, err)
}

func TestJobFailureOnExecutionTimeout(t *testing.T) {
236
	e := new(MockExecutor)
Shinya Maeda's avatar
Shinya Maeda committed
237 238
	defer e.AssertExpectations(t)

239
	p := new(MockExecutorProvider)
Shinya Maeda's avatar
Shinya Maeda committed
240 241 242 243 244 245 246
	defer p.AssertExpectations(t)

	// Create executor
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

247
	p.On("Create").Return(e).Times(1)
Shinya Maeda's avatar
Shinya Maeda committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
		Return(nil).Times(1)
	e.On("Cleanup").Return().Times(1)

	// Succeed a build script
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
	e.On("Run", matchBuildStage(BuildStageUserScript)).Run(func(arguments mock.Arguments) {
		time.Sleep(2 * time.Second)
	}).Return(nil)
	e.On("Run", mock.Anything).Return(nil)
	e.On("Finish", mock.Anything).Return().Once()

262
	RegisterExecutor("build-run-job-failure-on-execution-timeout", p)
Shinya Maeda's avatar
Shinya Maeda committed
263 264 265 266 267 268 269 270 271 272

	successfulBuild, err := GetSuccessfulBuild()
	assert.NoError(t, err)

	successfulBuild.RunnerInfo.Timeout = 1

	build := &Build{
		JobResponse: successfulBuild,
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
Shinya Maeda's avatar
Shinya Maeda committed
273
				Executor: "build-run-job-failure-on-execution-timeout",
Shinya Maeda's avatar
Shinya Maeda committed
274 275
			},
		},
Shinya Maeda's avatar
Shinya Maeda committed
276
	}
Shinya Maeda's avatar
Shinya Maeda committed
277 278 279 280 281 282 283 284 285 286 287 288

	trace := new(MockJobTrace)
	defer trace.AssertExpectations(t)
	trace.On("Write", mock.Anything).Return(0, nil)
	trace.On("IsStdout").Return(true)
	trace.On("SetCancelFunc", mock.Anything).Once()
	trace.On("Fail", mock.Anything, JobExecutionTimeout).Run(func(arguments mock.Arguments) {
		assert.Error(t, arguments.Get(0).(error))
	}).Once()

	err = build.Run(&Config{}, trace)
	require.IsType(t, &BuildError{}, err)
289 290
}

Kamil Trzciński's avatar
Kamil Trzciński committed
291 292 293 294 295 296
func matchBuildStage(buildStage BuildStage) interface{} {
	return mock.MatchedBy(func(cmd ExecutorCommand) bool {
		return cmd.Stage == buildStage
	})
}

Kamil Trzciński's avatar
Kamil Trzciński committed
297
func TestRunFailureRunsAfterScriptAndArtifactsOnFailure(t *testing.T) {
Kamil Trzciński's avatar
Kamil Trzciński committed
298 299 300 301 302 303 304
	e := MockExecutor{}
	defer e.AssertExpectations(t)

	p := MockExecutorProvider{}
	defer p.AssertExpectations(t)

	// Create executor
Kamil Trzciński's avatar
Kamil Trzciński committed
305 306 307 308
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

Kamil Trzciński's avatar
Kamil Trzciński committed
309 310 311 312 313 314 315 316
	p.On("Create").Return(&e).Once()

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
	e.On("Cleanup").Return().Once()

	// Fail a build script
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
Kamil Trzciński's avatar
Kamil Trzciński committed
317 318 319 320 321 322 323
	e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageDownloadArtifacts)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageUserScript)).Return(errors.New("build fail")).Once()
	e.On("Run", matchBuildStage(BuildStageAfterScript)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()
James Lopez's avatar
James Lopez committed
324
	e.On("Finish", errors.New("build fail")).Return().Once()
Kamil Trzciński's avatar
Kamil Trzciński committed
325 326 327

	RegisterExecutor("build-run-run-failure", &p)

328
	failedBuild, err := GetFailedBuild()
329
	assert.NoError(t, err)
Kamil Trzciński's avatar
Kamil Trzciński committed
330
	build := &Build{
331
		JobResponse: failedBuild,
Kamil Trzciński's avatar
Kamil Trzciński committed
332 333 334 335 336 337
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-run-run-failure",
			},
		},
	}
338 339 340 341 342 343 344 345 346 347 348 349
	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
	assert.EqualError(t, err, "build fail")
}

func TestGetSourcesRunFailure(t *testing.T) {
	e := MockExecutor{}
	defer e.AssertExpectations(t)

	p := MockExecutorProvider{}
	defer p.AssertExpectations(t)

	// Create executor
Kamil Trzciński's avatar
Kamil Trzciński committed
350 351 352 353
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

354 355 356 357 358 359 360 361
	p.On("Create").Return(&e).Once()

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
	e.On("Cleanup").Return()

	// Fail a build script
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
Kamil Trzciński's avatar
Kamil Trzciński committed
362 363 364
	e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageGetSources)).Return(errors.New("build fail")).Times(3)
	e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()
365 366 367 368 369 370 371
	e.On("Finish", errors.New("build fail")).Return().Once()

	RegisterExecutor("build-get-sources-run-failure", &p)

	successfulBuild, err := GetSuccessfulBuild()
	assert.NoError(t, err)
	build := &Build{
372
		JobResponse: successfulBuild,
373 374 375 376 377 378 379
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-get-sources-run-failure",
			},
		},
	}

380
	build.Variables = append(build.Variables, JobVariable{Key: "GET_SOURCES_ATTEMPTS", Value: "3"})
381 382 383 384 385 386 387 388 389 390 391 392
	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
	assert.EqualError(t, err, "build fail")
}

func TestArtifactDownloadRunFailure(t *testing.T) {
	e := MockExecutor{}
	defer e.AssertExpectations(t)

	p := MockExecutorProvider{}
	defer p.AssertExpectations(t)

	// Create executor
Kamil Trzciński's avatar
Kamil Trzciński committed
393 394 395 396
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

397 398 399 400 401 402 403 404
	p.On("Create").Return(&e).Once()

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
	e.On("Cleanup").Return()

	// Fail a build script
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
Kamil Trzciński's avatar
Kamil Trzciński committed
405 406 407 408 409
	e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageDownloadArtifacts)).Return(errors.New("build fail")).Times(3)
	e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()
410 411 412 413 414 415 416
	e.On("Finish", errors.New("build fail")).Return().Once()

	RegisterExecutor("build-artifacts-run-failure", &p)

	successfulBuild, err := GetSuccessfulBuild()
	assert.NoError(t, err)
	build := &Build{
417
		JobResponse: successfulBuild,
418 419 420 421 422 423 424
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-artifacts-run-failure",
			},
		},
	}

425
	build.Variables = append(build.Variables, JobVariable{Key: "ARTIFACT_DOWNLOAD_ATTEMPTS", Value: "3"})
426 427 428 429
	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
	assert.EqualError(t, err, "build fail")
}

430 431 432 433 434 435 436 437
func TestArtifactUploadRunFailure(t *testing.T) {
	e := MockExecutor{}
	defer e.AssertExpectations(t)

	p := MockExecutorProvider{}
	defer p.AssertExpectations(t)

	// Create executor
Kamil Trzciński's avatar
Kamil Trzciński committed
438 439 440 441
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

442 443 444 445 446 447 448 449
	p.On("Create").Return(&e).Once()

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
	e.On("Cleanup").Return()

	// Successful build script
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"}).Times(8)
Kamil Trzciński's avatar
Kamil Trzciński committed
450 451 452 453 454 455 456 457
	e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageDownloadArtifacts)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageUserScript)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageAfterScript)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageArchiveCache)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageUploadOnSuccessArtifacts)).Return(errors.New("upload fail")).Once()
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	e.On("Finish", errors.New("upload fail")).Return().Once()

	RegisterExecutor("build-upload-artifacts-run-failure", &p)

	successfulBuild, err := GetSuccessfulBuild()
	successfulBuild.Artifacts = make(Artifacts, 1)
	successfulBuild.Artifacts[0] = Artifact{
		Name:      "my-artifact",
		Untracked: false,
		Paths:     ArtifactPaths{"cached/*"},
		When:      ArtifactWhenAlways,
	}
	assert.NoError(t, err)
	build := &Build{
		JobResponse: successfulBuild,
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-upload-artifacts-run-failure",
			},
		},
	}

	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
	assert.EqualError(t, err, "upload fail")
}

484 485 486 487 488 489 490 491
func TestRestoreCacheRunFailure(t *testing.T) {
	e := MockExecutor{}
	defer e.AssertExpectations(t)

	p := MockExecutorProvider{}
	defer p.AssertExpectations(t)

	// Create executor
Kamil Trzciński's avatar
Kamil Trzciński committed
492 493 494 495
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

496 497 498 499 500 501 502 503
	p.On("Create").Return(&e).Once()

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
	e.On("Cleanup").Return()

	// Fail a build script
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
Kamil Trzciński's avatar
Kamil Trzciński committed
504 505 506 507
	e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()
	e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(errors.New("build fail")).Times(3)
	e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()
508 509 510 511 512 513 514
	e.On("Finish", errors.New("build fail")).Return().Once()

	RegisterExecutor("build-cache-run-failure", &p)

	successfulBuild, err := GetSuccessfulBuild()
	assert.NoError(t, err)
	build := &Build{
515
		JobResponse: successfulBuild,
516 517 518 519 520 521
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-cache-run-failure",
			},
		},
	}
522

523
	build.Variables = append(build.Variables, JobVariable{Key: "RESTORE_CACHE_ATTEMPTS", Value: "3"})
524
	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
Kamil Trzciński's avatar
Kamil Trzciński committed
525 526
	assert.EqualError(t, err, "build fail")
}
527 528 529 530 531 532 533 534

func TestRunWrongAttempts(t *testing.T) {
	e := MockExecutor{}

	p := MockExecutorProvider{}
	defer p.AssertExpectations(t)

	// Create executor
Kamil Trzciński's avatar
Kamil Trzciński committed
535 536 537 538
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

539 540 541 542 543 544 545 546
	p.On("Create").Return(&e)

	// Prepare plan
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
	e.On("Cleanup").Return()

	// Fail a build script
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
547 548 549
	e.On("Run", mock.Anything).Return(nil).Once()
	e.On("Run", mock.Anything).Return(errors.New("Number of attempts out of the range [1, 10] for stage: get_sources"))
	e.On("Finish", errors.New("Number of attempts out of the range [1, 10] for stage: get_sources")).Return()
550 551 552 553 554 555

	RegisterExecutor("build-run-attempt-failure", &p)

	successfulBuild, err := GetSuccessfulBuild()
	assert.NoError(t, err)
	build := &Build{
556
		JobResponse: successfulBuild,
557 558 559 560 561 562 563
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-run-attempt-failure",
			},
		},
	}

564
	build.Variables = append(build.Variables, JobVariable{Key: "GET_SOURCES_ATTEMPTS", Value: "0"})
565
	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
566
	assert.EqualError(t, err, "Number of attempts out of the range [1, 10] for stage: get_sources")
567 568 569 570 571 572 573
}

func TestRunSuccessOnSecondAttempt(t *testing.T) {
	e := MockExecutor{}
	p := MockExecutorProvider{}

	// Create executor only once
Kamil Trzciński's avatar
Kamil Trzciński committed
574 575 576 577
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Twice()

578 579 580 581 582 583 584 585 586 587
	p.On("Create").Return(&e).Once()

	// We run everything once
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
	e.On("Finish", mock.Anything).Return().Twice()
	e.On("Cleanup").Return().Twice()

	// Run script successfully
	e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})

588
	e.On("Run", mock.Anything).Return(nil)
589 590 591 592 593 594 595 596
	e.On("Run", mock.Anything).Return(errors.New("build fail")).Once()
	e.On("Run", mock.Anything).Return(nil)

	RegisterExecutor("build-run-success-second-attempt", &p)

	successfulBuild, err := GetSuccessfulBuild()
	assert.NoError(t, err)
	build := &Build{
597
		JobResponse: successfulBuild,
598 599 600 601 602 603 604
		Runner: &RunnerConfig{
			RunnerSettings: RunnerSettings{
				Executor: "build-run-success-second-attempt",
			},
		},
	}

605
	build.Variables = append(build.Variables, JobVariable{Key: "GET_SOURCES_ATTEMPTS", Value: "3"})
606 607
	err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
	assert.NoError(t, err)
608
}
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

func TestDebugTrace(t *testing.T) {
	build := &Build{}
	assert.False(t, build.IsDebugTraceEnabled(), "IsDebugTraceEnabled should be false if CI_DEBUG_TRACE is not set")

	successfulBuild, err := GetSuccessfulBuild()
	assert.NoError(t, err)

	successfulBuild.Variables = append(successfulBuild.Variables, JobVariable{"CI_DEBUG_TRACE", "false", true, true, false})
	build = &Build{
		JobResponse: successfulBuild,
	}
	assert.False(t, build.IsDebugTraceEnabled(), "IsDebugTraceEnabled should be false if CI_DEBUG_TRACE is set to false")

	successfulBuild, err = GetSuccessfulBuild()
	assert.NoError(t, err)

	successfulBuild.Variables = append(successfulBuild.Variables, JobVariable{"CI_DEBUG_TRACE", "true", true, true, false})
	build = &Build{
		JobResponse: successfulBuild,
	}
	assert.True(t, build.IsDebugTraceEnabled(), "IsDebugTraceEnabled should be true if CI_DEBUG_TRACE is set to true")
}
632

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
func TestDefaultEnvVariables(t *testing.T) {
	assert := assert.New(t)
	buildDir := "/tmp/test-build/dir"
	build := Build{
		BuildDir: buildDir,
	}

	vars := build.GetAllVariables().StringList()

	assert.NotNil(vars)

	assert.Contains(vars, "CI_PROJECT_DIR="+filepath.FromSlash(buildDir))
	assert.Contains(vars, "CI_SERVER=yes")
}

648 649 650 651
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)
652 653 654
			build := Build{
				ExecutorFeatures: FeaturesInfo{Shared: shared},
			}
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
			vars := build.GetAllVariables().StringList()

			assert.NotNil(vars)

			present := "CI_SHARED_ENVIRONMENT=true"
			absent := "CI_DISPOSABLE_ENVIRONMENT=true"
			if !shared {
				tmp := present
				present = absent
				absent = tmp
			}

			assert.Contains(vars, present)
			assert.NotContains(vars, absent)
			// we never expose false
			assert.NotContains(vars, "CI_SHARED_ENVIRONMENT=false")
			assert.NotContains(vars, "CI_DISPOSABLE_ENVIRONMENT=false")
		})
	}
}
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

func TestGetRemoteURL(t *testing.T) {
	testCases := []struct {
		runner RunnerSettings
		result string
	}{
		{
			runner: RunnerSettings{
				CloneURL: "http://test.local/",
			},
			result: "http://gitlab-ci-token:1234567@test.local/h5bp/html5-boilerplate.git",
		},
		{
			runner: RunnerSettings{
				CloneURL: "https://test.local",
			},
			result: "https://gitlab-ci-token:1234567@test.local/h5bp/html5-boilerplate.git",
		},
		{
			runner: RunnerSettings{},
			result: "http://fallback.url",
		},
	}

	for _, tc := range testCases {
		build := &Build{
			Runner: &RunnerConfig{
				RunnerSettings: tc.runner,
			},
			allVariables: JobVariables{
				JobVariable{Key: "CI_JOB_TOKEN", Value: "1234567"},
				JobVariable{Key: "CI_PROJECT_PATH", Value: "h5bp/html5-boilerplate"},
			},
			JobResponse: JobResponse{
				GitInfo: GitInfo{RepoURL: "http://fallback.url"},
			},
		}

		assert.Equal(t, tc.result, build.GetRemoteURL())
	}
}
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785

type featureFlagOnTestCase struct {
	value          string
	expectedStatus bool
	expectedError  bool
}

func TestIsFeatureFlagOn(t *testing.T) {
	hook := test.NewGlobal()

	tests := map[string]featureFlagOnTestCase{
		"no value": {
			value:          "",
			expectedStatus: false,
			expectedError:  false,
		},
		"true": {
			value:          "true",
			expectedStatus: true,
			expectedError:  false,
		},
		"1": {
			value:          "1",
			expectedStatus: true,
			expectedError:  false,
		},
		"false": {
			value:          "false",
			expectedStatus: false,
			expectedError:  false,
		},
		"0": {
			value:          "0",
			expectedStatus: false,
			expectedError:  false,
		},
		"invalid value": {
			value:          "test",
			expectedStatus: false,
			expectedError:  true,
		},
	}

	for name, testCase := range tests {
		t.Run(name, func(t *testing.T) {
			build := new(Build)
			build.Variables = JobVariables{
				{Key: "FF_TEST_FEATURE", Value: testCase.value},
			}

			status := build.IsFeatureFlagOn("FF_TEST_FEATURE")
			assert.Equal(t, testCase.expectedStatus, status)

			entry := hook.LastEntry()
			if testCase.expectedError {
				require.NotNil(t, entry)

				logrusOutput, err := entry.String()
				require.NoError(t, err)

				assert.Contains(t, logrusOutput, "Error while parsing the value of feature flag")
			} else {
				assert.Nil(t, entry)
			}

			hook.Reset()
		})
	}

}