Browse Source

compose: Add service labels

Signed-off-by: Djordje Lukic <[email protected]>
Djordje Lukic 4 years ago
parent
commit
033941d890

+ 0 - 3
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)
 }

+ 14 - 10
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

+ 9 - 3
local/compose/e2e/compose_test.go → 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)
 	})

+ 0 - 0
local/compose/e2e/volume-test/docker-compose.yml → tests/compose-e2e/volume-test/docker-compose.yml


+ 0 - 0
local/compose/e2e/volume-test/nginx-build/Dockerfile → tests/compose-e2e/volume-test/nginx-build/Dockerfile


+ 0 - 0
local/compose/e2e/volume-test/static/index.html → tests/compose-e2e/volume-test/static/index.html


+ 3 - 1
tests/composefiles/demo_multi_port.yaml

@@ -12,4 +12,6 @@ services:
     build: aci-demo/web
     image: gtardif/sentences-web
     ports:
-      - "80:80"
+      - "80:80"
+    labels:
+      - "my-label=test"