#!/bin/bash

# lab-grade-nfs

# This script is to be run on desktopX and tests the following:

# determines the ip of desktopX
# verifies that there is an nfs service running on serverX
# verifies that the nfs services on desktopX are using these ports:
# LOCKD_TCPPORT=32803
# LOCKD_UDPPORT=32769
# MOUNTD_PORT=892
# verifies that serverX is exporting /exports/share and that it is 
# available to desktopX
# verifies that desktopX has serverX:/exports/share mounted at /share
# verifies that after running these two commands, /share is still mounted:
# umount /share; mount -a

# Set environment and declare global variables
. /usr/local/lib/labtool.shlib
trap on_exit EXIT

# Check to make sure we are running as root
check_root

# Check to make sure we are running on the correct host
check_host "desktop"

IPA=$(hostname -i | cut -d. -f4)

# determines the ip of serverX
host server${IPA}.example.com | grep address

# verifies that there is an nfs service running on desktopX
if  rpcinfo -p server${IPA}.example.com | grep -q nfs; then
    echo "server${IPA}.example.com: (nfs service on) PASS"	    
else
    echo "server${IPA}.example.com: (nfs service on) FAIL"
    echo ""
    echo "Please check your configuration and try again."
    exit
fi

# UPDATE: Not for NFSv4
# verifies that the nfs services on serverX are using specific ports
#if  rpcinfo -p server${IPA}.example.com | grep -q 32803; then
#    echo "server${IPA}.example.com: (LOCKD_TCPPORT) PASS"	    
#else
#    echo "server${IPA}.example.com: (LOCKD_TCPPORT) FAIL"
#    echo ""
#    echo "Please check your configuration and try again."
#    exit
#fi
#
#if  rpcinfo -p server${IPA}.example.com | grep -q 32769; then
#    echo "server${IPA}.example.com: (LOCKD_UDPPORT) PASS"	    
#else
#    echo "server${IPA}.example.com: (LOCKD_UDPPORT) FAIL"
#    echo ""
#    echo "Please check your configuration and try again."
#    exit
#fi

if  rpcinfo -p server${IPA}.example.com | grep -q 892; then
    echo "server${IPA}.example.com: (MOUNTD_PORT) PASS"	    
else
    echo "server${IPA}.example.com: (MOUNTD_PORT) FAIL"
    echo ""
    echo "Please check your configuration and try again."
    exit
fi

# verifies that serverX is exporting /share/current
if  showmount -e server${IPA}.example.com | grep -q "/share/current"; then
    echo "server${IPA}.example.com:/exports/share (nfs share) PASS"	    
else
    echo "server${IPA}.example.com:/exports/share (nfs share) FAIL"
    echo ""
    echo "Please check your configuration and try again."
    exit
fi

# verifies that desktopX has serverX:/share/current mounted at /sales/current
if  mount | grep -q share; then
    echo "server${IPA}.example.com:/share/current /sales/current (share mounted) PASS"	    
else
    echo "server${IPA}.example.com:/share/current /sales/current (share mounted) FAIL"
    echo ""
    echo "Please check your configuration and try again."
    exit
fi

# verifies that after running umount;mount -a, /share is still mounted
umount /share &> /dev/null || echo "/share could not be unmounted, so we can't verify your fstab file really works."
echo "Please check your configuration and try again."

mount -a 

if  mount | grep -q share; then
    echo "server${IPA}.example.com:/exports/share /share (nfs fstab) PASS"	    
else
    echo "server${IPA}.example.com:/exports/share /share (nfs fstab) FAIL"
    echo ""
    echo "Please check your configuration and try again."
    exit
fi
