# hpc-testing
# Copyright (C) 2018 SUSE LLC
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# 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/>.

test_nfs()
{
	local server=$1
	local server_ip=$2
	local client=$3

	tp $client "! (mount | grep /tmp/RAM) || umount -f /tmp/RAM"

	subnet=$(echo $server_ip | sed -e 's/\.[0-9]\+$/.0/')
	tp $server "exportfs -u -a &&
	   		   systemctl stop nfs-server &&
			   (! (mount | grep /tmp/RAM) || umount -f /tmp/RAM) &&
			   mkdir -p /tmp/RAM && mount -t tmpfs -o size=2G tmpfs /tmp/RAM &&
			   echo '/tmp/RAM   $subnet/255.255.255.0(fsid=0,rw,async,insecure,no_root_squash)'> /etc/exports &&
			   systemctl start nfs-server &&
			   modprobe svcrdma &&
			   echo 'rdma 20049' > /proc/fs/nfsd/portlist &&
	   		   exportfs -a &&
			   dd if=/dev/urandom bs=1M count=64 of=/tmp/RAM/input"

	tp $client "modprobe xprtrdma &&
	   		   mkdir -p /tmp/RAM &&
			   mount -o rdma,port=20049 $server_ip:/tmp/RAM /tmp/RAM &&
			   (cat /proc/mounts | grep /tmp/RAM  | grep proto=rdma) &&
			   sleep 1 &&
			   dd if=/tmp/RAM/input bs=1M count=1024 of=/tmp/RAM/output &&
			   diff -q /tmp/RAM/input /tmp/RAM/output"
	tp $server "diff -q /tmp/RAM/input /tmp/RAM/output"

	# Cleanup
	tp $client "umount -f /tmp/RAM"
	tp $server "exportfs -u -a &&
	   		   systemctl stop nfs-server &&
			   sleep 1 &&
			   umount -f /tmp/RAM &&
			   echo > /etc/exports"
}
