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

« back to all changes in this revision

Viewing changes to assess_min_version.py

  • Committer: Aaron Bentley
  • Date: 2015-09-02 17:46:47 UTC
  • mto: This revision was merged to the branch mainline in revision 1082.
  • Revision ID: aaron.bentley@canonical.com-20150902174647-06vmnsooo6yzd46t
Stop supplying env to subprocess calls.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
from __future__ import print_function
3
 
 
4
 
import argparse
5
 
import logging
6
 
import os
7
 
import sys
8
 
import subprocess
9
 
 
10
 
import yaml
11
 
 
12
 
from deploy_stack import BootstrapManager
13
 
from utility import (
14
 
    add_basic_testing_arguments,
15
 
    configure_logging,
16
 
    temp_dir,
17
 
)
18
 
 
19
 
 
20
 
__metaclass__ = type
21
 
 
22
 
 
23
 
log = logging.getLogger("assess_version")
24
 
 
25
 
 
26
 
class JujuAssertionError(AssertionError):
27
 
    """Exception for juju assertion failures."""
28
 
 
29
 
 
30
 
def assert_fail(client, charm, ver, cur, name):
31
 
    try:
32
 
        client.deploy(charm, service=name)
33
 
    except subprocess.CalledProcessError:
34
 
        return
35
 
    raise JujuAssertionError(
36
 
        'assert_fail failed min: {} cur: {}'.format(ver, cur))
37
 
 
38
 
 
39
 
def assert_pass(client, charm, ver, cur, name):
40
 
    try:
41
 
        client.deploy(charm, service=name)
42
 
        client.wait_for_started()
43
 
    except subprocess.CalledProcessError:
44
 
        raise JujuAssertionError(
45
 
            'assert_pass failed min: {} cur: {}'.format(ver, cur))
46
 
 
47
 
 
48
 
def make_charm(charm_dir, ver, name='dummy'):
49
 
    metadata = os.path.join(charm_dir, 'metadata.yaml')
50
 
    content = {}
51
 
    content['name'] = name
52
 
    content['min-juju-version'] = ver
53
 
    content['summary'] = 'summary'
54
 
    content['description'] = 'description'
55
 
    content['series'] = ['trusty']
56
 
    with open(metadata, 'w') as f:
57
 
        yaml.dump(content, f, default_flow_style=False)
58
 
 
59
 
 
60
 
def get_current_version(client, juju_path):
61
 
    current = client.get_version(juju_path=juju_path).split('-')[:-2]
62
 
    return '-'.join(current)
63
 
 
64
 
 
65
 
def assess_deploy(client, assertion, ver, current, name):
66
 
    with temp_dir() as charm_dir:
67
 
        log.info("Testing min version {}".format(ver))
68
 
        make_charm(charm_dir, ver)
69
 
        assertion(client, charm_dir, ver, current, name)
70
 
 
71
 
 
72
 
def assess_min_version(client, args):
73
 
    current = get_current_version(client, args.juju_bin)
74
 
    tests = [['1.25.0', 'name1250', assert_pass],
75
 
             ['99.9.9', 'name9999', assert_fail],
76
 
             ['99.9-alpha1', 'name999alpha1', assert_fail],
77
 
             ['1.2-beta1', 'name12beta1', assert_pass],
78
 
             ['1.25.5.1', 'name12551', assert_pass],
79
 
             ['2.0-alpha1', 'name20alpha1', assert_pass],
80
 
             [current, 'current', assert_pass]]
81
 
    for ver, name, assertion in tests:
82
 
        assess_deploy(client, assertion, ver, current, name)
83
 
 
84
 
 
85
 
def parse_args(argv):
86
 
    """Parse all arguments."""
87
 
    parser = argparse.ArgumentParser(description="Juju min version")
88
 
    add_basic_testing_arguments(parser)
89
 
    return parser.parse_args(argv)
90
 
 
91
 
 
92
 
def main(argv=None):
93
 
    args = parse_args(argv)
94
 
    configure_logging(args.verbose)
95
 
    bs_manager = BootstrapManager.from_args(args)
96
 
    with bs_manager.booted_context(args.upload_tools):
97
 
        assess_min_version(bs_manager.client, args)
98
 
    return 0
99
 
 
100
 
 
101
 
if __name__ == '__main__':
102
 
    sys.exit(main())