~mbruzek/charms/precise/haproxy/trunk

« back to all changes in this revision

Viewing changes to hooks/tests/test_peer_hooks.py

[jseutter] adds support for the backend service to specify errorfiles in the service configuration. If errorfiles are supplied, the haproxy charm will write them to /var/lib/haproxy/<service_name>/<http status>.html and configure haproxy to use them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import base64
1
2
import os
2
3
import yaml
3
4
 
194
195
                hooks.write_service_config(services_dict)
195
196
 
196
197
                create_listen_stanza.assert_called_with(
197
 
                    'bar', 'some-host', 'some-port', 'some-options', (1, 2))
 
198
                    'bar', 'some-host', 'some-port', 'some-options', (1, 2), [])
198
199
                mock_open.assert_called_with(
199
200
                    '/var/run/haproxy/bar.service', 'w')
200
201
                mock_file.write.assert_called_with('some content')
 
202
 
 
203
    @patch('hooks.create_listen_stanza')
 
204
    def test_writes_errorfiles(self, create_listen_stanza):
 
205
        create_listen_stanza.return_value = 'some content'
 
206
 
 
207
        content = ("HTTP/1.0 403 Forbidden\r\n"
 
208
                   "Content-Type: text/html\r\n"
 
209
                   "\r\n"
 
210
                   "<html></html>")
 
211
        services_dict = {
 
212
            'foo': {
 
213
                'service_name': 'bar',
 
214
                'service_host': 'some-host',
 
215
                'service_port': 'some-port',
 
216
                'service_options': 'some-options',
 
217
                'servers': (1, 2),
 
218
                'errorfiles': [{
 
219
                    'http_status': 403,
 
220
                    'content': base64.b64encode(content)
 
221
                }]
 
222
            },
 
223
        }
 
224
 
 
225
        with patch.object(os.path, "exists") as exists:
 
226
            exists.return_value = True
 
227
            with patch_open() as (mock_open, mock_file):
 
228
                hooks.write_service_config(services_dict)
 
229
 
 
230
                mock_open.assert_any_call(
 
231
                    '/var/lib/haproxy/service_bar/403.http', 'w')
 
232
                mock_file.write.assert_any_call(content)
 
233
        self.assertTrue(create_listen_stanza.called)
 
234