~tribaal/charms/precise/storage/refactor-mount-volume

« back to all changes in this revision

Viewing changes to hooks/testing.py

  • Committer: David Britton
  • Date: 2014-02-05 20:46:42 UTC
  • mfrom: (26.1.89 storage)
  • Revision ID: dpb@canonical.com-20140205204642-qfwv0x6314bulcx7
merging chad's python/nfs additions

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Common test classes and functions used by unit tests"""
 
2
import os
 
3
 
 
4
 
 
5
class TestHookenv(object):
 
6
    """
 
7
    Testing object to intercept juju calls and inject data, or make sure
 
8
    certain data is set.
 
9
    """
 
10
 
 
11
    _log = ()
 
12
    _incoming_relation_data = ()
 
13
    _outgoing_relation_data = ()
 
14
    _relation_list = ()
 
15
    _config = ()
 
16
 
 
17
    DEBUG = 0
 
18
    INFO = 1
 
19
    WARNING = 2
 
20
    ERROR = 3
 
21
    CRITICAL = 4
 
22
 
 
23
    def __init__(self, config={}):
 
24
        for key, value in config.iteritems():
 
25
            self._config = self._config + ((key, value),)
 
26
 
 
27
    def config(self, scope=None):
 
28
        """Return our initialized config information or a specific value"""
 
29
        if scope:
 
30
            for key, value in self._config:
 
31
                if key == scope:
 
32
                    return value
 
33
            return None
 
34
        return dict((key, value) for key, value in self._config)
 
35
 
 
36
    def relation_set(self, relid=None, relation_settings={}):
 
37
        """
 
38
        Capture result of relation_set into _outgoing_relation_data, which
 
39
        can then be checked later.
 
40
        """
 
41
        for key, value in relation_settings.iteritems():
 
42
            self._outgoing_relation_data = (
 
43
                self._outgoing_relation_data + ((key, value),))
 
44
 
 
45
    def relation_ids(self, relation_name="website"):
 
46
        """
 
47
        Hardcode expected relation_ids for tests.  Feel free to expand
 
48
        as more tests are added.
 
49
        """
 
50
        return ["%s:1" % relation_name]
 
51
 
 
52
    def related_units(self, relation_id="website:1"):
 
53
        """
 
54
        Hardcode expected related_units for tests.  Feel free to expand
 
55
        as more tests are added.
 
56
        """
 
57
        return ["%s/0" % relation_id.split(":")[0]]
 
58
 
 
59
    def relation_list(self):
 
60
        """
 
61
        Hardcode expected relation_list for tests.  Feel free to expand
 
62
        as more tests are added.
 
63
        """
 
64
        return list(self._relation_list)
 
65
 
 
66
    def unit_get(self, *args):
 
67
        """
 
68
        for now the only thing this is called for is "public-address",
 
69
        so it's a simplistic return.
 
70
        """
 
71
        return "localhost"
 
72
 
 
73
    def local_unit(self):
 
74
        return os.environ["JUJU_UNIT_NAME"]
 
75
 
 
76
    def log(self, message, level):
 
77
        self._log = self._log + (message,)
 
78
 
 
79
    def is_logged(self, message):
 
80
        for line in self._log:
 
81
            if message in line:
 
82
                return True
 
83
        return False
 
84
 
 
85
    def config_get(self, scope=None):
 
86
        if scope:
 
87
            for key, value in self._config:
 
88
                if key == scope:
 
89
                    return scope
 
90
            return None
 
91
        return dict((key, value) for key, value in self._config)
 
92
 
 
93
    def relation_get(self, scope=None, unit=None, rid=None):
 
94
        data = self._incoming_relation_data
 
95
        if scope:
 
96
            for (rel_id, key, value) in data:
 
97
                if rel_id == rid and key == scope:
 
98
                    return value
 
99
            return None
 
100
        return dict(
 
101
            (key, value) for (rel_id, key, value) in data if rel_id == rid)