diff mbox series

[v1,1/5] Rewrite mountns01 test using new LTP API

Message ID 20220203123522.28604-2-andrea.cervesato@suse.de
State Superseded
Headers show
Series Rewrite mountns testing suite | expand

Commit Message

Andrea Cervesato Feb. 3, 2022, 12:35 p.m. UTC
Removed libclone from Makefile and used LTP API to replace it.
mountns01 has been adapted to use the new LTP API.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
 testcases/kernel/containers/mountns/Makefile  |  21 +--
 testcases/kernel/containers/mountns/common.h  |  53 +++++++
 .../kernel/containers/mountns/mountns01.c     | 148 ++++++++----------
 3 files changed, 124 insertions(+), 98 deletions(-)
 create mode 100644 testcases/kernel/containers/mountns/common.h

Comments

Petr Vorel Feb. 7, 2022, 10:02 p.m. UTC | #1
Hi Andrea,

> Removed libclone from Makefile and used LTP API to replace it.
> mountns01 has been adapted to use the new LTP API.

Thanks for this effort!

> +++ b/testcases/kernel/containers/mountns/Makefile
...
>  include $(top_srcdir)/include/mk/testcases.mk
> -include $(abs_srcdir)/../Makefile.inc
> -
> -LDLIBS                  := -lclone $(LDLIBS)
> -

Removing this makes this commit (and I guess all but the last commits) uncompilable.
IMHO this is no-go as it breaks bisecting.

But even keeping it for all not yet rewritten tests:

include $(abs_srcdir)/../Makefile.inc
mountns02 mountns03 mountns04 mountns05: LDLIBS                  := -lclone $(LDLIBS)

breaks the compilation:

$ make mountns02
make -C ../libclone -f "testcases/kernel/containers/mountns/../libclone/Makefile" all
make[1]: Entering directory 'testcases/kernel/containers/libclone'
../../../../include/mk/sparse.mk:6: warning: overriding recipe for target 'tools/sparse/sparse-ltp'
../../../../include/mk/sparse.mk:6: warning: ignoring old recipe for target 'tools/sparse/sparse-ltp'
../../../../include/mk/sparse.mk:9: warning: overriding recipe for target 'tools/sparse'
../../../../include/mk/sparse.mk:9: warning: ignoring old recipe for target 'tools/sparse'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory 'testcases/kernel/containers/libclone'
/usr/bin/ld: testcases/kernel/containers/mountns/../libclone/libclone.a(libclone.o): in function `do_unshare_tests':
testcases/kernel/containers/libclone/libclone.c:59: undefined reference to `tst_brk'
collect2: error: ld returned 1 exit status
make: *** [../../../../include/mk/rules.mk:37: mountns02] Error 1


>  include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/containers/mountns/common.h b/testcases/kernel/containers/mountns/common.h
> new file mode 100644
> index 000000000..971f2381a
> --- /dev/null
> +++ b/testcases/kernel/containers/mountns/common.h
nit: maybe mountns.h would be a better name.

...
> +static void check_newns(void)
> +{
> +	int pid, status;
> +
> +	if (tst_kvercmp(2, 4, 19) < 0)
> +		tst_brk(TCONF, "CLONE_NEWNS not supported");
We can safely remove check for kernel 2.4 :).
> +
> +	pid = ltp_clone_quick(CLONE_NEWNS | SIGCHLD, dummy_child, NULL);
> +	if (pid < 0)
> +		tst_brk(TCONF, "CLONE_NEWNS not supported");

Yes, I'd also be for keeping ltp_clone_quick() from quite obscure cloner.[hc].
SAFE_CLONE() / tst_clone() use nice clone3() interface, but that's was added
quite recently (kernel 5.3). It'd be nice one day rewrite/cleanup also also
cloner.[hc].


> diff --git a/testcases/kernel/containers/mountns/mountns01.c b/testcases/kernel/containers/mountns/mountns01.c
...
>  #define _GNU_SOURCE
IMHO _GNU_SOURCE is not needed. Or at least it compiles without it
> +
>  #include <sys/wait.h>
>  #include <sys/mount.h>
> -#include <stdio.h>
> -#include <errno.h>
> -#include "mountns_helper.h"
> -#include "test.h"
> -#include "safe_macros.h"
> -
> -char *TCID	= "mountns01";
> -int TST_TOTAL	= 2;
> +#include "common.h"
> +#include "tst_test.h"

>  #if defined(MS_SHARED) && defined(MS_PRIVATE) && defined(MS_REC)
These were added in glibc in 2010 (glibc 2.12), we can safely remove this check.

