#!/bin/bash

# Copyright (c) 2010 Novell Inc, John Shi
#           All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.

die()
{
  local str
  str="$1"

  echo "ERROR: $str" >&2
  exit 1
}

warn()
{
  local str
  str="$1"

  echo "WARNING: $str" >&2
}

parse_die()
{
  local str
  str="$1"

  die "${agent}: line ${line_num}: ${str}"
}

# add quotes to string for Here Documents
add_quotes()
{
  local typ str a b
  typ="$1"
  str="$2"

  case "$typ" in
    1) a=\'; b=\";;
    2) a=\"; b=\';;
  esac

  echo "$str" | sed "s/$a/$a$b$a$b$a/g; 1 s/^/$a/; $ s/$/$a/"
}

# split strings
explode()
{
  local str
  str="$1"

  echo "$str" | awk -F'"' '{
    if (NF > 0 && NF%2 == 0)
      exit(1);
    for (i=1; i<=NF; i++) {
      if (i%2 == 0)
        print $i;
      else {
        split($i, str, /[[:blank:]]+/);
        for (j=0; j<length(str); j++) {
          sb = sub(/#.*/, "", str[j]);
          if (str[j] != "")
            print str[j];
          if (sb)
            exit(0);
        }
      }
    }
  }'
}

# phase 1: parse the string to 'command' and 'argument collection'.
line2trunk()
{
  trunk[0]="${line%%[[:blank:]]*}"
  trunk[1]="${line#*[[:blank:]]}"
}

# phase 2: split the argument collection.
trunk2branch()
{
  local IFS

  # Some of statements need one parameter at least.
  if [ "$line" = "${trunk[0]}" ]; then
    parse_die "missing parameter."
  fi

  IFS=$'\n'
  branch=($(explode "${trunk[1]}"))
  if [ $? -ne 0 ]; then
    parse_die "missing '\"'."
  fi
}

preparse_cfg()
{
  local agent line trunk branch macro num host
  agent="$1"

  if [ ! -r "$opt_cfgsdir/$agent" ]; then
    die "${agent}: configuration file not found."
  fi

  rm -f $CASES_DIR/${agent}_macro.*
  rm -f $CASES_DIR/${agent}.preparse

  line_num=0
  while read -r line; do
    let line_num++
    num=" $line_num"

    case "$line" in
      ""|\#*) continue;;
    esac

    line2trunk
    case "${trunk[0]}" in
      CASE-BLOCK)
        trunk2branch
        macro="$CASES_DIR/${agent}_macro.${branch[0]}"
        continue
        ;;
      Include|Include@*)
        host=$(echo "${trunk[0]}" | awk -F@ '{print $2}')
        trunk2branch
        if [ ! -r "$CASES_DIR/${agent}_macro.${branch[0]}" ]; then
          parse_die "Macro '${branch[0]}' not found."
        fi
        if [ -n "$host" ]; then
          line="$(sed -e 's/^\([^[:blank:]]*\)@[^[:blank:]]*/\1/' -e "s/^[^[:blank:]]*/&@$host/" "$CASES_DIR/${agent}_macro.${branch[0]}")"
        else
          line="$(<"$CASES_DIR/${agent}_macro.${branch[0]}")"
        fi
        num=
        ;;
      *[!A-Z-]*)
        :
        ;;
      *)
        macro=
        ;;
    esac

    if [ -n "$macro" ]; then
      if ! touch "$macro"; then
        die "No permission to create macro file: ${macro}."
      fi
      echo "$line$num" >>"$macro"
    else
      echo "$line$num" >>"$CASES_DIR/${agent}.preparse"
    fi
  done <"$opt_cfgsdir/$agent"

  rm -f $CASES_DIR/${agent}_macro.*
}

case_finish()
{
  local host

  if [ -n "$sh" ]; then
    cat >>$sh <<EOF
if [ -n "\$OCFT_VERBOSE" ]; then
  echo
fi
# Clean up and exit
EOF
    for host in $hosts; do
      echo "backbash_stop $host" >>$sh
    done
    echo "quit 0" >>$sh
  fi
  atexit_num=0
  hosts=
  sh=
}

