Skip to content

Commit

Permalink
Merge pull request #3819 from haytok/fix-to-clean-up-an-orphaned-etch…
Browse files Browse the repository at this point in the history
…osts-directory

fix: clean up an orphaned etchosts directory for the container that failed to create
  • Loading branch information
AkihiroSuda authored Jan 17, 2025
2 parents 1259a55 + fcb900e commit f15d0ad
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
55 changes: 47 additions & 8 deletions cmd/nerdctl/container/container_run_network_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ import (
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"time"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/opencontainers/go-digest"
"github.com/vishvananda/netlink"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"

"github.com/containerd/containerd/v2/defaults"
"github.com/containerd/containerd/v2/pkg/netns"
"github.com/containerd/errdefs"

Expand All @@ -41,6 +44,7 @@ import (
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
)

func extractHostPort(portMapping string, port string) (string, error) {
Expand Down Expand Up @@ -350,15 +354,50 @@ func TestRunPort(t *testing.T) {
}

func TestRunWithInvalidPortThenCleanUp(t *testing.T) {
testCase := nerdtest.Setup()
// docker does not set label restriction to 4096 bytes
testutil.DockerIncompatible(t)
t.Parallel()
base := testutil.NewBase(t)
containerName := testutil.Identifier(t)
defer base.Cmd("rm", "-f", containerName).Run()
base.Cmd("run", "--rm", "--name", containerName, "-p", "22200-22299:22200-22299", testutil.CommonImage).AssertFail()
base.Cmd("run", "--rm", "--name", containerName, "-p", "22200-22299:22200-22299", testutil.CommonImage).AssertCombinedOutContains(errdefs.ErrInvalidArgument.Error())
base.Cmd("run", "--rm", "--name", containerName, testutil.CommonImage).AssertOK()
testCase.Require = test.Not(nerdtest.Docker)

testCase.SubTests = []*test.Case{
{
Description: "Run a container with invalid ports, and then clean up.",
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "--data-root", data.TempDir(), "-f", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("run", "--data-root", data.TempDir(), "--rm", "--name", data.Identifier(), "-p", "22200-22299:22200-22299", testutil.CommonImage)
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 1,
Errors: []error{errdefs.ErrInvalidArgument},
Output: func(stdout string, info string, t *testing.T) {
getAddrHash := func(addr string) string {
const addrHashLen = 8

d := digest.SHA256.FromString(addr)
h := d.Encoded()[0:addrHashLen]

return h
}

dataRoot := data.TempDir()
h := getAddrHash(defaults.DefaultAddress)
dataStore := filepath.Join(dataRoot, h)
namespace := string(helpers.Read(nerdtest.Namespace))
etchostsPath := filepath.Join(dataStore, "etchosts", namespace)

etchostsDirs, err := os.ReadDir(etchostsPath)

assert.NilError(t, err)
assert.Equal(t, len(etchostsDirs), 0)
},
}
},
},
}

testCase.Run(t)
}

func TestRunContainerWithStaticIP(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,17 @@ func generateGcFunc(ctx context.Context, container containerd.Container, ns, id,
if netGcErr != nil {
log.G(ctx).WithError(netGcErr).Warnf("failed to revert container %q networking settings", id)
}
} else {
hs, err := hostsstore.New(dataStore, internalLabels.namespace)
if err != nil {
log.G(ctx).WithError(err).Warnf("failed to instantiate hostsstore for %q", internalLabels.namespace)
} else {
if _, err := hs.HostsPath(id); err != nil {
log.G(ctx).WithError(err).Warnf("an etchosts directory for container %q dosen't exist", id)
} else if err = hs.Delete(id); err != nil {
log.G(ctx).WithError(err).Warnf("failed to remove an etchosts directory for container %q", id)
}
}
}

ipc, ipcErr := ipcutil.DecodeIPCLabel(internalLabels.ipc)
Expand Down

0 comments on commit f15d0ad

Please sign in to comment.