#!/bin/sh

set -e

if [ -z "$AWS_INSTANCE_ID" ] || [ -z "$AWS_REGION" ] || \
   [ -z "$AWS_ACCESS_KEY" ] || [ -z "$AWS_SECRET_KEY" ];
then
  echo "AWS environment not found, skipping test."
  exit 77
fi

test_aws() {
  printf "\n=== $1 ===\n"
  /usr/sbin/fence_aws -r $AWS_REGION -a $AWS_ACCESS_KEY -s $AWS_SECRET_KEY -n $AWS_INSTANCE_ID -o $1
}

printf  "=== version ===\n"
/usr/sbin/fence_aws -V

test_aws list

if test_aws status
then # instance is running
  test_aws reboot
  sleep 30
  test_aws status

  test_aws off
  sleep 30
  test_aws status || true

  test_aws on
  sleep 30
  test_aws status
else # instance is stopped
  test_aws on
  sleep 30
  test_aws status

  test_aws reboot
  sleep 30
  test_aws status

  test_aws off
  sleep 30
  test_aws status || true
fi
