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.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100755 systest.py (limited to 'systest.py') 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