parse_cfg()
{
  local agents i line stat sh trunk branch atexit_num host hosts

  if [ $# -eq 0 ]; then
    agents=($opt_cfgsdir/*)
  else
    agents=("$@")
  fi

  for agent in "${agents[@]}"; do
    i=0
    agent="$(basename "$agent")"
    rm -f $CASES_DIR/*_${agent}.sh
    rm -f $CASES_DIR/${agent}_setup

    echo "Making '$agent': "
    preparse_cfg "$agent"
    while read -r line; do
      line_num="${line##* }"
      line="${line% *}"
      line2trunk

      # state switch
      case "${trunk[0]}" in
        CONFIG)
          case_finish
          stat=1
          continue
          ;;
	SETUP-AGENT)
          case_finish
          stat=2
          continue
          ;;
        CASE)
          case_finish
          trunk2branch
          sh="$CASES_DIR/${i}_${agent}.sh"
          echo "    - case ${i}: ${branch[0]}"
          cat >$sh <<EOF
#!/bin/bash

# Agent:   $agent
# Summary: ${branch[0]}

. $OCFT_DIR/caselib || {
  echo "ERROR: '$OCFT_DIR/caselib' not found."
  exit 2
}

MYROOT="$cfg_agent_root"
EOF
          if [ -n "$cfg_install_package" ]; then
            cat >>$sh <<EOF
agent_install ${cfg_install_package[@]}
EOF
          fi
          if [ -r "$CASES_DIR/${agent}_setup" ]; then
            cat >>$sh <<EOF
agent_setup $agent <<'CMD'
$(cat "$CASES_DIR/${agent}_setup")
CMD
EOF
          fi
          cat >>$sh <<EOF
if [ -n "\$OCFT_VERBOSE" ]; then
  echo -e $(add_quotes 1 "Starting '\\033[33m${agent}\\033[0m' case $i '\\033[33m${branch[0]}\\033[0m':")
else
  echo -n "'${agent}' case ${i}: "
fi
EOF
          chmod a+x $sh
          let i++
          stat=3
          continue
          ;;
      esac

      case "$stat" in
        1)
          case "${trunk[0]}" in
            AgentRoot)
              trunk2branch
              cfg_agent_root="${branch[0]}"
              ;;
            InstallPackage)
              trunk2branch
              cfg_install_package=("${branch[@]}")
              ;;
            HangTimeout)
              trunk2branch
              if ! echo "${branch[0]}" | grep -qxE '[0-9]+'; then
                parse_die "numeric argument required."
              fi
              cfg_hang_timeout="${branch[0]}"
              ;;
            *)
              parse_die "unimplemented statement: ${trunk[0]}"
              ;;
          esac
          ;;
        2)
          echo "$line" >>$CASES_DIR/${agent}_setup
          ;;
        3)
          host=$(echo ${trunk[0]} | awk -F@ '{print $2}')
          if [ -n "$host" ]; then
            if ! echo "$hosts" | grep -q "$host"; then
              hosts=$hosts$'\n'$host
              cat >>$sh <<EOF
# Initialize remote shell
backbash_start $host
backbash $host <<CMD
OCFT_VERBOSE=\$OCFT_VERBOSE
showhost="${host}: "
CMD
backbash $host <$OCFT_DIR/caselib
backbash $host <<'CMD'
MYROOT="$cfg_agent_root"
EOF
              if [ -n "$cfg_install_package" ]; then
                cat >>$sh <<EOF
agent_install ${cfg_install_package[@]}
EOF
              fi
              if [ -r "$CASES_DIR/${agent}_setup" ]; then
                cat >>$sh <<EOF
agent_setup $agent <<'END'
$(cat "$CASES_DIR/${agent}_setup")
END
EOF
              fi
              echo "CMD" >>$sh
            fi
          fi

          echo "
# CASE statement: $line" >>$sh

          if [ -n "$host" ]; then
            echo "backbash $host <<'CMD'" >>$sh
          fi

          case "${trunk[0]}" in
            Var|Var@*)
              cat >>$sh <<EOF
if [ -n "\$OCFT_VERBOSE" ]; then
  echo $(add_quotes 2 "    \${showhost}Setting agent environment:    export ${trunk[1]}")
fi
export ${trunk[1]}
check_success \$? $(add_quotes 1 "export ${trunk[1]}")
EOF
              ;;
            Unvar|Unvar@*)
              cat >>$sh <<EOF
if [ -n "\$OCFT_VERBOSE" ]; then
  echo $(add_quotes 2 "    \${showhost}Removing agent environment:   unset ${trunk[1]}")
fi
unset ${trunk[1]}
check_success \$? $(add_quotes 1 "unset ${trunk[1]}")
EOF
              ;;
            AgentRun|AgentRun@*)
              trunk2branch
              if [ -z "${branch[1]}" ]; then
                if [ "${branch[0]}" = "start" ]; then
                  cat >>$sh <<EOF
agent_run $(add_quotes 1 "$agent") status $cfg_hang_timeout
rc=\$?
if [ \$rc -eq \$OCF_ERR_UNIMPLEMENTED ]; then
  agent_run $(add_quotes 1 "$agent") monitor $cfg_hang_timeout
  rc=\$?
fi

if [ \$rc -eq \$OCF_SUCCESS ]; then
  : #The status I want, so I can do nothing.
elif [ \$rc -eq \$OCF_NOT_RUNNING ]; then
  if [ -n "\$OCFT_VERBOSE" ]; then
    echo $(add_quotes 2 "    \${showhost}Running agent:                ./$agent start")
  fi
  agent_run $(add_quotes 1 "$agent") start $cfg_hang_timeout
  check_success \$? $(add_quotes 1 "./$agent start")
else
  check_success \$rc $(add_quotes 1 "./$agent status|monitor")
fi
EOF
                elif [ "${branch[0]}" = "stop" ]; then
                  cat >>$sh <<EOF
agent_run $(add_quotes 1 "$agent") status $cfg_hang_timeout
rc=\$?
if [ \$rc -eq \$OCF_ERR_UNIMPLEMENTED ]; then
  agent_run $(add_quotes 1 "$agent") monitor $cfg_hang_timeout
  rc=\$?
fi

if [ \$rc -eq \$OCF_NOT_RUNNING ]; then
  : #The status I want, so I can do nothing.
elif [ \$rc -eq \$OCF_SUCCESS ]; then
  if [ -n "\$OCFT_VERBOSE" ]; then
    echo $(add_quotes 2 "    \${showhost}Running agent:                ./$agent stop")
  fi
  agent_run $(add_quotes 1 "$agent") stop $cfg_hang_timeout
  check_success \$? $(add_quotes 1 "./$agent stop")
else
  check_success \$rc $(add_quotes 1 "./$agent status|monitor")
fi
EOF
                elif [ "${branch[0]}" = "status" -o "${branch[0]}" = "monitor" ]; then
                  cat >>$sh <<EOF
agent_run $(add_quotes 1 "$agent") $(add_quotes 1 "${branch[0]}") $cfg_hang_timeout
EOF
                else
                  cat >>$sh <<EOF
agent_run $(add_quotes 1 "$agent") $(add_quotes 1 "${branch[0]}") $cfg_hang_timeout
check_success \$? $(add_quotes 1 "./$agent ${branch[0]}")
EOF
                fi
              else
                cat >>$sh <<EOF
test -n $(add_quotes 2 "\$${branch[1]}")
check_success \$? $(add_quotes 1 "test -n \"\$${branch[1]}\"")
if [ -n "\$OCFT_VERBOSE" ]; then
  echo $(add_quotes 2 "    \${showhost}Running agent:                ./$agent ${branch[0]}")
fi
agent_run $(add_quotes 1 "$agent") $(add_quotes 1 "${branch[0]}") $cfg_hang_timeout
ret=\$?
if [ -n "\$OCFT_VERBOSE" ]; then
  echo -n "    \${showhost}Checking return value:"
fi
if [ -n "\${retval[ret]}" ]; then
  retstr="\${retval[ret]}"
else
  retstr=\$ret
fi
if [ \$ret -eq \$${branch[1]} ]; then
  if [ -n "\$OCFT_VERBOSE" ]; then
    echo -e $(add_quotes 2 "        \\033[32mOK\\033[0m. The return value '\\033[34m\$retstr\\033[0m' == '\\033[34m${branch[1]}\\033[0m'")
  else
    echo -e "\t\\033[32mOK\\033[0m."
  fi
else
  if [ -n "\$OCFT_VERBOSE" ]; then
    echo -en $(add_quotes 2 "        \\033[31mFAILED\\033[0m. The return value '\\033[34m\$retstr\\033[0m' != '\\033[34m${branch[1]}\\033[0m'. ")
  else
    echo -en "\t\\033[31mFAILED\\033[0m. Agent returns unexpected value: '\$retstr'. "
  fi
  echo "See details below:"
  cat /tmp/.ocft_runlog
  echo
  quit 1
fi
EOF
              fi
              ;;
            Bash|Bash@*)
              cat >>$sh <<EOF
if [ -n "\$OCFT_VERBOSE" ]; then
  echo $(add_quotes 2 "    \${showhost}Setting system environment:   ${trunk[1]}")
fi
echo $(add_quotes 1 "${trunk[1]}") | /bin/bash
check_success \$? $(add_quotes 1 "${trunk[1]}")
EOF
              ;;
            BashAtExit|BashAtExit@*)
              let atexit_num++
              cat >>$sh <<EOF
atexit${atexit_num}()
{
  if [ -n "\$OCFT_VERBOSE" ]; then
    echo $(add_quotes 2 "    \${showhost}Setting system environment:   ${trunk[1]}")
  fi
  echo $(add_quotes 1 "${trunk[1]}") | /bin/bash
}
let atexit_num++
EOF
              ;;
            *)
              parse_die "unimplemented statement: ${trunk[0]}"
              ;;
          esac
          if [ -n "$host" ]; then
            echo 'CMD' >>$sh
          fi
          ;;
        *)
          parse_die "unimplemented statement: ${trunk[0]}"
          ;;
      esac
    done <$CASES_DIR/${agent}.preparse
    rm -f $CASES_DIR/${agent}.preparse
    rm -f $CASES_DIR/${agent}_setup
    case_finish
  done
}

start_test()
{
  local sh shs agents line ret

  if ! cd $CASES_DIR >/dev/null 2>&1; then
    die "cases directory not found."
  fi

  export OCFT_VERBOSE=$opt_verbose

  if [ $# -eq 0 ]; then
    agents=($(ls -1 *.sh 2>/dev/null | sed 's/.*_\([^_]*\)\.sh$/\1/' | sort | uniq))
  else
    agents=("$@")
  fi

  for shs in "${agents[@]}"; do
    for sh in $(ls -1 *_${shs}.sh 2>/dev/null | sort -n); do
      ./$sh
      ret=$?
      if [ $ret -eq 3 ]; then
        die "core function failed, break all tests."
      fi
      if [ $ret -eq 2 ]; then
        warn "core function failed, break all tests of '$shs'."
        break
      fi
    done
  done | while read -r line; do
    echo "$line"
    echo "$(date '+%F %T'): $line" | cat -A |
    sed -r 's/\^\[\[[0-9]+m|\^I|.$//g' >>ocft.log
  done
}

delete_cases()
{
  local shs

  if [ $# -eq 0 ]; then
    rm -f $CASES_DIR/*.sh
  else
    for shs in "$@"; do
      rm -f $CASES_DIR/*_${shs}.sh
    done
  fi
}

usage()
{
  cat <<EOF
$0 ACTION [OPTION] [agent1 [agent2] [...]]
ACTIONs include:
     make [-d dir]   Generate the testing shell scripts.
                       -d  The directory that contains 
		           configuration of cases.
     test [-v]       Execute the testing shell scripts.
                       -v  Verbose output mode.
     clean           Delete the testing shell scripts.
     help [-v]       Show this help and exit.
                       -v  Show HOWTO and exit.
Version 0.42
See '$OCFT_DIR/README' for detail.
EOF
}

howto()
{
  cat <<EOF
HOW TO USE THIS TOOL

  - Ocft is a testing tool for resource agents. Instead of the policy of HA,
    it mainly concerns whether resource agents run correct locally. It can 
    design types of complicated environments to test the reliability of 
    resource agents. Precisely, it is to display whether resource agents can 
    return to correct or expected value. The advantage of the tool provides 
    us with competence to design conditions which can be recorded or reproduced. 
    Hence it is useful to debuggers.

* Components
    ** Test case generator (/usr/bin/ocft)
      - Turning configuration files of test case to executable scripts.

    ** Configuration file  ($CONFIGS_DIR/)
      - Every configuration file directs only one resource agent and share the same 
        name with resource agent but contains more test cases.

    ** The testing script  ($CASES_DIR/)
      - After the generator reads configuration files and generates many testing 
        scripts and the script is underway, the test begins.

* How to customize the environment of testing
  - Ocft designs the running conditions through two ways, one is changing the 
    environment variables of resource agents (it is the interface left by OCF itself), 
    the other is modifying the OS environment of resource agents, such as altering 
    the permission of some key file or IP address of the machine.

* How to test
  - Firstly, you need to sketch the all complex and uncommon environments against 
    a certain resource agent and keep in mind what consequences may be caused by 
    these uncommon environments. 
    Secondly, write the designed conditions and foreknown consequences into 
    configuration files, and then run the generator to translate the test case to 
    executable scripts. 
    Finally, you need running these scripts to observe the output and learn 
    the running status of each test case, which will compares the predicated result 
    with the actual one. If they differ, you will be able to find the bugs of the 
    resource agent.
EOF
}


export LANG=C

# system variable
OCFT_DIR=/usr/share/resource-agents/ocft
CONFIGS_DIR=/usr/share/resource-agents/ocft/configs
CASES_DIR=/var/lib/resource-agents/ocft/cases

# global variable
agent=
line_num=

# default configuration
cfg_agent_root=
cfg_install_package=
cfg_hang_timeout=20

# default option
opt_verbose=
opt_cfgsdir=$CONFIGS_DIR

command="$1"
shift

case "$command" in
  make)
    if [ "$1" = "-d" ]; then
      if [ ! -d "$2" ]; then
        usage
        exit 1
      fi
      opt_cfgsdir="$2"
      shift 2
    fi
    if [ ! -d "$CASES_DIR" ]; then
      mkdir -p "$CASES_DIR" || die "Can not create directory: ${CASES_DIR}."
    fi
    parse_cfg "$@"
    ;;
  test)
    if [ "$1" = "-v" ]; then
      opt_verbose=1
      shift
    fi
    start_test "$@"
    ;;
  clean)
    delete_cases "$@"
    ;;
  help)
    if [ "$1" = "-v" ]; then
      howto
    else
      usage
    fi
    exit 0
    ;;
  *)
    usage
    exit 1
    ;;
esac
