executor_docker_command_test.go 26.3 KB
Newer Older
1 2 3
package docker_test

import (
4
	"bytes"
5
	"context"
6
	"fmt"
7
	"net/url"
8
	"os"
9
	"os/exec"
10
	"regexp"
11
	"strings"
Kamil Trzciński's avatar
Kamil Trzciński committed
12 13
	"testing"
	"time"
14

15
	"github.com/hashicorp/go-version"
16 17
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
18

19 20 21
	"gitlab.com/gitlab-org/gitlab-runner/common"
	"gitlab.com/gitlab-org/gitlab-runner/helpers"
	"gitlab.com/gitlab-org/gitlab-runner/helpers/docker"
22 23 24
)

func TestDockerCommandSuccessRun(t *testing.T) {
25
	if helpers.SkipIntegrationTests(t, "docker", "info") {
26 27 28
		return
	}

29 30
	successfulBuild, err := common.GetRemoteSuccessfulBuild()
	assert.NoError(t, err)
31
	build := &common.Build{
32
		JobResponse: successfulBuild,
33 34 35 36
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
37
					Image:      common.TestAlpineImage,
38
					PullPolicy: common.PullPolicyIfNotPresent,
39 40 41 42 43
				},
			},
		},
	}

44
	err = build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
45 46 47
	assert.NoError(t, err)
}

48 49 50 51 52 53 54 55
func TestDockerCommandNoRootImage(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	successfulBuild, err := common.GetRemoteSuccessfulBuildWithDumpedVariables()

	assert.NoError(t, err)
56
	successfulBuild.Image.Name = common.TestAlpineNoRootImage
57 58 59 60 61
	build := &common.Build{
		JobResponse: successfulBuild,
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
62 63 64
				Docker: &common.DockerConfig{
					PullPolicy: common.PullPolicyIfNotPresent,
				},
65 66 67 68 69 70 71 72
			},
		},
	}

	err = build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
	assert.NoError(t, err)
}

73
func TestDockerCommandBuildFail(t *testing.T) {
74
	if helpers.SkipIntegrationTests(t, "docker", "info") {
75 76 77
		return
	}

78 79
	failedBuild, err := common.GetRemoteFailedBuild()
	assert.NoError(t, err)
80
	build := &common.Build{
81
		JobResponse: failedBuild,
82 83 84 85
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
86
					Image:      common.TestAlpineImage,
87
					PullPolicy: common.PullPolicyIfNotPresent,
88 89 90 91 92
				},
			},
		},
	}

93
	err = build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
94 95
	require.Error(t, err, "error")
	assert.IsType(t, err, &common.BuildError{})
Kamil Trzciński's avatar
Kamil Trzciński committed
96
	assert.Contains(t, err.Error(), "exit code 1")
97 98
}

99 100 101 102 103 104 105 106 107
func TestDockerCommandWithAllowedImagesRun(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	successfulBuild, err := common.GetRemoteSuccessfulBuild()
	successfulBuild.Image = common.Image{Name: "$IMAGE_NAME"}
	successfulBuild.Variables = append(successfulBuild.Variables, common.JobVariable{
		Key:      "IMAGE_NAME",
108
		Value:    common.TestAlpineImage,
109 110 111 112
		Public:   true,
		Internal: false,
		File:     false,
	})
113
	successfulBuild.Services = append(successfulBuild.Services, common.Image{Name: common.TestDockerDindImage})
114 115 116 117 118 119 120
	assert.NoError(t, err)
	build := &common.Build{
		JobResponse: successfulBuild,
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
121 122
					AllowedImages:   []string{common.TestAlpineImage},
					AllowedServices: []string{common.TestDockerDindImage},
123
					Privileged:      true,
124
					PullPolicy:      common.PullPolicyIfNotPresent,
125 126 127 128 129 130 131 132 133
				},
			},
		},
	}

	err = build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
	assert.NoError(t, err)
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
func TestDockerCommandDisableEntrypointOverwrite(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	tests := []struct {
		name     string
		services bool
		disabled bool
	}{
		{
			name:     "Disabled - no services",
			disabled: true,
		},
		{
			name:     "Disabled - services",
			disabled: true,
			services: true,
		},
		{
			name: "Enabled - no services",
		},
		{
			name:     "Enabled - services",
			services: true,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			successfulBuild, err := common.GetRemoteSuccessfulBuild()
			require.NoError(t, err)

			successfulBuild.Image.Entrypoint = []string{"/bin/sh", "-c", "echo 'image overwritten'"}

			if test.services {
				successfulBuild.Services = common.Services{
					common.Image{
						Name:       common.TestDockerDindImage,
						Entrypoint: []string{"/bin/sh", "-c", "echo 'service overwritten'"},
					},
				}
			}

			build := &common.Build{
				JobResponse: successfulBuild,
				Runner: &common.RunnerConfig{
					RunnerSettings: common.RunnerSettings{
						Executor: "docker",
						Docker: &common.DockerConfig{
							Privileged:                 true,
							Image:                      common.TestAlpineImage,
							PullPolicy:                 common.PullPolicyIfNotPresent,
							DisableEntrypointOverwrite: test.disabled,
						},
					},
				},
			}

			var buffer bytes.Buffer
			err = build.Run(&common.Config{}, &common.Trace{Writer: &buffer})
			assert.NoError(t, err)
			out := buffer.String()
			if test.disabled {
				assert.NotContains(t, out, "image overwritten")
				assert.NotContains(t, out, "service overwritten")
				assert.Contains(t, out, "Entrypoint override disabled")
			} else {
				assert.Contains(t, out, "image overwritten")
				if test.services {
					assert.Contains(t, out, "service overwritten")
				}
			}
		})
	}
}

