<commit_before>#!/usr/local/bin/bash
# Author: <NAME>
# License: BSD
# Location for tests into REST API of FreeNAS 9.3
# Resty Docs: https://github.com/micha/resty
# jsawk: https://github.com/micha/jsawk

# Where is the pcbsd-build program installed
PROGDIR="`realpath | sed 's|/scripts||g'`" ; export PROGDIR

# IP of client we are testing
if [ -n "$1" ] ; then
  ip="$1"
  manualip="YES"
else
  ip="192.168.56.100"
  manualip="NO"
fi

# Set the username / pass of FreeNAS for REST calls
if [ -n "$2" ] ; then
  fuser="$2"
else
  fuser="root"
fi
if [ -n "$3" ] ; then
  fpass="$3"
else
  fpass="<PASSWORD>"
fi

# Source our resty / jsawk functions
. ${PROGDIR}/../utils/resty -W "http://${ip}:80/api/v1.0" -H "Accept: application/json" -H "Content-Type: application/json" -u ${fuser}:${fpass}

# Set variable to call jsawk utility
JSAWK="${PROGDIR}/../utils/jsawk -j js24"

# Log files
RESTYOUT=/tmp/resty.out
RESTYERR=/tmp/resty.err

TOTALCOUNT="0"

#################################################################
# Run the tests now!
#################################################################

echo_ok()
{
  echo -e " - OK"
}

# Check for $1 REST response, error out if not found
check_rest_response()
{
  grep -q "$1" ${RESTYERR}
  if [ $? -ne 0 ] ; then
    cat ${RESTYERR}
    cat ${RESTYOUT}
    exit 1
  fi
}

check_rest_response_continue()
{
  grep -q "$1" ${RESTYERR}
  return $?
}

# $1 = Command to run
# $2 = Command to run if $1 fails
rc_halt()
{
  ${1} >/tmp/.cmdTest.$$ 2>/.cmdTest.$$
  if [ $? -ne 0 ] ; then
     echo -e " - FAILED"
     ${2}
     echo "Failed running: $1"
     cat /tmp/.cmdTest.$$
     rm /tmp/.cmdTest.$$
     exit 1
  fi
  rm /tmp/.cmdTest.$$
}

set_test_group_text()
{
  GROUPTEXT="$1"
  TOTALTESTS="$2"
  TCOUNT=0
}

echo_test_title()
{
  TCOUNT=`expr $TCOUNT + 1`
  TOTALCOUNT=`expr $TOTALCOUNT + 1`
  sync
  echo -e "Running $GROUPTEXT ($TCOUNT/$TOTALTESTS) - $1\c"
}

storage_tests()
{
  # Set the group text and number of tests
  set_test_group_text "Storage tests" "6"

  # Check getting disks
  echo_test_title "Disks / API functionality"
  GET /storage/disk/ -v 2>${RESTYERR} >${RESTYOUT}
  check_rest_response "200 OK"
  echo_ok

  # Check creating a zpool
  echo_test_title "Creating volume"
  POST /storage/volume/ '{ "volume_name" : "tank", "layout" : [ { "vdevtype" : "stripe", "disks" : [ "ada1", "ada2" ] } ] }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok

  # Check creating a dataset /share
  echo_test_title "Creating dataset tank/share"
  POST /storage/volume/tank/datasets/ '{ "name": "share" }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok

  # Check creating a dataset /jails
  echo_test_title "Creating dataset tank/jails"
  POST /storage/volume/tank/datasets/ '{ "name": "jails" }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok

  # Set the permissions of the dataset
  echo_test_title "Changing permissions on /mnt/tank"
  PUT /storage/permission/ '{ "mp_path": "/mnt/tank", "mp_acl": "unix", "mp_mode": "777", "mp_user": "root", "mp_group": "wheel" }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok

  # Set the permissions of the dataset
  echo_test_title "Changing permissions on /mnt/tank/share"
  PUT /storage/permission/ '{ "mp_path": "/mnt/tank/share", "mp_acl": "unix", "mp_mode": "777", "mp_user": "root", "mp_group": "wheel" }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok
}

