Debian Codegraph @

tests/test_requests.py

raw
1 #!/usr/bin/python
2
3 from utils import *
4
5 class RequestsTestMeta(type):
6 def __new__(cls, name, parents, attrs):
7 root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'xml')
8 for machine in os.listdir(root):
9 mdir = os.path.join(root, machine)
10 if not os.path.isdir(mdir):
11 continue
12
13 files = os.listdir(mdir)
14 for argsfile in files:
15 if not argsfile.endswith('.args'):
16 continue
17 method = argsfile[:argsfile.find('-')]
18 reqfile = argsfile.replace('.args', '.request')
19 reqtmpfile = argsfile.replace('.args', '.request.tmp')
20 argspath = os.path.join(mdir, argsfile)
21 reqpath = os.path.join(mdir, reqfile)
22 reqtmppath = os.path.join(mdir, reqtmpfile)
23 fname = 'test_%s_%s' % (machine, argsfile[:-5])
24 fname = re.sub('[^a-zA-Z_0-9]', '_', fname).lower()
25 attrs[fname] = eval("lambda self: self._test_request('%s', '%s', '%s', '%s', '%s')" %
26 (machine, method, argspath, reqpath, reqtmppath))
27 return super(RequestsTestMeta, cls).__new__(cls, name, parents, attrs)
28
29 class RequestsTest(unittest.TestCase):
30 __metaclass__ = RequestsTestMeta
31 maxDiff = None
32
33 def _test_request(self, machine, method, argsfile, reqfile, reqtmpfile):
34 ilo = hpilo.Ilo('nonexistent-machine','Administrator','TestPassword')
35 if 'ilo3' in machine.lower() or 'ilo4' in machine.lower():
36 ilo.protocol = hpilo.ILO_HTTP
37 else:
38 ilo.protocol = hpilo.ILO_RAW
39 if os.path.exists(reqtmpfile):
40 os.unlink(reqtmpfile)
41 ilo.save_request = reqtmpfile
42 with open(argsfile) as fd:
43 args, kwargs = eval(fd.read()), {}
44 if isinstance(args, dict):
45 args, kwargs = [], args
46
47 if not os.path.exists(reqfile):
48 self.assertRaises(ValueError, getattr(ilo, method), *args, **kwargs)
49 return
50
51 getattr(ilo, method)(*args, **kwargs)
52 with open(reqfile) as fd:
53 old = fd.read()
54 with open(reqtmpfile) as fd:
55 new = fd.read()
56 self.assertMultiLineEqual(old, new)
57 os.unlink(reqtmpfile)
58
59 if __name__ == '__main__':
60 unittest.main()