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

« back to all changes in this revision

Viewing changes to tests/test_assess_block.py

  • Committer: Curtis Hovey
  • Date: 2015-06-16 20:18:37 UTC
  • mto: This revision was merged to the branch mainline in revision 995.
  • Revision ID: curtis@canonical.com-20150616201837-l44eyp22o501g6ee
Ensure the env name is in the config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Tests for assess_block module."""
2
 
 
3
 
import logging
4
 
import StringIO
5
 
 
6
 
from mock import Mock, patch, call
7
 
 
8
 
from assess_block import (
9
 
    DisableCommandTypes,
10
 
    assess_block,
11
 
    make_block_list,
12
 
    main,
13
 
    parse_args,
14
 
    )
15
 
from tests import (
16
 
    parse_error,
17
 
    TestCase,
18
 
    )
19
 
 
20
 
 
21
 
class TestParseArgs(TestCase):
22
 
 
23
 
    def test_common_args(self):
24
 
        args = parse_args(["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
25
 
        self.assertEqual("an-env", args.env)
26
 
        self.assertEqual("/bin/juju", args.juju_bin)
27
 
        self.assertEqual("/tmp/logs", args.logs)
28
 
        self.assertEqual("an-env-mod", args.temp_env_name)
29
 
        self.assertEqual(False, args.debug)
30
 
 
31
 
    def test_help(self):
32
 
        fake_stdout = StringIO.StringIO()
33
 
        with parse_error(self) as fake_stderr:
34
 
            with patch("sys.stdout", fake_stdout):
35
 
                parse_args(["--help"])
36
 
        self.assertEqual("", fake_stderr.getvalue())
37
 
        self.assertNotIn("TODO", fake_stdout.getvalue())
38
 
 
39
 
 
40
 
class TestMain(TestCase):
41
 
 
42
 
    def test_main(self):
43
 
        argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
44
 
        client = Mock(spec=["is_jes_enabled"])
45
 
        with patch("assess_block.configure_logging",
46
 
                   autospec=True) as mock_cl:
47
 
            with patch("assess_block.BootstrapManager.booted_context",
48
 
                       autospec=True) as mock_bc:
49
 
                with patch("deploy_stack.client_from_config",
50
 
                           return_value=client) as mock_c:
51
 
                    with patch("assess_block.assess_block",
52
 
                               autospec=True) as mock_assess:
53
 
                        main(argv)
54
 
        mock_cl.assert_called_once_with(logging.DEBUG)
55
 
        mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False,
56
 
                                       soft_deadline=None)
57
 
        self.assertEqual(mock_bc.call_count, 1)
58
 
        mock_assess.assert_called_once_with(client, 'trusty')
59
 
 
60
 
 
61
 
class TestAssess(TestCase):
62
 
 
63
 
    def test_block(self):
64
 
        mock_client = Mock(spec=[
65
 
            "juju", "wait_for_started", "list_disabled_commands",
66
 
            "disable_command", "enable_command",
67
 
            "remove_service", "env", "deploy", "expose",
68
 
            "destroy-model", "remove-machine", "get_status"])
69
 
        mock_client.destroy_model_command = 'destroy-model'
70
 
        mock_client.list_disabled_commands.side_effect = [
71
 
            make_block_list([]),
72
 
            make_block_list([DisableCommandTypes.destroy_mode]),
73
 
            make_block_list([]),
74
 
            make_block_list([DisableCommandTypes.remove_object]),
75
 
            make_block_list([]),
76
 
            make_block_list([DisableCommandTypes.all]),
77
 
            make_block_list([]),
78
 
            ]
79
 
        mock_client.env.environment = 'foo'
80
 
        mock_client.version = '1.25'
81
 
        with patch('assess_block.deploy_dummy_stack', autospec=True):
82
 
            with patch('assess_block.wait_for_removed_services',
83
 
                       autospec=True):
84
 
                assess_block(mock_client, 'trusty')
85
 
        mock_client.wait_for_started.assert_called_with()
86
 
        self.assertEqual([call('destroy-model',
87
 
                               ('-y', 'foo'), include_e=False),
88
 
                          call('destroy-model',
89
 
                               ('-y', 'foo'), include_e=False),
90
 
                          call('remove-unit',
91
 
                               ('dummy-source/1',), include_e=True),
92
 
                          call('remove-relation',
93
 
                               ('dummy-source', 'dummy-sink'), include_e=True),
94
 
                          call('remove-relation',
95
 
                               ('dummy-source', 'dummy-sink')),
96
 
                          call('add-relation',
97
 
                               ('dummy-source', 'dummy-sink'), include_e=True),
98
 
                          call('unexpose',
99
 
                               ('dummy-sink',), include_e=True),
100
 
                          call('unexpose', ('dummy-sink',)),
101
 
                          call('expose', ('dummy-sink',), include_e=True),
102
 
                          call('destroy-model',
103
 
                               ('-y', 'foo'), include_e=False)],
104
 
                         mock_client.juju.mock_calls)
105
 
        self.assertEqual([call('destroy-model'),
106
 
                          call('remove-object'),
107
 
                          call('all'),
108
 
                          call('all'),
109
 
                          call('all')],
110
 
                         mock_client.disable_command.mock_calls)
111
 
        self.assertEqual([call('destroy-model'),
112
 
                          call('remove-object'),
113
 
                          call('all'),
114
 
                          call('all'),
115
 
                          call('all')],
116
 
                         mock_client.enable_command.mock_calls)
117
 
        self.assertEqual([call('dummy-source'),
118
 
                          call('dummy-sink'),
119
 
                          call('dummy-source'),
120
 
                          call('dummy-sink'),
121
 
                          call('dummy-sink')],
122
 
                         mock_client.remove_service.mock_calls)