Debian Codegraph @

tests/test_responses.py

raw
1 #!/usr/bin/python
2
3 from utils import *
4 import pprint
5
6 class ResponsesTestMeta(type):
7 def __new__(cls, name, parents, attrs):
8 root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'xml')
9 for machine in os.listdir(root):
10 mdir = os.path.join(root, machine)
11 if not os.path.isdir(mdir):
12 continue
13
14 files = os.listdir(mdir)
15 for rawfile in files:
16 if not rawfile.endswith('.raw'):
17 continue
18 method = rawfile.replace('.raw', '')
19 parsedfile = rawfile.replace('.raw', '.parsed')
20 rawpath = os.path.join(mdir, rawfile)
21 parsedpath = os.path.join(mdir, parsedfile)
22 fname = 'test_%s_%s' % (machine, method)
23 fname = re.sub('[^a-zA-Z_0-9]', '_', fname).lower()
24 attrs[fname] = eval("lambda self: self._test_response('%s', '%s', '%s', '%s')" %
25 (machine, method, rawpath, parsedpath))
26 return super(ResponsesTestMeta, cls).__new__(cls, name, parents, attrs)
27
28 class ResponsesTest(unittest.TestCase):
29 __metaclass__ = ResponsesTestMeta
30 maxDiff = None
31 method_args = {
32 'get_user': ['Administrator'],
33 'get_federation_group': ['slartibartfast'],
34 }
35 def _test_response(self, machine, method, rawfile, parsedfile):
36 ilo = hpilo.Ilo('nonexistent-machine','Administrator','TestPassword')
37 if 'ilo3' in machine.lower() or 'ilo4' in machine.lower():
38 ilo.protocol = hpilo.ILO_HTTP
39 else:
40 ilo.protocol = hpilo.ILO_RAW
41 ilo.read_response = rawfile
42
43 args = self.method_args.get(method, [])
44 if not os.path.exists(parsedfile):
45 self.assertRaises(hpilo.IloError, getattr(ilo, method), *args)
46 return
47 response = getattr(ilo, method)(*args)
48 new = pprint.pformat(response) + '\n'
49 with open(parsedfile) as fd:
50 old = fd.read()
51 self.assertMultiLineEqual(old, new)
52
53 if __name__ == '__main__':
54 unittest.main()