~justin-fathomdb/charms/trusty/haproxy/trunk

« back to all changes in this revision

Viewing changes to hooks/tests/test_website_hooks.py

  • Committer: Marco Ceppi
  • Date: 2013-10-17 03:33:47 UTC
  • mfrom: (60.1.27 haproxy)
  • Revision ID: marco@ceppi.net-20131017033347-7ha6tdiivmpardtx
[sidnei] The 'all_services' config now supports a static list of servers to be used *in addition* to the ones provided via relation.
[sidnei] When more than one haproxy units exist, the configured service is upgraded in-place to a mode where traffic is routed to a single haproxy unit (the first one in unit-name order) and the remaining ones are configured as 'backup'. This is done to allow the enforcement of a 'maxconn' session in the configured services, which would not be possible to enforce otherwise.
[sidnei] Changes to the configured services are properly propagated to the upstream relation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from testtools import TestCase
 
2
from mock import patch, call
 
3
 
 
4
import hooks
 
5
 
 
6
 
 
7
class WebsiteRelationTest(TestCase):
 
8
 
 
9
    def setUp(self):
 
10
        super(WebsiteRelationTest, self).setUp()
 
11
        self.notify_website = self.patch_hook("notify_website")
 
12
 
 
13
    def patch_hook(self, hook_name):
 
14
        mock_controller = patch.object(hooks, hook_name)
 
15
        mock = mock_controller.start()
 
16
        self.addCleanup(mock_controller.stop)
 
17
        return mock
 
18
 
 
19
    def test_website_interface_none(self):
 
20
        self.assertEqual(None, hooks.website_interface(hook_name=None))
 
21
        self.notify_website.assert_not_called()
 
22
 
 
23
    def test_website_interface_joined(self):
 
24
        hooks.website_interface(hook_name="joined")
 
25
        self.notify_website.assert_called_once_with(
 
26
            changed=False, relation_ids=(None,))
 
27
 
 
28
    def test_website_interface_changed(self):
 
29
        hooks.website_interface(hook_name="changed")
 
30
        self.notify_website.assert_called_once_with(
 
31
            changed=True, relation_ids=(None,))
 
32
 
 
33
 
 
34
class NotifyRelationTest(TestCase):
 
35
 
 
36
    def setUp(self):
 
37
        super(NotifyRelationTest, self).setUp()
 
38
 
 
39
        self.relations_for_id = self.patch_hook("relations_for_id")
 
40
        self.relation_set = self.patch_hook("relation_set")
 
41
        self.config_get = self.patch_hook("config_get")
 
42
        self.get_relation_ids = self.patch_hook("get_relation_ids")
 
43
        self.get_hostname = self.patch_hook("get_hostname")
 
44
        self.log = self.patch_hook("log")
 
45
        self.get_config_services = self.patch_hook("get_config_service")
 
46
 
 
47
    def patch_hook(self, hook_name):
 
48
        mock_controller = patch.object(hooks, hook_name)
 
49
        mock = mock_controller.start()
 
50
        self.addCleanup(mock_controller.stop)
 
51
        return mock
 
52
 
 
53
    def test_notify_website_relation_no_relation_ids(self):
 
54
        hooks.notify_relation("website")
 
55
        self.get_relation_ids.return_value = ()
 
56
        self.relation_set.assert_not_called()
 
57
        self.get_relation_ids.assert_called_once_with("website")
 
58
 
 
59
    def test_notify_website_relation_with_default_relation(self):
 
60
        self.get_relation_ids.return_value = ()
 
61
        self.get_hostname.return_value = "foo.local"
 
62
        self.relations_for_id.return_value = [{}]
 
63
        self.config_get.return_value = {"services": ""}
 
64
 
 
65
        hooks.notify_relation("website", relation_ids=(None,))
 
66
 
 
67
        self.get_hostname.assert_called_once_with()
 
68
        self.relations_for_id.assert_called_once_with(None)
 
69
        self.relation_set.assert_called_once_with(
 
70
            relation_id=None, port="80", hostname="foo.local",
 
71
            all_services="")
 
72
        self.get_relation_ids.assert_not_called()
 
73
 
 
74
    def test_notify_website_relation_with_relations(self):
 
75
        self.get_relation_ids.return_value = ("website:1",
 
76
                                              "website:2")
 
77
        self.get_hostname.return_value = "foo.local"
 
78
        self.relations_for_id.return_value = [{}]
 
79
        self.config_get.return_value = {"services": ""}
 
80
 
 
81
        hooks.notify_relation("website")
 
82
 
 
83
        self.get_hostname.assert_called_once_with()
 
84
        self.get_relation_ids.assert_called_once_with("website")
 
85
        self.relations_for_id.assert_has_calls([
 
86
            call("website:1"),
 
87
            call("website:2"),
 
88
            ])
 
89
 
 
90
        self.relation_set.assert_has_calls([
 
91
            call(relation_id="website:1", port="80", hostname="foo.local",
 
92
                 all_services=""),
 
93
            call(relation_id="website:2", port="80", hostname="foo.local",
 
94
                 all_services=""),
 
95
            ])
 
96
 
 
97
    def test_notify_website_relation_with_different_sitenames(self):
 
98
        self.get_relation_ids.return_value = ("website:1",)
 
99
        self.get_hostname.return_value = "foo.local"
 
100
        self.relations_for_id.return_value = [{"service_name": "foo"},
 
101
                                              {"service_name": "bar"}]
 
102
        self.config_get.return_value = {"services": ""}
 
103
 
 
104
        hooks.notify_relation("website")
 
105
 
 
106
        self.get_hostname.assert_called_once_with()
 
107
        self.get_relation_ids.assert_called_once_with("website")
 
108
        self.relations_for_id.assert_has_calls([
 
109
            call("website:1"),
 
110
            ])
 
111
 
 
112
        self.relation_set.assert_has_calls([
 
113
            call.relation_set(
 
114
                relation_id="website:1", port="80", hostname="foo.local",
 
115
                all_services=""),
 
116
            ])
 
117
        self.log.assert_called_once_with(
 
118
            "Remote units requested than a single service name."
 
119
            "Falling back to default host/port.")
 
120
 
 
121
    def test_notify_website_relation_with_same_sitenames(self):
 
122
        self.get_relation_ids.return_value = ("website:1",)
 
123
        self.get_hostname.side_effect = ["foo.local", "bar.local"]
 
124
        self.relations_for_id.return_value = [{"service_name": "bar"},
 
125
                                              {"service_name": "bar"}]
 
126
        self.config_get.return_value = {"services": ""}
 
127
        self.get_config_services.return_value = {"service_host": "bar.local",
 
128
                                                 "service_port": "4242"}
 
129
 
 
130
        hooks.notify_relation("website")
 
131
 
 
132
        self.get_hostname.assert_has_calls([
 
133
            call(),
 
134
            call("bar.local")])
 
135
        self.get_relation_ids.assert_called_once_with("website")
 
136
        self.relations_for_id.assert_has_calls([
 
137
            call("website:1"),
 
138
            ])
 
139
 
 
140
        self.relation_set.assert_has_calls([
 
141
            call.relation_set(
 
142
                relation_id="website:1", port="4242", hostname="bar.local",
 
143
                all_services=""),
 
144
            ])
 
145
        self.log.assert_not_called()