Use os.ReadDir instead of filepath.Walk in netns cleanup

- Use `os.ReadDir()` instead of `filepath.WalkDir()`, because
  `filepath.WalkDir()` does a lot of extra things we don't need
- Log errors on directory removal (omitting `os.IsNotExist`)
- Touch-up error messages for consistency

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2018-03-07 14:05:26 +01:00
parent 17c3829528
commit 5d8286b039
2 changed files with 27 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
package daemon // import "github.com/docker/docker/testutil/daemon"
import (
"errors"
"fmt"
"os"
"path/filepath"
@@ -18,13 +19,32 @@ func cleanupNetworkNamespace(t testing.TB, d *Daemon) {
// daemon instance and has no chance of getting
// cleaned up when a new daemon is instantiated with a
// new exec root.
filepath.WalkDir(filepath.Join(d.execRoot, "netns"), func(path string, _ os.DirEntry, _ error) error {
if err := unix.Unmount(path, unix.MNT_DETACH); err != nil && err != unix.EINVAL && err != unix.ENOENT {
t.Logf("[%s] unmount of %s failed: %v", d.id, path, err)
netnsPath := filepath.Join(d.execRoot, "netns")
files, err := os.ReadDir(netnsPath)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
t.Logf("[%s] unable to read netns path (%s): %s", d.id, netnsPath, err)
}
os.Remove(path)
return nil
})
return
}
for _, dir := range files {
if !dir.IsDir() {
continue
}
path := filepath.Join(netnsPath, dir.Name())
// Unmount the network namespace using MNT_DETACH (lazy unmount).
// Ignore EINVAL ("target is not a mount point"), and ENOENT (not found),
// which could occur if the network namespace was already gone.
if err := unix.Unmount(path, unix.MNT_DETACH); err != nil && err != unix.EINVAL && err != unix.ENOENT {
t.Logf("[%s] unmount of network namespace %s failed: %v", d.id, path, err)
}
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
t.Logf("[%s] unable to remove network namespace %s: %s", d.id, path, err)
}
}
}
// CgroupNamespace returns the cgroup namespace the daemon is running in

View File

@@ -22,7 +22,7 @@ func cleanupMount(t testing.TB, d *Daemon) {
// SignalDaemonDump sends a signal to the daemon to write a dump file
func SignalDaemonDump(pid int) {
unix.Kill(pid, unix.SIGQUIT)
_ = unix.Kill(pid, unix.SIGQUIT)
}
func signalDaemonReload(pid int) error {