2021-08-23 09:14:53 -04:00
|
|
|
//go:build !darwin && !windows
|
2020-11-10 11:43:44 -05:00
|
|
|
// +build !darwin,!windows
|
|
|
|
|
pkg/system: move EnsureRemoveAll() to pkg/containerfs
pkg/system historically has been a bit of a kitchen-sink of things that were
somewhat "system" related, but didn't have a good place for. EnsureRemoveAll()
is one of those utilities. EnsureRemoveAll() is used to both unmount and remove
a path, for which it depends on both github.com/moby/sys/mount, which in turn
depends on github.com/moby/sys/mountinfo.
pkg/system is imported in the CLI, but neither EnsureRemoveAll(), nor any of its
moby/sys dependencies are used on the client side, so let's move this function
somewhere else, to remove those dependencies from the CLI.
I looked for plausible locations that were related; it's used in:
- daemon
- daemon/graphdriver/XXX/
- plugin
I considered moving it into a (e.g.) "utils" package within graphdriver (but not
a huge fan of "utils" packages), and given that it felt (mostly) related to
cleaning up container filesystems, I decided to move it there.
Some things to follow-up on after this:
- Verify if this function is still needed (it feels a bit like a big hammer in
a "YOLO, let's try some things just in case it fails")
- Perhaps it should be integrated in `containerfs.Remove()` (so that it's used
automatically)
- Look if there's other implementations (and if they should be consolidated),
although (e.g.) the one in containerd is a copy of ours:
https://github.com/containerd/containerd/blob/v1.5.9/pkg/cri/server/helpers_linux.go#L200
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-02 16:43:07 -05:00
|
|
|
package containerfs // import "github.com/docker/docker/pkg/containerfs"
|
2017-02-14 13:35:20 -05:00
|
|
|
|
|
|
|
import (
|
2020-11-10 11:43:44 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-02-14 13:35:20 -05:00
|
|
|
"testing"
|
2020-11-10 11:43:44 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/moby/sys/mount"
|
2017-02-14 13:35:20 -05:00
|
|
|
)
|
|
|
|
|
2020-11-10 11:43:44 -05:00
|
|
|
func TestEnsureRemoveAllWithMount(t *testing.T) {
|
pkg/system: move EnsureRemoveAll() to pkg/containerfs
pkg/system historically has been a bit of a kitchen-sink of things that were
somewhat "system" related, but didn't have a good place for. EnsureRemoveAll()
is one of those utilities. EnsureRemoveAll() is used to both unmount and remove
a path, for which it depends on both github.com/moby/sys/mount, which in turn
depends on github.com/moby/sys/mountinfo.
pkg/system is imported in the CLI, but neither EnsureRemoveAll(), nor any of its
moby/sys dependencies are used on the client side, so let's move this function
somewhere else, to remove those dependencies from the CLI.
I looked for plausible locations that were related; it's used in:
- daemon
- daemon/graphdriver/XXX/
- plugin
I considered moving it into a (e.g.) "utils" package within graphdriver (but not
a huge fan of "utils" packages), and given that it felt (mostly) related to
cleaning up container filesystems, I decided to move it there.
Some things to follow-up on after this:
- Verify if this function is still needed (it feels a bit like a big hammer in
a "YOLO, let's try some things just in case it fails")
- Perhaps it should be integrated in `containerfs.Remove()` (so that it's used
automatically)
- Look if there's other implementations (and if they should be consolidated),
although (e.g.) the one in containerd is a copy of ours:
https://github.com/containerd/containerd/blob/v1.5.9/pkg/cri/server/helpers_linux.go#L200
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-02 16:43:07 -05:00
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("skipping test that requires root")
|
|
|
|
}
|
2017-02-14 13:35:20 -05:00
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
dir1, err := os.MkdirTemp("", "test-ensure-removeall-with-dir1")
|
2017-02-14 13:35:20 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
dir2, err := os.MkdirTemp("", "test-ensure-removeall-with-dir2")
|
2020-11-10 11:43:44 -05:00
|
|
|
if err != nil {
|
2017-02-14 13:35:20 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-10 11:43:44 -05:00
|
|
|
defer os.RemoveAll(dir2)
|
2017-02-14 13:35:20 -05:00
|
|
|
|
2020-11-10 11:43:44 -05:00
|
|
|
bindDir := filepath.Join(dir1, "bind")
|
|
|
|
if err := os.MkdirAll(bindDir, 0755); err != nil {
|
2017-02-14 13:35:20 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-10 11:43:44 -05:00
|
|
|
|
|
|
|
if err := mount.Mount(dir2, bindDir, "none", "bind"); err != nil {
|
2017-02-14 13:35:20 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-10 11:43:44 -05:00
|
|
|
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
go func() {
|
|
|
|
err = EnsureRemoveAll(dir1)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for EnsureRemoveAll to finish")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(dir1); !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("expected %q to not exist", dir1)
|
|
|
|
}
|
2017-02-14 13:35:20 -05:00
|
|
|
}
|