github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/trusty/haproxy/hooks/tests/test_install.py (about)

     1  from mock import patch
     2  import os
     3  from testtools import TestCase
     4  
     5  import hooks
     6  
     7  
     8  class InstallTests(TestCase):
     9  
    10      def setUp(self):
    11          super(InstallTests, self).setUp()
    12          self.add_source = self.patch_hook('add_source')
    13          self.apt_update = self.patch_hook('apt_update')
    14          self.apt_install = self.patch_hook('apt_install')
    15          self.ensure_package_status = self.patch_hook('ensure_package_status')
    16          self.enable_haproxy = self.patch_hook('enable_haproxy')
    17          self.config_get = self.patch_hook('config_get')
    18          path_exists = patch.object(os.path, "exists")
    19          self.path_exists = path_exists.start()
    20          self.path_exists.return_value = True
    21          self.addCleanup(path_exists.stop)
    22  
    23      def patch_hook(self, hook_name):
    24          mock_controller = patch.object(hooks, hook_name)
    25          mock = mock_controller.start()
    26          self.addCleanup(mock_controller.stop)
    27          return mock
    28  
    29      @patch('os.mkdir')
    30      def test_makes_config_dir(self, mkdir):
    31          self.path_exists.return_value = False
    32          hooks.install_hook()
    33          self.path_exists.assert_called_once_with(
    34              hooks.default_haproxy_service_config_dir)
    35          mkdir.assert_called_once_with(
    36              hooks.default_haproxy_service_config_dir, 0600)
    37  
    38      @patch('os.mkdir')
    39      def test_config_dir_already_exists(self, mkdir):
    40          hooks.install_hook()
    41          self.path_exists.assert_called_once_with(
    42              hooks.default_haproxy_service_config_dir)
    43          self.assertFalse(mkdir.called)
    44  
    45      def test_install_packages(self):
    46          hooks.install_hook()
    47          calls = self.apt_install.call_args_list
    48          self.assertEqual((['haproxy', 'python-jinja2'],), calls[0][0])
    49          self.assertEqual({'fatal': True}, calls[0][1])
    50          self.assertEqual(
    51              (['python-pyasn1', 'python-pyasn1-modules'],), calls[1][0])
    52          self.assertEqual({'fatal': False}, calls[1][1])
    53  
    54      def test_add_source(self):
    55          hooks.install_hook()
    56          self.config_get.assert_called_once_with()
    57          self.add_source.assert_called_once_with(
    58              self.config_get.return_value.get("source"),
    59              self.config_get.return_value.get("key"))
    60  
    61      def test_apt_update(self):
    62          hooks.install_hook()
    63          self.apt_update.assert_called_once_with(fatal=True)
    64  
    65      def test_add_source_with_backports(self):
    66          self.config_get.return_value = {
    67              'source': 'backports', 'package_status': 'install'}
    68          with patch("hooks.lsb_release") as lsb_release:
    69              lsb_release.return_value = {'DISTRIB_CODENAME': 'trusty'}
    70              with patch("hooks.add_backports_preferences") as add_apt_prefs:
    71                  hooks.install_hook()
    72                  add_apt_prefs.assert_called_once_with('trusty')
    73          self.config_get.assert_called_once_with()
    74          source = ("deb http://archive.ubuntu.com/ubuntu trusty-backports "
    75                    "main restricted universe multiverse")
    76          self.add_source.assert_called_once_with(
    77              source,
    78              self.config_get.return_value.get("key"))
    79  
    80      def test_ensures_package_status(self):
    81          hooks.install_hook()
    82          self.config_get.assert_called_once_with()
    83          self.ensure_package_status.assert_called_once_with(
    84              hooks.service_affecting_packages,
    85              self.config_get.return_value["package_status"])
    86  
    87      def test_calls_enable_haproxy(self):
    88          hooks.install_hook()
    89          self.enable_haproxy.assert_called_once_with()