Skip to contentSkip to conversation

shell: Add tst_mount() helper

PATCH Superseded Petr Vorel 2 participants 8 messages Feb 20, 2019 – Mar 13, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH 1/4 shell: Add tst_mount() helper

and TST_MOUNT, TST_MOUNT_PARAMS} variables.
+ document changes.

---
 doc/test-writing-guidelines.txt | 12 ++++++++----
 testcases/lib/tst_test.sh       | 22 +++++++++++++++++++++-
 2 files changed, 29 insertions(+), 5 deletions(-)
doc/test-writing-guidelines.txt +8 -4
index f2f72c4d6..5aa7e0279 100644
@@ -2006,12 +2006,16 @@ The 'tst_mkfs' helper will format device with the filesystem.
 tst_mkfs ext2 $TST_DEVICE
 -------------------------------------------------------------------------------
 
-Umounting filesystems
-+++++++++++++++++++++
+Mounting and unmounting filesystems
++++++++++++++++++++++++++++++++++++
+
+The 'tst_mount' and 'tst_umount' helpers are a safe way to mount/umount a filesystem.
 
-The 'tst_umount' helper is a safe way to umount a filesystem.
+The 'tst_mount' mounts '$TST_DEVICE' to '$TST_MOUNT', using optional '$TST_MOUNT_PARAMS'.
+It creates before mounting the '$TST_MOUNT' directory if not exists and fails
+if mounting was unsuccessful.
 
-If the path passed to the function is not mounted (present in '/proc/mounts')
+If the path passed to the 'tst_umount' is not mounted (present in '/proc/mounts')
 it's noop.
 
 Otherwise it retries to umount the filesystem a few times on a failure, which
testcases/lib/tst_test.sh +21 -1
index 3d2f5afde..4abea6665 100644
@@ -235,6 +235,25 @@ TST_RTNL_CHK()
 	tst_brk TBROK "$@ failed: $output"
 }
 
+tst_mount()
+{
+	if [ -z "$FS_TYPE" ]; then
+		tst_brk TBROK "Missing FS_TYPE variable"
+	fi
+
+	ROD_SILENT mkdir -p $TST_MOUNT
+	mount -t $FS_TYPE $TST_DEVICE $TST_MOUNT $TST_MOUNT_PARAMS
+	local ret=$?
+
+	if [ $ret -eq 32 ]; then
+		tst_brk TCONF "Cannot mount $FS_TYPE, missing driver?"
+	fi
+
+	if [ $ret -ne 0 ]; then
+		tst_brk TBROK "Failed to mount device: mount exit = $ret"
+	fi
+}
+
 tst_umount()
 {
 	local device="$1"
@@ -401,7 +420,7 @@ tst_run()
 			OPTS|USAGE|PARSE_ARGS|POS_ARGS);;
 			NEEDS_ROOT|NEEDS_TMPDIR|TMPDIR|NEEDS_DEVICE|DEVICE);;
 			NEEDS_CMDS|NEEDS_MODULE|MODPATH|DATAROOT);;
-			NEEDS_DRIVERS);;
+			NEEDS_DRIVERS|MOUNT|MOUNT_PARAMS);;
 			IPV6|IPVER|TEST_DATA|TEST_DATA_IFS);;
 			RETRY_FUNC|RETRY_FN_EXP_BACKOFF);;
 			*) tst_res TWARN "Reserved variable TST_$_tst_i used!";;
@@ -461,6 +480,7 @@ tst_run()
 		cd "$TST_TMPDIR"
 	fi
 
+	TST_MOUNT="${TST_MOUNT:-mntpoint}"
 	if [ "$TST_NEEDS_DEVICE" = 1 ]; then
 		if [ -z ${TST_TMPDIR} ]; then
 			tst_brk TBROK "Use TST_NEEDS_TMPDIR must be set for TST_NEEDS_DEVICE"
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 2/4 ima, commands/{df,mkfs}: Use tst_mount

to reduce duplicity.