211
func isDockerOlderThan17_07(t *testing.T) bool {
212
	client, err := docker_helpers.New(docker_helpers.DockerCredentials{}, "")
213
	require.NoError(t, err, "should be able to connect to docker")
214

215 216 217 218
	types, err := client.Info(context.Background())
	require.NoError(t, err, "should be able to get docker info")

	localVersion, err := version.NewVersion(types.ServerVersion)
219 220 221 222 223 224 225 226
	require.NoError(t, err)

	checkedVersion, err := version.NewVersion("17.07.0-ce")
	require.NoError(t, err)

	return localVersion.LessThan(checkedVersion)
}

227
func TestDockerCommandMissingImage(t *testing.T) {
228
	if helpers.SkipIntegrationTests(t, "docker", "info") {
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		return
	}

	build := &common.Build{
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
					Image: "some/non-existing/image",
				},
			},
		},
	}

	err := build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
	require.Error(t, err)
245
	assert.IsType(t, &common.BuildError{}, err)
246 247 248 249 250 251 252

	contains := "repository does not exist"
	if isDockerOlderThan17_07(t) {
		contains = "not found"
	}

	assert.Contains(t, err.Error(), contains)
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
}

func TestDockerCommandMissingTag(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	build := &common.Build{
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
					Image: "docker:missing-tag",
				},
			},
		},
	}

	err := build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
	require.Error(t, err)
273
	assert.IsType(t, &common.BuildError{}, err)
274
	assert.Contains(t, err.Error(), "not found")
275 276 277
}

func TestDockerCommandBuildAbort(t *testing.T) {
278
	if helpers.SkipIntegrationTests(t, "docker", "info") {
279 280 281
		return
	}

282 283
	longRunningBuild, err := common.GetRemoteLongRunningBuild()
	assert.NoError(t, err)
284
	build := &common.Build{
285
		JobResponse: longRunningBuild,
286 287 288 289
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
290
					Image:      common.TestAlpineImage,
291
					PullPolicy: common.PullPolicyIfNotPresent,
292 293 294
				},
			},
		},
295
		SystemInterrupt: make(chan os.Signal, 1),
296 297 298 299
	}

	abortTimer := time.AfterFunc(time.Second, func() {
		t.Log("Interrupt")
300
		build.SystemInterrupt <- os.Interrupt
301 302 303 304 305 306 307 308 309
	})
	defer abortTimer.Stop()

	timeoutTimer := time.AfterFunc(time.Minute, func() {
		t.Log("Timedout")
		t.FailNow()
	})
	defer timeoutTimer.Stop()

310
	err = build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
311 312 313 314
	assert.EqualError(t, err, "aborted: interrupt")
}

