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

« back to all changes in this revision

Viewing changes to assess_storage.py

  • Committer: Curtis Hovey
  • Date: 2016-05-25 20:59:08 UTC
  • Revision ID: curtis@canonical.com-20160525205908-1yndw393rgkmxxcc
Revert git_gate.py change.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
"""Assess juju charm storage."""
3
 
 
4
 
from __future__ import print_function
5
 
import os
6
 
 
7
 
import argparse
8
 
import copy
9
 
import json
10
 
import logging
11
 
import sys
12
 
 
13
 
from deploy_stack import (
14
 
    BootstrapManager,
15
 
)
16
 
from jujucharm import (
17
 
    Charm,
18
 
    local_charm_path,
19
 
)
20
 
from utility import (
21
 
    add_basic_testing_arguments,
22
 
    configure_logging,
23
 
    JujuAssertionError,
24
 
    temp_dir,
25
 
)
26
 
 
27
 
 
28
 
__metaclass__ = type
29
 
 
30
 
 
31
 
log = logging.getLogger("assess_storage")
32
 
 
33
 
 
34
 
DEFAULT_STORAGE_POOL_DETAILS = {}
35
 
 
36
 
 
37
 
AWS_DEFAULT_STORAGE_POOL_DETAILS = {
38
 
    'ebs': {
39
 
        'provider': 'ebs'},
40
 
    'ebs-ssd': {
41
 
        'attrs': {
42
 
            'volume-type': 'ssd'},
43
 
        'provider': 'ebs'},
44
 
    'tmpfs': {
45
 
        'provider': 'tmpfs'},
46
 
    'loop': {
47
 
        'provider': 'loop'},
48
 
    'rootfs': {
49
 
        'provider': 'rootfs'}
50
 
}
51
 
 
52
 
 
53
 
storage_pool_details = {
54
 
    "loopy":
55
 
        {
56
 
            "provider": "loop",
57
 
            "attrs":
58
 
                {
59
 
                    "size": "1G"
60
 
                }},
61
 
    "rooty":
62
 
        {
63
 
            "provider": "rootfs",
64
 
            "attrs":
65
 
                {
66
 
                    "size": "1G"
67
 
                }},
68
 
    "tempy":
69
 
        {
70
 
            "provider": "tmpfs",
71
 
            "attrs":
72
 
                {
73
 
                    "size": "1G"
74
 
                }},
75
 
    "ebsy":
76
 
        {
77
 
            "provider": "ebs",
78
 
            "attrs":
79
 
                {
80
 
                    "size": "1G"
81
 
                }}
82
 
}
83
 
 
84
 
storage_pool_1x = copy.deepcopy(storage_pool_details)
85
 
storage_pool_1x["ebs-ssd"] = {
86
 
    "provider": "ebs",
87
 
    "attrs":
88
 
        {
89
 
            "volume-type": "ssd"
90
 
        }
91
 
}
92
 
 
93
 
storage_list_expected = {
94
 
    "storage":
95
 
        {"data/0":
96
 
            {
97
 
                "kind": "filesystem",
98
 
                "attachments":
99
 
                    {
100
 
                        "units":
101
 
                            {
102
 
                                "dummy-storage-fs/0":
103
 
                                    {"location": "/srv/data"}}}}}}
104
 
 
105
 
storage_list_expected_2 = copy.deepcopy(storage_list_expected)
106
 
storage_list_expected_2["storage"]["disks/1"] = {
107
 
    "kind": "block",
108
 
    "attachments":
109
 
        {
110
 
            "units":
111
 
                {"dummy-storage-lp/0":
112
 
                    {
113
 
                        "location": ""}}}}
114
 
storage_list_expected_3 = copy.deepcopy(storage_list_expected_2)
115
 
storage_list_expected_3["storage"]["disks/2"] = {
116
 
    "kind": "block",
117
 
    "attachments":
118
 
        {
119
 
            "units":
120
 
                {"dummy-storage-lp/0":
121
 
                    {
122
 
                        "location": ""}}}}
123
 
storage_list_expected_4 = copy.deepcopy(storage_list_expected_3)
124
 
storage_list_expected_4["storage"]["data/3"] = {
125
 
    "kind": "filesystem",
126
 
    "attachments":
127
 
        {
128
 
            "units":
129
 
                {"dummy-storage-tp/0":
130
 
                    {
131
 
                        "location": "/srv/data"}}}}
132
 
storage_list_expected_5 = copy.deepcopy(storage_list_expected_4)
133
 
storage_list_expected_5["storage"]["data/4"] = {
134
 
    "kind": "filesystem",
135
 
    "attachments":
136
 
        {
137
 
            "units":
138
 
                {"dummy-storage-np/0":
139
 
                    {
140
 
                        "location": "/srv/data"}}}}
141
 
storage_list_expected_6 = copy.deepcopy(storage_list_expected_5)
142
 