nfs_tests()
{
  # Set the group text and number of tests
  set_test_group_text "NFS tests" "9"

  # Enable NFS server
  echo_test_title "Creating the NFS server"
  PUT /services/nfs/ '{ "nfs_srv_bindip": "'"${ip}"'", "nfs_srv_mountd_port": 618, "nfs_srv_allow_nonroot": false, "nfs_srv_servers": 10, "nfs_srv_udp": false, "nfs_srv_rpcstatd_port": 871, "nfs_srv_rpclockd_port": 32803, "nfs_srv_v4": false, "id": 1 }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "200 OK"
  echo_ok

  # Check creating a NFS share
  echo_test_title "Creating a NFS share on /mnt/tank/share"
  POST /sharing/nfs/ '{ "nfs_comment": "My Test Share", "nfs_paths": ["/mnt/tank/share"], "nfs_security": "sys" }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok

  # Now start the service
  echo_test_title "Starting NFS service"
  PUT /services/services/nfs/ '{ "srv_enable": true }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "200 OK"
  echo_ok

  # Now check if we can mount NFS / create / rename / copy / delete / umount
  echo_test_title "Mounting NFS"
  rc_halt "mkdir /tmp/nfs-mnt.$$"
  rc_halt "mount_nfs ${ip}:/mnt/tank/share /tmp/nfs-mnt.$$" "umount /tmp/nfs-mnt.$$ ; rmdir /tmp/nfs-mnt.$$"
  echo_ok

  echo_test_title "Creating NFS file"<commit_msg>Enable NFSv4 in testing, along with krb option. This is a workaround to the recent regression that jpaetzel and I have found where NFS mounts didn't work due to gssd not being started. Josh is still investigating the regression.<commit_after>#!/usr/local/bin/bash
# Author: <NAME>
# License: BSD
# Location for tests into REST API of FreeNAS 9.3
# Resty Docs: https://github.com/micha/resty
# jsawk: https://github.com/micha/jsawk

# Where is the pcbsd-build program installed
PROGDIR="`realpath | sed 's|/scripts||g'`" ; export PROGDIR

# IP of client we are testing
if [ -n "$1" ] ; then
  ip="$1"
  manualip="YES"
else
  ip="192.168.56.100"
  manualip="NO"
fi

# Set the username / pass of FreeNAS for REST calls
if [ -n "$2" ] ; then
  fuser="$2"
else
  fuser="root"
fi
if [ -n "$3" ] ; then
  fpass="$3"
else
  fpass="testing"
fi

# Source our resty / jsawk functions
. ${PROGDIR}/../utils/resty -W "http://${ip}:80/api/v1.0" -H "Accept: application/json" -H "Content-Type: application/json" -u ${fuser}:${fpass}

# Set variable to call jsawk utility
JSAWK="${PROGDIR}/../utils/jsawk -j js24"

# Log files
RESTYOUT=/tmp/resty.out
RESTYERR=/tmp/resty.err

TOTALCOUNT="0"

#################################################################
# Run the tests now!
#################################################################

echo_ok()
{
  echo -e " - OK"
}

# Check for $1 REST response, error out if not found
check_rest_response()
{
  grep -q "$1" ${RESTYERR}
  if [ $? -ne 0 ] ; then
    cat ${RESTYERR}
    cat ${RESTYOUT}
    exit 1
  fi
}

check_rest_response_continue()
{
  grep -q "$1" ${RESTYERR}
  return $?
}

# $1 = Command to run
# $2 = Command to run if $1 fails
rc_halt()
{
  ${1} >/tmp/.cmdTest.$$ 2>/.cmdTest.$$
  if [ $? -ne 0 ] ; then
     echo -e " - FAILED"
     ${2}
     echo "Failed running: $1"
     cat /tmp/.cmdTest.$$
     rm /tmp/.cmdTest.$$
     exit 1
  fi
  rm /tmp/.cmdTest.$$
}

set_test_group_text()
{
  GROUPTEXT="$1"
  TOTALTESTS="$2"
  TCOUNT=0
}

echo_test_title()
{
  TCOUNT=`expr $TCOUNT + 1`
  TOTALCOUNT=`expr $TOTALCOUNT + 1`
  sync
  echo -e "Running $GROUPTEXT ($TCOUNT/$TOTALTESTS) - $1\c"
}

storage_tests()
{
  # Set the group text and number of tests
  set_test_group_text "Storage tests" "6"

  # Check getting disks
  echo_test_title "Disks / API functionality"
  GET /storage/disk/ -v 2>${RESTYERR} >${RESTYOUT}
  check_rest_response "200 OK"
  echo_ok

  # Check creating a zpool
  echo_test_title "Creating volume"
  POST /storage/volume/ '{ "volume_name" : "tank", "layout" : [ { "vdevtype" : "stripe", "disks" : [ "ada1", "ada2" ] } ] }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok

  # Check creating a dataset /share
  echo_test_title "Creating dataset tank/share"
  POST /storage/volume/tank/datasets/ '{ "name": "share" }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok

  # Check creating a dataset /jails
  echo_test_title "Creating dataset tank/jails"
  POST /storage/volume/tank/datasets/ '{ "name": "jails" }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok

  # Set the permissions of the dataset
  echo_test_title "Changing permissions on /mnt/tank"
  PUT /storage/permission/ '{ "mp_path": "/mnt/tank", "mp_acl": "unix", "mp_mode": "777", "mp_user": "root", "mp_group": "wheel" }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok

  # Set the permissions of the dataset
  echo_test_title "Changing permissions on /mnt/tank/share"
  PUT /storage/permission/ '{ "mp_path": "/mnt/tank/share", "mp_acl": "unix", "mp_mode": "777", "mp_user": "root", "mp_group": "wheel" }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok
}

nfs_tests()
{
  # Set the group text and number of tests
  set_test_group_text "NFS tests" "9"

  # Enable NFS server
  echo_test_title "Creating the NFS server"
  PUT /services/nfs/ '{ "nfs_srv_bindip": "'"${ip}"'", "nfs_srv_mountd_port": 618, "nfs_srv_allow_nonroot": false, "nfs_srv_servers": 10, "nfs_srv_udp": false, "nfs_srv_rpcstatd_port": 871, "nfs_srv_rpclockd_port": 32803, "nfs_srv_v4": true, "nfs_srv_v4_krb": true, "id": 1 }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "200 OK"
  echo_ok

  # Check creating a NFS share
  echo_test_title "Creating a NFS share on /mnt/tank/share"
  POST /sharing/nfs/ '{ "nfs_comment": "My Test Share", "nfs_paths": ["/mnt/tank/share"], "nfs_security": "sys" }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "201 CREATED"
  echo_ok

  # Now start the service
  echo_test_title "Starting NFS service"
  PUT /services/services/nfs/ '{ "srv_enable": true }' -v >${RESTYOUT} 2>${RESTYERR}
  check_rest_response "200 OK"
  echo_ok

  # Now check if we can mount NFS / create / rename / copy / delete / umount
  echo_test_title "Mounting NFS"
  rc_halt "mkdir /tmp/nfs-mnt.$$"
  rc_halt "mount_nfs ${ip}:/mnt/tank/share /tmp/nfs-mnt.$$" "umount /tmp/nfs-mnt.$$ ; rmdir /tmp/nfs-mnt.$$"
  echo_ok

  echo_test_title "Creating NFS file"
{'fraction_non_alphanumeric': 0.18223489864520728, 'fraction_numerical': 0.022002648466945097, 'mean_word_length': 3.8797216699801194, 'pattern_counts': {'":': 51, '<': 6, '<?xml version=': 0, '>': 46, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '788465', 'n_tokens_mistral': 4114, 'n_tokens_neox': 3815, 'n_words': 989}