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/
*/coverage.txt
**/coverage.txt

View File

@ -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

View File

@ -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}

View File

@ -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-")

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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.")
}