From cacda40317192bfc73c098634eb4fc2d66639a5b Mon Sep 17 00:00:00 2001 From: Samuel Karp Date: Tue, 24 Jan 2017 16:10:48 -0800 Subject: [PATCH] snapshot: Separate tests using root from non-root Signed-off-by: Samuel Karp --- .gitignore | 2 +- .travis.yml | 4 +++- Makefile | 17 +++++++++++++++-- snapshot/btrfs/btrfs_test.go | 1 + snapshot/manager_test.go | 1 + snapshot/naive/naive_test.go | 2 ++ snapshot/overlay/overlay_test.go | 5 ++--- snapshot/testutil/helpers.go | 21 +++++++++++++++++++++ 8 files changed, 46 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 083ecd9..d2153da 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ /bin/ -*/coverage.txt +**/coverage.txt diff --git a/.travis.yml b/.travis.yml index f884f3f..ec20eb0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,4 +20,6 @@ install: - export PATH=$PATH:/tmp/protobuf/bin/ script: - - sudo PATH=$PATH GOPATH=$GOPATH make binaries coverage + - make binaries + - make coverage + - sudo PATH=$PATH GOPATH=$GOPATH make root-coverage diff --git a/Makefile b/Makefile index 83bd173..4fb65bc 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ PROJECT_ROOT=github.com/docker/containerd # Project packages. PACKAGES=$(shell go list ./... | grep -v /vendor/) INTEGRATION_PACKAGE=${PROJECT_ROOT}/integration +SNAPSHOT_PACKAGES=$(shell go list ./snapshot/...) # Project binaries. COMMANDS=ctr containerd containerd-shim protoc-gen-gogoctrd dist @@ -87,10 +88,14 @@ build: ## build the go packages @echo "🐳 $@" @go build -i -v ${GO_LDFLAGS} ${GO_GCFLAGS} ${PACKAGES} -test: ## run tests, except integration tests +test: ## run tests, except integration tests and tests that require root @echo "🐳 $@" @go test ${TESTFLAGS} $(filter-out ${INTEGRATION_PACKAGE},${PACKAGES}) +root-test: ## run tests, except integration tests + @echo "🐳 $@" + @go test ${TESTFLAGS} ${SNAPSHOT_PACKAGES} -test.root + integration: ## run integration tests @echo "🐳 $@" @go test ${TESTFLAGS} ${INTEGRATION_PACKAGE} @@ -120,13 +125,21 @@ uninstall: @echo "🐳 $@" @rm -f $(addprefix $(DESTDIR)/bin/,$(notdir $(BINARIES))) -coverage: ## generate coverprofiles from the unit tests + +coverage: ## generate coverprofiles from the unit tests, except tests that require root @echo "🐳 $@" @( for pkg in $(filter-out ${INTEGRATION_PACKAGE},${PACKAGES}); do \ go test -i ${TESTFLAGS} -test.short -coverprofile="../../../$$pkg/coverage.txt" -covermode=atomic $$pkg || exit; \ go test ${TESTFLAGS} -test.short -coverprofile="../../../$$pkg/coverage.txt" -covermode=atomic $$pkg || exit; \ done ) +root-coverage: ## generae coverage profiles for the unit tests + @echo "🐳 $@" + @( for pkg in ${SNAPSHOT_PACKAGES}; do \ + go test -i ${TESTFLAGS} -test.short -coverprofile="../../../$$pkg/coverage.txt" -covermode=atomic $$pkg -test.root || exit; \ + go test ${TESTFLAGS} -test.short -coverprofile="../../../$$pkg/coverage.txt" -covermode=atomic $$pkg -test.root || exit; \ + done ) + coverage-integration: ## generate coverprofiles from the integration tests @echo "🐳 $@" go test ${TESTFLAGS} -test.short -coverprofile="../../../${INTEGRATION_PACKAGE}/coverage.txt" -covermode=atomic ${INTEGRATION_PACKAGE} diff --git a/snapshot/btrfs/btrfs_test.go b/snapshot/btrfs/btrfs_test.go index 3146317..651e9e3 100644 --- a/snapshot/btrfs/btrfs_test.go +++ b/snapshot/btrfs/btrfs_test.go @@ -19,6 +19,7 @@ const ( ) func TestBtrfs(t *testing.T) { + testutil.RequiresRoot(t) device := setupBtrfsLoopbackDevice(t) defer removeBtrfsLoopbackDevice(t, device) root, err := ioutil.TempDir(device.mountPoint, "TestBtrfsPrepare-") diff --git a/snapshot/manager_test.go b/snapshot/manager_test.go index 5eb8dc9..3fe2c65 100644 --- a/snapshot/manager_test.go +++ b/snapshot/manager_test.go @@ -14,6 +14,7 @@ import ( // examples we've discussed thus far. It does perform mounts, so you must run // as root. func TestSnapshotManagerBasic(t *testing.T) { + testutil.RequiresRoot(t) tmpDir, err := ioutil.TempDir("", "test-sm-") if err != nil { t.Fatal(err) diff --git a/snapshot/naive/naive_test.go b/snapshot/naive/naive_test.go index bbf6f13..afae5b9 100644 --- a/snapshot/naive/naive_test.go +++ b/snapshot/naive/naive_test.go @@ -7,9 +7,11 @@ import ( "testing" "github.com/docker/containerd" + "github.com/docker/containerd/snapshot/testutil" ) func TestSnapshotNaiveBasic(t *testing.T) { + testutil.RequiresRoot(t) tmpDir, err := ioutil.TempDir("", "test-naive-") if err != nil { t.Fatal(err) diff --git a/snapshot/overlay/overlay_test.go b/snapshot/overlay/overlay_test.go index ca6beda..4b0697f 100644 --- a/snapshot/overlay/overlay_test.go +++ b/snapshot/overlay/overlay_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/docker/containerd" + "github.com/docker/containerd/snapshot/testutil" ) func TestOverlay(t *testing.T) { @@ -126,9 +127,7 @@ func TestOverlayOverlayMount(t *testing.T) { } func TestOverlayOverlayRead(t *testing.T) { - if os.Getuid() != 0 { - t.Skip("not running as root") - } + testutil.RequiresRoot(t) root, err := ioutil.TempDir("", "overlay") if err != nil { t.Fatal(err) diff --git a/snapshot/testutil/helpers.go b/snapshot/testutil/helpers.go index 74974df..49ed165 100644 --- a/snapshot/testutil/helpers.go +++ b/snapshot/testutil/helpers.go @@ -1,13 +1,34 @@ package testutil import ( + "flag" + "os" "syscall" "testing" + + "github.com/stretchr/testify/assert" ) +var rootEnabled bool + +func init() { + flag.BoolVar(&rootEnabled, "test.root", false, "enable tests that require root") +} + +// 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 := syscall.Unmount(mountPoint, 0); 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.T) { + if !rootEnabled { + t.Skip("skipping test that requires root") + return + } + assert.Equal(t, 0, os.Getuid(), "This test must be run as root.") +}