2011-05-18 21:46:49 +00:00
|
|
|
from nose.tools import eq_ as eq
|
|
|
|
|
|
|
|
import fudge
|
|
|
|
import nose
|
|
|
|
|
|
|
|
from .util import assert_raises
|
|
|
|
|
|
|
|
from .. import connection
|
|
|
|
|
|
|
|
|
|
|
|
def test_split_user_just_host():
|
|
|
|
got = connection.split_user('somehost.example.com')
|
|
|
|
eq(got, (None, 'somehost.example.com'))
|
|
|
|
|
|
|
|
|
|
|
|
def test_split_user_both():
|
|
|
|
got = connection.split_user('jdoe@somehost.example.com')
|
|
|
|
eq(got, ('jdoe', 'somehost.example.com'))
|
|
|
|
|
|
|
|
|
|
|
|
def test_split_user_empty_user():
|
|
|
|
s = '@somehost.example.com'
|
|
|
|
e = assert_raises(AssertionError, connection.split_user, s)
|
|
|
|
eq(str(e), 'Bad input to split_user: {s!r}'.format(s=s))
|
|
|
|
|
|
|
|
|
|
|
|
@nose.with_setup(fudge.clear_expectations)
|
|
|
|
@fudge.with_fakes
|
|
|
|
def test_connect():
|
|
|
|
sshclient = fudge.Fake('SSHClient')
|
|
|
|
ssh = sshclient.expects_call().with_args().returns_fake()
|
|
|
|
ssh.remember_order()
|
|
|
|
ssh.expects('load_system_host_keys').with_args()
|
|
|
|
ssh.expects('connect').with_args(
|
|
|
|
hostname='orchestra.test.newdream.net.invalid',
|
|
|
|
username='jdoe',
|
|
|
|
timeout=60,
|
|
|
|
)
|
2011-12-05 17:55:02 +00:00
|
|
|
transport = ssh.expects('get_transport').with_args().returns_fake()
|
|
|
|
transport.remember_order()
|
|
|
|
transport.expects('set_keepalive').with_args(False)
|
2011-05-18 21:46:49 +00:00
|
|
|
got = connection.connect(
|
|
|
|
'jdoe@orchestra.test.newdream.net.invalid',
|
|
|
|
_SSHClient=sshclient,
|
|
|
|
)
|
|
|
|
assert got is ssh
|
2011-07-15 19:08:59 +00:00
|
|
|
|
|
|
|
@nose.with_setup(fudge.clear_expectations)
|
|
|
|
@fudge.with_fakes
|
|
|
|
def test_connect_override_hostkeys():
|
|
|
|
sshclient = fudge.Fake('SSHClient')
|
|
|
|
ssh = sshclient.expects_call().with_args().returns_fake()
|
|
|
|
ssh.remember_order()
|
|
|
|
host_keys = fudge.Fake('HostKeys')
|
|
|
|
host_keys.expects('add').with_args(
|
|
|
|
hostname='orchestra.test.newdream.net.invalid',
|
|
|
|
keytype='ssh-rsa',
|
2011-07-15 19:45:18 +00:00
|
|
|
key='frobnitz',
|
2011-07-15 19:08:59 +00:00
|
|
|
)
|
|
|
|
ssh.expects('get_host_keys').with_args().returns(host_keys)
|
|
|
|
ssh.expects('connect').with_args(
|
|
|
|
hostname='orchestra.test.newdream.net.invalid',
|
|
|
|
username='jdoe',
|
|
|
|
timeout=60,
|
|
|
|
)
|
2011-12-05 17:55:02 +00:00
|
|
|
transport = ssh.expects('get_transport').with_args().returns_fake()
|
|
|
|
transport.remember_order()
|
|
|
|
transport.expects('set_keepalive').with_args(False)
|
2011-07-15 19:45:18 +00:00
|
|
|
create_key = fudge.Fake('create_key')
|
|
|
|
create_key.expects_call().with_args('ssh-rsa', 'testkey').returns('frobnitz')
|
2011-07-15 19:08:59 +00:00
|
|
|
got = connection.connect(
|
|
|
|
'jdoe@orchestra.test.newdream.net.invalid',
|
|
|
|
host_key='ssh-rsa testkey',
|
|
|
|
_SSHClient=sshclient,
|
2011-07-15 19:45:18 +00:00
|
|
|
_create_key=create_key,
|
2011-07-15 19:08:59 +00:00
|
|
|
)
|
|
|
|
assert got is ssh
|