~niedbalski/charm-helpers/fix-test_requirements

« back to all changes in this revision

Viewing changes to tests/contrib/peerstorage/test_peerstorage.py

  • Committer: Marco Ceppi
  • Date: 2014-03-25 10:48:58 UTC
  • mfrom: (126.2.10 charm-helpers)
  • Revision ID: marco@ceppi.net-20140325104858-12inonu70kiajaod
[james-page] Tweaks to AMQP context for HA changes
[yolanda.robla] New peerstorage helper which is useful for storing and replicating passwords etc.. across peers in a cluster.
[yolanda.robla] Fix to get_hostname to make fqdn flag work properly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from tests.helpers import FakeRelation
 
2
from testtools import TestCase
 
3
from mock import patch
 
4
from charmhelpers.contrib import peerstorage
 
5
 
 
6
 
 
7
TO_PATCH = ['relation_ids', 'relation_set', 'relation_get', 'local_unit']
 
8
FAKE_RELATION_NAME = 'cluster'
 
9
FAKE_RELATION = {
 
10
    'cluster:0': {
 
11
        'cluster/0': {
 
12
        },
 
13
        'cluster/1': {
 
14
        },
 
15
        'cluster/2': {
 
16
        },
 
17
    },
 
18
 
 
19
}
 
20
FAKE_RELATION_IDS = ['cluster:0']
 
21
FAKE_LOCAL_UNIT = 'test_host'
 
22
 
 
23
 
 
24
class TestPeerStorage(TestCase):
 
25
    def setUp(self):
 
26
        super(TestPeerStorage, self).setUp()
 
27
        for m in TO_PATCH:
 
28
            setattr(self, m, self._patch(m))
 
29
        self.fake_relation_name = FAKE_RELATION_NAME
 
30
        self.fake_relation = FakeRelation(FAKE_RELATION)
 
31
        self.local_unit.return_value = FAKE_LOCAL_UNIT
 
32
        self.relation_get.return_value = {'key1': 'value1',
 
33
                                          'key2': 'value2',
 
34
                                          'private-address': '127.0.0.1',
 
35
                                          'public-address': '91.189.90.159'}
 
36
 
 
37
    def _patch(self, method):
 
38
        _m = patch('charmhelpers.contrib.peerstorage.' + method)
 
39
        mock = _m.start()
 
40
        self.addCleanup(_m.stop)
 
41
        return mock
 
42
 
 
43
    def test_peer_retrieve_no_relation(self):
 
44
        self.relation_ids.return_value = []
 
45
        self.assertRaises(ValueError, peerstorage.peer_retrieve, 'key', relation_name=self.fake_relation_name)
 
46
 
 
47
    def test_peer_retrieve_with_relation(self):
 
48
        self.relation_ids.return_value = FAKE_RELATION_IDS
 
49
        peerstorage.peer_retrieve('key', self.fake_relation_name)
 
50
        self.relation_get.assert_called_with(attribute='key', rid=FAKE_RELATION_IDS[0], unit=FAKE_LOCAL_UNIT)
 
51
 
 
52
    def test_peer_store_no_relation(self):
 
53
        self.relation_ids.return_value = []
 
54
        self.assertRaises(ValueError, peerstorage.peer_store, 'key', 'value', relation_name=self.fake_relation_name)
 
55
 
 
56
    def test_peer_store_with_relation(self):
 
57
        self.relation_ids.return_value = FAKE_RELATION_IDS
 
58
        peerstorage.peer_store('key', 'value', self.fake_relation_name)
 
59
        self.relation_set.assert_called_with(relation_id=FAKE_RELATION_IDS[0],
 
60
                                             relation_settings={'key': 'value'})
 
61
 
 
62
    def test_peer_echo_no_includes(self):
 
63
        peerstorage.peer_echo()
 
64
        self.relation_set.assert_called_with(relation_settings={'key1': 'value1',
 
65
                                                                'key2': 'value2'})
 
66
 
 
67
    def test_peer_echo_includes(self):
 
68
        peerstorage.peer_echo(['key1'])
 
69
        self.relation_set.assert_called_with(relation_settings={'key1': 'value1'})