~adam-collard/landscape-charm/fix-landscape-readme

« back to all changes in this revision

Viewing changes to hooks/test_hooks.py

  • Committer: Landscape Builder
  • Author(s): Andreas Hasenack
  • Date: 2014-10-16 15:46:28 UTC
  • mfrom: (207.1.18 service-names-1379133)
  • Revision ID: landscape_builder-20141016154628-vy4pvknv61bzvqdm
Merge service-names-1379133 [f=1379133] [r=chad.smith,benji] [a=Andreas Hasenack]
Update the haproxy jinja variables in the apache vhost template files according to whatever service name haproxy was deployed as.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from configobj import ConfigObj
2
 
from itertools import product
3
2
import base64
4
3
import hooks
5
4
import mocker
62
61
        Hardcode expected relation_list for tests.  Feel free to expand
63
62
        as more tests are added.
64
63
        """
65
 
        return list(self._relation_list)
 
64
        if relation_id == "website:1":
 
65
            return ["landscape-haproxy/0"]
 
66
        else:
 
67
            return list(self._relation_list)
66
68
 
67
69
    def unit_get(self, *args):
68
70
        """
204
206
        self.mocker.replay()
205
207
        with unittest.TestCase.assertRaises(self, ValueError) as invalid_email:
206
208
            hooks.util.create_landscape_admin(db_user, db_password, db_host,
207
 
                admin_name, admin_email, admin_password)
 
209
                                              admin_name, admin_email,
 
210
                                              admin_password)
208
211
        self.assertEqual("Invalid administrator email %s" % admin_email,
209
 
            invalid_email.exception.message)
 
212
                         invalid_email.exception.message)
210
213
 
211
214
    def test_first_admin_not_created_if_account_not_empty(self):
212
215
        """
1575
1578
        self._service_conf.seek(0)
1576
1579
        self.assertFalse(hooks._is_db_up())
1577
1580
 
 
1581
    def test__get_haproxy_service_name(self):
 
1582
        """
 
1583
        _get_haproxy_service_name() returns the jinja-ready service name used
 
1584
        to deploy haproxy.
 
1585
        """
 
1586
        haproxy_service_name = hooks._get_haproxy_service_name()
 
1587
        self.assertEqual(haproxy_service_name, "landscapehaproxy")
 
1588
 
 
1589
    def test_no_haproxy_service_name_if_not_related_to_haproxy(self):
 
1590
        """
 
1591
        _get_haproxy_service_name() returns None if we are not related to
 
1592
        haproxy.
 
1593
        """
 
1594
        def no_website_relation(relation_name):
 
1595
            return None
 
1596
 
 
1597
        self.addCleanup(setattr, hooks.juju, "relation_ids",
 
1598
                        hooks.juju.relation_ids)
 
1599
        hooks.juju.relation_ids = no_website_relation
 
1600
        haproxy_service_name = hooks._get_haproxy_service_name()
 
1601
        self.assertIsNone(haproxy_service_name)
 
1602
 
 
1603
    def test__get_vhost_template(self):
 
1604
        """
 
1605
        The haproxy prefix in the template variables is replaced by the
 
1606
        name of the actual haproxy service that is part of the deployment.
 
1607
        """
 
1608
        template_file = "vhostssl.tmpl"
 
1609
        with open("%s/config/%s" % (hooks.ROOT, template_file), "r") as t:
 
1610
            original_template = t.read()
 
1611
        new_template = hooks._get_vhost_template(template_file,
 
1612
                                                 "landscape-haproxy")
 
1613
        self.assertIn("{{ haproxy_msgserver }}", original_template)
 
1614
        self.assertNotIn("{{ landscape-haproxy_msgserver }}",
 
1615
                         original_template)
 
1616
        self.assertIn("{{ landscape-haproxy_msgserver }}", new_template)
 
1617
 
1578
1618
 
1579
1619
class TestHooksServiceMock(TestHooks):
1580
1620
 
1759
1799
        """
1760
1800
        notify the vhost-config relation on a separate ID.
