~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to tests/test_assess_ssh_keys.py

  • Committer: Curtis Hovey
  • Date: 2016-09-20 01:59:47 UTC
  • mto: This revision was merged to the branch mainline in revision 1602.
  • Revision ID: curtis@canonical.com-20160920015947-ko27xkj3a4i774h6
Convert juju instance=ids to true azuzre ids.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Tests for assess_ssh_keys module."""
 
2
 
 
3
import logging
 
4
from mock import Mock, patch
 
5
import StringIO
 
6
 
 
7
from assess_ssh_keys import (
 
8
    assess_ssh_keys,
 
9
    main,
 
10
    parse_args,
 
11
    parse_ssh_keys_output,
 
12
)
 
13
from tests import (
 
14
    parse_error,
 
15
    TestCase,
 
16
)
 
17
from tests.test_jujupy import fake_juju_client
 
18
 
 
19
 
 
20
class TestParseArgs(TestCase):
 
21
 
 
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)
 
29
 
 
30
    def test_help(self):
 
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())
 
37
 
 
38
 
 
39
class TestMain(TestCase):
 
40
 
 
41
    def test_main(self):
 
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:
 
52
                        main(argv)
 
53
        mock_cl.assert_called_once_with(logging.DEBUG)
 
54
        mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False)
 
55
        self.assertEqual(mock_bc.call_count, 1)
 
56
        mock_assess.assert_called_once_with(client)
 
57
 
 
58
 
 
59
class TestParseSSHKeysOutput(TestCase):
 
60
 
 
61
    header_model = "Keys used in model: admin@local/assesssshkeys-env\n"
 
62
    header_admin = "Keys for user admin:\n"
 
63
    key_output = (
 
64
        "47:2d:88:82:a6:84:9a:ca:44:3e:54:79:ed:bc:e4:64 (abentley@speedy)\n"
 
65
        "b4:e9:ce:a4:a4:d0:71:5b:d1:da:ae:a6:53:97:80:c2 (juju-system-key)\n"
 
66
        "7b:36:c7:2c:14:74:69:50:65:37:49:c3:af:f6:db:94 (juju-client-key)\n"
 
67
    )
 
68
 
 
69
    def test_ssh_keys(self):
 
70
        output_with_model = self.header_model + self.key_output
 
71
        model = "admin@local/assesssshkeys-env"
 
72
        keys = parse_ssh_keys_output(output_with_model, model)
 
73
        self.assertEqual(map(str, keys), output_with_model.splitlines()[1:])
 
74
 
 
75
    def test_modelless(self):
 
76
        output_user_only = self.header_admin + self.key_output
 
77
        keys = parse_ssh_keys_output(output_user_only, "assesssshkeys-env")
 
78
        self.assertEqual(map(str, keys), output_user_only.splitlines()[1:])
 
79
 
 
80
 
 
81
class TestAssess(TestCase):
 
82
 
 
83
    def test_ssh_keys(self):
 
84
        fake_client = Mock(wraps=fake_juju_client())
 
85
        fake_client.env.environment = "name"
 
86
        fake_client.bootstrap()
 
87
        assess_ssh_keys(fake_client)
 
88
        # TODO: validate some things