storage_list_expected_6["storage"]["data/5"] = {
143
 
    "kind": "filesystem",
144
 
    "attachments":
145
 
        {
146
 
            "units":
147
 
                {"dummy-storage-mp/0":
148
 
                    {
149
 
                        "location": "/srv/data"}}}}
150
 
 
151
 
 
152
 
def storage_list(client):
153
 
    """Return the storage list."""
154
 
    list_storage = json.loads(client.list_storage())
155
 
    for instance in list_storage["storage"].keys():
156
 
        try:
157
 
            list_storage["storage"][instance].pop("status")
158
 
            list_storage["storage"][instance].pop("persistent")
159
 
            attachments = list_storage["storage"][instance]["attachments"]
160
 
            unit = attachments["units"].keys()[0]
161
 
            attachments["units"][unit].pop("machine")
162
 
            if instance.startswith("disks"):
163
 
                attachments["units"][unit]["location"] = ""
164
 
        except Exception:
165
 
            pass
166
 
    return list_storage
167
 
 
168
 
 
169
 
def storage_pool_list(client):
170
 
    """Return the list of storage pool."""
171
 
    return json.loads(client.list_storage_pool())
172
 
 
173
 
 
174
 
def create_storage_charm(charm_dir, name, summary, storage):
175
 
    """Manually create a temporary charm to test with storage."""
176
 
    storage_charm = Charm(name, summary, storage=storage, series=['trusty'])
177
 
    charm_root = storage_charm.to_repo_dir(charm_dir)
178
 
    return charm_root
179
 
 
180
 
 
181
 
def assess_create_pool(client):
182
 
    """Test creating storage pool."""
183
 
    for name, pool in storage_pool_details.iteritems():
184
 
        client.create_storage_pool(name, pool["provider"],
185
 
                                   pool["attrs"]["size"])
186
 
 
187
 
 
188
 
def assess_add_storage(client, unit, storage_type, amount="1"):
189
 
    """Test adding storage instances to service.
190
 
 
191
 
    Only type 'disk' is able to add instances.
192
 
    """
193
 
    client.add_storage(unit, storage_type, amount)
194
 
 
195
 
 
196
 
def deploy_storage(client, charm, series, pool, amount="1G", charm_repo=None):
197
 
    """Test deploying charm with storage."""
198
 
    if pool == "loop":
199
 
        client.deploy(charm, series=series, repository=charm_repo,
200
 
                      storage="disks=" + pool + "," + amount)
201
 
    elif pool is None:
202
 
        client.deploy(charm, series=series, repository=charm_repo,
203
 
                      storage="data=" + amount)
204
 
    else:
205
 
        client.deploy(charm, series=series, repository=charm_repo,
206
 
                      storage="data=" + pool + "," + amount)
207
 
    client.wait_for_started()
208
 
 
209
 
 
210
 
def assess_deploy_storage(client, charm_series,
211
 
                          charm_name, provider_type, pool=None):
212
 
    """Set up the test for deploying charm with storage."""
213
 
    if provider_type == "block":
214
 
        storage = {
215
 
            "disks": {
216
 
                "type": provider_type,
217
 
                "multiple": {
218
 
                    "range": "0-10"
219
 
                }
220
 
            }
221
 
        }
222
 
    else:
223
 
        storage = {
224
 
            "data": {
225
 
                "type": provider_type,
226
 
                "location": "/srv/data"
227
 
            }
228
 
        }
229
 
    with temp_dir() as charm_dir:
230
 
        charm_root = create_storage_charm(charm_dir, charm_name,
231
 
                                          'Test charm for storage', storage)
232
 
        platform = 'ubuntu'
233
 
        charm = local_charm_path(charm=charm_name, juju_ver=client.version,
234
 
                                 series=charm_series,
235
 
                                 repository=os.path.dirname(charm_root),
236
 
                                 platform=platform)
237
 
        deploy_storage(client, charm, charm_series, pool, "1G", charm_dir)
238
 
 
239
 
 
240
 
def assess_multiple_provider(client, charm_series, amount, charm_name,
241
 
                             provider_1, provider_2, pool_1, pool_2):
242
 
    storage = {}
243
 
    for provider in [provider_1, provider_2]:
244
 
        if provider == "block":
245
 
            storage.update({
246
 
                "disks": {
247
 
                    "type": provider,
248
 
                    "multiple": {
249
 
                        "range": "0-10"
250
 
                    }
251
 
                }
252
 
            })
253
 
        else:
254
 
            storage.update({
255
 
                "data": {
256
 
                    "type": provider,
257
 
                    "location": "/srv/data"
258
 
                }
259
 
            })
260
 
    with temp_dir() as charm_dir:
261
 
        charm_root = create_storage_charm(charm_dir, charm_name,
262
 
                                          'Test charm for storage', storage)
263
 
        platform = 'ubuntu'
264
 
        charm = local_charm_path(charm=charm_name, juju_ver=client.version,
265
 
                                 series=charm_series,
266
 
                                 repository=os.path.dirname(charm_root),
267
 
                                 platform=platform)
