#!/bin/sh

# Test repeatedly blocking and unblocking iSCSI and verifying that we get
# METADATA_LUN_{BROKEN,HEALTHY} alerts.

# NB the METADATA_LUN must be turned on already.

set -e

pool=$(xe pool-list params=uuid --minimal)

clear_messages() {
  IFS=","; for message in $(xe message-list params=uuid --minimal); do
    xe message-destroy uuid=$message
  done
}

block(){
  echo "Blocking LUN"
  iptables -I OUTPUT -p tcp --dport 3260 -j DROP
}

unblock(){
  echo "Unblocking LUN"
  iptables -F
}

wait_for_message() {
  local healthy=$1
  if [ ${healthy} -eq 1 ]; then
     echo "Waiting for LUN healthy"
  else
     echo "Waiting for LUN broken"
  fi
  for((retry=1;retry<600;retry++)); do
    # Prodding the system with a write here seems to "unstick" it:
    xe pool-param-set uuid=${pool} other-config:attempt=${retry}
    finished=1
    IFS=","; for message in $(xe message-list params=uuid --minimal); do
      name=$(xe message-list uuid=$message params=name --minimal)
      echo "Examining ${name}"
      if [ "${name}" = "METADATA_LUN_HEALTHY" ]; then
         if [ ${healthy} -eq 1 ]; then
	     echo "${name}-- expected (${retry} s)";
	     return 0
         else
	     echo "${name}-- unexpected";
             return 1
         fi
      fi
      if [ "${name}" = "METADATA_LUN_BROKEN" ]; then
         if [ ${healthy} -eq 1 ]; then
	     echo "${name}-- unexpected";
             return 1
         else
	     echo "${name}-- expected (${retry} s)";
             return 0
	 fi
      fi
    done
    echo -n .
    sleep 1s
  done
  return 1
}

for ((i=0;i<1000;i++));
do
  echo Iteration $i
  xe pool-param-set uuid=${pool} name-description=1
  clear_messages
  block
  xe pool-param-set uuid=${pool} name-description=2
  wait_for_message 0
  clear_messages
  unblock
  xe pool-param-set uuid=${pool} name-description=3
  wait_for_message 1
done