From f8f7e0d380488665cb77d8213d816bfdeeb7a252 Mon Sep 17 00:00:00 2001 From: Jamie Anderson Date: Wed, 18 Aug 2021 22:33:15 +0000 Subject: [PATCH] Add test skip helpers to cli/testutil Signed-off-by: Jamie Anderson --- testutil/helpers.go | 14 ++++++++++++++ testutil/helpers_unix.go | 34 ++++++++++++++++++++++++++++++++++ testutil/helpers_windows.go | 12 ++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 testutil/helpers.go create mode 100644 testutil/helpers_unix.go create mode 100644 testutil/helpers_windows.go diff --git a/testutil/helpers.go b/testutil/helpers.go new file mode 100644 index 0000000..0eeff25 --- /dev/null +++ b/testutil/helpers.go @@ -0,0 +1,14 @@ +package testutil // import "github.com/docker/cli/testutil" + +import ( + "flag" +) + +// This variable and the init() function copied from +// https://github.com/containerd/containerd/blob/master/pkg/testutil/helpers.go +// Original Copyright The Containerd Authors. Licensed under the Apache License, Version 2.0. +var rootEnabled bool + +func init() { + flag.BoolVar(&rootEnabled, "test.root", false, "enable tests that require root") +} diff --git a/testutil/helpers_unix.go b/testutil/helpers_unix.go new file mode 100644 index 0000000..c846775 --- /dev/null +++ b/testutil/helpers_unix.go @@ -0,0 +1,34 @@ +// +build !windows + +package testutil + +import ( + "os" + "testing" + + "gotest.tools/v3/assert" +) + +// This function was copied from +// https://github.com/containerd/containerd/blob/master/pkg/testutil/helpers_unix.go +// Original Copyright The Containerd Authors. Licensed under the Apache License, Version 2.0. +// 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") + } + assert.Equal(t, 0, os.Getuid(), "This test must be run as root.") +} + +// This function is adapted from a previous patch applied by Amazon +// The existence of /.initialized or /builddir is a marker that we're in a build chroot +// Tests that perform filesystem operations, generate non-localhost network traffic, +// and possibly other things, are likely to not work correctly +func SkipIfBuildroot(t testing.TB) { + if _, err := os.Stat("/.initialized"); err == nil { + t.Skip("not appropriate for a buildroot environment") + } else if _, err := os.Stat("/builddir"); err == nil { + t.Skip("not appropriate for a buildroot environment") + } +} diff --git a/testutil/helpers_windows.go b/testutil/helpers_windows.go new file mode 100644 index 0000000..aefeca0 --- /dev/null +++ b/testutil/helpers_windows.go @@ -0,0 +1,12 @@ +// +build windows + +package testutil + +import "testing" + +func RequiresRoot(t testing.TB) { +} + +func SkipIfBuildroot(t testing.TB) { +} + -- 2.31.0.rc2