From 3f991867c03b96d5f687f194664c4cb419f8cf12 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 3 Sep 2009 16:35:09 +0200
Subject: [PATCH] Add simple unittest for hooks documentation

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Luca Bigliardi <shammash@google.com>
---
 Makefile.am           |  3 +-
 test/docs_unittest.py | 70 +++++++++++++++++++++++++++++++++++++++++++
 test/testutils.py     |  7 +++--
 3 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100755 test/docs_unittest.py

diff --git a/Makefile.am b/Makefile.am
index 97c1463ef..0beee686c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -264,7 +264,8 @@ dist_TESTS = \
 	test/ganeti.serializer_unittest.py \
 	test/ganeti.ssh_unittest.py \
 	test/ganeti.utils_unittest.py \
-	test/ganeti.workerpool_unittest.py
+	test/ganeti.workerpool_unittest.py \
+	test/docs_unittest.py
 
 nodist_TESTS =
 
diff --git a/test/docs_unittest.py b/test/docs_unittest.py
new file mode 100755
index 000000000..1021c3543
--- /dev/null
+++ b/test/docs_unittest.py
@@ -0,0 +1,70 @@
+#!/usr/bin/python
+#
+
+# Copyright (C) 2009 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.
+
+
+"""Script for unittesting documentation"""
+
+import unittest
+import re
+
+from ganeti import utils
+from ganeti import cmdlib
+
+import testutils
+
+
+class TestDocs(unittest.TestCase):
+  """Documentation tests"""
+
+  @staticmethod
+  def _ReadDocFile(filename):
+    return utils.ReadFile("%s/doc/%s" %
+                          (testutils.GetSourceDir(), filename))
+
+  def testHookDocs(self):
+    """Check whether all hooks are documented.
+
+    """
+    hooksdoc = self._ReadDocFile("hooks.rst")
+
+    for name in dir(cmdlib):
+      obj = getattr(cmdlib, name)
+
+      if (isinstance(obj, type) and
+          issubclass(obj, cmdlib.LogicalUnit) and
+          hasattr(obj, "HPATH")):
+        self._CheckHook(name, obj, hooksdoc)
+
+  def _CheckHook(self, name, lucls, hooksdoc):
+    if lucls.HTYPE is None:
+      return
+
+    # TODO: Improve this test (e.g. find hooks documented but no longer
+    # existing)
+
+    pattern = r"^:directory:\s*%s\s*$" % re.escape(lucls.HPATH)
+
+    self.assert_(re.findall(pattern, hooksdoc, re.M),
+                 msg=("Missing documentation for hook %s/%s" %
+                      (lucls.HTYPE, lucls.HPATH)))
+
+
+if __name__ == "__main__":
+  unittest.main()
diff --git a/test/testutils.py b/test/testutils.py
index b4d286e15..b719ce239 100644
--- a/test/testutils.py
+++ b/test/testutils.py
@@ -29,6 +29,10 @@ import unittest
 from ganeti import utils
 
 
+def GetSourceDir():
+  return os.environ.get("TOP_SRCDIR", ".")
+
+
 class GanetiTestCase(unittest.TestCase):
   """Helper class for unittesting.
 
@@ -83,8 +87,7 @@ class GanetiTestCase(unittest.TestCase):
         be used in 'make distcheck' rules
 
     """
-    prefix = os.environ.get("TOP_SRCDIR", ".")
-    return "%s/test/data/%s" % (prefix, name)
+    return "%s/test/data/%s" % (GetSourceDir(), name)
 
   @classmethod
   def _ReadTestData(cls, name):
-- 
GitLab