Skip to content

Commit a3c783b

Browse files
committed
compose: add unit tests for config hash and dependency handling
- TestComposeCreateWritesConfigHashLabel: verify config-hash label is written - TestComposeUpNoRecreateDependencies: ensure dependencies aren't recreated Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent cabb02f commit a3c783b

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

cmd/nerdctl/compose/compose_create_linux_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/containerd/nerdctl/mod/tigron/test"
2828
"github.com/containerd/nerdctl/mod/tigron/tig"
2929

30+
"github.com/containerd/nerdctl/v2/pkg/composer/serviceparser"
3031
"github.com/containerd/nerdctl/v2/pkg/testutil"
3132
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
3233
)
@@ -204,3 +205,24 @@ services:
204205
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "svc0").AssertOutContains(imageSvc0)
205206
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0", "-a").AssertOutContainsAny("Created", "created")
206207
}
208+
209+
func TestComposeCreateWritesConfigHashLabel(t *testing.T) {
210+
var dockerComposeYAML = fmt.Sprintf(`
211+
services:
212+
svc0:
213+
image: %s
214+
`, testutil.CommonImage)
215+
216+
base := testutil.NewBase(t)
217+
comp := testutil.NewComposeDir(t, dockerComposeYAML)
218+
defer comp.CleanUp()
219+
projectName := comp.ProjectName()
220+
t.Logf("projectName=%q", projectName)
221+
222+
base.ComposeCmd("-f", comp.YAMLFullPath(), "create").AssertOK()
223+
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()
224+
225+
container := serviceparser.DefaultContainerName(projectName, "svc0", "1")
226+
base.Cmd("inspect", "--format", "{{json .Config.Labels}}", container).
227+
AssertOutContains("com.docker.compose.config-hash")
228+
}

cmd/nerdctl/compose/compose_up_linux_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,46 @@ services:
377377
base.ComposeCmd("-f", comp.YAMLFullPath(), "down").AssertOK()
378378
}
379379

380+
func TestComposeUpNoRecreateDependencies(t *testing.T) {
381+
base := testutil.NewBase(t)
382+
383+
var dockerComposeYAML = fmt.Sprintf(`
384+
services:
385+
foo:
386+
image: %s
387+
command: "sleep infinity"
388+
bar:
389+
image: %s
390+
command: "sleep infinity"
391+
depends_on:
392+
- foo
393+
`, testutil.CommonImage, testutil.CommonImage)
394+
395+
comp := testutil.NewComposeDir(t, dockerComposeYAML)
396+
defer comp.CleanUp()
397+
projectName := comp.ProjectName()
398+
t.Logf("projectName=%q", projectName)
399+
400+
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d", "foo").AssertOK()
401+
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()
402+
403+
fooName := serviceparser.DefaultContainerName(projectName, "foo", "1")
404+
id1Cmd := base.Cmd("inspect", fooName, "--format", "{{.Id}}")
405+
id1Res := id1Cmd.Run()
406+
out1 := strings.TrimSpace(id1Res.Stdout())
407+
assert.Assert(id1Cmd.Base.T, id1Res.ExitCode == 0, id1Res.Stdout()+id1Res.Stderr())
408+
409+
// Bring up dependent service; ensure foo is not recreated (ID unchanged)
410+
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d", "bar").AssertOK()
411+
412+
id2Cmd := base.Cmd("inspect", fooName, "--format", "{{.Id}}")
413+
id2Res := id2Cmd.Run()
414+
out2 := strings.TrimSpace(id2Res.Stdout())
415+
assert.Assert(id2Cmd.Base.T, id2Res.ExitCode == 0, id2Res.Stdout()+id2Res.Stderr())
416+
417+
assert.Equal(base.T, out1, out2)
418+
}
419+
380420
func TestComposeUpWithExternalNetwork(t *testing.T) {
381421
testCase := nerdtest.Setup()
382422

0 commit comments

Comments
 (0)