~nskaggs/juju-ci-tools/add-assess-terms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
"""Assess juju blocks prevent users from making changes and models"""

from __future__ import print_function

import argparse
import logging
import sys

from assess_min_version import (
    JujuAssertionError
    )
from deploy_stack import (
    BootstrapManager,
    deploy_dummy_stack,
    )
from jujupy import EnvJujuClient1X
from utility import (
    add_basic_testing_arguments,
    configure_logging,
    )


__metaclass__ = type


log = logging.getLogger("assess_block")


def make_block_list(client, disabled_commands):
    """Return a manually made list of blocks and their status

    :param client: The client whose rules will be used.
    :param disabled_commands: list of client.command_set_* elements to
        include in simulated output.
    """
    if isinstance(client, EnvJujuClient1X):
        blocks = []
        commands = [
            client.command_set_destroy_model,
            client.command_set_remove_object,
            client.command_set_all]
        for command in commands:
            if command in disabled_commands:
                blocks.append({
                    'message': '',
                    'enabled': True,
                    'block': command
                    })
            else:
                blocks.append({
                    'enabled': False,
                    'block': command
                    })
        return blocks

    if not disabled_commands:
        return []

    return [{'command-set': ','.join(disabled_commands)}]


def test_disabled(client, command, args, include_e=True):
    """Test if a command is disabled as expected"""
    try:
        if command == 'deploy':
            client.deploy(args)
        elif command == 'remove-service':
            client.remove_service(args)
        else:
            client.juju(command, args, include_e=include_e)
        raise JujuAssertionError()
    except Exception:
        pass


def assess_block_destroy_model(client, charm_series):
    """Test disabling the destroy-model command.

    When "disable-command destroy-model" is set,
    the model cannot be destroyed, but objects
    can be added, related, and removed.
    """
    client.disable_command(client.destroy_model_command)
    block_list = client.list_disabled_commands()
    if block_list != make_block_list(
            client, [client.command_set_destroy_model]):
        raise JujuAssertionError(block_list)
    test_disabled(
        client, client.destroy_model_command,
        ('-y', client.env.environment), include_e=False)
    # Adding, relating, and removing are not blocked.
    deploy_dummy_stack(client, charm_series)


def assess_block_remove_object(client, charm_series):
    """Test block remove-object

    When "disable-command remove-object" is set,
    objects can be added and related, but they
    cannot be removed or the model/environment deleted.
    """
    client.disable_command(client.command_set_remove_object)
    block_list = client.list_disabled_commands()
    if block_list != make_block_list(
            client, [client.command_set_remove_object]):
        raise JujuAssertionError(block_list)
    test_disabled(
        client, client.destroy_model_command,
        ('-y', client.env.environment), include_e=False)
    # Adding and relating are not blocked.
    deploy_dummy_stack(client, charm_series)
    test_disabled(client, 'remove-service', 'dummy-source')
    test_disabled(client, 'remove-unit', ('dummy-source/1',))
    test_disabled(client, 'remove-relation', ('dummy-source', 'dummy-sink'))


def assess_block_all_changes(client, charm_series):
    """Test Block Functionality: block all-changes"""
    client.juju('remove-relation', ('dummy-source', 'dummy-sink'))
    client.disable_command(client.command_set_all)
    block_list = client.list_disabled_commands()
    if block_list != make_block_list(client, [client.command_set_all]):
        raise JujuAssertionError(block_list)
    test_disabled(client, 'add-relation', ('dummy-source', 'dummy-sink'))
    test_disabled(client, 'unexpose', ('dummy-sink',))
    test_disabled(client, 'remove-service', 'dummy-sink')
    client.enable_command(client.command_set_all)
    client.juju('unexpose', ('dummy-sink',))
    client.disable_command(client.command_set_all)
    test_disabled(client, 'expose', ('dummy-sink',))
    client.enable_command(client.command_set_all)
    client.remove_service('dummy-sink')
    client.wait_for_started()
    client.disable_command(client.command_set_all)
    test_disabled(client, 'deploy', ('dummy-sink',))
    test_disabled(
        client, client.destroy_model_command,
        ('-y', client.env.environment), include_e=False)


def assess_unblock(client, type):
    """Test Block Functionality
    unblock destroy-model/remove-object/all-changes."""
    client.enable_command(type)
    block_list = client.list_disabled_commands()
    if block_list != make_block_list(client, []):
        raise JujuAssertionError(block_list)
    if type == client.destroy_model_command:
        client.remove_service('dummy-source')
        client.wait_for_started()
        client.remove_service('dummy-sink')
        client.wait_for_started()


def assess_block(client, charm_series):
    """Test Block Functionality:
    block/unblock destroy-model/remove-object/all-changes.
    """
    log.info('Test started')
    block_list = client.list_disabled_commands()
    client.wait_for_started()
    expected_none_blocked = make_block_list(client, [])
    if block_list != expected_none_blocked:
        log.error('Controller is not in the expected starting state')
        raise JujuAssertionError(
            'Expected {}\nFound {}'.format(expected_none_blocked, block_list))
    assess_block_destroy_model(client, charm_series)
    assess_unblock(client, client.destroy_model_command)
    assess_block_remove_object(client, charm_series)
    assess_unblock(client, client.command_set_remove_object)
    assess_block_all_changes(client, charm_series)
    assess_unblock(client, client.command_set_all)
    log.info('Test PASS')


def parse_args(argv):
    """Parse all arguments."""
    parser = argparse.ArgumentParser(description="Test Block Functionality")
    add_basic_testing_arguments(parser)
    parser.set_defaults(series='trusty')
    return parser.parse_args(argv)


def main(argv=None):
    args = parse_args(argv)
    configure_logging(args.verbose)
    bs_manager = BootstrapManager.from_args(args)
    with bs_manager.booted_context(args.upload_tools):
        assess_block(bs_manager.client, bs_manager.series)
    return 0


if __name__ == '__main__':
    sys.exit(main())