1
"""Tests for assess_ssh_keys module."""
4
from mock import Mock, patch
7
from assess_ssh_keys import (
11
parse_ssh_keys_output,
17
from tests.test_jujupy import fake_juju_client
20
class TestParseArgs(TestCase):
22
def test_common_args(self):
23
args = parse_args(["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
24
self.assertEqual("an-env", args.env)
25
self.assertEqual("/bin/juju", args.juju_bin)
26
self.assertEqual("/tmp/logs", args.logs)
27
self.assertEqual("an-env-mod", args.temp_env_name)
28
self.assertEqual(False, args.debug)
31
fake_stdout = StringIO.StringIO()
32
with parse_error(self) as fake_stderr:
33
with patch("sys.stdout", fake_stdout):
34
parse_args(["--help"])
35
self.assertEqual("", fake_stderr.getvalue())
36
self.assertIn("ssh key", fake_stdout.getvalue())
39
class TestMain(TestCase):
42
argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
43
client = Mock(spec=["is_jes_enabled"])
44
with patch("assess_ssh_keys.configure_logging",
45
autospec=True) as mock_cl:
46
with patch("assess_ssh_keys.BootstrapManager.booted_context",
47
autospec=True) as mock_bc:
48
with patch('deploy_stack.client_from_config',
49
return_value=client) as mock_c:
50
with patch("assess_ssh_keys.assess_ssh_keys",
51
autospec=True) as mock_assess:
53
mock_cl.assert_called_once_with(logging.DEBUG)
54
mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False,
56
self.assertEqual(mock_bc.call_count, 1)
57
mock_assess.assert_called_once_with(client)
60
class TestParseSSHKeysOutput(TestCase):
62
header_model = "Keys used in model: admin@local/assesssshkeys-env\n"
63
header_admin = "Keys for user admin:\n"
65
"47:2d:88:82:a6:84:9a:ca:44:3e:54:79:ed:bc:e4:64 (abentley@speedy)\n"
66
"b4:e9:ce:a4:a4:d0:71:5b:d1:da:ae:a6:53:97:80:c2 (juju-system-key)\n"
67
"7b:36:c7:2c:14:74:69:50:65:37:49:c3:af:f6:db:94 (juju-client-key)\n"
70
def test_ssh_keys(self):
71
output_with_model = self.header_model + self.key_output
72
model = "admin@local/assesssshkeys-env"
73
keys = parse_ssh_keys_output(output_with_model, model)
74
self.assertEqual(map(str, keys), output_with_model.splitlines()[1:])
76
def test_modelless(self):
77
output_user_only = self.header_admin + self.key_output
78
keys = parse_ssh_keys_output(output_user_only, "assesssshkeys-env")
79
self.assertEqual(map(str, keys), output_user_only.splitlines()[1:])
82
class TestAssess(TestCase):
84
def test_ssh_keys(self):
85
fake_client = Mock(wraps=fake_juju_client())
86
fake_client.env.environment = "name"
87
fake_client.bootstrap()
88
assess_ssh_keys(fake_client)
89
# TODO: validate some things