Statistics
| Branch: | Tag: | Revision:

root / test / docs_unittest.py @ bf968b7f

History | View | Annotate | Download (3.2 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 utils
28
from ganeti import cmdlib
29
from ganeti.rapi import connector
30

    
31
import testutils
32

    
33

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

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

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

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

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

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

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

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

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

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

    
69

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

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

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

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

    
84
    titles = []
85

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

    
91
      prevline = line
92

    
93
    undocumented = []
94

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

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

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

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

    
117

    
118
if __name__ == "__main__":
119
  unittest.main()