PyORAm
[iotcloud.git] / PyORAM / src / pyoram / tests / test_examples.py
1 import os
2 import glob
3 import sys
4 import unittest2
5
6 thisfile = os.path.abspath(__file__)
7 thisdir = os.path.dirname(thisfile)
8 topdir = os.path.dirname(
9     os.path.dirname(
10         os.path.dirname(thisdir)))
11 exdir = os.path.join(topdir, 'examples')
12 examples = glob.glob(os.path.join(exdir,"*.py"))
13
14 assert os.path.exists(exdir)
15 assert thisfile not in examples
16
17 tdict = {}
18 for fname in examples:
19     basename = os.path.basename(fname)
20     assert basename.endswith('.py')
21     assert len(basename) >= 3
22     basename = basename[:-3]
23     tname = 'test_'+basename
24     tdict[tname] = fname, basename
25
26 assert len(tdict) == len(examples)
27
28 assert 'test_encrypted_storage_s3' in tdict
29 assert 'test_path_oram_s3' in tdict
30 if 'PYORAM_AWS_TEST_BUCKET' not in os.environ:
31     del tdict['test_encrypted_storage_s3']
32     del tdict['test_path_oram_s3']
33 assert 'test_encrypted_storage_sftp' in tdict
34 assert 'test_path_oram_sftp' in tdict
35 assert 'test_path_oram_sftp_setup' in tdict
36 assert 'test_path_oram_sftp_test' in tdict
37 if 'PYORAM_SSH_TEST_HOST' not in os.environ:
38     del tdict['test_encrypted_storage_sftp']
39     del tdict['test_path_oram_sftp']
40     del tdict['test_path_oram_sftp_setup']
41     del tdict['test_path_oram_sftp_test']
42
43 def _execute_example(example_name):
44     filename, basename = tdict[example_name]
45     assert os.path.exists(filename)
46     try:
47         sys.path.insert(0, exdir)
48         m = __import__(basename)
49         m.main()
50     finally:
51         sys.path.remove(exdir)
52
53 # this is recognized by nosetests as
54 # a dynamic test generator
55 def test_generator():
56     for example_name in sorted(tdict):
57         yield _execute_example, example_name
58
59 if __name__ == "__main__":
60     for tfunc, tname in test_generator():              # pragma: no cover
61         tfunc(tname)                                   # pragma: no cover