From 021e33a5b2c3384243e3ae76444bb90006214f5a Mon Sep 17 00:00:00 2001
From: Jamie Anderson <jamieand@amazon.com>
Date: Wed, 18 Aug 2021 22:17:52 +0000
Subject: [PATCH] Add test skip helpers to testutil

Signed-off-by: Jamie Anderson <jamieand@amazon.com>
---
 testutil/helpers.go         | 10 ++++++++++
 testutil/helpers_unix.go    | 34 ++++++++++++++++++++++++++++++++++
 testutil/helpers_windows.go | 12 ++++++++++++
 3 files changed, 56 insertions(+)
 create mode 100644 testutil/helpers_unix.go
 create mode 100644 testutil/helpers_windows.go

diff --git a/testutil/helpers.go b/testutil/helpers.go
index e522b5a..6de9de5 100644
--- a/testutil/helpers.go
+++ b/testutil/helpers.go
@@ -1,9 +1,19 @@
 package testutil // import "github.com/docker/docker/testutil"
 
 import (
+	"flag"
 	"io"
 )
 
+// 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")
+}
+
 // DevZero acts like /dev/zero but in an OS-independent fashion.
 var DevZero io.Reader = devZero{}
 
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

