Refactor and speed-up a little docker tests

parent ab8437bc
......@@ -181,9 +181,6 @@ code_quality:
- export SUITE_INDEX=${JOB_NAME[-2]}
- export SUITE_TOTAL=${JOB_NAME[-1]}
- source ci/touch_make_dependencies
- docker pull alpine
- docker pull docker:dind
- docker pull docker:git
- make parallel_test_execute
artifacts:
paths:
......
......@@ -129,7 +129,7 @@ parallel_test_prepare: $(GOPATH_SETUP)
# Preparing test commands
@./scripts/go_test_with_coverage_report prepare
parallel_test_execute: $(GOPATH_SETUP)
parallel_test_execute: $(GOPATH_SETUP) pull_images_for_tests
# executing tests
@./scripts/go_test_with_coverage_report execute
......@@ -137,6 +137,10 @@ parallel_test_coverage_report: $(GOPATH_SETUP)
# Preparing coverage report
@./scripts/go_test_with_coverage_report coverage
pull_images_for_tests: $(GOPATH_SETUP)
# Pulling images required for some tests
@go run ./scripts/pull-images-for-tests/main.go
install:
go install --ldflags="$(GO_LDFLAGS)" $(PKG)
......
......@@ -27,3 +27,10 @@ const DefaultCacheRequestTimeout = 10
const DefaultNetworkClientTimeout = 60 * time.Minute
var PreparationRetryInterval = 3 * time.Second
const (
TestAlpineImage = "alpine:3.7"
TestAlpineNoRootImage = "registry.gitlab.com/gitlab-org/gitlab-runner/alpine-no-root:latest"
TestDockerDindImage = "docker:18-dind"
TestDockerGitImage = "docker:18-git"
)
......@@ -35,7 +35,7 @@ func TestDockerCommandSuccessRun(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
},
},
......@@ -54,13 +54,15 @@ func TestDockerCommandNoRootImage(t *testing.T) {
successfulBuild, err := common.GetRemoteSuccessfulBuildWithDumpedVariables()
assert.NoError(t, err)
successfulBuild.Image.Name = "registry.gitlab.com/gitlab-org/gitlab-runner/alpine-no-root"
successfulBuild.Image.Name = common.TestAlpineNoRootImage
build := &common.Build{
JobResponse: successfulBuild,
Runner: &common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{},
Docker: &common.DockerConfig{
PullPolicy: common.PullPolicyIfNotPresent,
},
},
},
}
......@@ -82,7 +84,7 @@ func TestDockerCommandBuildFail(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
},
},
......@@ -104,12 +106,12 @@ func TestDockerCommandWithAllowedImagesRun(t *testing.T) {
successfulBuild.Image = common.Image{Name: "$IMAGE_NAME"}
successfulBuild.Variables = append(successfulBuild.Variables, common.JobVariable{
Key: "IMAGE_NAME",
Value: "alpine",
Value: common.TestAlpineImage,
Public: true,
Internal: false,
File: false,
})
successfulBuild.Services = append(successfulBuild.Services, common.Image{Name: "docker:dind"})
successfulBuild.Services = append(successfulBuild.Services, common.Image{Name: common.TestDockerDindImage})
assert.NoError(t, err)
build := &common.Build{
JobResponse: successfulBuild,
......@@ -117,9 +119,10 @@ func TestDockerCommandWithAllowedImagesRun(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
AllowedImages: []string{"alpine"},
AllowedServices: []string{"docker:dind"},
AllowedImages: []string{common.TestAlpineImage},
AllowedServices: []string{common.TestDockerDindImage},
Privileged: true,
PullPolicy: common.PullPolicyIfNotPresent,
},
},
},
......@@ -209,7 +212,7 @@ func TestDockerCommandBuildAbort(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
},
},
......@@ -246,7 +249,7 @@ func TestDockerCommandBuildCancel(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
},
},
......@@ -279,8 +282,8 @@ func TestDockerCommandTwoServicesFromOneImage(t *testing.T) {
successfulBuild, err := common.GetRemoteSuccessfulBuild()
successfulBuild.Services = common.Services{
{Name: "alpine", Alias: "service-1"},
{Name: "alpine", Alias: "service-2"},
{Name: common.TestAlpineImage, Alias: "service-1"},
{Name: common.TestAlpineImage, Alias: "service-2"},
}
assert.NoError(t, err)
build := &common.Build{
......@@ -289,7 +292,7 @@ func TestDockerCommandTwoServicesFromOneImage(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
},
},
......@@ -320,7 +323,7 @@ func TestDockerCommandOutput(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
},
},
......@@ -363,7 +366,7 @@ func TestDockerPrivilegedServiceAccessingBuildsFolder(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
Privileged: true,
},
......@@ -378,10 +381,10 @@ func TestDockerPrivilegedServiceAccessingBuildsFolder(t *testing.T) {
AllowFailure: false,
},
}
build.Image.Name = "docker:git"
build.Image.Name = common.TestDockerGitImage
build.Services = common.Services{
common.Image{
Name: "docker:dind",
Name: common.TestDockerDindImage,
},
}
build.Variables = append(build.Variables, common.JobVariable{
......@@ -407,7 +410,7 @@ func getTestDockerJob(t *testing.T) *common.Build {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
Privileged: true,
},
......@@ -451,8 +454,8 @@ func TestDockerExtendedConfigurationFromJob(t *testing.T) {
},
variables: common.JobVariables{
{Key: "DOCKER_HOST", Value: "tcp://my-docker-service:2375"},
{Key: "IMAGE_NAME", Value: "docker:git"},
{Key: "SERVICE_NAME", Value: "docker:dind"},
{Key: "IMAGE_NAME", Value: common.TestDockerGitImage},
{Key: "SERVICE_NAME", Value: common.TestDockerDindImage},
},
},
{
......@@ -466,8 +469,8 @@ func TestDockerExtendedConfigurationFromJob(t *testing.T) {
},
variables: common.JobVariables{
{Key: "DOCKER_HOST", Value: "tcp://docker:2375"},
{Key: "IMAGE_NAME", Value: "docker:git"},
{Key: "SERVICE_NAME", Value: "docker:dind"},
{Key: "IMAGE_NAME", Value: common.TestDockerGitImage},
{Key: "SERVICE_NAME", Value: common.TestDockerDindImage},
},
},
}
......@@ -524,7 +527,7 @@ func TestCacheInContainer(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
Volumes: []string{"/cache"},
},
......@@ -565,7 +568,7 @@ func TestDockerImageNameFromVariable(t *testing.T) {
successfulBuild, err := common.GetRemoteSuccessfulBuild()
successfulBuild.Variables = append(successfulBuild.Variables, common.JobVariable{
Key: "CI_REGISTRY_IMAGE",
Value: "alpine",
Value: common.TestAlpineImage,
})
successfulBuild.Image = common.Image{
Name: "$CI_REGISTRY_IMAGE",
......@@ -577,9 +580,9 @@ func TestDockerImageNameFromVariable(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
AllowedServices: []string{"alpine"},
AllowedServices: []string{common.TestAlpineImage},
},
},
},
......@@ -599,7 +602,7 @@ func TestDockerServiceNameFromVariable(t *testing.T) {
successfulBuild, err := common.GetRemoteSuccessfulBuild()
successfulBuild.Variables = append(successfulBuild.Variables, common.JobVariable{
Key: "CI_REGISTRY_IMAGE",
Value: "alpine",
Value: common.TestAlpineImage,
})
successfulBuild.Services = append(successfulBuild.Services, common.Image{
Name: "$CI_REGISTRY_IMAGE",
......@@ -611,9 +614,9 @@ func TestDockerServiceNameFromVariable(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
AllowedServices: []string{"alpine"},
AllowedServices: []string{common.TestAlpineImage},
},
},
},
......@@ -707,7 +710,7 @@ func testDockerVersion(t *testing.T, version string) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
DockerCredentials: credentials,
CPUS: "0.1",
......@@ -808,7 +811,7 @@ func TestDockerCommandWithBrokenGitSSLCAInfo(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
},
},
......@@ -840,7 +843,7 @@ func TestDockerCommandWithGitSSLCAInfo(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
PullPolicy: common.PullPolicyIfNotPresent,
},
},
......@@ -871,8 +874,9 @@ func TestDockerCommandWithHelperImageConfig(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "alpine",
Image: common.TestAlpineImage,
HelperImage: helperImageConfig,
PullPolicy: common.PullPolicyIfNotPresent,
},
},
},
......@@ -882,7 +886,6 @@ func TestDockerCommandWithHelperImageConfig(t *testing.T) {
err = build.Run(&common.Config{}, &common.Trace{Writer: &buffer})
assert.NoError(t, err)
out := buffer.String()
assert.Contains(t, out, "Pulling docker image "+helperImageConfig)
assert.Contains(t, out, "Using docker image sha256:bbd86c6ba107ae2feb8dbf9024df4b48597c44e1b584a3d901bba91f7fc500e3 for gitlab/gitlab-runner-helper:x86_64-64eea86c ...")
}
......@@ -912,7 +915,7 @@ func TestDockerCommandWithDoingPruneAndAfterScript(t *testing.T) {
RunnerSettings: common.RunnerSettings{
Executor: "docker",
Docker: &common.DockerConfig{
Image: "docker:git",
Image: common.TestDockerGitImage,
PullPolicy: common.PullPolicyIfNotPresent,
Volumes: []string{
"/var/run/docker.sock:/var/run/docker.sock",
......
......@@ -848,15 +848,16 @@ func TestDockerWatchOn_1_12_4(t *testing.T) {
e.Config = common.RunnerConfig{}
e.Config.Docker = &common.DockerConfig{
PullPolicy: common.PullPolicyAlways,
PullPolicy: common.PullPolicyIfNotPresent,
}
e.Trace = &common.Trace{Writer: os.Stdout}
output := bytes.NewBufferString("")
e.Trace = &common.Trace{Writer: output}
err := e.connectDocker()
assert.NoError(t, err)
container, err := e.createContainer("build", common.Image{Name: "alpine"}, []string{"/bin/sh"}, []string{})
container, err := e.createContainer("build", common.Image{Name: common.TestAlpineImage}, []string{"/bin/sh"}, []string{})
assert.NoError(t, err)
assert.NotNil(t, container)
......@@ -868,13 +869,13 @@ func TestDockerWatchOn_1_12_4(t *testing.T) {
go func() {
err = e.watchContainer(e.Context, container.ID, input)
assert.NoError(t, err)
t.Log(err)
finished <- true
wg.Done()
}()
select {
case <-finished:
assert.Equal(t, "script\n", output.String())
case <-time.After(15 * time.Second):
t.Error("Container script not finished")
}
......
......@@ -1256,13 +1256,15 @@ func TestKubernetesSuccessRun(t *testing.T) {
successfulBuild, err := common.GetRemoteSuccessfulBuild()
assert.NoError(t, err)
successfulBuild.Image.Name = "docker:git"
successfulBuild.Image.Name = common.TestDockerGitImage
build := &common.Build{
JobResponse: successfulBuild,
Runner: &common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{},
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{
PullPolicy: common.PullPolicyIfNotPresent,
},
},
},
}
......@@ -1279,13 +1281,15 @@ func TestKubernetesNoRootImage(t *testing.T) {
successfulBuild, err := common.GetRemoteSuccessfulBuildWithDumpedVariables()
assert.NoError(t, err)
successfulBuild.Image.Name = "registry.gitlab.com/gitlab-org/gitlab-runner/alpine-no-root"
successfulBuild.Image.Name = common.TestAlpineNoRootImage
build := &common.Build{
JobResponse: successfulBuild,
Runner: &common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{},
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{
PullPolicy: common.PullPolicyIfNotPresent,
},
},
},
}
......@@ -1305,12 +1309,14 @@ func TestKubernetesBuildFail(t *testing.T) {
JobResponse: failedBuild,
Runner: &common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{},
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{
PullPolicy: common.PullPolicyIfNotPresent,
},
},
},
}
build.Image.Name = "docker:git"
build.Image.Name = common.TestDockerGitImage
err = build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
require.Error(t, err, "error")
......@@ -1377,13 +1383,15 @@ func TestKubernetesBuildAbort(t *testing.T) {
JobResponse: failedBuild,
Runner: &common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{},
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{
PullPolicy: common.PullPolicyIfNotPresent,
},
},
},
SystemInterrupt: make(chan os.Signal, 1),
}
build.Image.Name = "docker:git"
build.Image.Name = common.TestDockerGitImage
abortTimer := time.AfterFunc(time.Second, func() {
t.Log("Interrupt")
......@@ -1412,13 +1420,15 @@ func TestKubernetesBuildCancel(t *testing.T) {
JobResponse: failedBuild,
Runner: &common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{},
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{
PullPolicy: common.PullPolicyIfNotPresent,
},
},
},
SystemInterrupt: make(chan os.Signal, 1),
}
build.Image.Name = "docker:git"
build.Image.Name = common.TestDockerGitImage
trace := &common.Trace{Writer: os.Stdout}
......@@ -1461,12 +1471,13 @@ func TestOverwriteNamespaceNotMatch(t *testing.T) {
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{
NamespaceOverwriteAllowed: "^not_a_match$",
PullPolicy: common.PullPolicyIfNotPresent,
},
},
},
SystemInterrupt: make(chan os.Signal, 1),
}
build.Image.Name = "docker:git"
build.Image.Name = common.TestDockerGitImage
err := build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
require.Error(t, err)
......@@ -1495,12 +1506,13 @@ func TestOverwriteServiceAccountNotMatch(t *testing.T) {
Executor: "kubernetes",
Kubernetes: &common.KubernetesConfig{
ServiceAccountOverwriteAllowed: "^not_a_match$",
PullPolicy: common.PullPolicyIfNotPresent,
},
},
},
SystemInterrupt: make(chan os.Signal, 1),
}
build.Image.Name = "docker:git"
build.Image.Name = common.TestDockerGitImage
err := build.Run(&common.Config{}, &common.Trace{Writer: os.Stdout})
require.Error(t, err)
......
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"sync"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
)
var images = []string{
common.TestAlpineImage,
common.TestAlpineNoRootImage,
common.TestDockerGitImage,
common.TestDockerDindImage,
}
type rewriter struct {
ctx context.Context
prefix string
input *bufio.Reader
}
func (rw *rewriter) watch() {
for {
select {
case <-rw.ctx.Done():
return
case err := <-rw.rewriteInput():
if err != nil {
rw.writeToOutput(fmt.Sprintf("Error while reading command output: %v", err))
return
}
}
}
}
func (rw *rewriter) rewriteInput() <-chan error {
e := make(chan error)
go func() {
line, err := rw.input.ReadString('\n')
if err == nil || err == io.EOF {
rw.writeToOutput(line)
e <- nil
return
}
e <- err
}()
return e
}
func (rw *rewriter) writeToOutput(line string) {
fmt.Printf("%s[%s]%s %s", helpers.ANSI_YELLOW, rw.prefix, helpers.ANSI_RESET, line)
}
func newRewriter(ctx context.Context, prefix string) io.Writer {
pr, pw, err := os.Pipe()
if err != nil {
panic(err)
}
w := &rewriter{
ctx: ctx,
prefix: prefix,
input: bufio.NewReader(pr),
}
go w.watch()
return pw
}
func pullImage(wg *sync.WaitGroup, name string) {
ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
wg.Done()
}()
output := newRewriter(ctx, name)
cmd := exec.Command("docker", "pull", name)
cmd.Stdout = output
cmd.Stderr = output
err := cmd.Run()
if err != nil {
panic(err)
}
}
func main() {
wg := new(sync.WaitGroup)
for _, image := range images {
wg.Add(1)
go pullImage(wg, image)
}
wg.Wait()
}
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