From 3ade0e0e30d19924f3d5dff21811b1a938c871f9 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Tue, 10 Apr 2012 23:09:57 +0200
Subject: [PATCH] Add example script for using RAPI test utilities
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This script shows a few examples on how to use the RAPI input test
client. It is also run at β€œmake check” time to ensure it's not
completely broken.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: RenΓ© Nussbaumer <rn@google.com>
---
 Makefile.am                    |  1 +
 autotools/run-in-tempdir       |  4 ++
 doc/examples/rapi_testutils.py | 70 ++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+)
 create mode 100755 doc/examples/rapi_testutils.py

diff --git a/Makefile.am b/Makefile.am
index f769518b3..31cd8fa5c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -786,6 +786,7 @@ TEST_FILES = \
 	test/htools-text-backend.test
 
 python_tests = \
+	doc/examples/rapi_testutils.py \
 	test/ganeti.asyncnotifier_unittest.py \
 	test/ganeti.backend_unittest.py \
 	test/ganeti.bdev_unittest.py \
diff --git a/autotools/run-in-tempdir b/autotools/run-in-tempdir
index 6e0b758cf..e656ffe73 100755
--- a/autotools/run-in-tempdir
+++ b/autotools/run-in-tempdir
@@ -8,7 +8,11 @@ set -e
 tmpdir=$(mktemp -d -t gntbuild.XXXXXXXX)
 trap "rm -rf $tmpdir" EXIT
 
+mkdir $tmpdir/doc
+
 cp -r autotools daemons scripts lib tools test $tmpdir
+cp -r doc/examples $tmpdir/doc
+
 mv $tmpdir/lib $tmpdir/ganeti
 ln -T -s $tmpdir/ganeti $tmpdir/lib
 mkdir -p $tmpdir/htools
diff --git a/doc/examples/rapi_testutils.py b/doc/examples/rapi_testutils.py
new file mode 100755
index 000000000..8a4ceab4b
--- /dev/null
+++ b/doc/examples/rapi_testutils.py
@@ -0,0 +1,70 @@
+#!/usr/bin/python
+#
+
+# Copyright (C) 2012 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.
+
+
+"""Example for using L{ganeti.rapi.testutils}"""
+
+import logging
+
+from ganeti import rapi
+
+import ganeti.rapi.testutils
+
+
+def main():
+  # Disable log output
+  logging.getLogger("").setLevel(logging.CRITICAL)
+
+  cl = rapi.testutils.InputTestClient()
+
+  print "Testing features ..."
+  assert isinstance(cl.GetFeatures(), list)
+
+  print "Testing node evacuation ..."
+  result = cl.EvacuateNode("inst1.example.com",
+                           mode=rapi.client.NODE_EVAC_PRI)
+  assert result is NotImplemented
+
+  print "Testing listing instances ..."
+  for bulk in [False, True]:
+    result = cl.GetInstances(bulk=bulk)
+    assert result is NotImplemented
+
+  print "Testing renaming instance ..."
+  result = cl.RenameInstance("inst1.example.com", "inst2.example.com")
+  assert result is NotImplemented
+
+  print "Testing renaming instance with error ..."
+  try:
+    # This test deliberately uses an invalid value for the boolean parameter
+    # "ip_check"
+    result = cl.RenameInstance("inst1.example.com", "inst2.example.com",
+                               ip_check=["non-boolean", "value"])
+  except rapi.testutils.VerificationError:
+    # Verification failed as expected
+    pass
+  else:
+    raise Exception("This test should have failed")
+
+  print "Success!"
+
+
+if __name__ == "__main__":
+  main()
-- 
GitLab