~corey.bryant/charms/trusty/quantum-gateway/end-of-life

« back to all changes in this revision

Viewing changes to unit_tests/test_utils.py

  • Committer: Corey Bryant
  • Date: 2015-07-16 19:59:31 UTC
  • Revision ID: corey.bryant@canonical.com-20150716195931-2p7sloju2305jsfx
quantum-gateway charm has reached end-of-life

Strip all functionality from charm and issue status message
reporting end-of-life and pointing users to neutron-gateway charm.

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