func TestDockerCommandBuildCancel(t *testing.T) {
315
	if helpers.SkipIntegrationTests(t, "docker", "info") {
316 317 318
		return
	}

319 320
	longRunningBuild, err := common.GetRemoteLongRunningBuild()
	assert.NoError(t, err)
321
	build := &common.Build{
322
		JobResponse: longRunningBuild,
323 324 325 326
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
327
					Image:      common.TestAlpineImage,
328
					PullPolicy: common.PullPolicyIfNotPresent,
329 330 331 332 333
				},
			},
		},
	}

334
	trace := &common.Trace{Writer: os.Stdout}
335 336 337

	abortTimer := time.AfterFunc(time.Second, func() {
		t.Log("Interrupt")
338
		trace.CancelFunc()
339 340 341 342 343 344 345 346 347
	})
	defer abortTimer.Stop()

	timeoutTimer := time.AfterFunc(time.Minute, func() {
		t.Log("Timedout")
		t.FailNow()
	})
	defer timeoutTimer.Stop()

348
	err = build.Run(&common.Config{}, trace)
349 350 351 352
	assert.IsType(t, err, &common.BuildError{})
	assert.EqualError(t, err, "canceled")
}

353 354 355 356 357 358 359
func TestDockerCommandTwoServicesFromOneImage(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	successfulBuild, err := common.GetRemoteSuccessfulBuild()
	successfulBuild.Services = common.Services{
360 361
		{Name: common.TestAlpineImage, Alias: "service-1"},
		{Name: common.TestAlpineImage, Alias: "service-2"},
362 363 364 365 366 367 368 369
	}
	assert.NoError(t, err)
	build := &common.Build{
		JobResponse: successfulBuild,
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
370
					Image:      common.TestAlpineImage,
371
					PullPolicy: common.PullPolicyIfNotPresent,
372 373 374 375 376
				},
			},
		},
	}

377
	var buffer bytes.Buffer
378

379
	err = build.Run(&common.Config{}, &common.Trace{Writer: &buffer})
380 381 382 383 384 385 386 387
	assert.NoError(t, err)
	str := buffer.String()

	re, err := regexp.Compile("(?m)Conflict. The container name [^ ]+ is already in use by container")
	require.NoError(t, err)
	assert.NotRegexp(t, re, str, "Both service containers should be started and use different name")
}

388 389 390 391 392 393 394 395
func TestDockerCommandOutput(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	successfulBuild, err := common.GetRemoteSuccessfulBuild()
	assert.NoError(t, err)
	build := &common.Build{
396
		JobResponse: successfulBuild,
397 398 399 400
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
401
					Image:      common.TestAlpineImage,
402
					PullPolicy: common.PullPolicyIfNotPresent,
403 404 405 406 407
				},
			},
		},
	}

408
	var buffer bytes.Buffer
409

410
	err = build.Run(&common.Config{}, &common.Trace{Writer: &buffer})
411 412
	assert.NoError(t, err)

413
	re, err := regexp.Compile("(?m)^Initialized empty Git repository in /builds/gitlab-org/ci-cd/tests/gitlab-test/.git/")
414 415 416 417
	assert.NoError(t, err)
	assert.Regexp(t, re, buffer.String())
}

418
func TestDockerPrivilegedServiceAccessingBuildsFolder(t *testing.T) {
419
	if helpers.SkipIntegrationTests(t, "docker", "info") {
420 421 422
		return
	}

423 424
	commands := []string{
		"docker info",
425
		"docker run -v $(pwd):$(pwd) -w $(pwd) busybox touch test",
426
		"cat test",
427
	}
428 429 430 431

	strategies := []string{
		"fetch",
		"clone",
432 433
	}

434 435
	for _, strategy := range strategies {
		t.Log("Testing", strategy, "strategy...")
436 437
		longRunningBuild, err := common.GetRemoteLongRunningBuild()
		assert.NoError(t, err)
438
		build := &common.Build{
439
			JobResponse: longRunningBuild,
440 441 442 443
			Runner: &common.RunnerConfig{
				RunnerSettings: common.RunnerSettings{
					Executor: "docker",
					Docker: &common.DockerConfig{
444
						Image:      common.TestAlpineImage,
445
						PullPolicy: common.PullPolicyIfNotPresent,
446 447 448 449 450
						Privileged: true,
					},
				},
			},
		}
451 452
		build.Steps = common.Steps{
			common.Step{
453
				Name:         common.StepNameScript,
454
				Script:       common.StepScript(commands),
455 456 457 458
				When:         common.StepWhenOnSuccess,
				AllowFailure: false,
			},
		}
459
		build.Image.Name = common.TestDockerGitImage
460 461
		build.Services = common.Services{
			common.Image{
462
				Name: common.TestDockerDindImage,
463 464
			},
		}
465
		build.Variables = append(build.Variables, common.JobVariable{
466 467 468
			Key: "GIT_STRATEGY", Value: strategy,
		})

469
		err = build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
470 471
		assert.NoError(t, err)
	}
472
}
473

