Add example script for using RAPI test utilities
authorMichael Hanselmann <hansmi@google.com>
Tue, 10 Apr 2012 21:09:57 +0000 (23:09 +0200)
committerMichael Hanselmann <hansmi@google.com>
Thu, 26 Apr 2012 19:42:49 +0000 (21:42 +0200)
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
autotools/run-in-tempdir
doc/examples/rapi_testutils.py [new file with mode: 0755]

index f769518..31cd8fa 100644 (file)
@@ -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 \
index 6e0b758..e656ffe 100755 (executable)
@@ -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 (executable)
index 0000000..8a4ceab
--- /dev/null
@@ -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()