Skip to content

Commit fcb900e

Browse files
committed
test: Add an test for added logic to TestRunWithInvalidPortThenCleanUp
This commit adds an test for added logic to clean up an orphaned etchosts directory for the container that failed to create to TestRunWithInvalidPortThenCleanUp. Signed-off-by: Hayato Kiwata <[email protected]>
1 parent 7bfbd31 commit fcb900e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

cmd/nerdctl/container/container_run_network_linux_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ import (
2222
"net"
2323
"os"
2424
"os/exec"
25+
"path/filepath"
2526
"regexp"
2627
"runtime"
2728
"strings"
2829
"testing"
2930
"time"
3031

3132
"github.com/containernetworking/plugins/pkg/ns"
33+
"github.com/opencontainers/go-digest"
3234
"github.com/vishvananda/netlink"
3335
"gotest.tools/v3/assert"
3436
"gotest.tools/v3/icmd"
3537

38+
"github.com/containerd/containerd/v2/defaults"
3639
"github.com/containerd/containerd/v2/pkg/netns"
3740
"github.com/containerd/errdefs"
3841

@@ -368,6 +371,27 @@ func TestRunWithInvalidPortThenCleanUp(t *testing.T) {
368371
return &test.Expected{
369372
ExitCode: 1,
370373
Errors: []error{errdefs.ErrInvalidArgument},
374+
Output: func(stdout string, info string, t *testing.T) {
375+
getAddrHash := func(addr string) string {
376+
const addrHashLen = 8
377+
378+
d := digest.SHA256.FromString(addr)
379+
h := d.Encoded()[0:addrHashLen]
380+
381+
return h
382+
}
383+
384+
dataRoot := data.TempDir()
385+
h := getAddrHash(defaults.DefaultAddress)
386+
dataStore := filepath.Join(dataRoot, h)
387+
namespace := string(helpers.Read(nerdtest.Namespace))
388+
etchostsPath := filepath.Join(dataStore, "etchosts", namespace)
389+
390+
etchostsDirs, err := os.ReadDir(etchostsPath)
391+
392+
assert.NilError(t, err)
393+
assert.Equal(t, len(etchostsDirs), 0)
394+
},
371395
}
372396
},
373397
},

0 commit comments

Comments
 (0)