Skip to content

Commit

Permalink
fix: Cleaning up orphaned directories and files
Browse files Browse the repository at this point in the history
Update tests to pass the CI.

Signed-off-by: Hayato Kiwata <[email protected]>
  • Loading branch information
haytok committed Sep 26, 2024
1 parent e3357f8 commit 5e6bfc3
Showing 1 changed file with 70 additions and 50 deletions.
120 changes: 70 additions & 50 deletions cmd/nerdctl/container/container_create_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func TestCreateWithTty(t *testing.T) {

// TestIssue2993 tests https://github.com/containerd/nerdctl/issues/2993
func TestIssue2993(t *testing.T) {
base := testutil.NewBase(t)
testutil.DockerIncompatible(t)
t.Parallel()

getAddrHash := func(addr string) string {
const addrHashLen = 8
Expand All @@ -193,59 +194,78 @@ func TestIssue2993(t *testing.T) {

return h
}
h := getAddrHash(defaults.DefaultAddress)
dataStore := filepath.Join(ncdefaults.DataRoot(), h)

containersPath := filepath.Join(dataStore, "containers", testutil.Namespace)
etchostsPath := filepath.Join(dataStore, "etchosts", testutil.Namespace)

type testCase struct {
description, containerName string
description, containerName, namespace string
command func(t *testing.T, containerName, namespace, containersPath, etchostsPath string)
}

tc := testCase{
description: "Issue #2993 - nerdctl no longer leaks containers and etchosts directories and files when container creation fails or a container is removed.",
containerName: testutil.Identifier(t),
testCases := []testCase{
{
description: "Issue #2993 - nerdctl no longer leaks containers and etchosts directories and files when container creation fails.",
containerName: "failure-of-container-creation",
namespace: "failure-of-container-creation",
command: func(t *testing.T, containerName, namespace, containersPath, etchostsPath string) {
base := testutil.NewBaseWithNamespace(t, namespace)

defer base.Cmd("rm", "-f", containerName).Run()
base.Cmd("run", "--name", containerName, "-d", testutil.AlpineImage, "sleep", "infinity").Run()

containersDirs, err := os.ReadDir(containersPath)
assert.NilError(t, err)
containersDirCounts := len(containersDirs)
etchostsDirs, err := os.ReadDir(etchostsPath)
assert.NilError(t, err)
etchostsDirCounts := len(etchostsDirs)

// fail to run a container
base.Cmd("run", "--name", containerName, "-d", testutil.AlpineImage, "sleep", "infinity").Run()

containersDirs, err = os.ReadDir(containersPath)
assert.NilError(t, err)
etchostsDirs, err = os.ReadDir(etchostsPath)
assert.NilError(t, err)
assert.Equal(t, len(containersDirs), containersDirCounts)
assert.Equal(t, len(etchostsDirs), etchostsDirCounts)
},
},
{
description: "Issue #2993 - nerdctl no longer leaks containers and etchosts directories and files when containers are removed.",
containerName: "container-remove",
namespace: "container-remove",
command: func(t *testing.T, containerName, namespace, containersPath, etchostsPath string) {
base := testutil.NewBaseWithNamespace(t, namespace)

base.Cmd("run", "--name", containerName, "-d", testutil.AlpineImage, "sleep", "infinity").Run()

containersDirs, err := os.ReadDir(containersPath)
assert.NilError(t, err)
containersDirCounts := len(containersDirs)
etchostsDirs, err := os.ReadDir(etchostsPath)
assert.NilError(t, err)
etchostsDirCounts := len(etchostsDirs)

base.Cmd("rm", "-f", containerName).Run()

containersDirs, err = os.ReadDir(containersPath)
assert.NilError(t, err)
etchostsDirs, err = os.ReadDir(etchostsPath)
assert.NilError(t, err)
assert.Equal(t, len(containersDirs), containersDirCounts-1)
assert.Equal(t, len(etchostsDirs), etchostsDirCounts-1)
},
},
}

t.Run(tc.description, func(t *testing.T) {
defer base.Cmd("rm", "-f", tc.containerName).Run()
base.Cmd("run", "--name", tc.containerName, "-d", testutil.AlpineImage, "sleep", "infinity").Run()

containersDirs, err := os.ReadDir(containersPath)
assert.NilError(t, err)
containersDirCounts := len(containersDirs)
etchostsDirs, err := os.ReadDir(etchostsPath)
assert.NilError(t, err)
etchostsDirCounts := len(etchostsDirs)

// fail to run a container
base.Cmd("run", "--name", tc.containerName, "-d", testutil.AlpineImage, "sleep", "infinity").Run()

containersDirs, err = os.ReadDir(containersPath)
assert.NilError(t, err)
etchostsDirs, err = os.ReadDir(etchostsPath)
assert.NilError(t, err)
assert.Equal(t, len(containersDirs), containersDirCounts)
assert.Equal(t, len(etchostsDirs), etchostsDirCounts)
})

t.Run(tc.description, func(t *testing.T) {
containersDirs, err := os.ReadDir(containersPath)
assert.NilError(t, err)
containersDirCounts := len(containersDirs)
etchostsDirs, err := os.ReadDir(etchostsPath)
assert.NilError(t, err)
etchostsDirCounts := len(etchostsDirs)

base.Cmd("run", "--name", tc.containerName, "-d", testutil.AlpineImage, "sleep", "infinity").Run()
base.Cmd("rm", "-f", tc.containerName).Run()

containersDirs, err = os.ReadDir(containersPath)
assert.NilError(t, err)
etchostsDirs, err = os.ReadDir(etchostsPath)
assert.NilError(t, err)
assert.Equal(t, len(containersDirs), containersDirCounts)
assert.Equal(t, len(etchostsDirs), etchostsDirCounts)
})
for _, test := range testCases {
currentTest := test
t.Run(currentTest.description, func(tt *testing.T) {
h := getAddrHash(defaults.DefaultAddress)
dataStore := filepath.Join(ncdefaults.DataRoot(), h)
containersPath := filepath.Join(dataStore, "containers", currentTest.namespace)
etchostsPath := filepath.Join(dataStore, "etchosts", currentTest.namespace)

currentTest.command(tt, currentTest.containerName, currentTest.namespace, containersPath, etchostsPath)
})
}
}

0 comments on commit 5e6bfc3

Please sign in to comment.