[PATCH 2/2] selftests: pid_namespace: Skip tests if not root

Tiezhu Yang posted 2 patches 9 months, 2 weeks ago
[PATCH 2/2] selftests: pid_namespace: Skip tests if not root
Posted by Tiezhu Yang 9 months, 2 weeks ago
CLONE_NEWPID or CLONE_NEWNS requires CAP_SYS_ADMIN, if the tests are not
running as root, just skip tests rather than fail and also give a warning
to the user.

Before:

  $ make -C tools/testing/selftests/pid_namespace
  $ tools/testing/selftests/pid_namespace/pid_max
  ...
  #  RUN           global.pid_max_simple ...
  # pid_max.c:337:pid_max_simple:Expected pid (-1) > 0 (0)
  ...
  #  RUN           global.pid_max_nested_limit ...
  # pid_max.c:346:pid_max_nested_limit:Expected pid (-1) > 0 (0)
  ...
  #  RUN           global.pid_max_nested ...
  # pid_max.c:355:pid_max_nested:Expected pid (-1) > 0 (0)
  ...
  # Totals: pass:0 fail:3 xfail:0 xpass:0 skip:0 error:0

After:

  $ make -C tools/testing/selftests/pid_namespace
  $ tools/testing/selftests/pid_namespace/pid_max
  ...
  #  RUN           global.pid_max_simple ...
  #      SKIP      Must be run as root
  ...
  #  RUN           global.pid_max_nested_limit ...
  #      SKIP      Must be run as root
  ...
  #  RUN           global.pid_max_nested ...
  #      SKIP      Must be run as root
  ...
  # Totals: pass:0 fail:0 xfail:0 xpass:0 skip:3 error:0

While at it, remove the blank line in the TEST(pid_max_simple).

Fixes: 615ab43b838b ("tests/pid_namespace: add pid_max tests")
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 tools/testing/selftests/pid_namespace/pid_max.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/pid_namespace/pid_max.c b/tools/testing/selftests/pid_namespace/pid_max.c
index 96f274f0582b..30937c5ee12b 100644
--- a/tools/testing/selftests/pid_namespace/pid_max.c
+++ b/tools/testing/selftests/pid_namespace/pid_max.c
@@ -332,8 +332,10 @@ TEST(pid_max_simple)
 {
 	pid_t pid;
 
-
 	pid = do_clone(pid_max_cb, NULL, CLONE_NEWPID | CLONE_NEWNS);
+	if (errno == EPERM)
+		SKIP(return, "Must be run as root");
+
 	ASSERT_GT(pid, 0);
 	ASSERT_EQ(0, wait_for_pid(pid));
 }
@@ -343,6 +345,9 @@ TEST(pid_max_nested_limit)
 	pid_t pid;
 
 	pid = do_clone(pid_max_nested_limit_outer, NULL, CLONE_NEWPID | CLONE_NEWNS);
+	if (errno == EPERM)
+		SKIP(return, "Must be run as root");
+
 	ASSERT_GT(pid, 0);
 	ASSERT_EQ(0, wait_for_pid(pid));
 }
@@ -352,6 +357,9 @@ TEST(pid_max_nested)
 	pid_t pid;
 
 	pid = do_clone(pid_max_nested_outer, NULL, CLONE_NEWPID | CLONE_NEWNS);
+	if (errno == EPERM)
+		SKIP(return, "Must be run as root");
+
 	ASSERT_GT(pid, 0);
 	ASSERT_EQ(0, wait_for_pid(pid));
 }
-- 
2.42.0