From d4ca13f7c0d0af7d1b8f88671548f7aa99cb1b10 Mon Sep 17 00:00:00 2001
From: Chris Evich <cevich@redhat.com>
Date: Wed, 30 Sep 2020 08:57:50 -0400
Subject: [PATCH] Distinguish userns vs containerized tests

The systemd test was inaccurately being skipped when a userns env. var.
flag was set.  At best this is confusing to new developers, and at worse
it actively blocks an otherwise legitimate class of tests.  Improve the
accuracy of skip-logic by adding/using a purpose built set of
functions.

Signed-off-by: Chris Evich <cevich@redhat.com>
---
 test/e2e/common_test.go  | 35 ++++++++++++++++++++++++-----------
 test/e2e/systemd_test.go |  4 +---
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/test/e2e/common_test.go b/test/e2e/common_test.go
index f4c80d8650..c663a4dca8 100644
--- a/test/e2e/common_test.go
+++ b/test/e2e/common_test.go
@@ -599,19 +599,21 @@ func (p *PodmanTestIntegration) CreateSeccompJson(in []byte) (string, error) {
 	return jsonFile, nil
 }
 
-func SkipIfRootlessCgroupsV1(reason string) {
+func checkReason(reason string) {
 	if len(reason) < 5 {
-		panic("SkipIfRootlessCgroupsV1 must specify a reason to skip")
+		panic("Test must specify a reason to skip")
 	}
+}
+
+func SkipIfRootlessCgroupsV1(reason string) {
+	checkReason(reason)
 	if os.Geteuid() != 0 && !CGROUPSV2 {
 		Skip("[rootless]: " + reason)
 	}
 }
 
 func SkipIfRootless(reason string) {
-	if len(reason) < 5 {
-		panic("SkipIfRootless must specify a reason to skip")
-	}
+	checkReason(reason)
 	if os.Geteuid() != 0 {
 		ginkgo.Skip("[rootless]: " + reason)
 	}
@@ -629,23 +631,34 @@ func isRootless() bool {
 }
 
 func SkipIfCgroupV1(reason string) {
-	if len(reason) < 5 {
-		panic("SkipIfCgroupV1 must specify a reason to skip")
-	}
+	checkReason(reason)
 	if !CGROUPSV2 {
 		Skip(reason)
 	}
 }
 
 func SkipIfCgroupV2(reason string) {
-	if len(reason) < 5 {
-		panic("SkipIfCgroupV2 must specify a reason to skip")
-	}
+	checkReason(reason)
 	if CGROUPSV2 {
 		Skip(reason)
 	}
 }
 
+func isContainerized() bool {
+	// This is set to "podman" by podman automatically
+	if os.Getenv("container") != "" {
+		return true
+	}
+	return false
+}
+
+func SkipIfContainerized(reason string) {
+	checkReason(reason)
+	if isContainerized() {
+		Skip(reason)
+	}
+}
+
 // PodmanAsUser is the exec call to podman on the filesystem with the specified uid/gid and environment
 func (p *PodmanTestIntegration) PodmanAsUser(args []string, uid, gid uint32, cwd string, env []string) *PodmanSessionIntegration {
 	podmanSession := p.PodmanAsUserBase(args, uid, gid, cwd, env, false, false, nil)
diff --git a/test/e2e/systemd_test.go b/test/e2e/systemd_test.go
index 4be8443e3b..9e717a0ebc 100644
--- a/test/e2e/systemd_test.go
+++ b/test/e2e/systemd_test.go
@@ -48,9 +48,7 @@ WantedBy=multi-user.target
 
 	It("podman start container by systemd", func() {
 		SkipIfRootless("rootless can not write to /etc")
-		if os.Getenv("SKIP_USERNS") != "" {
-			Skip("Skip userns tests.")
-		}
+		SkipIfContainerized("test does not have systemd as pid 1")
 
 		sys_file := ioutil.WriteFile("/etc/systemd/system/redis.service", []byte(systemd_unit_file), 0644)
 		Expect(sys_file).To(BeNil())