~andrewjbeach/juju-ci-tools/get-juju-dict

1402.3.1 by Leo Zhang
Start block test
1
#!/usr/bin/env python
1402.3.4 by Leo Zhang
updates from review
2
"""Assess juju blocks prevent users from making changes and models"""
1402.3.1 by Leo Zhang
Start block test
3
4
from __future__ import print_function
5
6
import argparse
7
import logging
8
import sys
1402.3.3 by Leo Zhang
Clean up test block
9
1402.3.4 by Leo Zhang
updates from review
10
from assess_min_version import (
11
    JujuAssertionError
12
)
1402.3.1 by Leo Zhang
Start block test
13
from deploy_stack import (
14
    BootstrapManager,
1402.3.7 by Leo Zhang
Some updates after review
15
    deploy_dummy_stack,
1402.3.1 by Leo Zhang
Start block test
16
)
17
from utility import (
18
    add_basic_testing_arguments,
19
    configure_logging,
1426.1.7 by Leo Zhang
updates after review
20
    wait_for_removed_services,
1402.3.1 by Leo Zhang
Start block test
21
)
22
23
24
__metaclass__ = type
25
26
27
log = logging.getLogger("assess_block")
28
29
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
30
class DisableCommandTypes:
31
    destroy_mode = 'destroy-model'
32
    remove_object = 'remove-object'
33
    all = 'all'
34
35
1580.2.2 by Christopher Lee
Remove un-needed argument to make_block_list
36
def make_block_list(disabled_commands):
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
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"""
1402.3.6 by Leo Zhang
Add tests for block functions
51
    try:
52
        if command == 'deploy':
53
            client.deploy(args)
1454.1.1 by Leo Zhang
block fixes
54
        elif command == 'remove-service':
55
            client.remove_service(args)
1402.3.7 by Leo Zhang
Some updates after review
56
        else:
1402.3.6 by Leo Zhang
Add tests for block functions
57
            client.juju(command, args, include_e=include_e)
58
        raise JujuAssertionError()
59
    except Exception:
60
        pass
61
62
1402.3.7 by Leo Zhang
Some updates after review
63
def assess_block_destroy_model(client, charm_series):
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
64
    """Test disabling the destroy-model command.
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
65
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
66
    When "disable-command destroy-model" is set,
1402.3.10 by Leo Zhang
fixed comment lines with overlength
67
    the model cannot be destroyed, but objects
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
68
    can be added, related, and removed.
69
    """
1611.1.3 by Andrew Beach
Added disable/enable_command to EnvJujuClient. Not working yet.
70
    client.disable_command(client.destroy_model_command)
1611.1.2 by Andrew Beach
Finished adding list_disabled_commands and the tests for it. assess_block now uses that method instead of calling get_juju_output directly.
71
    block_list = client.list_disabled_commands()
1580.2.2 by Christopher Lee
Remove un-needed argument to make_block_list
72
    if block_list != make_block_list([DisableCommandTypes.destroy_mode]):
1402.3.6 by Leo Zhang
Add tests for block functions
73
        raise JujuAssertionError(block_list)
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
74
    test_disabled(
75
        client, client.destroy_model_command,
76
        ('-y', client.env.environment), include_e=False)
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
77
    # Adding, relating, and removing are not blocked.
1402.3.7 by Leo Zhang
Some updates after review
78
    deploy_dummy_stack(client, charm_series)
79
80
81
def assess_block_remove_object(client, charm_series):
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
82
    """Test block remove-object
83
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
84
    When "disable-command remove-object" is set,
1402.3.10 by Leo Zhang
fixed comment lines with overlength
85
    objects can be added and related, but they
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
86
    cannot be removed or the model/environment deleted.