Kind regards,
Petr
diff mbox series

Patch

diff --git a/testcases/kernel/containers/mountns/Makefile b/testcases/kernel/containers/mountns/Makefile
index bd42bf41b..16284f4d5 100644
--- a/testcases/kernel/containers/mountns/Makefile
+++ b/testcases/kernel/containers/mountns/Makefile
@@ -1,23 +1,8 @@ 
+# SPDX-License-Identifier: GPL-2.0-or-later
 # Copyright (c) 2014 Red Hat, Inc.
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of version 2 the GNU General Public License as
-# published by the Free Software Foundation.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-##############################################################################
+# Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
 
-top_srcdir              ?= ../../../..
+top_srcdir		?= ../../../..
 
 include $(top_srcdir)/include/mk/testcases.mk
-include $(abs_srcdir)/../Makefile.inc
-
-LDLIBS                  := -lclone $(LDLIBS)
-
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/containers/mountns/common.h b/testcases/kernel/containers/mountns/common.h
new file mode 100644
index 000000000..971f2381a
--- /dev/null
+++ b/testcases/kernel/containers/mountns/common.h
@@ -0,0 +1,53 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+#ifndef COMMON_H
+#define COMMON_H
+
+#include "tst_test.h"
+#include "lapi/namespaces_constants.h"
+
+#define DIRA "A"
+#define DIRB "B"
+
+static int dummy_child(void *v)
+{
+	(void)v;
+	return 0;
+}
+
+static void check_newns(void)
+{
+	int pid, status;
+
+	if (tst_kvercmp(2, 4, 19) < 0)
+		tst_brk(TCONF, "CLONE_NEWNS not supported");
+
+	pid = ltp_clone_quick(CLONE_NEWNS | SIGCHLD, dummy_child, NULL);
+	if (pid < 0)
+		tst_brk(TCONF, "CLONE_NEWNS not supported");
+
+	SAFE_WAIT(&status);
+}
+
+static void umount_folders(void)
+{
+	if (tst_is_mounted(DIRA))
+		SAFE_UMOUNT(DIRA);
+
+	if (tst_is_mounted(DIRB))
+		SAFE_UMOUNT(DIRB);
+}
+
+static void create_folders(void)
+{
+	SAFE_MKDIR(DIRA, 0777);
+	SAFE_MKDIR(DIRB, 0777);
+	SAFE_TOUCH(DIRA "/A", 0, NULL);
+	SAFE_TOUCH(DIRB "/B", 0, NULL);
+}
+
+#endif
diff --git a/testcases/kernel/containers/mountns/mountns01.c b/testcases/kernel/containers/mountns/mountns01.c
index 0bd0c592c..57f419271 100644
--- a/testcases/kernel/containers/mountns/mountns01.c
+++ b/testcases/kernel/containers/mountns/mountns01.c
@@ -1,22 +1,17 @@ 
-/* Copyright (c) 2014 Red Hat, Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of version 2 the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- ***********************************************************************
- * File: mountns01.c
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
  *
  * Tests a shared mount: shared mount can be replicated to as many
  * mountpoints and all the replicas continue to be exactly same.
- * Description:
+ *
+ * [Algorithm]
+ *
  * 1. Creates directories "A", "B" and files "A/A", "B/B"
  * 2. Unshares mount namespace and makes it private (so mounts/umounts
  *    have no effect on a real system)
@@ -25,125 +20,118 @@ 
  * 5. Clones a new child process with CLONE_NEWNS flag
  * 6. There are two test cases (where X is parent namespace and Y child
  *    namespace):
- *    1)
+ * 1)
  *	X: bind mounts "B" to "A"
  *	Y: must see "A/B"
  *	X: umounts "A"
- *    2)
+ * 2)
  *	Y: bind mounts "B" to "A"
  *	X: must see "A/B"
  *	Y: umounts "A"
- ***********************************************************************/
+ */
 
 #define _GNU_SOURCE
+
 #include <sys/wait.h>
 #include <sys/mount.h>
-#include <stdio.h>
-#include <errno.h>
-#include "mountns_helper.h"
-#include "test.h"
-#include "safe_macros.h"
-
-char *TCID	= "mountns01";
-int TST_TOTAL	= 2;
+#include "common.h"
+#include "tst_test.h"
 
 #if defined(MS_SHARED) && defined(MS_PRIVATE) && defined(MS_REC)
 
