~lazypower/charms/trusty/haproxy/trunk

« back to all changes in this revision

Viewing changes to hooks/tests/test_helpers.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:
258
258
        self.assertEqual(expected, result)
259
259
 
260
260
    @patch.dict(os.environ, {"JUJU_UNIT_NAME": "haproxy/2"})
 
261
    def test_creates_a_listen_stanza_string_server_options(self):
 
262
        service_name = 'some-name'
 
263
        service_ip = '10.11.12.13'
 
264
        service_port = 1234
 
265
        service_options = ('foo', 'bar')
 
266
        server_entries = [
 
267
            ('name-1', 'ip-1', 'port-1', 'foo1 bar1'),
 
268
            ('name-2', 'ip-2', 'port-2', 'foo2 bar2'),
 
269
        ]
 
270
 
 
271
        result = hooks.create_listen_stanza(service_name, service_ip,
 
272
                                            service_port, service_options,
 
273
                                            server_entries)
 
274
 
 
275
        expected = '\n'.join((
 
276
            'frontend haproxy-2-1234',
 
277
            '    bind 10.11.12.13:1234',
 
278
            '    default_backend some-name',
 
279
            '',
 
280
            'backend some-name',
 
281
            '    foo',
 
282
            '    bar',
 
283
            '    server name-1 ip-1:port-1 foo1 bar1',
 
284
            '    server name-2 ip-2:port-2 foo2 bar2',
 
285
        ))
 
286
 
 
287
        self.assertEqual(expected, result)
 
288
 
 
289
    @patch.dict(os.environ, {"JUJU_UNIT_NAME": "haproxy/2"})
261
290
    def test_create_listen_stanza_filters_frontend_options(self):
262
291
        service_name = 'some-name'
263
292
        service_ip = '10.11.12.13'
540
569
                    'foo-1': 123,
541
570
                },
542
571
                'service_options': ['foo1', 'foo2'],
543
 
                'server_options': 'baz1 baz2',
 
572
                'server_options': 'baz1, baz2',
544
573
            },
545
574
            {
546
575
                'service_name': 'bar',
547
576
                'service_options': ['bar1', 'bar2'],
548
 
                'server_options': 'baz1 baz2',
 
577
                'server_options': 'baz1, baz2',
549
578
            },
550
579
        ]
551
580
        is_proxy.return_value = False
569
598
 
570
599
        self.assertEqual(expected, result)
571
600
 
 
601
    @patch('hooks.is_proxy')
 
602
    @patch('hooks.config_get')
 
603
    @patch('yaml.safe_load')
 
604
    def test_gets_config_services_with_proxy_no_forward(self, safe_load,
 
605
                                                        config_get, is_proxy):
 
606
        config_get.return_value = {
 
607
            'services': 'some-services',
 
608
        }
 
609
        safe_load.return_value = [
 
610
            {
 
611
                'service_name': 'foo',
 
612
                'service_options': {
 
613
                    'foo-1': 123,
 
614
                },
 
615
                'service_options': ['foo1', 'foo2'],
 
616
                'server_options': 'baz1, baz2',
 
617
            },
 
618
            {
 
619
                'service_name': 'bar',
 
620
                'service_options': ['bar1', 'bar2'],
 
621
                'server_options': 'baz1, baz2',
 
622
            },
 
623
        ]
 
624
        is_proxy.return_value = True
 
625
 
 
626
        result = hooks.get_config_services()
 
627
        expected = {
 
628
            None: {
 
629
                'service_name': 'foo',
 
630
            },
 
631
            'foo': {
 
632
                'service_name': 'foo',
 
633
                'service_options': ['foo1', 'foo2', 'option forwardfor'],
 
634
                'server_options': ['baz1', 'baz2'],
 
635
            },
 
636
            'bar': {
 
637
                'service_name': 'bar',
 
638
                'service_options': ['bar1', 'bar2', 'option forwardfor'],
 
639
                'server_options': ['baz1', 'baz2'],
 
640
            },
 
641
        }
 
642
 
 
643
        self.assertEqual(expected, result)
 
644
 
 
645
    @patch('hooks.is_proxy')
 
646
    @patch('hooks.config_get')
 
647
    @patch('yaml.safe_load')
 
648
    def test_gets_config_services_no_service_options(self, safe_load,
 
649
                                                     config_get, is_proxy):
 
650
        config_get.return_value = {
 
651
            'services': '',
 
652
        }
 
653
        safe_load.return_value = [
 
654
            {
 
655
                'service_name': 'foo',
 
656
                'server_options': 'baz1, baz2',
 
657
            },
 
658
            {
 
659
                'service_name': 'bar',
 
660
                'server_options': 'baz1, baz2',
 
661
            },
 
662
        ]
 
663
        is_proxy.return_value = True
 
664
 
 
665
        result = hooks.get_config_services()
 
666
        expected = {
 
667
            None: {
 
668
                'service_name': 'foo',
 
669
            },
 
670
            'foo': {
 
671
                'service_name': 'foo',
 
672
                'server_options': ['baz1', 'baz2'],
 
673
            },
 
674
            'bar': {
 
675
                'service_name': 'bar',
 
676
                'server_options': ['baz1', 'baz2'],
 
677
            },
 
678
        }
 
679
 
 
680
        self.assertEqual(expected, result)
 
681
 
572
682
    @patch('hooks.get_config_services')
573
683
    def test_gets_a_service_config(self, get_config_services):
574
684
        get_config_services.return_value = {