474 475 476 477 478 479 480 481 482 483 484 485 486 487
func getTestDockerJob(t *testing.T) *common.Build {
	commands := []string{
		"docker info",
	}

	longRunningBuild, err := common.GetRemoteLongRunningBuild()
	assert.NoError(t, err)

	build := &common.Build{
		JobResponse: longRunningBuild,
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
488
					Image:      common.TestAlpineImage,
489
					PullPolicy: common.PullPolicyIfNotPresent,
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
					Privileged: true,
				},
			},
		},
	}
	build.Steps = common.Steps{
		common.Step{
			Name:         common.StepNameScript,
			Script:       common.StepScript(commands),
			When:         common.StepWhenOnSuccess,
			AllowFailure: false,
		},
	}

	return build
}

func TestDockerExtendedConfigurationFromJob(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	examples := []struct {
		image     common.Image
		services  common.Services
		variables common.JobVariables
	}{
		{
			image: common.Image{
				Name:       "$IMAGE_NAME",
520
				Entrypoint: []string{"sh", "-c"},
521 522 523 524
			},
			services: common.Services{
				common.Image{
					Name:       "$SERVICE_NAME",
525 526
					Entrypoint: []string{"sh", "-c"},
					Command:    []string{"dockerd-entrypoint.sh"},
527 528 529 530 531
					Alias:      "my-docker-service",
				},
			},
			variables: common.JobVariables{
				{Key: "DOCKER_HOST", Value: "tcp://my-docker-service:2375"},
532 533
				{Key: "IMAGE_NAME", Value: common.TestDockerGitImage},
				{Key: "SERVICE_NAME", Value: common.TestDockerDindImage},
534 535 536 537 538 539 540 541 542 543 544 545 546
			},
		},
		{
			image: common.Image{
				Name: "$IMAGE_NAME",
			},
			services: common.Services{
				common.Image{
					Name: "$SERVICE_NAME",
				},
			},
			variables: common.JobVariables{
				{Key: "DOCKER_HOST", Value: "tcp://docker:2375"},
547 548
				{Key: "IMAGE_NAME", Value: common.TestDockerGitImage},
				{Key: "SERVICE_NAME", Value: common.TestDockerDindImage},
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
			},
		},
	}

	for exampleID, example := range examples {
		t.Run(fmt.Sprintf("example-%d", exampleID), func(t *testing.T) {
			build := getTestDockerJob(t)
			build.Image = example.image
			build.Services = example.services
			build.Variables = append(build.Variables, example.variables...)

			err := build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
			assert.NoError(t, err)
		})
	}
}

566
func runTestJobWithOutput(t *testing.T, build *common.Build) (output string) {
567
	var buffer bytes.Buffer
568

569
	err := build.Run(&common.Config{}, &common.Trace{Writer: &buffer})
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
	assert.NoError(t, err)

	output = buffer.String()
	return
}

func TestCacheInContainer(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	successfulBuild, err := common.GetRemoteSuccessfulBuild()
	assert.NoError(t, err)

	successfulBuild.JobInfo.ProjectID = int(time.Now().Unix())
	successfulBuild.Steps[0].Script = common.StepScript{
		"(test -d cached/ && ls -lh cached/) || echo \"no cached directory\"",
		"(test -f cached/date && cat cached/date) || echo \"no cached date\"",
		"mkdir -p cached",
		"date > cached/date",
	}
	successfulBuild.Cache = common.Caches{
		common.Cache{
593 594 595
			Key:    "key",
			Paths:  common.ArtifactPaths{"cached/*"},
			Policy: common.CachePolicyPullPush,
596 597 598 599 600 601 602 603 604
		},
	}

	build := &common.Build{
		JobResponse: successfulBuild,
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
605
					Image:      common.TestAlpineImage,
606 607
					PullPolicy: common.PullPolicyIfNotPresent,
					Volumes:    []string{"/cache"},
608 609 610 611 612
				},
			},
		},
	}

