Merge branch 'devel-2.1'
[ganeti-local] / test / docs_unittest.py
index 1021c35..95d81a8 100755 (executable)
 import unittest
 import re
 
+from ganeti import _autoconf
 from ganeti import utils
 from ganeti import cmdlib
+from ganeti import build
+from ganeti.rapi import connector
 
 import testutils
 
@@ -66,5 +69,84 @@ class TestDocs(unittest.TestCase):
                       (lucls.HTYPE, lucls.HPATH)))
 
 
+  def testRapiDocs(self):
+    """Check whether all RAPI resources are documented.
+
+    """
+    rapidoc = self._ReadDocFile("rapi.rst")
+
+    node_name = "[node_name]"
+    instance_name = "[instance_name]"
+    job_id = "[job_id]"
+
+    resources = connector.GetHandlers(re.escape(node_name),
+                                      re.escape(instance_name),
+                                      re.escape(job_id))
+
+    titles = []
+
+    prevline = None
+    for line in rapidoc.splitlines():
+      if re.match(r"^\++$", line):
+        titles.append(prevline)
+
+      prevline = line
+
+    undocumented = []
+
+    for key, handler in resources.iteritems():
+      # Regex objects
+      if hasattr(key, "match"):
+        found = False
+        for title in titles:
+          if (title.startswith("``") and
+              title.endswith("``") and
+              key.match(title[2:-2])):
+            found = True
+            break
+
+        if not found:
+          # TODO: Find better way of identifying resource
+          undocumented.append(str(handler))
+
+      elif ("``%s``" % key) not in titles:
+        undocumented.append(key)
+
+    self.failIf(undocumented,
+                msg=("Missing RAPI resource documentation for %s" %
+                     utils.CommaJoin(undocumented)))
+
+
+class TestManpages(unittest.TestCase):
+  """Manpage tests"""
+
+  @staticmethod
+  def _ReadManFile(name):
+    return utils.ReadFile("%s/man/%s.sgml" %
+                          (testutils.GetSourceDir(), name))
+
+  @staticmethod
+  def _LoadScript(name):
+    return build.LoadModule("scripts/%s" % name)
+
+  def test(self):
+    for script in _autoconf.GNT_SCRIPTS:
+      self._CheckManpage(script,
+                         self._ReadManFile(script),
+                         self._LoadScript(script).commands.keys())
+
+  def _CheckManpage(self, script, mantext, commands):
+    missing = []
+
+    for cmd in commands:
+      pattern = "<cmdsynopsis>\s*<command>%s</command>" % re.escape(cmd)
+      if not re.findall(pattern, mantext, re.S):
+        missing.append(cmd)
+
+    self.failIf(missing,
+                msg=("Manpage for '%s' missing documentation for %s" %
+                     (script, utils.CommaJoin(missing))))
+
+
 if __name__ == "__main__":
-  unittest.main()
+  testutils.GanetiTestProgram()