From 3e7f6031bd2ebdbd653976cf6cbbbe7d4b5089bf Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin <tomasz@maczukin.pl> Date: Tue, 1 Aug 2017 13:48:46 +0200 Subject: [PATCH] Fix services usage when service name is using variable --- executors/docker/executor_docker.go | 3 +- .../docker/executor_docker_command_test.go | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/executors/docker/executor_docker.go b/executors/docker/executor_docker.go index 71d97e870..edc38755c 100644 --- a/executors/docker/executor_docker.go +++ b/executors/docker/executor_docker.go @@ -621,13 +621,12 @@ func (s *executor) getServicesDefinitions() (common.Services, error) { for _, service := range s.Build.Services { serviceName := s.Build.GetAllVariables().ExpandValue(service.Name) - err := s.verifyAllowedImage(service.Name, "services", s.Config.Docker.AllowedServices, s.Config.Docker.Services) + err := s.verifyAllowedImage(serviceName, "services", s.Config.Docker.AllowedServices, s.Config.Docker.Services) if err != nil { return nil, err } service.Name = serviceName - serviceDefinitions = append(serviceDefinitions, service) } diff --git a/executors/docker/executor_docker_command_test.go b/executors/docker/executor_docker_command_test.go index 6071cfe25..51c0bd1da 100644 --- a/executors/docker/executor_docker_command_test.go +++ b/executors/docker/executor_docker_command_test.go @@ -470,6 +470,72 @@ func TestCacheInContainer(t *testing.T) { assert.NotContains(t, output, skipCacheUpload, "Cache upload should be performed with policy: push") } +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", + Value: "alpine", + }) + 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{ + Image: "alpine", + AllowedServices: []string{"alpine"}, + }, + }, + }, + } + + 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", + Value: "alpine", + }) + 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{ + Image: "alpine", + AllowedServices: []string{"alpine"}, + }, + }, + }, + } + + 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") +} + func runDockerInDocker(version string) (id string, err error) { cmd := exec.Command("docker", "run", "--detach", "--privileged", "-p", "2375", "docker:"+version+"-dind") cmd.Stderr = os.Stderr -- GitLab