From b6e1aaf11332a1df750fcd61e7f1a9c6244a4fb6 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 28 Jun 2011 20:12:51 +0100 Subject: Move systest base class to its own module. --- systest | 54 ++-------------------------------------------- systest.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 52 deletions(-) create mode 100755 systest.py diff --git a/systest b/systest index 7142d98..bf650b5 100755 --- a/systest +++ b/systest @@ -6,60 +6,10 @@ import re import subprocess import unittest +import systest -class AssertionFailure(cliapp.AppException): - def __init__(self, msg): - self._str = msg - - def __str__(self): - return self._str - - -class TestCase(unittest.TestCase): - - '''Base class for system tests. - - This extends ``unittest.TestCase`` with some things that are - relevant to system tests. - - ''' - - 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) - 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): - '''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), - msg=('pattern %s does not match %s' % (pat, text)) or msg) - - -class DebianBaseTests(TestCase): +class DebianBaseTests(systest.TestCase): def test_only_ssh_port(self): out = self.hostcmd(['nmap', self.settings['target']]) diff --git a/systest.py b/systest.py new file mode 100755 index 0000000..028480a --- /dev/null +++ b/systest.py @@ -0,0 +1,72 @@ +# Copyright 2011 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +'''System test classes. + +This is like ``unittest``, except for testing running systems. + +An extension is that the test runner will set up a member, ``settings``, +which acts as a dictionary, and has run-time settings for things like +the hostname of the target system. + +''' + + +import re +import subprocess +import unittest + + +class TestCase(unittest.TestCase): + + '''Base class for system tests. + + This extends ``unittest.TestCase`` with some things that are + relevant to system tests. + + ''' + + 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) + 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) + raise AssertionError(msg) + return out + + def targetcmd(self, argv, *args, **kwargs): + '''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) + raise AssertionError(msg) + return out + + def assertMatches(self, pat, text, msg=None): + self.assert_(re.match(pat, text), + msg=('pattern %s does not match %s' % (pat, text)) or msg) + -- cgit v1.2.1