613 614 615
	cacheNotPresentRE := regexp.MustCompile("(?m)^no cached directory")
	skipCacheDownload := "Not downloading cache key due to policy"
	skipCacheUpload := "Not uploading cache key due to policy"
616

617
	// The first job lacks any cache to pull, but tries to both pull and push
618
	output := runTestJobWithOutput(t, build)
619 620 621
	assert.Regexp(t, cacheNotPresentRE, output, "First job execution should not have cached data")
	assert.NotContains(t, output, skipCacheDownload, "Cache download should be performed with policy: %s", common.CachePolicyPullPush)
	assert.NotContains(t, output, skipCacheUpload, "Cache upload should be performed with policy: %s", common.CachePolicyPullPush)
622

623 624
	// pull-only jobs should skip the push step
	build.JobResponse.Cache[0].Policy = common.CachePolicyPull
625
	output = runTestJobWithOutput(t, build)
626 627 628 629 630 631 632 633 634 635
	assert.NotRegexp(t, cacheNotPresentRE, output, "Second job execution should have cached data")
	assert.NotContains(t, output, skipCacheDownload, "Cache download should be performed with policy: %s", common.CachePolicyPull)
	assert.Contains(t, output, skipCacheUpload, "Cache upload should be skipped with policy: %s", common.CachePolicyPull)

	// push-only jobs should skip the pull step
	build.JobResponse.Cache[0].Policy = common.CachePolicyPush
	output = runTestJobWithOutput(t, build)
	assert.Regexp(t, cacheNotPresentRE, output, "Third job execution should not have cached data")
	assert.Contains(t, output, skipCacheDownload, "Cache download be skipped with policy: push")
	assert.NotContains(t, output, skipCacheUpload, "Cache upload should be performed with policy: push")
636 637
}

638 639 640 641 642 643 644 645
func TestDockerImageNameFromVariable(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	successfulBuild, err := common.GetRemoteSuccessfulBuild()
	successfulBuild.Variables = append(successfulBuild.Variables, common.JobVariable{
		Key:   "CI_REGISTRY_IMAGE",
646
		Value: common.TestAlpineImage,
647 648 649 650 651 652 653 654 655 656 657
	})
	successfulBuild.Image = common.Image{
		Name: "$CI_REGISTRY_IMAGE",
	}
	assert.NoError(t, err)
	build := &common.Build{
		JobResponse: successfulBuild,
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
658
					Image:           common.TestAlpineImage,
659
					PullPolicy:      common.PullPolicyIfNotPresent,
660
					AllowedServices: []string{common.TestAlpineImage},
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
				},
			},
		},
	}

	re := regexp.MustCompile("(?m)^ERROR: The [^ ]+ is not present on list of allowed images")

	output := runTestJobWithOutput(t, build)
	assert.NotRegexp(t, re, output, "Image's name should be expanded from variable")
}

func TestDockerServiceNameFromVariable(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	successfulBuild, err := common.GetRemoteSuccessfulBuild()
	successfulBuild.Variables = append(successfulBuild.Variables, common.JobVariable{
		Key:   "CI_REGISTRY_IMAGE",
680
		Value: common.TestAlpineImage,
681 682 683 684 685 686 687 688 689 690 691
	})
	successfulBuild.Services = append(successfulBuild.Services, common.Image{
		Name: "$CI_REGISTRY_IMAGE",
	})
	assert.NoError(t, err)
	build := &common.Build{
		JobResponse: successfulBuild,
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
692
					Image:           common.TestAlpineImage,
693
					PullPolicy:      common.PullPolicyIfNotPresent,
694
					AllowedServices: []string{common.TestAlpineImage},
695 696 697 698 699 700 701 702 703 704 705
				},
			},
		},
	}

	re := regexp.MustCompile("(?m)^ERROR: The [^ ]+ is not present on list of allowed services")

	output := runTestJobWithOutput(t, build)
	assert.NotRegexp(t, re, output, "Service's name should be expanded from variable")
}

