snapshot: Separate tests using root from non-root

Signed-off-by: Samuel Karp <skarp@amazon.com>
This commit is contained in:
Samuel Karp 2017-01-24 16:10:48 -08:00
parent 682a37f25c
commit cacda40317
8 changed files with 46 additions and 7 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
/bin/ /bin/
*/coverage.txt **/coverage.txt

View file

@ -20,4 +20,6 @@ install:
- export PATH=$PATH:/tmp/protobuf/bin/ - export PATH=$PATH:/tmp/protobuf/bin/
script: script:
- sudo PATH=$PATH GOPATH=$GOPATH make binaries coverage - make binaries
- make coverage
- sudo PATH=$PATH GOPATH=$GOPATH make root-coverage

View file

@ -12,6 +12,7 @@ PROJECT_ROOT=github.com/docker/containerd
# Project packages. # Project packages.
PACKAGES=$(shell go list ./... | grep -v /vendor/) PACKAGES=$(shell go list ./... | grep -v /vendor/)
INTEGRATION_PACKAGE=${PROJECT_ROOT}/integration INTEGRATION_PACKAGE=${PROJECT_ROOT}/integration
SNAPSHOT_PACKAGES=$(shell go list ./snapshot/...)
# Project binaries. # Project binaries.
COMMANDS=ctr containerd containerd-shim protoc-gen-gogoctrd dist COMMANDS=ctr containerd containerd-shim protoc-gen-gogoctrd dist
@ -87,10 +88,14 @@ build: ## build the go packages
@echo "🐳 $@" @echo "🐳 $@"
@go build -i -v ${GO_LDFLAGS} ${GO_GCFLAGS} ${PACKAGES} @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 "🐳 $@" @echo "🐳 $@"
@go test ${TESTFLAGS} $(filter-out ${INTEGRATION_PACKAGE},${PACKAGES}) @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 integration: ## run integration tests
@echo "🐳 $@" @echo "🐳 $@"
@go test ${TESTFLAGS} ${INTEGRATION_PACKAGE} @go test ${TESTFLAGS} ${INTEGRATION_PACKAGE}
@ -120,13 +125,21 @@ uninstall:
@echo "🐳 $@" @echo "🐳 $@"
@rm -f $(addprefix $(DESTDIR)/bin/,$(notdir $(BINARIES))) @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 "🐳 $@" @echo "🐳 $@"
@( for pkg in $(filter-out ${INTEGRATION_PACKAGE},${PACKAGES}); do \ @( 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 -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; \ go test ${TESTFLAGS} -test.short -coverprofile="../../../$$pkg/coverage.txt" -covermode=atomic $$pkg || exit; \
done ) 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 coverage-integration: ## generate coverprofiles from the integration tests
@echo "🐳 $@" @echo "🐳 $@"
go test ${TESTFLAGS} -test.short -coverprofile="../../../${INTEGRATION_PACKAGE}/coverage.txt" -covermode=atomic ${INTEGRATION_PACKAGE} go test ${TESTFLAGS} -test.short -coverprofile="../../../${INTEGRATION_PACKAGE}/coverage.txt" -covermode=atomic ${INTEGRATION_PACKAGE}

View file

@ -19,6 +19,7 @@ const (
) )
func TestBtrfs(t *testing.T) { func TestBtrfs(t *testing.T) {
testutil.RequiresRoot(t)
device := setupBtrfsLoopbackDevice(t) device := setupBtrfsLoopbackDevice(t)
defer removeBtrfsLoopbackDevice(t, device) defer removeBtrfsLoopbackDevice(t, device)
root, err := ioutil.TempDir(device.mountPoint, "TestBtrfsPrepare-") root, err := ioutil.TempDir(device.mountPoint, "TestBtrfsPrepare-")

View file

@ -14,6 +14,7 @@ import (
// examples we've discussed thus far. It does perform mounts, so you must run // examples we've discussed thus far. It does perform mounts, so you must run
// as root. // as root.
func TestSnapshotManagerBasic(t *testing.T) { func TestSnapshotManagerBasic(t *testing.T) {
testutil.RequiresRoot(t)
tmpDir, err := ioutil.TempDir("", "test-sm-") tmpDir, err := ioutil.TempDir("", "test-sm-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View file

@ -7,9 +7,11 @@ import (
"testing" "testing"
"github.com/docker/containerd" "github.com/docker/containerd"
"github.com/docker/containerd/snapshot/testutil"
) )
func TestSnapshotNaiveBasic(t *testing.T) { func TestSnapshotNaiveBasic(t *testing.T) {
testutil.RequiresRoot(t)
tmpDir, err := ioutil.TempDir("", "test-naive-") tmpDir, err := ioutil.TempDir("", "test-naive-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View file

@ -8,6 +8,7 @@ import (
"testing" "testing"
"github.com/docker/containerd" "github.com/docker/containerd"
"github.com/docker/containerd/snapshot/testutil"
) )
func TestOverlay(t *testing.T) { func TestOverlay(t *testing.T) {
@ -126,9 +127,7 @@ func TestOverlayOverlayMount(t *testing.T) {
} }
func TestOverlayOverlayRead(t *testing.T) { func TestOverlayOverlayRead(t *testing.T) {
if os.Getuid() != 0 { testutil.RequiresRoot(t)
t.Skip("not running as root")
}
root, err := ioutil.TempDir("", "overlay") root, err := ioutil.TempDir("", "overlay")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View file

@ -1,13 +1,34 @@
package testutil package testutil
import ( import (
"flag"
"os"
"syscall" "syscall"
"testing" "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) { func Unmount(t *testing.T, mountPoint string) {
t.Log("unmount", mountPoint) t.Log("unmount", mountPoint)
if err := syscall.Unmount(mountPoint, 0); err != nil { if err := syscall.Unmount(mountPoint, 0); err != nil {
t.Error("Could not umount", mountPoint, err) 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.")
}