summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsystest2
-rwxr-xr-xsystest.py28
2 files changed, 25 insertions, 5 deletions
diff --git a/systest b/systest
index 1d5d673..577adc1 100755
--- a/systest
+++ b/systest
@@ -32,6 +32,8 @@ class SystemTest(cliapp.Application):
self.settings.integer(['ssh-port'], 'target ssh port', default=22)
self.settings.string(['user'], 'user on target')
self.settings.string(['user-password'], 'password for target user')
+ self.settings.string(['user-ssh-private-key'],
+ 'user\'s private key for logging in')
def process_args(self, args):
loader = unittest.defaultTestLoader
diff --git a/systest.py b/systest.py
index bdcb3b6..4bfbcfb 100755
--- a/systest.py
+++ b/systest.py
@@ -26,8 +26,10 @@ the hostname of the target system.
import logging
+import os
import re
import subprocess
+import tempfile
import unittest
@@ -59,16 +61,32 @@ class TestCase(unittest.TestCase):
def targetcmd(self, argv, *args, **kwargs):
'''Run command on test target.'''
- full_argv = (['ssh', '-l', self.settings['user'],
- '-p', str(self.settings['ssh-port']),
- self.settings['target']] +
- argv)
- returncode, out, err = self.runcmd(full_argv, *args, **kwargs)
+
+ self.setup_known_hosts()
+
+ prefix = ['ssh',
+ '-l', self.settings['user'],
+ '-p', str(self.settings['ssh-port']),
+ '-o', 'UserKnownHostsFile %s' % self.known_hosts,
+ ]
+ if self.settings['user-ssh-private-key']:
+ prefix += ['-i', self.settings['user-ssh-private-key']]
+ prefix += [self.settings['target'], '--']
+
+ returncode, out, err = self.runcmd(prefix + argv, *args, **kwargs)
if returncode:
msg = 'target command failed: %s\n%s' % (' '.join(argv), err)
raise AssertionError(msg)
return out
+ def setup_known_hosts(self):
+ '''Setup a temporary known hosts file, and add target's key there.'''
+
+ fd, self.known_hosts = tempfile.mkstemp()
+ out = self.hostcmd(['ssh-keyscan', self.settings['target']])
+ os.write(fd, out)
+ os.close(fd)
+
def assertMatches(self, pat, text, msg=None):
self.assert_(re.match(pat, text),
msg=('pattern %s does not match %s' % (pat, text)) or msg)