-from nose.tools import eq_ as eq
-
import fudge
-import nose
from .. import cluster, remote
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_init_empty():
- c = cluster.Cluster()
- eq(c.remotes, {})
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_init():
- r1 = fudge.Fake('Remote')
- r2 = fudge.Fake('Remote')
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['baz']),
- ],
- )
- r3 = fudge.Fake('Remote')
- c.add(r3, ['xyzzy', 'thud', 'foo'])
- eq(c.remotes, {
+class TestCluster(object):
+ @fudge.with_fakes
+ def test_init_empty(self):
+ fudge.clear_expectations()
+ c = cluster.Cluster()
+ assert c.remotes == {}
+
+ @fudge.with_fakes
+ def test_init(self):
+ fudge.clear_expectations()
+ r1 = fudge.Fake('Remote')
+ r2 = fudge.Fake('Remote')
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['baz']),
+ ],
+ )
+ r3 = fudge.Fake('Remote')
+ c.add(r3, ['xyzzy', 'thud', 'foo'])
+ assert c.remotes == {
r1: ['foo', 'bar'],
r2: ['baz'],
r3: ['xyzzy', 'thud', 'foo'],
- })
+ }
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_repr():
- r1 = remote.Remote('r1', ssh=fudge.Fake('SSH'))
- r2 = remote.Remote('r2', ssh=fudge.Fake('SSH'))
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['baz']),
- ],
- )
- eq(repr(c), "Cluster(remotes={Remote(name='r1'): ['foo', 'bar'], Remote(name='r2'): ['baz']})")
+ @fudge.with_fakes
+ def test_repr(self):
+ fudge.clear_expectations()
+ r1 = remote.Remote('r1', ssh=fudge.Fake('SSH'))
+ r2 = remote.Remote('r2', ssh=fudge.Fake('SSH'))
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['baz']),
+ ],
+ )
+ assert repr(c) == "Cluster(remotes={Remote(name='r1'): ['foo', 'bar'], Remote(name='r2'): ['baz']})" # noqa
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_str():
- r1 = remote.Remote('r1', ssh=fudge.Fake('SSH'))
- r2 = remote.Remote('r2', ssh=fudge.Fake('SSH'))
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['baz']),
- ],
- )
- eq(str(c), "r1[foo,bar] r2[baz]")
+ @fudge.with_fakes
+ def test_str(self):
+ fudge.clear_expectations()
+ r1 = remote.Remote('r1', ssh=fudge.Fake('SSH'))
+ r2 = remote.Remote('r2', ssh=fudge.Fake('SSH'))
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['baz']),
+ ],
+ )
+ assert str(c) == "r1[foo,bar] r2[baz]"
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_all():
- r1 = fudge.Fake('Remote').has_attr(name='r1')
- ret1 = fudge.Fake('RemoteProcess')
- r1.expects('run').with_args(args=['test']).returns(ret1)
- r2 = fudge.Fake('Remote').has_attr(name='r2')
- ret2 = fudge.Fake('RemoteProcess')
- r2.expects('run').with_args(args=['test']).returns(ret2)
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['baz']),
- ],
- )
- got = c.run(args=['test'])
- eq(len(got), 2)
- eq(got, [ret1, ret2])
- # check identity not equality
- assert got[0] is ret1
- assert got[1] is ret2
+ @fudge.with_fakes
+ def test_run_all(self):
+ fudge.clear_expectations()
+ r1 = fudge.Fake('Remote').has_attr(name='r1')
+ ret1 = fudge.Fake('RemoteProcess')
+ r1.expects('run').with_args(args=['test']).returns(ret1)
+ r2 = fudge.Fake('Remote').has_attr(name='r2')
+ ret2 = fudge.Fake('RemoteProcess')
+ r2.expects('run').with_args(args=['test']).returns(ret2)
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['baz']),
+ ],
+ )
+ got = c.run(args=['test'])
+ assert len(got) == 2
+ assert got, [ret1 == ret2]
+ # check identity not equality
+ assert got[0] is ret1
+ assert got[1] is ret2
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_only_one():
- r1 = fudge.Fake('r1')
- r2 = fudge.Fake('r2')
- r3 = fudge.Fake('r3')
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['bar']),
- (r3, ['foo']),
- ],
- )
- c_foo = c.only('foo')
- eq(c_foo.remotes, {r1: ['foo', 'bar'], r3: ['foo']})
+ @fudge.with_fakes
+ def test_only_one(self):
+ fudge.clear_expectations()
+ r1 = fudge.Fake('r1')
+ r2 = fudge.Fake('r2')
+ r3 = fudge.Fake('r3')
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['bar']),
+ (r3, ['foo']),
+ ],
+ )
+ c_foo = c.only('foo')
+ assert c_foo.remotes == {r1: ['foo', 'bar'], r3: ['foo']}
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_only_two():
- r1 = fudge.Fake('r1')
- r2 = fudge.Fake('r2')
- r3 = fudge.Fake('r3')
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['bar']),
- (r3, ['foo']),
- ],
- )
- c_both = c.only('foo', 'bar')
- eq(c_both.remotes, {r1: ['foo', 'bar']})
+ @fudge.with_fakes
+ def test_only_two(self):
+ fudge.clear_expectations()
+ r1 = fudge.Fake('r1')
+ r2 = fudge.Fake('r2')
+ r3 = fudge.Fake('r3')
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['bar']),
+ (r3, ['foo']),
+ ],
+ )
+ c_both = c.only('foo', 'bar')
+ assert c_both.remotes, {r1: ['foo' == 'bar']}
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_only_none():
- r1 = fudge.Fake('r1')
- r2 = fudge.Fake('r2')
- r3 = fudge.Fake('r3')
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['bar']),
- (r3, ['foo']),
- ],
- )
- c_none = c.only('impossible')
- eq(c_none.remotes, {})
+ @fudge.with_fakes
+ def test_only_none(self):
+ fudge.clear_expectations()
+ r1 = fudge.Fake('r1')
+ r2 = fudge.Fake('r2')
+ r3 = fudge.Fake('r3')
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['bar']),
+ (r3, ['foo']),
+ ],
+ )
+ c_none = c.only('impossible')
+ assert c_none.remotes == {}
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_only_match():
- r1 = fudge.Fake('r1')
- r2 = fudge.Fake('r2')
- r3 = fudge.Fake('r3')
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['bar']),
- (r3, ['foo']),
- ],
- )
- c_foo = c.only('foo', lambda role: role.startswith('b'))
- eq(c_foo.remotes, {r1: ['foo', 'bar']})
+ @fudge.with_fakes
+ def test_only_match(self):
+ fudge.clear_expectations()
+ r1 = fudge.Fake('r1')
+ r2 = fudge.Fake('r2')
+ r3 = fudge.Fake('r3')
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['bar']),
+ (r3, ['foo']),
+ ],
+ )
+ c_foo = c.only('foo', lambda role: role.startswith('b'))
+ assert c_foo.remotes, {r1: ['foo' == 'bar']}
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_exclude_one():
- r1 = fudge.Fake('r1')
- r2 = fudge.Fake('r2')
- r3 = fudge.Fake('r3')
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['bar']),
- (r3, ['foo']),
- ],
- )
- c_foo = c.exclude('foo')
- eq(c_foo.remotes, {r2: ['bar']})
+ @fudge.with_fakes
+ def test_exclude_one(self):
+ fudge.clear_expectations()
+ r1 = fudge.Fake('r1')
+ r2 = fudge.Fake('r2')
+ r3 = fudge.Fake('r3')
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['bar']),
+ (r3, ['foo']),
+ ],
+ )
+ c_foo = c.exclude('foo')
+ assert c_foo.remotes == {r2: ['bar']}
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_exclude_two():
- r1 = fudge.Fake('r1')
- r2 = fudge.Fake('r2')
- r3 = fudge.Fake('r3')
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['bar']),
- (r3, ['foo']),
- ],
- )
- c_both = c.exclude('foo', 'bar')
- eq(c_both.remotes, {r2: ['bar'], r3: ['foo']})
+ @fudge.with_fakes
+ def test_exclude_two(self):
+ fudge.clear_expectations()
+ r1 = fudge.Fake('r1')
+ r2 = fudge.Fake('r2')
+ r3 = fudge.Fake('r3')
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['bar']),
+ (r3, ['foo']),
+ ],
+ )
+ c_both = c.exclude('foo', 'bar')
+ assert c_both.remotes == {r2: ['bar'], r3: ['foo']}
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_exclude_none():
- r1 = fudge.Fake('r1')
- r2 = fudge.Fake('r2')
- r3 = fudge.Fake('r3')
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['bar']),
- (r3, ['foo']),
- ],
- )
- c_none = c.exclude('impossible')
- eq(c_none.remotes, {r1: ['foo', 'bar'], r2: ['bar'], r3: ['foo']})
+ @fudge.with_fakes
+ def test_exclude_none(self):
+ fudge.clear_expectations()
+ r1 = fudge.Fake('r1')
+ r2 = fudge.Fake('r2')
+ r3 = fudge.Fake('r3')
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['bar']),
+ (r3, ['foo']),
+ ],
+ )
+ c_none = c.exclude('impossible')
+ assert c_none.remotes == {r1: ['foo', 'bar'], r2: ['bar'], r3: ['foo']}
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_exclude_match():
- r1 = fudge.Fake('r1')
- r2 = fudge.Fake('r2')
- r3 = fudge.Fake('r3')
- c = cluster.Cluster(
- remotes=[
- (r1, ['foo', 'bar']),
- (r2, ['bar']),
- (r3, ['foo']),
- ],
- )
- c_foo = c.exclude('foo', lambda role: role.startswith('b'))
- eq(c_foo.remotes, {r2: ['bar'], r3: ['foo']})
+ @fudge.with_fakes
+ def test_exclude_match(self):
+ fudge.clear_expectations()
+ r1 = fudge.Fake('r1')
+ r2 = fudge.Fake('r2')
+ r3 = fudge.Fake('r3')
+ c = cluster.Cluster(
+ remotes=[
+ (r1, ['foo', 'bar']),
+ (r2, ['bar']),
+ (r3, ['foo']),
+ ],
+ )
+ c_foo = c.exclude('foo', lambda role: role.startswith('b'))
+ assert c_foo.remotes == {r2: ['bar'], r3: ['foo']}
-from nose.tools import eq_ as eq
from cStringIO import StringIO
import fudge
import gevent.event
-import nose
import logging
from .. import run
from .util import assert_raises
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_log_simple():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo 'bar baz'")
- in_ = fudge.Fake('ChannelFile(stdin)')
- out = fudge.Fake('ChannelFile(stdout)')
- err = fudge.Fake('ChannelFile(stderr)')
- cmd.returns((in_, out, err))
- in_.expects('close').with_args()
- in_chan = fudge.Fake('channel')
- in_chan.expects('shutdown_write').with_args()
- in_.has_attr(channel=in_chan)
- out.expects('xreadlines').with_args().returns(['foo', 'bar'])
- err.expects('xreadlines').with_args().returns(['bad'])
- logger = fudge.Fake('logger')
- log_err = fudge.Fake('log_err')
- logger.expects('getChild').with_args('err').returns(log_err)
- log_err.expects('log').with_args(logging.INFO, '[HOST]: bad')
- log_out = fudge.Fake('log_out')
- logger.expects('getChild').with_args('out').returns(log_out)
- log_out.expects('log').with_args(logging.INFO, '[HOST]: foo')
- log_out.expects('log').with_args(logging.INFO, '[HOST]: bar')
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(0)
- r = run.run(
- client=ssh,
- logger=logger,
- args=['foo', 'bar baz'],
- )
- eq(r.exitstatus, 0)
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_capture_stdout():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo 'bar baz'")
- in_ = fudge.Fake('ChannelFile(stdin)')
- out = fudge.Fake('ChannelFile(stdout)')
- err = fudge.Fake('ChannelFile(stderr)')
- cmd.returns((in_, out, err))
- in_.expects('close').with_args()
- in_chan = fudge.Fake('channel')
- in_chan.expects('shutdown_write').with_args()
- in_.has_attr(channel=in_chan)
- out.remember_order()
- out.expects('read').with_args().returns('foo\nb')
- out.expects('read').with_args().returns('ar\n')
- out.expects('read').with_args().returns('')
- err.expects('xreadlines').with_args().returns(['bad'])
- logger = fudge.Fake('logger')
- log_err = fudge.Fake('log_err')
- logger.expects('getChild').with_args('err').returns(log_err)
- log_err.expects('log').with_args(logging.INFO, '[HOST]: bad')
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(0)
- out_f = StringIO()
- r = run.run(
- client=ssh,
- logger=logger,
- args=['foo', 'bar baz'],
- stdout=out_f,
- )
- eq(r.exitstatus, 0)
- assert r.stdout is out_f
- eq(r.stdout.getvalue(), 'foo\nbar\n')
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_status_bad():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo")
- in_ = fudge.Fake('ChannelFile').is_a_stub()
- out = fudge.Fake('ChannelFile').is_a_stub()
- err = fudge.Fake('ChannelFile').is_a_stub()
- cmd.returns((in_, out, err))
- out.expects('xreadlines').with_args().returns([])
- err.expects('xreadlines').with_args().returns([])
- logger = fudge.Fake('logger').is_a_stub()
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(42)
- e = assert_raises(
- run.CommandFailedError,
- run.run,
- client=ssh,
- logger=logger,
- args=['foo'],
- )
- eq(e.command, 'foo')
- eq(e.exitstatus, 42)
- eq(str(e), "Command failed on HOST with status 42: 'foo'")
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_status_bad_nocheck():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo")
- in_ = fudge.Fake('ChannelFile').is_a_stub()
- out = fudge.Fake('ChannelFile').is_a_stub()
- err = fudge.Fake('ChannelFile').is_a_stub()
- cmd.returns((in_, out, err))
- out.expects('xreadlines').with_args().returns([])
- err.expects('xreadlines').with_args().returns([])
- logger = fudge.Fake('logger').is_a_stub()
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(42)
- r = run.run(
- client=ssh,
- logger=logger,
- args=['foo'],
- check_status=False,
- )
- eq(r.exitstatus, 42)
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_status_crash():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- transport.expects('is_active').with_args().returns(True)
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo")
- in_ = fudge.Fake('ChannelFile').is_a_stub()
- out = fudge.Fake('ChannelFile').is_a_stub()
- err = fudge.Fake('ChannelFile').is_a_stub()
- cmd.returns((in_, out, err))
- out.expects('xreadlines').with_args().returns([])
- err.expects('xreadlines').with_args().returns([])
- logger = fudge.Fake('logger').is_a_stub()
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(-1)
- e = assert_raises(
- run.CommandCrashedError,
- run.run,
- client=ssh,
- logger=logger,
- args=['foo'],
- )
- eq(e.command, 'foo')
- eq(str(e), "Command crashed: 'foo'")
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_status_crash_nocheck():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo")
- in_ = fudge.Fake('ChannelFile').is_a_stub()
- out = fudge.Fake('ChannelFile').is_a_stub()
- err = fudge.Fake('ChannelFile').is_a_stub()
- cmd.returns((in_, out, err))
- out.expects('xreadlines').with_args().returns([])
- err.expects('xreadlines').with_args().returns([])
- logger = fudge.Fake('logger').is_a_stub()
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(-1)
- r = run.run(
- client=ssh,
- logger=logger,
- args=['foo'],
- check_status=False,
- )
- assert r.exitstatus is None
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_status_lost():
- ssh = fudge.Fake('SSHConnection')
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo")
- in_ = fudge.Fake('ChannelFile').is_a_stub()
- out = fudge.Fake('ChannelFile').is_a_stub()
- err = fudge.Fake('ChannelFile').is_a_stub()
- cmd.returns((in_, out, err))
- out.expects('xreadlines').with_args().returns([])
- err.expects('xreadlines').with_args().returns([])
- logger = fudge.Fake('logger').is_a_stub()
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(-1)
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- transport.expects('is_active').with_args().returns(False)
- e = assert_raises(
- run.ConnectionLostError,
- run.run,
- client=ssh,
- logger=logger,
- args=['foo'],
- )
-
- eq(e.command, 'foo')
- eq(str(e), "SSH connection was lost: 'foo'")
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_status_lost_nocheck():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo")
- in_ = fudge.Fake('ChannelFile').is_a_stub()
- out = fudge.Fake('ChannelFile').is_a_stub()
- err = fudge.Fake('ChannelFile').is_a_stub()
- cmd.returns((in_, out, err))
- out.expects('xreadlines').with_args().returns([])
- err.expects('xreadlines').with_args().returns([])
- logger = fudge.Fake('logger').is_a_stub()
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(-1)
- r = run.run(
- client=ssh,
- logger=logger,
- args=['foo'],
- check_status=False,
- )
- assert r.exitstatus is None
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_nowait():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo")
- in_ = fudge.Fake('ChannelFile').is_a_stub()
- out = fudge.Fake('ChannelFile').is_a_stub()
- err = fudge.Fake('ChannelFile').is_a_stub()
- cmd.returns((in_, out, err))
- out.expects('xreadlines').with_args().returns([])
- err.expects('xreadlines').with_args().returns([])
- logger = fudge.Fake('logger').is_a_stub()
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(42)
- r = run.run(
- client=ssh,
- logger=logger,
- args=['foo'],
- wait=False,
- )
- eq(r.command, 'foo')
- assert isinstance(r.exitstatus, gevent.event.AsyncResult)
- e = assert_raises(
- run.CommandFailedError,
- r.exitstatus.get,
- )
- eq(e.exitstatus, 42)
- eq(str(e), "Command failed on HOST with status 42: 'foo'")
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_stdin_pipe():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo")
- in_ = fudge.Fake('ChannelFile').is_a_stub()
- out = fudge.Fake('ChannelFile').is_a_stub()
- err = fudge.Fake('ChannelFile').is_a_stub()
- cmd.returns((in_, out, err))
- out.expects('xreadlines').with_args().returns([])
- err.expects('xreadlines').with_args().returns([])
- logger = fudge.Fake('logger').is_a_stub()
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(0)
- r = run.run(
- client=ssh,
- logger=logger,
- args=['foo'],
- stdin=run.PIPE,
- wait=False,
- )
- r.stdin.write('bar')
- eq(r.command, 'foo')
- assert isinstance(r.exitstatus, gevent.event.AsyncResult)
- eq(r.exitstatus.ready(), False)
- got = r.exitstatus.get()
- eq(got, 0)
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_stdout_pipe():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo")
- in_ = fudge.Fake('ChannelFile').is_a_stub()
- out = fudge.Fake('ChannelFile').is_a_stub()
- err = fudge.Fake('ChannelFile').is_a_stub()
- cmd.returns((in_, out, err))
- out.expects('read').with_args().returns('one')
- out.expects('read').with_args().returns('two')
- out.expects('read').with_args().returns('')
- err.expects('xreadlines').with_args().returns([])
- logger = fudge.Fake('logger').is_a_stub()
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(0)
- r = run.run(
- client=ssh,
- logger=logger,
- args=['foo'],
- stdout=run.PIPE,
- wait=False,
- )
- eq(r.command, 'foo')
- assert isinstance(r.exitstatus, gevent.event.AsyncResult)
- eq(r.exitstatus.ready(), False)
- eq(r.stdout.read(), 'one')
- eq(r.stdout.read(), 'two')
- eq(r.stdout.read(), '')
- got = r.exitstatus.get()
- eq(got, 0)
-
-
-@nose.with_setup(fudge.clear_expectations)
-@fudge.with_fakes
-def test_run_stderr_pipe():
- ssh = fudge.Fake('SSHConnection')
- transport = ssh.expects('get_transport').with_args().returns_fake()
- transport.expects('getpeername').with_args().returns(('HOST', 22))
- cmd = ssh.expects('exec_command')
- cmd.with_args("foo")
- in_ = fudge.Fake('ChannelFile').is_a_stub()
- out = fudge.Fake('ChannelFile').is_a_stub()
- err = fudge.Fake('ChannelFile').is_a_stub()
- cmd.returns((in_, out, err))
- out.expects('xreadlines').with_args().returns([])
- err.expects('read').with_args().returns('one')
- err.expects('read').with_args().returns('two')
- err.expects('read').with_args().returns('')
- logger = fudge.Fake('logger').is_a_stub()
- channel = fudge.Fake('channel')
- out.has_attr(channel=channel)
- channel.expects('recv_exit_status').with_args().returns(0)
- r = run.run(
- client=ssh,
- logger=logger,
- args=['foo'],
- stderr=run.PIPE,
- wait=False,
- )
- eq(r.command, 'foo')
- assert isinstance(r.exitstatus, gevent.event.AsyncResult)
- eq(r.exitstatus.ready(), False)
- eq(r.stderr.read(), 'one')
- eq(r.stderr.read(), 'two')
- eq(r.stderr.read(), '')
- got = r.exitstatus.get()
- eq(got, 0)
-
-
-def test_quote_simple():
- got = run.quote(['a b', ' c', 'd e '])
- eq(got, "'a b' ' c' 'd e '")
-
-def test_quote_and_quote():
- got = run.quote(['echo', 'this && is embedded', '&&', 'that was standalone'])
- eq(got, "echo 'this && is embedded' '&&' 'that was standalone'")
-
-def test_quote_and_raw():
- got = run.quote(['true', run.Raw('&&'), 'echo', 'yay'])
- eq(got, "true && echo yay")
+class TestRun(object):
+ @fudge.with_fakes
+ def test_run_log_simple(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo 'bar baz'")
+ in_ = fudge.Fake('ChannelFile(stdin)')
+ out = fudge.Fake('ChannelFile(stdout)')
+ err = fudge.Fake('ChannelFile(stderr)')
+ cmd.returns((in_, out, err))
+ in_.expects('close').with_args()
+ in_chan = fudge.Fake('channel')
+ in_chan.expects('shutdown_write').with_args()
+ in_.has_attr(channel=in_chan)
+ out.expects('xreadlines').with_args().returns(['foo', 'bar'])
+ err.expects('xreadlines').with_args().returns(['bad'])
+ logger = fudge.Fake('logger')
+ log_err = fudge.Fake('log_err')
+ logger.expects('getChild').with_args('err').returns(log_err)
+ log_err.expects('log').with_args(logging.INFO, '[HOST]: bad')
+ log_out = fudge.Fake('log_out')
+ logger.expects('getChild').with_args('out').returns(log_out)
+ log_out.expects('log').with_args(logging.INFO, '[HOST]: foo')
+ log_out.expects('log').with_args(logging.INFO, '[HOST]: bar')
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(0)
+ r = run.run(
+ client=ssh,
+ logger=logger,
+ args=['foo', 'bar baz'],
+ )
+ assert r.exitstatus == 0
+
+ @fudge.with_fakes
+ def test_run_capture_stdout(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo 'bar baz'")
+ in_ = fudge.Fake('ChannelFile(stdin)')
+ out = fudge.Fake('ChannelFile(stdout)')
+ err = fudge.Fake('ChannelFile(stderr)')
+ cmd.returns((in_, out, err))
+ in_.expects('close').with_args()
+ in_chan = fudge.Fake('channel')
+ in_chan.expects('shutdown_write').with_args()
+ in_.has_attr(channel=in_chan)
+ out.remember_order()
+ out.expects('read').with_args().returns('foo\nb')
+ out.expects('read').with_args().returns('ar\n')
+ out.expects('read').with_args().returns('')
+ err.expects('xreadlines').with_args().returns(['bad'])
+ logger = fudge.Fake('logger')
+ log_err = fudge.Fake('log_err')
+ logger.expects('getChild').with_args('err').returns(log_err)
+ log_err.expects('log').with_args(logging.INFO, '[HOST]: bad')
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(0)
+ out_f = StringIO()
+ r = run.run(
+ client=ssh,
+ logger=logger,
+ args=['foo', 'bar baz'],
+ stdout=out_f,
+ )
+ assert r.exitstatus == 0
+ assert r.stdout is out_f
+ assert r.stdout.getvalue() == 'foo\nbar\n'
+
+ @fudge.with_fakes
+ def test_run_status_bad(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo")
+ in_ = fudge.Fake('ChannelFile').is_a_stub()
+ out = fudge.Fake('ChannelFile').is_a_stub()
+ err = fudge.Fake('ChannelFile').is_a_stub()
+ cmd.returns((in_, out, err))
+ out.expects('xreadlines').with_args().returns([])
+ err.expects('xreadlines').with_args().returns([])
+ logger = fudge.Fake('logger').is_a_stub()
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(42)
+ e = assert_raises(
+ run.CommandFailedError,
+ run.run,
+ client=ssh,
+ logger=logger,
+ args=['foo'],
+ )
+ assert e.command == 'foo'
+ assert e.exitstatus == 42
+ assert str(e) == "Command failed on HOST with status 42: 'foo'"
+
+ @fudge.with_fakes
+ def test_run_status_bad_nocheck(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo")
+ in_ = fudge.Fake('ChannelFile').is_a_stub()
+ out = fudge.Fake('ChannelFile').is_a_stub()
+ err = fudge.Fake('ChannelFile').is_a_stub()
+ cmd.returns((in_, out, err))
+ out.expects('xreadlines').with_args().returns([])
+ err.expects('xreadlines').with_args().returns([])
+ logger = fudge.Fake('logger').is_a_stub()
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(42)
+ r = run.run(
+ client=ssh,
+ logger=logger,
+ args=['foo'],
+ check_status=False,
+ )
+ assert r.exitstatus == 42
+
+ @fudge.with_fakes
+ def test_run_status_crash(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ transport.expects('is_active').with_args().returns(True)
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo")
+ in_ = fudge.Fake('ChannelFile').is_a_stub()
+ out = fudge.Fake('ChannelFile').is_a_stub()
+ err = fudge.Fake('ChannelFile').is_a_stub()
+ cmd.returns((in_, out, err))
+ out.expects('xreadlines').with_args().returns([])
+ err.expects('xreadlines').with_args().returns([])
+ logger = fudge.Fake('logger').is_a_stub()
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(-1)
+ e = assert_raises(
+ run.CommandCrashedError,
+ run.run,
+ client=ssh,
+ logger=logger,
+ args=['foo'],
+ )
+ assert e.command == 'foo'
+ assert str(e) == "Command crashed: 'foo'"
+
+ @fudge.with_fakes
+ def test_run_status_crash_nocheck(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo")
+ in_ = fudge.Fake('ChannelFile').is_a_stub()
+ out = fudge.Fake('ChannelFile').is_a_stub()
+ err = fudge.Fake('ChannelFile').is_a_stub()
+ cmd.returns((in_, out, err))
+ out.expects('xreadlines').with_args().returns([])
+ err.expects('xreadlines').with_args().returns([])
+ logger = fudge.Fake('logger').is_a_stub()
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(-1)
+ r = run.run(
+ client=ssh,
+ logger=logger,
+ args=['foo'],
+ check_status=False,
+ )
+ assert r.exitstatus is None
+
+ @fudge.with_fakes
+ def test_run_status_lost(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo")
+ in_ = fudge.Fake('ChannelFile').is_a_stub()
+ out = fudge.Fake('ChannelFile').is_a_stub()
+ err = fudge.Fake('ChannelFile').is_a_stub()
+ cmd.returns((in_, out, err))
+ out.expects('xreadlines').with_args().returns([])
+ err.expects('xreadlines').with_args().returns([])
+ logger = fudge.Fake('logger').is_a_stub()
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(-1)
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ transport.expects('is_active').with_args().returns(False)
+ e = assert_raises(
+ run.ConnectionLostError,
+ run.run,
+ client=ssh,
+ logger=logger,
+ args=['foo'],
+ )
+
+ assert e.command == 'foo'
+ assert str(e) == "SSH connection was lost: 'foo'"
+
+ @fudge.with_fakes
+ def test_run_status_lost_nocheck(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo")
+ in_ = fudge.Fake('ChannelFile').is_a_stub()
+ out = fudge.Fake('ChannelFile').is_a_stub()
+ err = fudge.Fake('ChannelFile').is_a_stub()
+ cmd.returns((in_, out, err))
+ out.expects('xreadlines').with_args().returns([])
+ err.expects('xreadlines').with_args().returns([])
+ logger = fudge.Fake('logger').is_a_stub()
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(-1)
+ r = run.run(
+ client=ssh,
+ logger=logger,
+ args=['foo'],
+ check_status=False,
+ )
+ assert r.exitstatus is None
+
+ @fudge.with_fakes
+ def test_run_nowait(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo")
+ in_ = fudge.Fake('ChannelFile').is_a_stub()
+ out = fudge.Fake('ChannelFile').is_a_stub()
+ err = fudge.Fake('ChannelFile').is_a_stub()
+ cmd.returns((in_, out, err))
+ out.expects('xreadlines').with_args().returns([])
+ err.expects('xreadlines').with_args().returns([])
+ logger = fudge.Fake('logger').is_a_stub()
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(42)
+ r = run.run(
+ client=ssh,
+ logger=logger,
+ args=['foo'],
+ wait=False,
+ )
+ assert r.command == 'foo'
+ assert isinstance(r.exitstatus, gevent.event.AsyncResult)
+ e = assert_raises(
+ run.CommandFailedError,
+ r.exitstatus.get,
+ )
+ assert e.exitstatus == 42
+ assert str(e) == "Command failed on HOST with status 42: 'foo'"
+
+ @fudge.with_fakes
+ def test_run_stdin_pipe(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo")
+ in_ = fudge.Fake('ChannelFile').is_a_stub()
+ out = fudge.Fake('ChannelFile').is_a_stub()
+ err = fudge.Fake('ChannelFile').is_a_stub()
+ cmd.returns((in_, out, err))
+ out.expects('xreadlines').with_args().returns([])
+ err.expects('xreadlines').with_args().returns([])
+ logger = fudge.Fake('logger').is_a_stub()
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(0)
+ r = run.run(
+ client=ssh,
+ logger=logger,
+ args=['foo'],
+ stdin=run.PIPE,
+ wait=False,
+ )
+ r.stdin.write('bar')
+ assert r.command == 'foo'
+ assert isinstance(r.exitstatus, gevent.event.AsyncResult)
+ assert r.exitstatus.ready() == False
+ got = r.exitstatus.get()
+ assert got == 0
+
+ @fudge.with_fakes
+ def test_run_stdout_pipe(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo")
+ in_ = fudge.Fake('ChannelFile').is_a_stub()
+ out = fudge.Fake('ChannelFile').is_a_stub()
+ err = fudge.Fake('ChannelFile').is_a_stub()
+ cmd.returns((in_, out, err))
+ out.expects('read').with_args().returns('one')
+ out.expects('read').with_args().returns('two')
+ out.expects('read').with_args().returns('')
+ err.expects('xreadlines').with_args().returns([])
+ logger = fudge.Fake('logger').is_a_stub()
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(0)
+ r = run.run(
+ client=ssh,
+ logger=logger,
+ args=['foo'],
+ stdout=run.PIPE,
+ wait=False,
+ )
+ assert r.command == 'foo'
+ assert isinstance(r.exitstatus, gevent.event.AsyncResult)
+ assert r.exitstatus.ready() == False
+ assert r.stdout.read() == 'one'
+ assert r.stdout.read() == 'two'
+ assert r.stdout.read() == ''
+ got = r.exitstatus.get()
+ assert got == 0
+
+ @fudge.with_fakes
+ def test_run_stderr_pipe(self):
+ fudge.clear_expectations()
+ ssh = fudge.Fake('SSHConnection')
+ transport = ssh.expects('get_transport').with_args().returns_fake()
+ transport.expects('getpeername').with_args().returns(('HOST', 22))
+ cmd = ssh.expects('exec_command')
+ cmd.with_args("foo")
+ in_ = fudge.Fake('ChannelFile').is_a_stub()
+ out = fudge.Fake('ChannelFile').is_a_stub()
+ err = fudge.Fake('ChannelFile').is_a_stub()
+ cmd.returns((in_, out, err))
+ out.expects('xreadlines').with_args().returns([])
+ err.expects('read').with_args().returns('one')
+ err.expects('read').with_args().returns('two')
+ err.expects('read').with_args().returns('')
+ logger = fudge.Fake('logger').is_a_stub()
+ channel = fudge.Fake('channel')
+ out.has_attr(channel=channel)
+ channel.expects('recv_exit_status').with_args().returns(0)
+ r = run.run(
+ client=ssh,
+ logger=logger,
+ args=['foo'],
+ stderr=run.PIPE,
+ wait=False,
+ )
+ assert r.command == 'foo'
+ assert isinstance(r.exitstatus, gevent.event.AsyncResult)
+ assert r.exitstatus.ready() is False
+ assert r.stderr.read() == 'one'
+ assert r.stderr.read() == 'two'
+ assert r.stderr.read() == ''
+ got = r.exitstatus.get()
+ assert got == 0
+
+ def test_quote_simple(self):
+ got = run.quote(['a b', ' c', 'd e '])
+ assert got == "'a b' ' c' 'd e '"
+
+ def test_quote_and_quote(self):
+ got = run.quote(['echo', 'this && is embedded', '&&',
+ 'that was standalone'])
+ assert got == "echo 'this && is embedded' '&&' 'that was standalone'"
+
+ def test_quote_and_raw(self):
+ got = run.quote(['true', run.Raw('&&'), 'echo', 'yay'])
+ assert got == "true && echo yay"