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

« back to all changes in this revision

Viewing changes to assess_block.py

  • Committer: Martin Packman
  • Date: 2015-12-16 03:59:38 UTC
  • mto: This revision was merged to the branch mainline in revision 1200.
  • Revision ID: martin.packman@canonical.com-20151216035938-881wbry7ezh8he5m
Add missing template test file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
"""Assess juju blocks prevent users from making changes and models"""
3
 
 
4
 
from __future__ import print_function
5
 
 
6
 
import argparse
7
 
import logging
8
 
import sys
9
 
 
10
 
from assess_min_version import (
11
 
    JujuAssertionError
12
 
)
13
 
from deploy_stack import (
14
 
    BootstrapManager,
15
 
    deploy_dummy_stack,
16
 
)
17
 
from utility import (
18
 
    add_basic_testing_arguments,
19
 
    configure_logging,
20
 
    wait_for_removed_services,
21
 
)
22
 
 
23
 
 
24
 
__metaclass__ = type
25
 
 
26
 
 
27
 
log = logging.getLogger("assess_block")
28
 
 
29
 
 
30
 
class DisableCommandTypes:
31
 
    destroy_mode = 'destroy-model'
32
 
    remove_object = 'remove-object'
33
 
    all = 'all'
34
 
 
35
 
 
36
 
def make_block_list(disabled_commands):
37
 
    """Return a manually made list of blocks and their status
38
 
 
39
 
    :param disabled_commands: list of DisableCommandTypes elements to include
40
 
      in simulated output.
41
 
 
42
 
    """
43
 
    if not disabled_commands:
44
 
        return []
45
 
 
46
 
    return [{'command-set': ','.join(disabled_commands)}]
47
 
 
48
 
 
49
 
def test_disabled(client, command, args, include_e=True):
50
 
    """Test if a command is disabled as expected"""
51
 
    try:
52
 
        if command == 'deploy':
53
 
            client.deploy(args)
54
 
        elif command == 'remove-service':
55
 
            client.remove_service(args)
56
 
        else:
57
 
            client.juju(command, args, include_e=include_e)
58
 
        raise JujuAssertionError()
59
 
    except Exception:
60
 
        pass
61
 
 
62
 
 
63
 
def assess_block_destroy_model(client, charm_series):
64
 
    """Test disabling the destroy-model command.
65
 
 
66
 
    When "disable-command destroy-model" is set,
67
 
    the model cannot be destroyed, but objects
68
 
    can be added, related, and removed.
69
 
    """
70
 
    client.disable_command(client.destroy_model_command)
71
 
    block_list = client.list_disabled_commands()
72
 
    if block_list != make_block_list([DisableCommandTypes.destroy_mode]):
73
 
        raise JujuAssertionError(block_list)
74
 
    test_disabled(
75
 
        client, client.destroy_model_command,
76
 
        ('-y', client.env.environment), include_e=False)
77
 
    # Adding, relating, and removing are not blocked.
78
 
    deploy_dummy_stack(client, charm_series)
79
 
 
80
 
 
81
 
def assess_block_remove_object(client, charm_series):
82
 
    """Test block remove-object
83
 
 
84
 
    When "disable-command remove-object" is set,
85
 
    objects can be added and related, but they
86
 
    cannot be removed or the model/environment deleted.
87
 
    """
88
 
    client.disable_command(DisableCommandTypes.remove_object)
89
 
    block_list = client.list_disabled_commands()
90
 
    if block_list != make_block_list([DisableCommandTypes.remove_object]):
91
 
        raise JujuAssertionError(block_list)
92
 
    test_disabled(
93
 
        client, client.destroy_model_command,
94
 
        ('-y', client.env.environment), include_e=False)
95
 
    # Adding and relating are not blocked.
96
 
    deploy_dummy_stack(client, charm_series)
97
 
    test_disabled(client, 'remove-service', 'dummy-source')
