Statistics
| Branch: | Tag: | Revision:

root / test / docs_unittest.py @ 36bf7973

History | View | Annotate | Download (4.1 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2009 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Script for unittesting documentation"""
23

    
24
import unittest
25
import re
26

    
27
from ganeti import _autoconf
28
from ganeti import utils
29
from ganeti import cmdlib
30
from ganeti.rapi import connector
31

    
32
import testutils
33

    
34

    
35
class TestDocs(unittest.TestCase):
36
  """Documentation tests"""
37

    
38
  @staticmethod
39
  def _ReadDocFile(filename):
40
    return utils.ReadFile("%s/doc/%s" %
41
                          (testutils.GetSourceDir(), filename))
42

    
43
  def testHookDocs(self):
44
    """Check whether all hooks are documented.
45

46
    """
47
    hooksdoc = self._ReadDocFile("hooks.rst")
48

    
49
    for name in dir(cmdlib):
50
      obj = getattr(cmdlib, name)
51

    
52
      if (isinstance(obj, type) and
53
          issubclass(obj, cmdlib.LogicalUnit) and
54
          hasattr(obj, "HPATH")):
55
        self._CheckHook(name, obj, hooksdoc)
56

    
57
  def _CheckHook(self, name, lucls, hooksdoc):
58
    if lucls.HTYPE is None:
59
      return
60

    
61
    # TODO: Improve this test (e.g. find hooks documented but no longer
62
    # existing)
63

    
64
    pattern = r"^:directory:\s*%s\s*$" % re.escape(lucls.HPATH)
65

    
66
    self.assert_(re.findall(pattern, hooksdoc, re.M),
67
                 msg=("Missing documentation for hook %s/%s" %
68
                      (lucls.HTYPE, lucls.HPATH)))
69

    
70

    
71
  def testRapiDocs(self):
72
    """Check whether all RAPI resources are documented.
73

74
    """
75
    rapidoc = self._ReadDocFile("rapi.rst")
76

    
77
    node_name = "[node_name]"
78
    instance_name = "[instance_name]"
79
    job_id = "[job_id]"
80

    
81
    resources = connector.GetHandlers(re.escape(node_name),
82
                                      re.escape(instance_name),
83
                                      re.escape(job_id))
84

    
85
    titles = []
86

    
87
    prevline = None
88
    for line in rapidoc.splitlines():
89
      if re.match(r"^\++$", line):
90
        titles.append(prevline)
91

    
92
      prevline = line
93

    
94
    undocumented = []
95

    
96
    for key, handler in resources.iteritems():
97
      # Regex objects
98
      if hasattr(key, "match"):
99
        found = False
100
        for title in titles:
101
          if (title.startswith("``") and
102
              title.endswith("``") and
103
              key.match(title[2:-2])):
104
            found = True
105
            break
106

    
107
        if not found:
108
          # TODO: Find better way of identifying resource
109
          undocumented.append(str(handler))
110

    
111
      elif ("``%s``" % key) not in titles:
112
        undocumented.append(key)
113

    
114
    self.failIf(undocumented,
115
                msg=("Missing RAPI resource documentation for %s" %
116
                     utils.CommaJoin(undocumented)))
117

    
118

    
119
class TestManpages(unittest.TestCase):
120
  """Manpage tests"""
121

    
122
  @staticmethod
123
  def _ReadManFile(name):
124
    return utils.ReadFile("%s/man/%s.sgml" %
125
                          (testutils.GetSourceDir(), name))
126

    
127
  @staticmethod
128
  def _LoadScript(name):
129
    return utils.LoadModule("scripts/%s" % name)
130

    
131
  def test(self):
132
    for script in _autoconf.GNT_SCRIPTS:
133
      self._CheckManpage(script,
134
                         self._ReadManFile(script),
135
                         self._LoadScript(script).commands.keys())
136

    
137
  def _CheckManpage(self, script, mantext, commands):
138
    missing = []
139

    
140
    for cmd in commands:
141
      pattern = "<cmdsynopsis>\s*<command>%s</command>" % re.escape(cmd)
142
      if not re.findall(pattern, mantext, re.S):
143
        missing.append(cmd)
144

    
145
    self.failIf(missing,
146
                msg=("Manpage for '%s' missing documentation for %s" %
147
                     (script, utils.CommaJoin(missing))))
148

    
149

    
150
if __name__ == "__main__":
151
  unittest.main()