-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mark Fiers
committed
Sep 13, 2012
1 parent
7d539f7
commit 55519fd
Showing
1 changed file
with
41 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,52 @@ | ||
import os | ||
import shutil | ||
import random | ||
import tempfile | ||
import subprocess | ||
import unittest2 as unittest | ||
import unittest | ||
import subprocess as sp | ||
|
||
def load_tests(loader, tests, ignore): | ||
tests.addTests(scriptTestSuite()) | ||
return tests | ||
|
||
class scriptTester(unittest.TestCase): | ||
|
||
def setScript(self, name, script): | ||
self.name = name | ||
self.script = script | ||
class TestScript(unittest.TestCase): | ||
|
||
def __init__(self, script, *args, **kwargs): | ||
self.script_name = script | ||
super(TestScript, self).__init__(*args, **kwargs) | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def runTest(self): | ||
if not hasattr(self, 'script'): | ||
# only test if a job is set | ||
# hence, only if it is called by RuffSuite() | ||
return | ||
d = tempfile.mkdtemp() | ||
shutil.copy(self.script, d) | ||
|
||
scriptBase = os.path.basename(self.script) | ||
P = subprocess.Popen( | ||
[os.path.join(d, scriptBase)], cwd = d, | ||
stdout = subprocess.PIPE, | ||
stderr = subprocess.PIPE) | ||
out, err = P.communicate() | ||
P = sp.Popen([self.script_name], shell=True, | ||
stdout=sp.PIPE, stderr=sp.PIPE) | ||
o, e = P.communicate() | ||
rc = P.returncode | ||
if not rc == 0: | ||
print "error executing %s" % self.name | ||
if err.strip(): | ||
print "stderr" + '-' * 70 | ||
print err | ||
if out.strip(): | ||
print "stdout" + '-' * 70 | ||
print out | ||
msg = "" | ||
if o: | ||
msg += "STDOUT:\n" | ||
msg += o.decode('utf-8') | ||
msg += "\n" | ||
if e: | ||
msg += "STDERR:\n" | ||
msg += e.decode('utf-8') | ||
msg += "\n" | ||
|
||
self.assertTrue(rc == 0, msg=msg) | ||
|
||
|
||
shutil.rmtree(d) | ||
self.assertEqual(rc, 0) | ||
|
||
|
||
def script_test_suite(subdir): | ||
test_suite = unittest.TestSuite() | ||
for script_name in os.listdir(subdir): | ||
if script_name[-1] == '~': | ||
continue | ||
script = os.path.join(subdir, script_name) | ||
if not os.path.isfile(script): | ||
continue | ||
test_suite.addTest(TestScript(script)) | ||
return test_suite | ||
|
||
def scriptTestSuite(): | ||
suite = unittest.TestSuite() | ||
scriptDir = os.path.join( | ||
os.path.dirname(os.path.abspath(__file__)), | ||
'scripts') | ||
|
||
for path, dirs, files in os.walk(scriptDir): | ||
for f in files: | ||
if f[-3:] != '.sh': continue | ||
scriptPath = os.path.join(path, f) | ||
scriptName = scriptPath.replace(scriptDir, '') | ||
tester = scriptTester() | ||
tester.setScript(scriptName, scriptPath) | ||
suite.addTest(tester) | ||
return suite | ||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
scriptdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), | ||
'scripts') | ||
for subdirname in os.listdir(scriptdir): | ||
subdir = os.path.join(scriptdir, subdirname) | ||
suite = script_test_suite(subdir) | ||
result = unittest.TextTestRunner(verbosity=2).run(suite) |