2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
import mitogen
|
|
|
|
import mitogen.ssh
|
|
|
|
import mitogen.utils
|
|
|
|
|
|
|
|
import testlib
|
2017-09-17 15:39:03 +00:00
|
|
|
import plain_old_module
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
|
2017-09-17 15:39:03 +00:00
|
|
|
class FakeSshTest(testlib.RouterMixin, unittest.TestCase):
|
|
|
|
def test_okay(self):
|
|
|
|
context = self.router.ssh(
|
2017-09-15 06:24:41 +00:00
|
|
|
hostname='hostname',
|
2017-09-17 15:39:03 +00:00
|
|
|
username='has-sudo',
|
|
|
|
ssh_path=testlib.data_path('fakessh.py'),
|
|
|
|
)
|
|
|
|
#context.call(mitogen.utils.log_to_file, '/tmp/log')
|
|
|
|
#context.call(mitogen.utils.disable_site_packages)
|
|
|
|
self.assertEquals(3, context.call(plain_old_module.add, 1, 2))
|
2017-09-17 12:41:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SshTest(testlib.DockerMixin, unittest.TestCase):
|
|
|
|
stream_class = mitogen.ssh.Stream
|
|
|
|
|
2017-09-29 10:34:09 +00:00
|
|
|
def test_stream_name(self):
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='has-sudo',
|
|
|
|
password='y',
|
|
|
|
)
|
2017-10-15 19:33:59 +00:00
|
|
|
self.assertEquals('ssh.localhost:%s' % (self.dockerized_ssh.port,),
|
2017-09-29 10:34:09 +00:00
|
|
|
context.name)
|
|
|
|
|
|
|
|
def test_via_stream_name(self):
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='has-sudo-nopw',
|
|
|
|
password='y',
|
|
|
|
)
|
|
|
|
sudo = self.router.sudo(via=context)
|
|
|
|
|
|
|
|
name = 'ssh.%s:%s.sudo.root' % (
|
|
|
|
self.dockerized_ssh.host,
|
|
|
|
self.dockerized_ssh.port,
|
|
|
|
)
|
|
|
|
self.assertEquals(name, sudo.name)
|
|
|
|
|
2017-09-17 12:41:48 +00:00
|
|
|
def test_password_required(self):
|
|
|
|
try:
|
2017-09-17 15:49:06 +00:00
|
|
|
context = self.docker_ssh(
|
2017-09-17 12:41:48 +00:00
|
|
|
username='has-sudo',
|
|
|
|
)
|
|
|
|
assert 0, 'exception not thrown'
|
|
|
|
except mitogen.ssh.PasswordError, e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert e[0] == self.stream_class.password_required_msg
|
|
|
|
|
|
|
|
def test_password_incorrect(self):
|
|
|
|
try:
|
2017-09-17 15:49:06 +00:00
|
|
|
context = self.docker_ssh(
|
2017-09-17 12:41:48 +00:00
|
|
|
username='has-sudo',
|
|
|
|
password='badpw',
|
|
|
|
)
|
|
|
|
assert 0, 'exception not thrown'
|
|
|
|
except mitogen.ssh.PasswordError, e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert e[0] == self.stream_class.password_incorrect_msg
|
|
|
|
|
|
|
|
def test_password_specified(self):
|
2017-09-17 15:49:06 +00:00
|
|
|
context = self.docker_ssh(
|
2017-09-17 12:41:48 +00:00
|
|
|
username='has-sudo',
|
|
|
|
password='y',
|
|
|
|
)
|
|
|
|
|
|
|
|
sentinel = 'i-am-mitogen-test-docker-image\n'
|
2017-09-17 15:39:03 +00:00
|
|
|
assert sentinel == context.call(plain_old_module.get_sentinel_value)
|
2017-09-17 12:41:48 +00:00
|
|
|
|
|
|
|
def test_pubkey_required(self):
|
|
|
|
try:
|
2017-09-17 15:49:06 +00:00
|
|
|
context = self.docker_ssh(
|
2017-09-17 12:41:48 +00:00
|
|
|
username='has-sudo-pubkey',
|
|
|
|
)
|
|
|
|
assert 0, 'exception not thrown'
|
|
|
|
except mitogen.ssh.PasswordError, e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert e[0] == self.stream_class.password_required_msg
|
|
|
|
|
|
|
|
def test_pubkey_specified(self):
|
2017-09-17 15:49:06 +00:00
|
|
|
context = self.docker_ssh(
|
2017-09-17 15:39:03 +00:00
|
|
|
username='has-sudo-pubkey',
|
|
|
|
identity_file=testlib.data_path('docker/has-sudo-pubkey.key'),
|
|
|
|
)
|
|
|
|
sentinel = 'i-am-mitogen-test-docker-image\n'
|
|
|
|
assert sentinel == context.call(plain_old_module.get_sentinel_value)
|
2017-09-29 10:34:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|