summaryrefslogtreecommitdiff
path: root/systest
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-06-15 16:19:12 +0100
committerLars Wirzenius <liw@liw.fi>2011-06-15 16:19:12 +0100
commit7e7fb4ccb29d831db6dc3c81ff41fdeacac17597 (patch)
treef9fac42fd9ed7827263ef44640499628e51a3cfc /systest
parent540300e151752112aae050300bc92b6b32631c2a (diff)
downloadsystest-7e7fb4ccb29d831db6dc3c81ff41fdeacac17597.tar.gz
Move test cases from application class to new test case base class.
Diffstat (limited to 'systest')
-rwxr-xr-xsystest125
1 files changed, 63 insertions, 62 deletions
diff --git a/systest b/systest
index c48d2d4..7142d98 100755
--- a/systest
+++ b/systest
@@ -1,8 +1,10 @@
#!/usr/bin/python
import cliapp
+import logging
import re
import subprocess
+import unittest
class AssertionFailure(cliapp.AppException):
@@ -14,82 +16,50 @@ class AssertionFailure(cliapp.AppException):
return self._str
-class SystemTest(cliapp.Application):
+class TestCase(unittest.TestCase):
- def add_settings(self):
- self.settings.boolean(['verbose', 'v'],
- 'print names of tests when run')
- self.settings.string(['target'], 'target domain name or IP address')
- self.settings.string(['user'], 'user on target')
- self.settings.string(['user-password'], 'password for target user')
- self.settings.boolean(['list-tests'], 'list known tests')
-
- def process_args(self, args):
- if self.settings['list-tests']:
- for testname in self.list_tests():
- self.output.write('%s\n' % testname)
- elif args:
- self.execute_tests(args)
- else:
- self.execute_tests(self.list_tests())
-
- def execute_tests(self, testnames):
- for i, testname in enumerate(testnames):
- methodname = self.mangle(testname)
- if hasattr(self, methodname):
- if self.settings['verbose']:
- self.output.write('test %d/%d: %s\n' %
- (i+1, len(testnames), testname))
- getattr(self, methodname)()
- else:
- raise cliapp.AppException('unknown test: %s' % testname)
-
- def list_tests(self):
- return [self.unmangle(x) for x in dir(self) if x.startswith('test')]
-
- def mangle(self, testname):
- return 'test_' + testname.replace('-', '_')
+ '''Base class for system tests.
- def unmangle(self, methodname):
- assert methodname.startswith('test_')
- methodname = methodname[len('test_'):]
- return methodname.replace('_', '-')
+ This extends ``unittest.TestCase`` with some things that are
+ relevant to system tests.
+
+ '''
- def hostcmd(self, argv, stdin=None):
+ def runcmd(self, argv, stdin='', *args, **kwargs):
p = subprocess.Popen(argv,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate(stdin)
- if p.returncode:
- raise cliapp.AppException('host command failed: %s\n%s' %
- (' '.join(argv), err))
+ return p.returncode, out, err
+
+ def hostcmd(self, argv, *args, **kwargs):
+ '''Run external command on test host.'''
+ returncode, out, err = self.runcmd(argv, *args, **kwargs)
+ if returncode:
+ msg = 'host command failed: %s\n%s' % (' '.join(argv), err)
+ logging.error(msg)
+ raise cliapp.AppException(msg)
return out
def targetcmd(self, argv, *args, **kwargs):
- return self.hostcmd(['ssh', '-l', self.settings['user'],
- self.settings['target']] + argv,
- *args, **kwargs)
-
- def assert_(self, cond, automsg=None, msg=None):
- if not cond:
- output = 'Assertion failed:'
- if automsg is None:
- output += ' ' + repr(cond)
- else:
- output += ' ' + automsg
- if msg is not None:
- output += ' ' + msg
- raise AssertionFailure(output)
-
- def assertEqual(self, v1, v2, msg=None):
- self.assert_(v1 == v2, automsg='%s != %s' % (repr(v1), repr(v2)),
- msg=msg)
+ '''Run command on test target.'''
+ full_argv = (['ssh', '-l', self.settings['user'],
+ self.settings['target']] +
+ argv)
+ returncode, out, err = self.runcmd(full_argv, *args, **kwargs)
+ if returncode:
+ msg = 'target command failed: %s\n%s' % (' '.join(argv), err)
+ logging.error(msg)
+ raise cliapp.AppException(msg)
+ return out
def assertMatches(self, pat, text, msg=None):
self.assert_(re.match(pat, text),
- automsg='pattern %s does not match %s' % (pat, text),
- msg=msg)
+ msg=('pattern %s does not match %s' % (pat, text)) or msg)
+
+
+class DebianBaseTests(TestCase):
def test_only_ssh_port(self):
out = self.hostcmd(['nmap', self.settings['target']])
@@ -123,5 +93,36 @@ class SystemTest(cliapp.Application):
# self.assertMatches(r'^uid=0\(root\)', out)
+
+class SystemTest(cliapp.Application):
+
+ def add_settings(self):
+ self.settings.boolean(['verbose', 'v'],
+ 'print names of tests when run')
+ self.settings.string(['target'], 'target domain name or IP address')
+ self.settings.string(['user'], 'user on target')
+ self.settings.string(['user-password'], 'password for target user')
+
+ def process_args(self, args):
+ loader = unittest.defaultTestLoader
+ loader.suiteClass = self.create_suite
+ suite = loader.loadTestsFromTestCase(DebianBaseTests)
+ unittest.TextTestRunner().run(suite)
+
+ def create_suite(self, tests):
+ for test in tests:
+ test.settings = self.settings
+ suite = unittest.TestSuite(tests)
+ return suite
+
+ def mangle(self, testname):
+ return 'test_' + testname.replace('-', '_')
+
+ def unmangle(self, methodname):
+ assert methodname.startswith('test_')
+ methodname = methodname[len('test_'):]
+ return methodname.replace('_', '-')
+
+
if __name__ == '__main__':
SystemTest().run()