diff mbox series

[V2] selinux-testsuite: Allow nfs test script to close cleanly

Message ID 20200221131825.91499-1-richard_c_haines@btinternet.com (mailing list archive)
State Accepted
Headers show
Series [V2] selinux-testsuite: Allow nfs test script to close cleanly | expand

Commit Message

Richard Haines Feb. 21, 2020, 1:18 p.m. UTC
Whenever a failure, close NFS cleanly.

Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>
---
V2 Changes:
Revamp error handling to cover all cases

 tools/nfs.sh | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

Comments

Stephen Smalley Feb. 25, 2020, 2:07 p.m. UTC | #1
On Fri, Feb 21, 2020 at 8:18 AM Richard Haines
<richard_c_haines@btinternet.com> wrote:
>
> Whenever a failure, close NFS cleanly.
>
> Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>

I'm not much of a shell programmer but this looks good to me.

Acked-by: Stephen Smalley <sds@tycho.nsa.gov>

> ---
> V2 Changes:
> Revamp error handling to cover all cases
>
>  tools/nfs.sh | 27 +++++++++++++++++++++++----
>  1 file changed, 23 insertions(+), 4 deletions(-)
>
> diff --git a/tools/nfs.sh b/tools/nfs.sh
> index 314f898..7ba4cfc 100755
> --- a/tools/nfs.sh
> +++ b/tools/nfs.sh
> @@ -1,14 +1,33 @@
>  #!/bin/sh -e
>  MOUNT=`stat --print %m .`
>  TESTDIR=`pwd`
> -systemctl start nfs-server
> +MAKE_TEST=0
> +
> +function err_exit() {
> +    if [ $MAKE_TEST -eq 1 ]; then
> +        echo "Closing down NFS"
> +        popd
> +    else
> +        echo "Error on line: $1 - Closing down NFS"
> +    fi
> +    umount /mnt/selinux-testsuite
> +    exportfs -u localhost:$MOUNT
> +    rmdir /mnt/selinux-testsuite
> +    systemctl stop nfs-server
> +    exit 1
> +}
> +
> +trap 'err_exit $LINENO' ERR
>
> +systemctl start nfs-server
>  # Run the full testsuite on a labeled NFS mount.
>  exportfs -orw,no_root_squash,security_label localhost:$MOUNT
>  mkdir -p /mnt/selinux-testsuite
>  mount -t nfs -o vers=4.2 localhost:$TESTDIR /mnt/selinux-testsuite
>  pushd /mnt/selinux-testsuite
> +MAKE_TEST=1
>  make test
> +MAKE_TEST=0
>  popd
>  umount /mnt/selinux-testsuite
>
> @@ -18,7 +37,7 @@ echo "Testing context mount of a security_label export."
>  fctx=`secon -t -f /mnt/selinux-testsuite`
>  if [ "$fctx" != "etc_t" ]; then
>      echo "Context mount failed: got $fctx instead of etc_t."
> -    exit 1
> +    err_exit $LINENO
>  fi
>  umount /mnt/selinux-testsuite
>  exportfs -u localhost:$MOUNT
> @@ -30,7 +49,7 @@ echo "Testing context mount of a non-security_label export."
>  fctx=`secon -t -f /mnt/selinux-testsuite`
>  if [ "$fctx" != "etc_t" ]; then
>      echo "Context mount failed: got $fctx instead of etc_t."
> -    exit 1
> +    err_exit $LINENO
>  fi
>  umount /mnt/selinux-testsuite
>
> @@ -40,7 +59,7 @@ echo "Testing non-context mount of a non-security_label export."
>  fctx=`secon -t -f /mnt/selinux-testsuite`
>  if [ "$fctx" != "nfs_t" ]; then
>      echo "Context mount failed: got $fctx instead of nfs_t."
> -    exit 1
> +    err_exit $LINENO
>  fi
>  umount /mnt/selinux-testsuite
>
> --
> 2.24.1
>
Stephen Smalley Feb. 27, 2020, 5:13 p.m. UTC | #2
On Tue, Feb 25, 2020 at 9:06 AM Stephen Smalley
<stephen.smalley@gmail.com> wrote:
>
> On Fri, Feb 21, 2020 at 8:18 AM Richard Haines
> <richard_c_haines@btinternet.com> wrote:
> >
> > Whenever a failure, close NFS cleanly.
> >
> > Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>
>
> I'm not much of a shell programmer but this looks good to me.
>
> Acked-by: Stephen Smalley <sds@tycho.nsa.gov>

Applied.
diff mbox series

Patch

diff --git a/tools/nfs.sh b/tools/nfs.sh
index 314f898..7ba4cfc 100755
--- a/tools/nfs.sh
+++ b/tools/nfs.sh
@@ -1,14 +1,33 @@ 
 #!/bin/sh -e
 MOUNT=`stat --print %m .`
 TESTDIR=`pwd`
-systemctl start nfs-server
+MAKE_TEST=0
+
+function err_exit() {
+    if [ $MAKE_TEST -eq 1 ]; then
+        echo "Closing down NFS"
+        popd
+    else
+        echo "Error on line: $1 - Closing down NFS"
+    fi
+    umount /mnt/selinux-testsuite
+    exportfs -u localhost:$MOUNT
+    rmdir /mnt/selinux-testsuite
+    systemctl stop nfs-server
+    exit 1
+}
+
+trap 'err_exit $LINENO' ERR
 
+systemctl start nfs-server
 # Run the full testsuite on a labeled NFS mount.
 exportfs -orw,no_root_squash,security_label localhost:$MOUNT
 mkdir -p /mnt/selinux-testsuite
 mount -t nfs -o vers=4.2 localhost:$TESTDIR /mnt/selinux-testsuite
 pushd /mnt/selinux-testsuite
+MAKE_TEST=1
 make test
+MAKE_TEST=0
 popd
 umount /mnt/selinux-testsuite
 
@@ -18,7 +37,7 @@  echo "Testing context mount of a security_label export."
 fctx=`secon -t -f /mnt/selinux-testsuite`
 if [ "$fctx" != "etc_t" ]; then
     echo "Context mount failed: got $fctx instead of etc_t."
-    exit 1
+    err_exit $LINENO
 fi
 umount /mnt/selinux-testsuite
 exportfs -u localhost:$MOUNT
@@ -30,7 +49,7 @@  echo "Testing context mount of a non-security_label export."
 fctx=`secon -t -f /mnt/selinux-testsuite`
 if [ "$fctx" != "etc_t" ]; then
     echo "Context mount failed: got $fctx instead of etc_t."
-    exit 1
+    err_exit $LINENO
 fi
 umount /mnt/selinux-testsuite
 
@@ -40,7 +59,7 @@  echo "Testing non-context mount of a non-security_label export."
 fctx=`secon -t -f /mnt/selinux-testsuite`
 if [ "$fctx" != "nfs_t" ]; then
     echo "Context mount failed: got $fctx instead of nfs_t."
-    exit 1
+    err_exit $LINENO
 fi
 umount /mnt/selinux-testsuite