~mbruzek/charms/precise/haproxy/trunk

« back to all changes in this revision

Viewing changes to hooks/tests/test_reverseproxy_hooks.py

[sidnei] This restores some functionality that got removed by accident during my refactoring and was even documented in README.md, namely, that a service can specify a piece of yaml via relation set services=<> to be used when generating the haproxy stanzas.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import yaml
 
2
 
1
3
from testtools import TestCase
2
4
from mock import patch, call
3
5
 
9
11
    def setUp(self):
10
12
        super(ReverseProxyRelationTest, self).setUp()
11
13
 
 
14
        self.config_get = self.patch_hook("config_get")
 
15
        self.config_get.return_value = {"monitoring_port": "10000"}
12
16
        self.relations_of_type = self.patch_hook("relations_of_type")
13
17
        self.get_config_services = self.patch_hook("get_config_services")
14
18
        self.log = self.patch_hook("log")
86
90
            "No private-address in relation data for 'foo/0', skipping.")])
87
91
        self.write_service_config.assert_not_called()
88
92
 
89
 
    def test_no_hostname_in_relation_data(self):
90
 
        self.get_config_services.return_value = {
91
 
            "service": {
92
 
                "service_name": "service",
93
 
                },
94
 
            }
95
 
        self.relations_of_type.return_value = [
96
 
            {"port": 4242,
97
 
             "private-address": "1.2.3.4",
98
 
             "__unit__": "foo/0"},
99
 
        ]
100
 
        self.assertIs(None, hooks.create_services())
101
 
        self.log.assert_has_calls([call.log(
102
 
            "No hostname in relation data for 'foo/0', skipping.")])
103
 
        self.write_service_config.assert_not_called()
104
 
 
105
93
    def test_relation_unknown_service(self):
106
94
        self.get_config_services.return_value = {
107
95
            "service": {
137
125
        expected = {
138
126
            'service': {
139
127
                'service_name': 'service',
 
128
                'service_host': '0.0.0.0',
 
129
                'service_port': 10002,
140
130
                'servers': [
141
131
                    ("legacy-backend", "1.2.3.1", 4242, ["maxconn 42"]),
142
132
                    ],
164
154
        expected = {
165
155
            'service': {
166
156
                'service_name': 'service',
 
157
                'service_host': '0.0.0.0',
 
158
                'service_port': 10002,
167
159
                'servers': [('foo-0-4242', '1.2.3.4', 4242, [])],
168
160
                },
169
161
            }
190
182
        expected = {
191
183
            'service': {
192
184
                'service_name': 'service',
 
185
                'service_host': '0.0.0.0',
 
186
                'service_port': 10002,
193
187
                'server_options': ["maxconn 4"],
194
188
                'servers': [('foo-0-4242', '1.2.3.4',
195
189
                             4242, ["maxconn 4"])],
219
213
        expected = {
220
214
            'foo_service': {
221
215
                'service_name': 'foo_service',
 
216
                'service_host': '0.0.0.0',
 
217
                'service_port': 10002,
222
218
                'server_options': ["maxconn 4"],
223
219
                'servers': [('foo-0-4242', '1.2.3.4',
224
220
                             4242, ["maxconn 4"])],
247
243
        expected = {
248
244
            'foo_service': {
249
245
                'service_name': 'foo_service',
 
246
                'service_host': '0.0.0.0',
 
247
                'service_port': 10002,
250
248
                'server_options': ["maxconn 4"],
251
249
                'servers': [('foo-1-4242', '1.2.3.4',
252
250
                             4242, ["maxconn 4"])],
276
274
        expected = {
277
275
            'foo_srv': {
278
276
                'service_name': 'foo_srv',
 
277
                'service_host': '0.0.0.0',
 
278
                'service_port': 10002,
279
279
                'server_options': ["maxconn 4"],
280
280
                'servers': [('foo-0-4242', '1.2.3.4',
281
281
                             4242, ["maxconn 4"])],
304
304
        expected = {
305
305
            'foo_service': {
306
306
                'service_name': 'foo_service',
 
307
                'service_host': '0.0.0.0',
 
308
                'service_port': 10002,
307
309
                'server_options': ["maxconn 4"],
308
310
                'servers': [('foo-1-4242', '1.2.3.4',
309
311
                             4242, ["maxconn 4"])],
336
338
        expected = {
337
339
            'foo': {
338
340
                'service_name': 'foo',
 
341
                'service_host': '0.0.0.0',
 
342
                'service_port': 10002,
339
343
                'server_options': ["maxconn 4"],
340
344
                'servers': [('foo-0-4242', '1.2.3.4',
341
345
                             4242, ["maxconn 4"])],
343
347
            }
344
348
        self.assertEqual(expected, hooks.create_services())
345
349
        self.write_service_config.assert_called_with(expected)
 
350
 
 
351
    def test_with_service_options_in_relation(self):
 
352
        self.get_config_services.return_value = {
 
353
            None: {
 
354
                "service_name": "service",
 
355
                },
 
356
            }
 
357
        self.relations_of_type.return_value = [
 
358
            {"port": 4242,
 
359
             "private-address": "1.2.3.4",
 
360
             "__unit__": "foo/0",
 
361
             "services": yaml.safe_dump([{
 
362
                 "service_name": "service",
 
363
                 "servers": [('foo-0', '1.2.3.4',
 
364
                              4242, ["maxconn 4"])]
 
365
                 }])
 
366
             },
 
367
        ]
 
368
 
 
369
        expected = {
 
370
            'service': {
 
371
                'service_name': 'service',
 
372
                'service_host': '0.0.0.0',
 
373
                'service_port': 10002,
 
374
                'servers': [('foo-0', '1.2.3.4',
 
375
                             4242, ["maxconn 4"])],
 
376
                },
 
377
            }
 
378
        self.assertEqual(expected, hooks.create_services())
 
379
        self.write_service_config.assert_called_with(expected)