From 033941d890bd76bc36e52610c905a5710d47469d Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Tue, 8 Dec 2020 15:23:24 +0100 Subject: [PATCH] compose: Add service labels Signed-off-by: Djordje Lukic --- local/compose/compose_test.go | 3 --- local/compose/create.go | 24 +++++++++++-------- .../e2e => tests/compose-e2e}/compose_test.go | 12 +++++++--- .../volume-test/docker-compose.yml | 0 .../volume-test/nginx-build/Dockerfile | 0 .../volume-test/static/index.html | 0 tests/composefiles/demo_multi_port.yaml | 4 +++- 7 files changed, 26 insertions(+), 17 deletions(-) rename {local/compose/e2e => tests/compose-e2e}/compose_test.go (90%) rename {local/compose/e2e => tests/compose-e2e}/volume-test/docker-compose.yml (100%) rename {local/compose/e2e => tests/compose-e2e}/volume-test/nginx-build/Dockerfile (100%) rename {local/compose/e2e => tests/compose-e2e}/volume-test/static/index.html (100%) diff --git a/local/compose/compose_test.go b/local/compose/compose_test.go index d57c25b7..94d8460b 100644 --- a/local/compose/compose_test.go +++ b/local/compose/compose_test.go @@ -17,7 +17,6 @@ package compose import ( - "os" "path/filepath" "testing" @@ -119,7 +118,5 @@ func TestBuildBindMount(t *testing.T) { mount, err := buildMount(volume) assert.NilError(t, err) assert.Assert(t, filepath.IsAbs(mount.Source)) - _, err = os.Stat(mount.Source) - assert.NilError(t, err) assert.Equal(t, mount.Type, mountTypes.TypeBind) } diff --git a/local/compose/create.go b/local/compose/create.go index 49b08e1d..94bae858 100644 --- a/local/compose/create.go +++ b/local/compose/create.go @@ -82,18 +82,22 @@ func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number i if err != nil { return nil, nil, nil, err } - // TODO: change oneoffLabel value for containers started with `docker compose run` - labels := map[string]string{ - projectLabel: p.Name, - serviceLabel: s.Name, - versionLabel: ComposeVersion, - oneoffLabel: "False", - configHashLabel: hash, - workingDirLabel: p.WorkingDir, - configFilesLabel: strings.Join(p.ComposeFiles, ","), - containerNumberLabel: strconv.Itoa(number), + + labels := map[string]string{} + for k, v := range s.Labels { + labels[k] = v } + // TODO: change oneoffLabel value for containers started with `docker compose run` + labels[projectLabel] = p.Name + labels[serviceLabel] = s.Name + labels[versionLabel] = ComposeVersion + labels[oneoffLabel] = "False" + labels[configHashLabel] = hash + labels[workingDirLabel] = p.WorkingDir + labels[configFilesLabel] = strings.Join(p.ComposeFiles, ",") + labels[containerNumberLabel] = strconv.Itoa(number) + var ( runCmd strslice.StrSlice entrypoint strslice.StrSlice diff --git a/local/compose/e2e/compose_test.go b/tests/compose-e2e/compose_test.go similarity index 90% rename from local/compose/e2e/compose_test.go rename to tests/compose-e2e/compose_test.go index eb8158ed..f9e5f4d4 100644 --- a/local/compose/e2e/compose_test.go +++ b/tests/compose-e2e/compose_test.go @@ -50,14 +50,14 @@ func TestLocalComposeUp(t *testing.T) { const projectName = "compose-e2e-demo" t.Run("build", func(t *testing.T) { - res := c.RunDockerCmd("compose", "build", "-f", "../../../tests/composefiles/demo_multi_port.yaml") + res := c.RunDockerCmd("compose", "build", "-f", "../composefiles/demo_multi_port.yaml") res.Assert(t, icmd.Expected{Out: "COPY words.sql /docker-entrypoint-initdb.d/"}) res.Assert(t, icmd.Expected{Out: "COPY pom.xml ."}) res.Assert(t, icmd.Expected{Out: "COPY static /static/"}) }) t.Run("up", func(t *testing.T) { - c.RunDockerCmd("compose", "up", "-d", "-f", "../../../tests/composefiles/demo_multi_port.yaml", "--project-name", projectName, "-d") + c.RunDockerCmd("compose", "up", "-d", "-f", "../composefiles/demo_multi_port.yaml", "--project-name", projectName, "-d") }) t.Run("check running project", func(t *testing.T) { @@ -78,7 +78,7 @@ func TestLocalComposeUp(t *testing.T) { res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "compose-e2e-demo"`}) res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "False",`}) res.Assert(t, icmd.Expected{Out: `"com.docker.compose.config-hash":`}) - res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.config_files": "../../../tests/composefiles/demo_multi_port.yaml"`}) + res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.config_files": "../composefiles/demo_multi_port.yaml"`}) res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.working_dir":`}) res.Assert(t, icmd.Expected{Out: `"com.docker.compose.service": "web"`}) res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version":`}) @@ -89,6 +89,12 @@ func TestLocalComposeUp(t *testing.T) { res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version": `}) }) + t.Run("check user labels", func(t *testing.T) { + res := c.RunDockerCmd("inspect", projectName+"_web_1") + res.Assert(t, icmd.Expected{Out: `"my-label": "test"`}) + + }) + t.Run("down", func(t *testing.T) { _ = c.RunDockerCmd("compose", "down", "--project-name", projectName) }) diff --git a/local/compose/e2e/volume-test/docker-compose.yml b/tests/compose-e2e/volume-test/docker-compose.yml similarity index 100% rename from local/compose/e2e/volume-test/docker-compose.yml rename to tests/compose-e2e/volume-test/docker-compose.yml diff --git a/local/compose/e2e/volume-test/nginx-build/Dockerfile b/tests/compose-e2e/volume-test/nginx-build/Dockerfile similarity index 100% rename from local/compose/e2e/volume-test/nginx-build/Dockerfile rename to tests/compose-e2e/volume-test/nginx-build/Dockerfile diff --git a/local/compose/e2e/volume-test/static/index.html b/tests/compose-e2e/volume-test/static/index.html similarity index 100% rename from local/compose/e2e/volume-test/static/index.html rename to tests/compose-e2e/volume-test/static/index.html diff --git a/tests/composefiles/demo_multi_port.yaml b/tests/composefiles/demo_multi_port.yaml index b5c1d1de..cba9f69f 100644 --- a/tests/composefiles/demo_multi_port.yaml +++ b/tests/composefiles/demo_multi_port.yaml @@ -12,4 +12,6 @@ services: build: aci-demo/web image: gtardif/sentences-web ports: - - "80:80" \ No newline at end of file + - "80:80" + labels: + - "my-label=test"