~james-page/charms/precise/cinder/pre-test

« back to all changes in this revision

Viewing changes to unit_tests/test_utils.py

  • Committer: Adam Gandelman
  • Date: 2013-10-17 21:48:08 UTC
  • Revision ID: adamg@canonical.com-20131017214808-k52pya40bowxzg4i
Merging python-redux and havana work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import logging
 
2
import unittest
 
3
import os
 
4
import yaml
 
5
 
 
6
from collections import OrderedDict
 
7
 
 
8
from mock import patch
 
9
 
 
10
 
 
11
RESTART_MAP = OrderedDict([
 
12
    ('/etc/cinder/cinder.conf',
 
13
     ['cinder-api', 'cinder-volume', 'cinder-scheduler', 'haproxy']),
 
14
    ('/etc/cinder/api-paste.ini', ['cinder-api']),
 
15
    ('/etc/ceph/ceph.conf', ['cinder-volume']),
 
16
    ('/etc/haproxy/haproxy.cfg', ['haproxy']),
 
17
    ('/etc/apache2/sites-available/openstack_https_frontend', ['apache2']),
 
18
    ('/etc/apache2/sites-available/openstack_https_frontend.conf', ['apache2'])
 
19
])
 
20
 
 
21
 
 
22
def load_config():
 
23
    '''
 
24
    Walk backwords from __file__ looking for config.yaml, load and return the
 
25
    'options' section'
 
26
    '''
 
27
    config = None
 
28
    f = __file__
 
29
    while config is None:
 
30
        d = os.path.dirname(f)
 
31
        if os.path.isfile(os.path.join(d, 'config.yaml')):
 
32
            config = os.path.join(d, 'config.yaml')
 
33
            break
 
34
        f = d
 
35
 
 
36
    if not config:
 
37
        logging.error('Could not find config.yaml in any parent directory '
 
38
                      'of %s. ' % file)
 
39
        raise Exception
 
40
 
 
41
    return yaml.safe_load(open(config).read())['options']
 
42
 
 
43
 
 
44
def get_default_config():
 
45
    '''
 
46
    Load default charm config from config.yaml return as a dict.
 
47
    If no default is set in config.yaml, its value is None.
 
48
    '''
 
49
    default_config = {}
 
50
    config = load_config()
 
51
    for k, v in config.iteritems():
 
52
        if 'default' in v:
 
53
            default_config[k] = v['default']
 
54
        else:
 
55
            default_config[k] = None
 
56
    return default_config
 
57
 
 
58
 
 
59
class CharmTestCase(unittest.TestCase):
 
60
    def setUp(self, obj, patches):
 
61
        super(CharmTestCase, self).setUp()
 
62
        self.patches = patches
 
63
        self.obj = obj
 
64
        self.test_config = TestConfig()
 
65
        self.test_relation = TestRelation()
 
66
        self.patch_all()
 
67
 
 
68
    def patch(self, method):
 
69
        _m = patch.object(self.obj, method)
 
70
        mock = _m.start()
 
71
        self.addCleanup(_m.stop)
 
72
        return mock
 
73
 
 
74
    def patch_all(self):
 
75
        for method in self.patches:
 
76
            setattr(self, method, self.patch(method))
 
77
 
 
78
 
 
79
class TestConfig(object):
 
80
    def __init__(self):
 
81
        self.config = get_default_config()
 
82
 
 
83
    def get(self, attr):
 
84
        try:
 
85
            return self.config[attr]
 
86
        except KeyError:
 
87
            return None
 
88
 
 
89
    def get_all(self):
 
90
        return self.config
 
91
 
 
92
    def set(self, attr, value):
 
93
            if attr not in self.config:
 
94
                raise KeyError
 
95
            self.config[attr] = value
 
96
 
 
97
 
 
98
class TestRelation(object):
 
99
    def __init__(self, relation_data={}):
 
100
        self.relation_data = relation_data
 
101
 
 
102
    def set(self, relation_data):
 
103
        self.relation_data = relation_data
 
104
 
 
105
    def get(self, attr=None, unit=None, rid=None):
 
106
        if attr is None:
 
107
            return self.relation_data
 
108
        elif attr in self.relation_data:
 
109
            return self.relation_data[attr]
 
110
        return None