87
    """
1611.1.3 by Andrew Beach
Added disable/enable_command to EnvJujuClient. Not working yet.
88
    client.disable_command(DisableCommandTypes.remove_object)
1611.1.2 by Andrew Beach
Finished adding list_disabled_commands and the tests for it. assess_block now uses that method instead of calling get_juju_output directly.
89
    block_list = client.list_disabled_commands()
1580.2.2 by Christopher Lee
Remove un-needed argument to make_block_list
90
    if block_list != make_block_list([DisableCommandTypes.remove_object]):
1402.3.6 by Leo Zhang
Add tests for block functions
91
        raise JujuAssertionError(block_list)
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
92
    test_disabled(
93
        client, client.destroy_model_command,
94
        ('-y', client.env.environment), include_e=False)
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
95
    # Adding and relating are not blocked.
1402.3.7 by Leo Zhang
Some updates after review
96
    deploy_dummy_stack(client, charm_series)
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
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'))
1402.3.7 by Leo Zhang
Some updates after review
100
101
102
def assess_block_all_changes(client, charm_series):
1402.3.6 by Leo Zhang
Add tests for block functions
103
    """Test Block Functionality: block all-changes"""
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
104
    client.juju('remove-relation', ('dummy-source', 'dummy-sink'))
1611.1.3 by Andrew Beach
Added disable/enable_command to EnvJujuClient. Not working yet.
105
    client.disable_command(DisableCommandTypes.all)
1611.1.2 by Andrew Beach
Finished adding list_disabled_commands and the tests for it. assess_block now uses that method instead of calling get_juju_output directly.
106
    block_list = client.list_disabled_commands()
1580.2.2 by Christopher Lee
Remove un-needed argument to make_block_list
107
    if block_list != make_block_list([DisableCommandTypes.all]):
1402.3.6 by Leo Zhang
Add tests for block functions
108
        raise JujuAssertionError(block_list)
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
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')
1611.1.3 by Andrew Beach
Added disable/enable_command to EnvJujuClient. Not working yet.
112
    client.enable_command(DisableCommandTypes.all)
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
113
    client.juju('unexpose', ('dummy-sink',))
1611.1.3 by Andrew Beach
Added disable/enable_command to EnvJujuClient. Not working yet.
114
    client.disable_command(DisableCommandTypes.all)
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
115
    test_disabled(client, 'expose', ('dummy-sink',))
1611.1.3 by Andrew Beach
Added disable/enable_command to EnvJujuClient. Not working yet.
116
    client.enable_command(DisableCommandTypes.all)
1454.1.1 by Leo Zhang
block fixes
117
    client.remove_service('dummy-sink')
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
118
    wait_for_removed_services(client, 'dummy-sink')
1611.1.3 by Andrew Beach
Added disable/enable_command to EnvJujuClient. Not working yet.
119
    client.disable_command(DisableCommandTypes.all)
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
120
    test_disabled(client, 'deploy', ('dummy-sink',))
121
    test_disabled(
122
        client, client.destroy_model_command,
123
        ('-y', client.env.environment), include_e=False)
1402.3.6 by Leo Zhang
Add tests for block functions
124
125
126
def assess_unblock(client, type):
1402.3.10 by Leo Zhang
fixed comment lines with overlength
127
    """Test Block Functionality
128
    unblock destroy-model/remove-object/all-changes."""
1611.1.3 by Andrew Beach
Added disable/enable_command to EnvJujuClient. Not working yet.
129
    client.enable_command(type)
1611.1.2 by Andrew Beach
Finished adding list_disabled_commands and the tests for it. assess_block now uses that method instead of calling get_juju_output directly.
130
    block_list = client.list_disabled_commands()
1580.2.2 by Christopher Lee
Remove un-needed argument to make_block_list
131
    if block_list != make_block_list([]):
1402.3.6 by Leo Zhang
Add tests for block functions
132
        raise JujuAssertionError(block_list)
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
133
    if type == client.destroy_model_command:
1402.3.7 by Leo Zhang
Some updates after review
134
        client.remove_service('dummy-source')
1402.3.8 by Leo Zhang
More updates after review
135
        wait_for_removed_services(client, 'dummy-source')
1402.3.7 by Leo Zhang
Some updates after review
136
        client.remove_service('dummy-sink')
1402.3.8 by Leo Zhang
More updates after review
137
        wait_for_removed_services(client, 'dummy-sink')
1402.3.7 by Leo Zhang
Some updates after review
138
139
140
def assess_block(client, charm_series):
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
141
    """Test Block Functionality:
1402.3.8 by Leo Zhang
More updates after review
142
    block/unblock destroy-model/remove-object/all-changes.
143
    """
1611.1.2 by Andrew Beach
Finished adding list_disabled_commands and the tests for it. assess_block now uses that method instead of calling get_juju_output directly.
144
    block_list = client.list_disabled_commands()
1402.3.1 by Leo Zhang
Start block test
145
    client.wait_for_started()
1580.2.2 by Christopher Lee
Remove un-needed argument to make_block_list
146
    expected_none_blocked = make_block_list([])
1402.3.6 by Leo Zhang
Add tests for block functions
147
    if block_list != expected_none_blocked:
148
        raise JujuAssertionError(block_list)
1402.3.7 by Leo Zhang
Some updates after review
149
    assess_block_destroy_model(client, charm_series)
1402.3.8 by Leo Zhang
More updates after review
150
    assess_unblock(client, client.destroy_model_command)
1402.3.7 by Leo Zhang
Some updates after review
151
    assess_block_remove_object(client, charm_series)
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
152
    assess_unblock(client, DisableCommandTypes.remove_object)
1402.3.7 by Leo Zhang
Some updates after review
153
    assess_block_all_changes(client, charm_series)
1580.2.1 by Christopher Lee
Update assess_block to meet new changes.
154
    assess_unblock(client, DisableCommandTypes.all)
1402.3.1 by Leo Zhang
Start block test
155
156
157
def parse_args(argv):
158
    """Parse all arguments."""
1402.3.3 by Leo Zhang
Clean up test block
159
    parser = argparse.ArgumentParser(description="Test Block Functionality")
1402.3.1 by Leo Zhang
Start block test
160
    add_basic_testing_arguments(parser)
1402.3.9 by Leo Zhang
Enriched assess_block_all_changes after review
161
    parser.set_defaults(series='trusty')
162
    return parser.parse_args(argv)
1402.3.1 by Leo Zhang
Start block test
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):
1402.3.7 by Leo Zhang
Some updates after review
170
        assess_block(bs_manager.client, bs_manager.series)
1402.3.1 by Leo Zhang
Start block test
171
    return 0
172
173
174
if __name__ == '__main__':
175
    sys.exit(main())