#!/bin/bash
#

# Copyright (C) 2010 Google Inc.
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

set -e

daemon_util=daemons/daemon-util

err() {
  echo "$@"
  echo 'Aborting'
  exit 1
}

$daemon_util >/dev/null 2>&1 &&
  err "daemon-util succeeded without command"

$daemon_util this-is-an-unimplemented-command >/dev/null 2>&1 &&
  err "daemon-util accepted unimplemented command"

$daemon_util list_start_daemons >/dev/null 2>&1 &&
  err "daemon-util accepted command with underscores"

$daemon_util check-exitcode 0 ||
  err "check-exitcode 0 failed"

for i in 1 2 3 4 20 25 33; do
  $daemon_util check-exitcode $i >/dev/null 2>&1 && rc=0 || rc=$?
  test "$rc" == 1 || err "check-exitcode $i didn't return 1"
done

$daemon_util check-exitcode 11 >/dev/null 2>&1 ||
  err "check-exitcode 11 (not master) didn't return 0"

tmp=$(echo $($daemon_util list-start-daemons))
test "$tmp" == "$(echo ganeti-{noded,masterd,rapi,confd})" ||
  err "list-start-daemons didn't return correct list of daemons"

tmp=$(echo $($daemon_util list-stop-daemons))
test "$tmp" == "$(echo ganeti-{confd,rapi,masterd,noded})" ||
  err "list-stop-daemons didn't return correct list of daemons"

$daemon_util is-daemon-name >/dev/null 2>&1 &&
  err "is-daemon-name didn't require daemon name"

for i in '' '.' '..' '-' 'not-a-daemon'; do
  $daemon_util is-daemon-name "$i" >/dev/null 2>&1 &&
    err "is-daemon-name thinks '$i' is a daemon name"
done

for i in ganeti-{confd,rapi,masterd,noded}; do
  $daemon_util is-daemon-name $i >/dev/null 2>&1 ||
    err "is-daemon-name doesn't think '$i' is a daemon name"
done