From 571b34f87a0559e53023cca553de387b94777f23 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 11 Mar 2010 18:52:59 +0100
Subject: [PATCH] Add unittest for daemon-util

This test doesn't cover everything, but it's better than nothing.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 Makefile.am                    |  4 ++
 autotools/run-in-tempdir       |  2 +-
 test/daemon-util_unittest.bash | 70 ++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100755 test/daemon-util_unittest.bash

diff --git a/Makefile.am b/Makefile.am
index bf97265ba..e48f1a62b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -342,6 +342,7 @@ python_tests = \
 	test/tempfile_fork_unittest.py
 
 dist_TESTS = \
+	test/daemon-util_unittest.bash \
 	$(python_tests)
 
 nodist_TESTS =
@@ -369,6 +370,7 @@ all_python_code = \
 
 srclink_files = \
 	man/footer.sgml \
+	test/daemon-util_unittest.bash \
 	$(all_python_code)
 
 check_python_code = \
@@ -381,6 +383,8 @@ lint_python_code = \
 	$(dist_tools_SCRIPTS) \
 	$(BUILD_BASH_COMPLETION)
 
+test/daemon-util_unittest.bash: daemons/daemon-util
+
 devel/upload: devel/upload.in $(REPLACE_VARS_SED)
 	sed -f $(REPLACE_VARS_SED) < $< > $@
 	chmod u+x $@
diff --git a/autotools/run-in-tempdir b/autotools/run-in-tempdir
index 4c87dfc63..9cab50fa1 100755
--- a/autotools/run-in-tempdir
+++ b/autotools/run-in-tempdir
@@ -5,7 +5,7 @@ set -e
 tmpdir=$(mktemp -d -t gntbuild.XXXXXXXX)
 trap "rm -rf $tmpdir" EXIT
 
-cp -r scripts lib tools test $tmpdir
+cp -r daemons scripts lib tools test $tmpdir
 mv $tmpdir/lib $tmpdir/ganeti
 
 cd $tmpdir && "$@"
diff --git a/test/daemon-util_unittest.bash b/test/daemon-util_unittest.bash
new file mode 100755
index 000000000..37e69c870
--- /dev/null
+++ b/test/daemon-util_unittest.bash
@@ -0,0 +1,70 @@
+#!/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
-- 
GitLab