706
func runDockerInDocker(version string) (id string, err error) {
Kamil Trzciński's avatar
Kamil Trzciński committed
707
	cmd := exec.Command("docker", "run", "--detach", "--privileged", "-p", "2375", "docker:"+version+"-dind")
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	cmd.Stderr = os.Stderr
	data, err := cmd.Output()
	if err != nil {
		return
	}
	id = strings.TrimSpace(string(data))
	return
}

func getDockerCredentials(id string) (credentials docker_helpers.DockerCredentials, err error) {
	cmd := exec.Command("docker", "port", id, "2375")
	cmd.Stderr = os.Stderr
	data, err := cmd.Output()
	if err != nil {
		return
	}

	hostPort := strings.Split(strings.TrimSpace(string(data)), ":")
726 727 728 729 730
	if dockerHost, err := url.Parse(os.Getenv("DOCKER_HOST")); err == nil {
		dockerHostPort := strings.Split(dockerHost.Host, ":")
		hostPort[0] = dockerHostPort[0]
	} else if hostPort[0] == "0.0.0.0" {
		hostPort[0] = "localhost"
731 732 733 734 735 736
	}
	credentials.Host = "tcp://" + hostPort[0] + ":" + hostPort[1]
	return
}

func waitForDocker(credentials docker_helpers.DockerCredentials) error {
737
	client, err := docker_helpers.New(credentials, "")
738 739 740 741 742
	if err != nil {
		return err
	}

	for i := 0; i < 20; i++ {
743
		_, err = client.Info(context.Background())
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
		if err == nil {
			break
		}
		time.Sleep(time.Second)
	}
	return err
}

func testDockerVersion(t *testing.T, version string) {
	t.Log("Running docker", version, "...")
	id, err := runDockerInDocker(version)
	if err != nil {
		t.Error("Docker run:", err)
		return
	}

	defer func() {
		exec.Command("docker", "rm", "-f", "-v", id).Run()
	}()

	t.Log("Getting address of", version, "...")
	credentials, err := getDockerCredentials(id)
	if err != nil {
		t.Error("Docker credentials:", err)
		return
	}

771
	t.Log("Connecting to", credentials.Host, "...")
772 773 774 775 776 777 778 779
	err = waitForDocker(credentials)
	if err != nil {
		t.Error("Wait for docker:", err)
		return
	}

	t.Log("Docker", version, "is running at", credentials.Host)

780 781
	successfulBuild, err := common.GetRemoteSuccessfulBuild()
	assert.NoError(t, err)
782
	build := &common.Build{
783
		JobResponse: successfulBuild,
784 785 786 787
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
788
					Image:             common.TestAlpineImage,
789
					PullPolicy:        common.PullPolicyIfNotPresent,
790
					DockerCredentials: credentials,
791
					CPUS:              "0.1",
792 793 794 795 796 797 798 799 800 801 802 803 804
				},
			},
		},
	}

	err = build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
	assert.NoError(t, err)
}

func TestDocker1_8Compatibility(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}
805 806 807 808
	if os.Getenv("CI") != "" {
		t.Skip("This test doesn't work in nested dind")
		return
	}
809 810 811 812 813 814 815 816

	testDockerVersion(t, "1.8")
}

func TestDocker1_9Compatibility(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}
817 818 819 820
	if os.Getenv("CI") != "" {
		t.Skip("This test doesn't work in nested dind")
		return
	}
821 822 823 824 825 826 827 828

	testDockerVersion(t, "1.9")
}

func TestDocker1_10Compatibility(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}
829 830 831 832
	if os.Getenv("CI") != "" {
		t.Skip("This test doesn't work in nested dind")
		return
	}
833 834 835 836 837 838 839 840

	testDockerVersion(t, "1.10")
}

func TestDocker1_11Compatibility(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}
841 842 843 844
	if os.Getenv("CI") != "" {
		t.Skip("This test doesn't work in nested dind")
		return
	}
845

846
	testDockerVersion(t, "1.11")
847 848 849 850 851 852
}

func TestDocker1_12Compatibility(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}
853 854 855 856
	if os.Getenv("CI") != "" {
		t.Skip("This test doesn't work in nested dind")
		return
	}
857 858 859

	testDockerVersion(t, "1.12")
}
860 861 862 863 864 865 866 867 868 869 870 871

func TestDocker1_13Compatibility(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}
	if os.Getenv("CI") != "" {
		t.Skip("This test doesn't work in nested dind")
		return
	}

	testDockerVersion(t, "1.13")
}
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888