-int child_func(void *arg LTP_ATTRIBUTE_UNUSED)
+static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
 {
 	int ret = 0;
 
-	TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
+	TST_CHECKPOINT_WAIT(0);
 
-	if (access(DIRA"/B", F_OK) == -1)
+	if (access(DIRA "/B", F_OK) < 0)
 		ret = 2;
 
-	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 
-	/* bind mounts DIRB to DIRA making contents of DIRB visible
-	 * in DIRA */
-	if (mount(DIRB, DIRA, "none", MS_BIND, NULL) == -1) {
-		perror("mount");
-		return 1;
-	}
+	/* bind mounts DIRB to DIRA making contents of DIRB visible in DIRA */
+	SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
 
-	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
+
+	SAFE_UMOUNT(DIRA);
 
-	umount(DIRA);
 	return ret;
 }
 
-static void test(void)
+static void run(void)
 {
-	int status;
+	int status, ret;
 
 	/* unshares the mount ns */
-	if (unshare(CLONE_NEWNS) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
+	SAFE_UNSHARE(CLONE_NEWNS);
+
 	/* makes sure parent mounts/umounts have no effect on a real system */
-	SAFE_MOUNT(cleanup, "none", "/", "none", MS_REC|MS_PRIVATE, NULL);
+	SAFE_MOUNT("none", "/", "none", MS_REC | MS_PRIVATE, NULL);
 
 	/* bind mounts DIRA to itself */
-	SAFE_MOUNT(cleanup, DIRA, DIRA, "none", MS_BIND, NULL);
+	SAFE_MOUNT(DIRA, DIRA, "none", MS_BIND, NULL);
 
 	/* makes mount DIRA shared */
-	SAFE_MOUNT(cleanup, "none", DIRA, "none", MS_SHARED, NULL);
+	SAFE_MOUNT("none", DIRA, "none", MS_SHARED, NULL);
 
-	if (do_clone_tests(CLONE_NEWNS, child_func, NULL, NULL, NULL) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
+	ret = ltp_clone_quick(CLONE_NEWNS | SIGCHLD, child_func, NULL);
+	if (ret < 0)
+		tst_brk(TBROK, "clone failed");
 
-	/* bind mounts DIRB to DIRA making contents of DIRB visible
-	 * in DIRA */
-	SAFE_MOUNT(cleanup, DIRB, DIRA, "none", MS_BIND, NULL);
+	/* bind mounts DIRB to DIRA making contents of DIRB visible in DIRA */
+	SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
 
-	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(cleanup, 0);
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 
-	SAFE_UMOUNT(cleanup, DIRA);
+	SAFE_UMOUNT(DIRA);
 
-	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(cleanup, 0);
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 
-	if (access(DIRA"/B", F_OK) == 0)
-		tst_resm(TPASS, "shared mount in child passed");
+	if (access(DIRA "/B", F_OK) == 0)
+		tst_res(TPASS, "shared mount in child passed");
 	else
-		tst_resm(TFAIL, "shared mount in child failed");
+		tst_res(TFAIL, "shared mount in child failed");
 
-	TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
+	TST_CHECKPOINT_WAKE(0);
 
+	SAFE_WAIT(&status);
 
-	SAFE_WAIT(cleanup, &status);
 	if (WIFEXITED(status)) {
 		if ((WEXITSTATUS(status) == 0))
-			tst_resm(TPASS, "shared mount in parent passed");
+			tst_res(TPASS, "shared mount in parent passed");
 		else
-			tst_resm(TFAIL, "shared mount in parent failed");
+			tst_res(TFAIL, "shared mount in parent failed");
 	}
+
 	if (WIFSIGNALED(status)) {
-		tst_resm(TBROK, "child was killed with signal %s",
-			 tst_strsig(WTERMSIG(status)));
-		return;
+		tst_brk(TBROK, "child was killed with signal %s",
+			tst_strsig(WTERMSIG(status)));
 	}
 
-	SAFE_UMOUNT(cleanup, DIRA);
+	SAFE_UMOUNT(DIRA);
 }
 
-int main(int argc, char *argv[])
+static void setup(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++)
-		test();
-
-	cleanup();
-	tst_exit();
+	check_newns();
+	create_folders();
 }
 
-#else
-int main(void)
+static void cleanup(void)
 {
-	tst_brkm(TCONF, NULL, "needed mountflags are not defined");
+	umount_folders();
 }
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run,
+	.needs_root = 1,
+	.needs_checkpoints = 1,
+};
+
+#else
+TST_TEST_TCONF("needed mountflags are not defined");
 #endif