summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMichael Still <mikal@stillhq.com>2013-05-06 16:45:09 +1000
committerMichael Still <mikal@stillhq.com>2013-05-07 08:36:33 +1000
commit3893ef8c807457e54ecaf02c001f4b31a5a58b14 (patch)
tree6ea0b322750ea7d65fc7fcc04af36a4405385e5a /tests
parentf9d502228752bcb909e58cafacd1cd948d9a8206 (diff)
downloadoslo-3893ef8c807457e54ecaf02c001f4b31a5a58b14.tar.gz
oslo-3893ef8c807457e54ecaf02c001f4b31a5a58b14.tar.xz
oslo-3893ef8c807457e54ecaf02c001f4b31a5a58b14.zip
Import trycmd and ssh_execute from nova.
This change required adding basic unit tests as well as these were both untested in nova. Change-Id: If6843e57810198aab3c61f9eb3c3a6f42f710f7c
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/test_processutils.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/unit/test_processutils.py b/tests/unit/test_processutils.py
index 2b99d24..e00a66e 100644
--- a/tests/unit/test_processutils.py
+++ b/tests/unit/test_processutils.py
@@ -17,7 +17,9 @@
from __future__ import print_function
+import fixtures
import os
+import StringIO
import tempfile
from openstack.common import processutils
@@ -164,3 +166,86 @@ grep foo
finally:
os.unlink(tmpfilename)
os.unlink(tmpfilename2)
+
+
+def fake_execute(*cmd, **kwargs):
+ return 'stdout', 'stderr'
+
+
+def fake_execute_raises(*cmd, **kwargs):
+ raise processutils.ProcessExecutionError(exit_code=42,
+ stdout='stdout',
+ stderr='stderr',
+ cmd=['this', 'is', 'a',
+ 'command'])
+
+
+class TryCmdTestCase(utils.BaseTestCase):
+ def test_keep_warnings(self):
+ self.useFixture(fixtures.MonkeyPatch(
+ 'openstack.common.processutils.execute', fake_execute))
+ o, e = processutils.trycmd('this is a command'.split(' '))
+ self.assertNotEqual('', o)
+ self.assertNotEqual('', e)
+
+ def test_keep_warnings_from_raise(self):
+ self.useFixture(fixtures.MonkeyPatch(
+ 'openstack.common.processutils.execute', fake_execute_raises))
+ o, e = processutils.trycmd('this is a command'.split(' '),
+ discard_warnings=True)
+ self.assertNotEqual(None, o)
+ self.assertNotEqual('', e)
+
+ def test_discard_warnings(self):
+ self.useFixture(fixtures.MonkeyPatch(
+ 'openstack.common.processutils.execute', fake_execute))
+ o, e = processutils.trycmd('this is a command'.split(' '),
+ discard_warnings=True)
+ self.assertNotEqual(None, o)
+ self.assertEqual('', e)
+
+
+class FakeSshChannel(object):
+ def __init__(self, rc):
+ self.rc = rc
+
+ def recv_exit_status(self):
+ return self.rc
+
+
+class FakeSshStream(StringIO.StringIO):
+ def setup_channel(self, rc):
+ self.channel = FakeSshChannel(rc)
+
+
+class FakeSshConnection(object):
+ def __init__(self, rc):
+ self.rc = rc
+
+ def exec_command(self, cmd):
+ stdout = FakeSshStream('stdout')
+ stdout.setup_channel(self.rc)
+ return (StringIO.StringIO(),
+ stdout,
+ StringIO.StringIO('stderr'))
+
+
+class SshExecuteTestCase(utils.BaseTestCase):
+ def test_invalid_addl_env(self):
+ self.assertRaises(processutils.InvalidArgumentError,
+ processutils.ssh_execute,
+ None, 'ls', addl_env='important')
+
+ def test_invalid_process_input(self):
+ self.assertRaises(processutils.InvalidArgumentError,
+ processutils.ssh_execute,
+ None, 'ls', process_input='important')
+
+ def test_works(self):
+ o, e = processutils.ssh_execute(FakeSshConnection(0), 'ls')
+ self.assertEqual('stdout', o)
+ self.assertEqual('stderr', e)
+
+ def test_fails(self):
+ self.assertRaises(processutils.ProcessExecutionError,
+ processutils.ssh_execute, FakeSshConnection(1), 'ls')