func TestDockerCommandWithBrokenGitSSLCAInfo(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	successfulBuild, err := common.GetRemoteBrokenTLSBuild()
	assert.NoError(t, err)
	build := &common.Build{
		JobResponse: successfulBuild,
		Runner: &common.RunnerConfig{
			RunnerCredentials: common.RunnerCredentials{
				URL: "https://gitlab.com",
			},
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
889
					Image:      common.TestAlpineImage,
890
					PullPolicy: common.PullPolicyIfNotPresent,
891 892 893 894 895
				},
			},
		},
	}

896
	var buffer bytes.Buffer
897

898
	err = build.Run(&common.Config{}, &common.Trace{Writer: &buffer})
899 900
	assert.Error(t, err)
	out := buffer.String()
901
	assert.Contains(t, out, "Created fresh repository")
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	assert.NotContains(t, out, "Updating/initializing submodules")
}

func TestDockerCommandWithGitSSLCAInfo(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	successfulBuild, err := common.GetRemoteGitLabComTLSBuild()
	assert.NoError(t, err)
	build := &common.Build{
		JobResponse: successfulBuild,
		Runner: &common.RunnerConfig{
			RunnerCredentials: common.RunnerCredentials{
				URL: "https://gitlab.com",
			},
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
921
					Image:      common.TestAlpineImage,
922
					PullPolicy: common.PullPolicyIfNotPresent,
923 924 925 926 927
				},
			},
		},
	}

928
	var buffer bytes.Buffer
929

930
	err = build.Run(&common.Config{}, &common.Trace{Writer: &buffer})
931 932
	assert.NoError(t, err)
	out := buffer.String()
933
	assert.Contains(t, out, "Created fresh repository")
934 935
	assert.Contains(t, out, "Updating/initializing submodules")
}
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951

func TestDockerCommandWithHelperImageConfig(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	helperImageConfig := "gitlab/gitlab-runner-helper:x86_64-64eea86c"

	successfulBuild, err := common.GetRemoteSuccessfulBuild()
	assert.NoError(t, err)
	build := &common.Build{
		JobResponse: successfulBuild,
		Runner: &common.RunnerConfig{
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
952
					Image:       common.TestAlpineImage,
953
					HelperImage: helperImageConfig,
954
					PullPolicy:  common.PullPolicyIfNotPresent,
955 956 957 958 959 960 961 962 963
				},
			},
		},
	}

	var buffer bytes.Buffer
	err = build.Run(&common.Config{}, &common.Trace{Writer: &buffer})
	assert.NoError(t, err)
	out := buffer.String()
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
	assert.Contains(t, out, "Using docker image sha256:bbd86c6ba107ae2feb8dbf9024df4b48597c44e1b584a3d901bba91f7fc500e3 for gitlab/gitlab-runner-helper:x86_64-64eea86c ...")
}

func TestDockerCommandWithDoingPruneAndAfterScript(t *testing.T) {
	if helpers.SkipIntegrationTests(t, "docker", "info") {
		return
	}

	successfulBuild, err := common.GetRemoteSuccessfulBuildWithAfterScript()

	// This scripts removes self-created containers that do exit
	// It will fail if: cannot be removed, or no containers is found
	// It is assuming that name of each runner created container starts
	// with `runner-doprune-`
	successfulBuild.Steps[0].Script = common.StepScript{
		"docker ps -a -f status=exited | grep runner-doprune-",
		"docker rm $(docker ps -a -f status=exited | grep runner-doprune- | awk '{print $1}')",
	}

	assert.NoError(t, err)
	build := &common.Build{
		JobResponse: successfulBuild,
		Runner: &common.RunnerConfig{
			RunnerCredentials: common.RunnerCredentials{
				Token: "doprune",
			},
			RunnerSettings: common.RunnerSettings{
				Executor: "docker",
				Docker: &common.DockerConfig{
993
					Image:      common.TestDockerGitImage,
994
					PullPolicy: common.PullPolicyIfNotPresent,
995 996 997 998 999 1000 1001 1002 1003 1004
					Volumes: []string{
						"/var/run/docker.sock:/var/run/docker.sock",
					},
				},
			},
		},
	}

	err = build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
	assert.NoError(t, err)
1005
}