268
 
        if pool_1 == "loop":
269
 
            command = "disks=" + pool_1 + "," + amount
270
 
        else:
271
 
            command = "data=" + pool_1 + "," + amount
272
 
        if pool_2 == "loop":
273
 
            command = command + ",disks=" + pool_2
274
 
        else:
275
 
            command = command + ",data=" + pool_2
276
 
        client.deploy(charm, series=charm_series, repository=charm_dir,
277
 
                      storage=command)
278
 
        client.wait_for_started()
279
 
 
280
 
 
281
 
def check_storage_list(client, expected):
282
 
    storage_list_derived = storage_list(client)
283
 
    if storage_list_derived != expected:
284
 
        raise JujuAssertionError(
285
 
            'Found: {} \nExpected: {}'.format(storage_list_derived, expected))
286
 
 
287
 
 
288
 
def assess_storage(client, charm_series):
289
 
    """Test the storage feature."""
290
 
 
291
 
    log.info('Assessing create-pool')
292
 
    assess_create_pool(client)
293
 
    log.info('create-pool PASSED')
294
 
 
295
 
    log.info('Assessing storage pool')
296
 
    if client.is_juju1x():
297
 
        expected_pool = storage_pool_1x
298
 
    else:
299
 
        if client.env.config['type'] == 'ec2':
300
 
            expected_pool = dict(AWS_DEFAULT_STORAGE_POOL_DETAILS)
301
 
        else:
302
 
            expected_pool = dict(DEFAULT_STORAGE_POOL_DETAILS)
303
 
        expected_pool.update(storage_pool_details)
304
 
    pool_list = storage_pool_list(client)
305
 
    if pool_list != expected_pool:
306
 
        raise JujuAssertionError(
307
 
            'Found: {} \nExpected: {}'.format(pool_list, expected_pool))
308
 
    log.info('Storage pool PASSED')
309
 
 
310
 
    log.info('Assessing filesystem rootfs')
311
 
    assess_deploy_storage(client, charm_series,
312
 
                          'dummy-storage-fs', 'filesystem', 'rootfs')
313
 
    check_storage_list(client, storage_list_expected)
314
 
    log.info('Filesystem rootfs PASSED')
315
 
 
316
 
    log.info('Assessing block loop')
317
 
    assess_deploy_storage(client, charm_series,
318
 
                          'dummy-storage-lp', 'block', 'loop')
319
 
    check_storage_list(client, storage_list_expected_2)
320
 
    log.info('Block loop PASSED')
321
 
 
322
 
    log.info('Assessing disk 1')
323
 
    assess_add_storage(client, 'dummy-storage-lp/0', 'disks', "1")
324
 
    check_storage_list(client, storage_list_expected_3)
325
 
    log.info('Disk 1 PASSED')
326
 
 
327
 
    log.info('Assessing filesystem tmpfs')
328
 
    assess_deploy_storage(client, charm_series,
329
 
                          'dummy-storage-tp', 'filesystem', 'tmpfs')
330
 
    check_storage_list(client, storage_list_expected_4)
331
 
    log.info('Filesystem tmpfs PASSED')
332
 
 
333
 
    log.info('Assessing filesystem')
334
 
    assess_deploy_storage(client, charm_series,
335
 
                          'dummy-storage-np', 'filesystem')
336
 
    check_storage_list(client, storage_list_expected_5)
337
 
    log.info('Filesystem tmpfs PASSED')
338
 
 
339
 
    log.info('Assessing multiple filesystem, block, rootfs, loop')
340
 
    assess_multiple_provider(client, charm_series, "1G", 'dummy-storage-mp',
341
 
                             'filesystem', 'block', 'rootfs', 'loop')
342
 
    check_storage_list(client, storage_list_expected_6)
343
 
    log.info('Multiple filesystem, block, rootfs, loop PASSED')
344
 
    # storage with provider 'ebs' is only available under 'aws'
345
 
    # assess_deploy_storage(client, charm_series,
346
 
    #                       'dummy-storage-eb', 'filesystem', 'ebs')
347
 
 
348
 
 
349
 
def parse_args(argv):
350
 
    """Parse all arguments."""
351
 
    parser = argparse.ArgumentParser(description="Test Storage Feature")
352
 
    add_basic_testing_arguments(parser)
353
 
    parser.set_defaults(series='trusty')
354
 
    return parser.parse_args(argv)
355
 
 
356
 
 
357
 
def main(argv=None):
358
 
    args = parse_args(argv)
359
 
    configure_logging(args.verbose)
360
 
    bs_manager = BootstrapManager.from_args(args)
361
 
    with bs_manager.booted_context(args.upload_tools):
362
 
        assess_storage(bs_manager.client, bs_manager.series)
363
 
    return 0
364
 
 
365
 
 
366
 
if __name__ == '__main__':
367
 
    sys.exit(main())