1761
1801
        """
1762
 
        hooks.notify_vhost_config_relation("foo/0")
 
1802
        hooks.notify_vhost_config_relation("haproxy", "foo/0")
1763
1803
        with open("%s/config/vhostssl.tmpl" % hooks.ROOT, 'r') as f:
1764
1804
            vhostssl_template = f.read()
1765
1805
        with open("%s/config/vhost.tmpl" % hooks.ROOT, 'r') as f:
1772
1812
 
1773
1813
    def test_notify_vhost_config_relation(self):
1774
1814
        """notify the vhost-config relation on the "current" ID."""
1775
 
        hooks.notify_vhost_config_relation()
 
1815
        hooks.notify_vhost_config_relation("haproxy")
1776
1816
        with open("%s/config/vhostssl.tmpl" % hooks.ROOT, 'r') as f:
1777
1817
            vhostssl_template = f.read()
1778
1818
        with open("%s/config/vhost.tmpl" % hooks.ROOT, 'r') as f:
1802
1842
                "user": "user",
1803
1843
                "password": "password"}})
1804
1844
        notify_vhost = self.mocker.replace(hooks.notify_vhost_config_relation)
1805
 
        notify_vhost(None)
 
1845
        notify_vhost(hooks._get_haproxy_service_name(), None)
1806
1846
        self.mocker.replay()
1807
1847
        self.assertRaises(SystemExit, hooks.vhost_config_relation_changed)
1808
1848
        self.assertIn('Waiting for data from apache', hooks.juju._logs[-1])
1820
1860
                "user": "user",
1821
1861
                "password": "password"}})
1822
1862
        notify_vhost = self.mocker.replace(hooks.notify_vhost_config_relation)
1823
 
        notify_vhost(None)
 
1863
        notify_vhost(hooks._get_haproxy_service_name(), None)
1824
1864
        is_db_up = self.mocker.replace(hooks._is_db_up)
1825
1865
        is_db_up()
1826
1866
        self.mocker.result(False)
1843
1883
                "user": "user",
1844
1884
                "password": "password"}})
1845
1885
        notify_vhost = self.mocker.replace(hooks.notify_vhost_config_relation)
1846
 
        notify_vhost(None)
 
1886
        notify_vhost(hooks._get_haproxy_service_name(), None)
1847
1887
        is_db_up = self.mocker.replace(hooks._is_db_up)
1848
1888
        is_db_up()
1849
1889
        self.mocker.result(True)
1868
1908
                "user": "user",
1869
1909
                "password": "password"}})
1870
1910
        notify_vhost = self.mocker.replace(hooks.notify_vhost_config_relation)
1871
 
        notify_vhost(None)
 
1911
        notify_vhost(hooks._get_haproxy_service_name(), None)
1872
1912
        mock_conn = self.mocker.mock()
1873
1913
        mock_conn.close()
1874
1914
        connect_exclusive = self.mocker.replace(hooks.util.connect_exclusive)
1906
1946
                "user": "user",
1907
1947
                "password": "password"}})
1908
1948
        notify_vhost = self.mocker.replace(hooks.notify_vhost_config_relation)
1909
 
        notify_vhost(None)
 
1949
        notify_vhost(hooks._get_haproxy_service_name(), None)
1910
1950
        is_db_up = self.mocker.replace(hooks._is_db_up)
1911
1951
        is_db_up()
1912
1952
        self.mocker.result(True)
1926
1966
        with open(hooks.SSL_CERT_LOCATION, 'r') as f:
1927
1967
            self.assertEqual("foobar", f.read())
1928
1968
 
 
1969
    def test_vhost_config_relation_exits_if_haproxy_not_ready(self):
 
1970
        """
 
1971
        notify_vhost_config_relation() is not called if the haproxy relation
 
1972
        is not there.
 
1973
        """
 
1974
        def should_not_be_here(*args):
 
1975
            raise AssertionError("notify_vhost_config_relation() should not "
 
1976
                                 "be called")
 
1977
 
 
1978
        self.addCleanup(setattr, hooks, "vhost_config_relation_changed",
 
1979
                        hooks.vhost_config_relation_changed)
 
1980
        hooks.notify_vhost_config_relation = should_not_be_here
 
1981
        get_haproxy_service_name = self.mocker.replace(
 
1982
            hooks._get_haproxy_service_name)
 
1983
        get_haproxy_service_name()
 
1984
        self.mocker.result(None)
 
1985
        self.mocker.replay()
 
1986
        hooks.vhost_config_relation_changed()
 
1987
 
1929
1988
 
1930
1989
class TestHooksUtils(TestHooks):
1931
1990
    def test__setup_apache(self):