---
 testcases/commands/df/df01.sh                  | 18 +++---------------
 testcases/commands/mkfs/mkfs01.sh              | 15 +--------------
 .../security/integrity/ima/tests/ima_setup.sh  |  9 ++-------
 3 files changed, 6 insertions(+), 36 deletions(-)
testcases/commands/df/df01.sh +3 -15
index ecd66fbd6..4f77500b8 100755
@@ -55,26 +55,14 @@ setup()
 		tst_test_cmds mkfs.${FS_TYPE}
 	fi
 
-	tst_mkfs ${FS_TYPE} ${TST_DEVICE}
-
-	ROD_SILENT mkdir -p mntpoint
-
-	mount ${TST_DEVICE} mntpoint
-	ret=$?
-	if [ $ret -eq 32 ]; then
-		tst_brk TCONF "Cannot mount ${FS_TYPE}, missing driver?"
-	fi
-
-	if [ $ret -ne 0 ]; then
-		tst_brk TBROK "Failed to mount device: mount exit = $ret"
-	fi
-
+	tst_mkfs $FS_TYPE $TST_DEVICE
+	tst_mount
 	DF_FS_TYPE=$(mount | grep "$TST_DEVICE" | awk '{print $5}')
 }
 
 cleanup()
 {
-	tst_umount ${TST_DEVICE}
+	tst_umount $TST_DEVICE
 }
 
 df_test()
testcases/commands/mkfs/mkfs01.sh +1 -14
index 68d40830a..25d185a05 100755
@@ -54,19 +54,6 @@ setup()
 	ROD_SILENT mkdir -p mntpoint
 }
 
