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

« back to all changes in this revision

Viewing changes to tests/test_assess_ssh_keys.py

  • Committer: Aaron Bentley
  • Date: 2015-06-15 19:04:10 UTC
  • mfrom: (976.2.4 fix-log-rotation)
  • Revision ID: aaron.bentley@canonical.com-20150615190410-vvhtl7yxn0xbtbiy
Fix error handling in assess_log_rotation.

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
 
                                       soft_deadline=None)
56
 
        self.assertEqual(mock_bc.call_count, 1)
57
 
        mock_assess.assert_called_once_with(client)
58
 
 
59
 
 
60
 
class TestParseSSHKeysOutput(TestCase):
61
 
 
62
 
    header_model = "Keys used in model: admin@local/assesssshkeys-env\n"
63
 
    header_admin = "Keys for user admin:\n"
64
 
    key_output = (
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"
68
 
    )
69
 
 
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:])
75
 
 
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:])
80
 
 
81
 
 
82
 
class TestAssess(TestCase):
83
 
 
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