98
 
    test_disabled(client, 'remove-unit', ('dummy-source/1',))
99
 
    test_disabled(client, 'remove-relation', ('dummy-source', 'dummy-sink'))
100
 
 
101
 
 
102
 
def assess_block_all_changes(client, charm_series):
103
 
    """Test Block Functionality: block all-changes"""
104
 
    client.juju('remove-relation', ('dummy-source', 'dummy-sink'))
105
 
    client.disable_command(DisableCommandTypes.all)
106
 
    block_list = client.list_disabled_commands()
107
 
    if block_list != make_block_list([DisableCommandTypes.all]):
108
 
        raise JujuAssertionError(block_list)
109
 
    test_disabled(client, 'add-relation', ('dummy-source', 'dummy-sink'))
110
 
    test_disabled(client, 'unexpose', ('dummy-sink',))
111
 
    test_disabled(client, 'remove-service', 'dummy-sink')
112
 
    client.enable_command(DisableCommandTypes.all)
113
 
    client.juju('unexpose', ('dummy-sink',))
114
 
    client.disable_command(DisableCommandTypes.all)
115
 
    test_disabled(client, 'expose', ('dummy-sink',))
116
 
    client.enable_command(DisableCommandTypes.all)
117
 
    client.remove_service('dummy-sink')
118
 
    wait_for_removed_services(client, 'dummy-sink')
119
 
    client.disable_command(DisableCommandTypes.all)
120
 
    test_disabled(client, 'deploy', ('dummy-sink',))
121
 
    test_disabled(
122
 
        client, client.destroy_model_command,
123
 
        ('-y', client.env.environment), include_e=False)
124
 
 
125
 
 
126
 
def assess_unblock(client, type):
127
 
    """Test Block Functionality
128
 
    unblock destroy-model/remove-object/all-changes."""
129
 
    client.enable_command(type)
130
 
    block_list = client.list_disabled_commands()
131
 
    if block_list != make_block_list([]):
132
 
        raise JujuAssertionError(block_list)
133
 
    if type == client.destroy_model_command:
134
 
        client.remove_service('dummy-source')
135
 
        wait_for_removed_services(client, 'dummy-source')
136
 
        client.remove_service('dummy-sink')
137
 
        wait_for_removed_services(client, 'dummy-sink')
138
 
 
139
 
 
140
 
def assess_block(client, charm_series):
141
 
    """Test Block Functionality:
142
 
    block/unblock destroy-model/remove-object/all-changes.
143
 
    """
144
 
    block_list = client.list_disabled_commands()
145
 
    client.wait_for_started()
146
 
    expected_none_blocked = make_block_list([])
147
 
    if block_list != expected_none_blocked:
148
 
        raise JujuAssertionError(block_list)
149
 
    assess_block_destroy_model(client, charm_series)
150
 
    assess_unblock(client, client.destroy_model_command)
151
 
    assess_block_remove_object(client, charm_series)
152
 
    assess_unblock(client, DisableCommandTypes.remove_object)
153
 
    assess_block_all_changes(client, charm_series)
154
 
    assess_unblock(client, DisableCommandTypes.all)
155
 
 
156
 
 
157
 
def parse_args(argv):
158
 
    """Parse all arguments."""
159
 
    parser = argparse.ArgumentParser(description="Test Block Functionality")
160
 
    add_basic_testing_arguments(parser)
161
 
    parser.set_defaults(series='trusty')
162
 
    return parser.parse_args(argv)
163
 
 
164
 
 
165
 
def main(argv=None):
166
 
    args = parse_args(argv)
167
 
    configure_logging(args.verbose)
168
 
    bs_manager = BootstrapManager.from_args(args)
169
 
    with bs_manager.booted_context(args.upload_tools):
170
 
        assess_block(bs_manager.client, bs_manager.series)
171
 
    return 0
172
 
 
173
 
 
174
 
if __name__ == '__main__':
175
 
    sys.exit(main())