From 5349fa31dfed014e8ca672e4a98023c8fdeec195 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Tue, 2 Oct 2018 13:08:29 +0900 Subject: [PATCH] remove pkg/testutil/loopback_linux.go and use continuity/testutil/loopback Signed-off-by: Akihiro Suda --- mount/{lookup_test => }/lookup_linux_test.go | 19 +++---- snapshots/btrfs/btrfs_test.go | 3 +- snapshots/overlay/check_test.go | 3 +- vendor.conf | 2 +- .../containerd/continuity/testutil/helpers.go | 27 +++++++++ .../continuity/testutil/helpers_unix.go | 57 +++++++++++++++++++ .../continuity/testutil/helpers_windows.go | 32 +++++++++++ .../testutil/loopback}/loopback_linux.go | 6 +- .../continuity/testutil/mount_linux.go | 6 +- .../continuity/testutil/mount_other.go | 21 +++++++ 10 files changed, 156 insertions(+), 20 deletions(-) rename mount/{lookup_test => }/lookup_linux_test.go (86%) create mode 100644 vendor/github.com/containerd/continuity/testutil/helpers.go create mode 100644 vendor/github.com/containerd/continuity/testutil/helpers_unix.go create mode 100644 vendor/github.com/containerd/continuity/testutil/helpers_windows.go rename {pkg/testutil => vendor/github.com/containerd/continuity/testutil/loopback}/loopback_linux.go (90%) rename mount/lookup_test/dummy.go => vendor/github.com/containerd/continuity/testutil/mount_linux.go (83%) create mode 100644 vendor/github.com/containerd/continuity/testutil/mount_other.go diff --git a/mount/lookup_test/lookup_linux_test.go b/mount/lookup_linux_test.go similarity index 86% rename from mount/lookup_test/lookup_linux_test.go rename to mount/lookup_linux_test.go index eb71b1045..7ec9e3ad6 100644 --- a/mount/lookup_test/lookup_linux_test.go +++ b/mount/lookup_linux_test.go @@ -16,14 +16,7 @@ limitations under the License. */ -// FIXME: we can't put this test to the mount package: -// import cycle not allowed in test -// package github.com/containerd/containerd/mount (test) -// imports github.com/containerd/containerd/pkg/testutil -// imports github.com/containerd/containerd/mount -// -// NOTE: we can't have this as lookup_test (compilation fails) -package lookuptest +package mount import ( "fmt" @@ -34,14 +27,16 @@ import ( "strings" "testing" - "github.com/containerd/containerd/mount" - "github.com/containerd/containerd/pkg/testutil" + // containerd/pkg/testutil has circular dependency on this mount pkg. + // so we use continuity/testutil instead. + "github.com/containerd/continuity/testutil" + "github.com/containerd/continuity/testutil/loopback" "gotest.tools/assert" ) func checkLookup(t *testing.T, fsType, mntPoint, dir string) { t.Helper() - info, err := mount.Lookup(dir) + info, err := Lookup(dir) assert.NilError(t, err) assert.Equal(t, fsType, info.FSType) assert.Equal(t, mntPoint, info.Mountpoint) @@ -55,7 +50,7 @@ func testLookup(t *testing.T, fsType string) { } defer os.RemoveAll(mnt) - deviceName, cleanupDevice, err := testutil.NewLoopback(100 << 20) // 100 MB + deviceName, cleanupDevice, err := loopback.New(100 << 20) // 100 MB if err != nil { t.Fatal(err) } diff --git a/snapshots/btrfs/btrfs_test.go b/snapshots/btrfs/btrfs_test.go index 73343112b..6e5102356 100644 --- a/snapshots/btrfs/btrfs_test.go +++ b/snapshots/btrfs/btrfs_test.go @@ -31,6 +31,7 @@ import ( "github.com/containerd/containerd/pkg/testutil" "github.com/containerd/containerd/snapshots" "github.com/containerd/containerd/snapshots/testsuite" + "github.com/containerd/continuity/testutil/loopback" "github.com/pkg/errors" "golang.org/x/sys/unix" ) @@ -51,7 +52,7 @@ func boltSnapshotter(t *testing.T) func(context.Context, string) (snapshots.Snap if os.Getpagesize() > 4096 { loopbackSize = int64(650 << 20) // 650 MB } - deviceName, cleanupDevice, err := testutil.NewLoopback(loopbackSize) + deviceName, cleanupDevice, err := loopback.New(loopbackSize) if err != nil { return nil, nil, err diff --git a/snapshots/overlay/check_test.go b/snapshots/overlay/check_test.go index d394b9522..490145e01 100644 --- a/snapshots/overlay/check_test.go +++ b/snapshots/overlay/check_test.go @@ -25,6 +25,7 @@ import ( "testing" "github.com/containerd/containerd/pkg/testutil" + "github.com/containerd/continuity/testutil/loopback" ) func testOverlaySupported(t testing.TB, expected bool, mkfs ...string) { @@ -35,7 +36,7 @@ func testOverlaySupported(t testing.TB, expected bool, mkfs ...string) { } defer os.RemoveAll(mnt) - deviceName, cleanupDevice, err := testutil.NewLoopback(100 << 20) // 100 MB + deviceName, cleanupDevice, err := loopback.New(100 << 20) // 100 MB if err != nil { t.Fatal(err) } diff --git a/vendor.conf b/vendor.conf index 83ee7b90c..c4cd8f1f4 100644 --- a/vendor.conf +++ b/vendor.conf @@ -4,7 +4,7 @@ github.com/containerd/cgroups 5e610833b72089b37d0e615de9a92dfc043757c2 github.com/containerd/typeurl a93fcdb778cd272c6e9b3028b2f42d813e785d40 github.com/containerd/fifo 3d5202aec260678c48179c56f40e6f38a095738c github.com/containerd/btrfs 2e1aa0ddf94f91fa282b6ed87c23bf0d64911244 -github.com/containerd/continuity 7f53d412b9eb1cbf744c2063185d703a0ee34700 +github.com/containerd/continuity bd77b46c8352f74eb12c85bdc01f4b90f69d66b4 github.com/coreos/go-systemd 48702e0da86bd25e76cfef347e2adeb434a0d0a6 github.com/docker/go-metrics 4ea375f7759c82740c893fc030bc37088d2ec098 github.com/docker/go-events 9461782956ad83b30282bf90e31fa6a70c255ba9 diff --git a/vendor/github.com/containerd/continuity/testutil/helpers.go b/vendor/github.com/containerd/continuity/testutil/helpers.go new file mode 100644 index 000000000..603d7a687 --- /dev/null +++ b/vendor/github.com/containerd/continuity/testutil/helpers.go @@ -0,0 +1,27 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package testutil + +import ( + "flag" +) + +var rootEnabled bool + +func init() { + flag.BoolVar(&rootEnabled, "test.root", false, "enable tests that require root") +} diff --git a/vendor/github.com/containerd/continuity/testutil/helpers_unix.go b/vendor/github.com/containerd/continuity/testutil/helpers_unix.go new file mode 100644 index 000000000..35568a420 --- /dev/null +++ b/vendor/github.com/containerd/continuity/testutil/helpers_unix.go @@ -0,0 +1,57 @@ +// +build !windows + +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package testutil + +import ( + "os" + "testing" + + "golang.org/x/sys/unix" +) + +// Unmount unmounts a given mountPoint and sets t.Error if it fails +func Unmount(t *testing.T, mountPoint string) { + t.Log("unmount", mountPoint) + if err := unmountAll(mountPoint); err != nil { + t.Error("Could not umount", mountPoint, err) + } +} + +// RequiresRoot skips tests that require root, unless the test.root flag has +// been set +func RequiresRoot(t testing.TB) { + if !rootEnabled { + t.Skip("skipping test that requires root") + return + } + if os.Getuid() != 0 { + t.Error("This test must be run as root.") + } +} + +func unmountAll(mountpoint string) error { + for { + if err := unix.Unmount(mountpoint, unmountFlags); err != nil { + if err == unix.EINVAL { + return nil + } + return err + } + } +} diff --git a/vendor/github.com/containerd/continuity/testutil/helpers_windows.go b/vendor/github.com/containerd/continuity/testutil/helpers_windows.go new file mode 100644 index 000000000..203d98771 --- /dev/null +++ b/vendor/github.com/containerd/continuity/testutil/helpers_windows.go @@ -0,0 +1,32 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package testutil + +import "testing" + +// RequiresRoot does nothing on Windows +func RequiresRoot(t testing.TB) { +} + +// RequiresRootM is similar to RequiresRoot but intended to be called from *testing.M. +func RequiresRootM() { +} + +// Unmount unmounts a given mountPoint and sets t.Error if it fails +// Does nothing on Windows +func Unmount(t *testing.T, mountPoint string) { +} diff --git a/pkg/testutil/loopback_linux.go b/vendor/github.com/containerd/continuity/testutil/loopback/loopback_linux.go similarity index 90% rename from pkg/testutil/loopback_linux.go rename to vendor/github.com/containerd/continuity/testutil/loopback/loopback_linux.go index 2d8428937..b17361170 100644 --- a/pkg/testutil/loopback_linux.go +++ b/vendor/github.com/containerd/continuity/testutil/loopback/loopback_linux.go @@ -16,7 +16,7 @@ limitations under the License. */ -package testutil +package loopback import ( "io/ioutil" @@ -28,8 +28,8 @@ import ( "github.com/sirupsen/logrus" ) -// NewLoopback creates a loopback device, and returns its device name (/dev/loopX), and its clean-up function. -func NewLoopback(size int64) (string, func() error, error) { +// New creates a loopback device, and returns its device name (/dev/loopX), and its clean-up function. +func New(size int64) (string, func() error, error) { // create temporary file for the disk image file, err := ioutil.TempFile("", "containerd-test-loopback") if err != nil { diff --git a/mount/lookup_test/dummy.go b/vendor/github.com/containerd/continuity/testutil/mount_linux.go similarity index 83% rename from mount/lookup_test/dummy.go rename to vendor/github.com/containerd/continuity/testutil/mount_linux.go index 8a733a6ed..3c453cfb0 100644 --- a/mount/lookup_test/dummy.go +++ b/vendor/github.com/containerd/continuity/testutil/mount_linux.go @@ -14,6 +14,8 @@ limitations under the License. */ -package lookuptest +package testutil -// FIXME: without this dummy file, `make build` fails with "no buildable Go source files" error +import "golang.org/x/sys/unix" + +const unmountFlags int = unix.MNT_DETACH diff --git a/vendor/github.com/containerd/continuity/testutil/mount_other.go b/vendor/github.com/containerd/continuity/testutil/mount_other.go new file mode 100644 index 000000000..0c7fde57e --- /dev/null +++ b/vendor/github.com/containerd/continuity/testutil/mount_other.go @@ -0,0 +1,21 @@ +// +build !linux,!windows + +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package testutil + +const unmountFlags int = 0