-mkfs_mount()
-{
-	mount ${TST_DEVICE} mntpoint
-	local ret=$?
-	if [ $ret -eq 32 ]; then
-		tst_brk TCONF "Cannot mount ${FS_TYPE}, missing driver?"
-	fi
-
-	if [ $ret -ne 0 ]; then
-		tst_brk TBROK "Failed to mount device: mount exit = $ret"
-	fi
-}
-
 mkfs_verify_type()
 {
 	if [ -z "$1" ]; then
@@ -82,7 +69,7 @@ mkfs_verify_type()
 
 mkfs_verify_size()
 {
-	mkfs_mount
+	tst_mount
 	local blocknum=`df -P -B 1k mntpoint | tail -n1 | awk '{print $2}'`
 	tst_umount "$TST_DEVICE"
 
testcases/kernel/security/integrity/ima/tests/ima_setup.sh +2 -7
index fe6098135..e51d3d70b 100644
@@ -55,13 +55,8 @@ mount_loop_device()
 
 	tst_test_cmds mkfs.$FS_TYPE
 	tst_mkfs $FS_TYPE $TST_DEVICE
-	ROD_SILENT mkdir -p mntpoint
-	mount ${TST_DEVICE} mntpoint
-	ret=$?
-	if [ $ret -ne 0 ]; then
-		tst_brk TBROK "failed to mount device (mount exit = $ret)"
-	fi
-	cd mntpoint
+	tst_mount
+	cd $TST_MOUNT
 }
 
 print_ima_config()
Signature · 1 line
2.20.1
author
#2 lore raw

PATCH 3/4 shell: Move mkfs.foo into tst_mkfs()

In reply to Petr Vorel

+ remove unused local ret

---
 testcases/commands/df/df01.sh                              | 6 ------
 testcases/kernel/security/integrity/ima/tests/ima_setup.sh | 1 -
 testcases/lib/tst_test.sh                                  | 3 ++-
 3 files changed, 2 insertions(+), 8 deletions(-)
testcases/commands/df/df01.sh +0 -6
index 4f77500b8..b93fb7ad7 100755
@@ -49,12 +49,6 @@ parse_args()
 
 setup()
 {
-	local ret
-
-	if [ -n "$FS_TYPE" ]; then
-		tst_test_cmds mkfs.${FS_TYPE}
-	fi
-
 	tst_mkfs $FS_TYPE $TST_DEVICE
 	tst_mount
 	DF_FS_TYPE=$(mount | grep "$TST_DEVICE" | awk '{print $5}')
testcases/kernel/security/integrity/ima/tests/ima_setup.sh +0 -1
index e51d3d70b..e000390e4 100644
@@ -53,7 +53,6 @@ mount_loop_device()
 {
 	local ret
 
-	tst_test_cmds mkfs.$FS_TYPE
 	tst_mkfs $FS_TYPE $TST_DEVICE
 	tst_mount
 	cd $TST_MOUNT
testcases/lib/tst_test.sh +2 -1
index 4abea6665..9ac45e04a 100644
@@ -296,8 +296,9 @@ tst_mkfs()
 		tst_brk TBROK "No device specified"
 	fi
 
-	tst_res TINFO "Formatting $device with $fs_type extra opts='$fs_opts'"
+	tst_test_cmds mkfs.$fs_type
 
+	tst_res TINFO "Formatting $device with $fs_type extra opts='$fs_opts'"
 	ROD_SILENT mkfs.$fs_type $fs_opts $device
 }
 
Signature · 1 line
2.20.1
author
#4 lore raw

PATCH 4/4 shell: Use $FS_TYPE and $TST_DEVICE as default params in tst_mkfs()

In reply to Petr Vorel
---
 doc/test-writing-guidelines.txt                            | 4 ++++
 testcases/commands/df/df01.sh                              | 2 +-
 testcases/kernel/security/integrity/ima/tests/ima_setup.sh | 2 +-
 testcases/lib/tst_test.sh                                  | 4 ++--
 4 files changed, 8 insertions(+), 4 deletions(-)
doc/test-writing-guidelines.txt +4 -0
index 5aa7e0279..81fa3b0bc 100644
@@ -2004,6 +2004,10 @@ The 'tst_mkfs' helper will format device with the filesystem.
 -------------------------------------------------------------------------------
 # format test device with ext2
 tst_mkfs ext2 $TST_DEVICE
+# default params are $FS_TYPE $TST_DEVICE
+tst_mkfs
+# optional parameters
+tst_mkfs ext4 /dev/device -T largefile
 -------------------------------------------------------------------------------
 
 Mounting and unmounting filesystems
testcases/commands/df/df01.sh +1 -1
index b93fb7ad7..426ba7560 100755
@@ -49,7 +49,7 @@ parse_args()
 
 setup()
 {
-	tst_mkfs $FS_TYPE $TST_DEVICE
+	tst_mkfs
 	tst_mount
 	DF_FS_TYPE=$(mount | grep "$TST_DEVICE" | awk '{print $5}')
 }
testcases/kernel/security/integrity/ima/tests/ima_setup.sh +1 -1
index e000390e4..100813b4d 100644
@@ -53,7 +53,7 @@ mount_loop_device()
 {
 	local ret
 
-	tst_mkfs $FS_TYPE $TST_DEVICE
+	tst_mkfs
 	tst_mount
 	cd $TST_MOUNT
 }
testcases/lib/tst_test.sh +2 -2
index 9ac45e04a..055eecbd6 100644
@@ -283,8 +283,8 @@ tst_umount()
 
 tst_mkfs()
 {
-	local fs_type=$1
-	local device=$2
+	local fs_type=${1:-$FS_TYPE}
+	local device=${2:-$TST_DEVICE}
 	shift 2
 	local fs_opts="$@"
 
Signature · 1 line
2.20.1
#5 lore raw

PATCH 4/4 shell: Use $FS_TYPE and $TST_DEVICE as default params in tst_mkfs()

Hi!
This one is OK apart from the names for the variables, i.e. FS_TYPE should get TST_ prefix and TST_MOUNT should became TST_MNTPOINT.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#6 lore raw

PATCH 4/4 shell: Use $FS_TYPE and $TST_DEVICE as default params in tst_mkfs()

Hi Cyril,

Quoted 3 lines

Hi!
This one is OK apart from the names for the variables, i.e. FS_TYPE should get TST_ prefix and TST_MOUNT should became TST_MNTPOINT.

Thanks for the review. Good points, do that + document TST_FS_TYPE.

Kind regards,
Petr

#7 lore raw

PATCH 1/4 shell: Add tst_mount() helper

In reply to Petr Vorel

Hi!

Quoted 1 line

and TST_MOUNT, TST_MOUNT_PARAMS} variables.

^
This should probably named TST_MNTPOINT and TST_MNT_PARAMS

Also we seem to use $FS_TYPE inside of the library which is not documented anywhere in the change to the documentation, I see that it will error out when users fails to set it but I do not like it at all.

Ideally we should either introduce TST_FS_TYPE or pass the fs type as an agrument to the tst_mount function.

Quoted 86 lines

+ document changes.

---
 doc/test-writing-guidelines.txt | 12 ++++++++----
 testcases/lib/tst_test.sh       | 22 +++++++++++++++++++++-
 2 files changed, 29 insertions(+), 5 deletions(-)
doc/test-writing-guidelines.txt +8 -4
index f2f72c4d6..5aa7e0279 100644
@@ -2006,12 +2006,16 @@ The 'tst_mkfs' helper will format device with the filesystem.
 tst_mkfs ext2 $TST_DEVICE
 -------------------------------------------------------------------------------
 
-Umounting filesystems
-+++++++++++++++++++++
+Mounting and unmounting filesystems
++++++++++++++++++++++++++++++++++++
+
+The 'tst_mount' and 'tst_umount' helpers are a safe way to mount/umount a filesystem.
 
-The 'tst_umount' helper is a safe way to umount a filesystem.
+The 'tst_mount' mounts '$TST_DEVICE' to '$TST_MOUNT', using optional '$TST_MOUNT_PARAMS'.
+It creates before mounting the '$TST_MOUNT' directory if not exists and fails
+if mounting was unsuccessful.
 
-If the path passed to the function is not mounted (present in '/proc/mounts')
+If the path passed to the 'tst_umount' is not mounted (present in '/proc/mounts')
 it's noop.
 
 Otherwise it retries to umount the filesystem a few times on a failure, which
testcases/lib/tst_test.sh +21 -1
index 3d2f5afde..4abea6665 100644
@@ -235,6 +235,25 @@ TST_RTNL_CHK()
 	tst_brk TBROK "$@ failed: $output"
 }
 
+tst_mount()
+{
+	if [ -z "$FS_TYPE" ]; then
+		tst_brk TBROK "Missing FS_TYPE variable"
+	fi
+
+	ROD_SILENT mkdir -p $TST_MOUNT
+	mount -t $FS_TYPE $TST_DEVICE $TST_MOUNT $TST_MOUNT_PARAMS
+	local ret=$?
+
+	if [ $ret -eq 32 ]; then
+		tst_brk TCONF "Cannot mount $FS_TYPE, missing driver?"
+	fi
+
+	if [ $ret -ne 0 ]; then
+		tst_brk TBROK "Failed to mount device: mount exit = $ret"
+	fi
+}
+
 tst_umount()
 {
 	local device="$1"
@@ -401,7 +420,7 @@ tst_run()
 			OPTS|USAGE|PARSE_ARGS|POS_ARGS);;
 			NEEDS_ROOT|NEEDS_TMPDIR|TMPDIR|NEEDS_DEVICE|DEVICE);;
 			NEEDS_CMDS|NEEDS_MODULE|MODPATH|DATAROOT);;
-			NEEDS_DRIVERS);;
+			NEEDS_DRIVERS|MOUNT|MOUNT_PARAMS);;
 			IPV6|IPVER|TEST_DATA|TEST_DATA_IFS);;
 			RETRY_FUNC|RETRY_FN_EXP_BACKOFF);;
 			*) tst_res TWARN "Reserved variable TST_$_tst_i used!";;
@@ -461,6 +480,7 @@ tst_run()
 		cd "$TST_TMPDIR"
 	fi
 
+	TST_MOUNT="${TST_MOUNT:-mntpoint}"
 	if [ "$TST_NEEDS_DEVICE" = 1 ]; then
 		if [ -z ${TST_TMPDIR} ]; then
 			tst_brk TBROK "Use TST_NEEDS_TMPDIR must be set for TST_NEEDS_DEVICE"
Signature · 5 lines
2.20.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]