Blame tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py

Packit Service a04d08
# This file is part of cloud-init. See LICENSE file for license information.
Packit Service a04d08
Packit Service a04d08
""" test_apt_custom_sources_list
Packit Service a04d08
Test templating of custom sources list
Packit Service a04d08
"""
Packit Service a04d08
import logging
Packit Service a04d08
import os
Packit Service a04d08
import shutil
Packit Service a04d08
import tempfile
Packit Service 751c4a
from unittest import mock
Packit Service 751c4a
from unittest.mock import call
Packit Service a04d08
Packit Service a04d08
from cloudinit import cloud
Packit Service a04d08
from cloudinit import distros
Packit Service a04d08
from cloudinit import helpers
Packit Service 751c4a
from cloudinit import subp
Packit Service a04d08
from cloudinit import util
Packit Service a04d08
Packit Service a04d08
from cloudinit.config import cc_apt_configure
Packit Service a04d08
from cloudinit.sources import DataSourceNone
Packit Service a04d08
Packit Service a04d08
from cloudinit.distros.debian import Distro
Packit Service a04d08
Packit Service a04d08
from cloudinit.tests import helpers as t_help
Packit Service a04d08
Packit Service a04d08
LOG = logging.getLogger(__name__)
Packit Service a04d08
Packit Service a04d08
TARGET = "/"
Packit Service a04d08
Packit Service a04d08
# Input and expected output for the custom template
Packit Service a04d08
YAML_TEXT_CUSTOM_SL = """
Packit Service a04d08
apt:
Packit Service a04d08
  primary:
Packit Service a04d08
    - arches: [default]
Packit Service a04d08
      uri: http://test.ubuntu.com/ubuntu/
Packit Service a04d08
  security:
Packit Service a04d08
    - arches: [default]
Packit Service a04d08
      uri: http://testsec.ubuntu.com/ubuntu/
Packit Service a04d08
  sources_list: |
Packit Service a04d08
Packit Service a04d08
      # Note, this file is written by cloud-init at install time. It should not
Packit Service a04d08
      # end up on the installed system itself.
Packit Service a04d08
      # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
Packit Service a04d08
      # newer versions of the distribution.
Packit Service a04d08
      deb $MIRROR $RELEASE main restricted
Packit Service a04d08
      deb-src $MIRROR $RELEASE main restricted
Packit Service a04d08
      deb $PRIMARY $RELEASE universe restricted
Packit Service a04d08
      deb $SECURITY $RELEASE-security multiverse
Packit Service a04d08
      # FIND_SOMETHING_SPECIAL
Packit Service a04d08
"""
Packit Service a04d08
Packit Service a04d08
EXPECTED_CONVERTED_CONTENT = """
Packit Service a04d08
# Note, this file is written by cloud-init at install time. It should not
Packit Service a04d08
# end up on the installed system itself.
Packit Service a04d08
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
Packit Service a04d08
# newer versions of the distribution.
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ fakerel main restricted
Packit Service a04d08
deb-src http://test.ubuntu.com/ubuntu/ fakerel main restricted
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ fakerel universe restricted
Packit Service a04d08
deb http://testsec.ubuntu.com/ubuntu/ fakerel-security multiverse
Packit Service a04d08
# FIND_SOMETHING_SPECIAL
Packit Service a04d08
"""
Packit Service a04d08
Packit Service a04d08
# mocked to be independent to the unittest system
Packit Service a04d08
MOCKED_APT_SRC_LIST = """
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ notouched main restricted
Packit Service a04d08
deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted
Packit Service a04d08
deb http://testsec.ubuntu.com/ubuntu/ notouched-security main restricted
Packit Service a04d08
"""
Packit Service a04d08
Packit Service a04d08
EXPECTED_BASE_CONTENT = ("""
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ notouched main restricted
Packit Service a04d08
deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted
Packit Service a04d08
deb http://testsec.ubuntu.com/ubuntu/ notouched-security main restricted
Packit Service a04d08
""")
Packit Service a04d08
Packit Service a04d08
EXPECTED_MIRROR_CONTENT = ("""
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ notouched main restricted
Packit Service a04d08
deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ notouched-security main restricted
Packit Service a04d08
""")
Packit Service a04d08
Packit Service a04d08
EXPECTED_PRIMSEC_CONTENT = ("""
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ notouched main restricted
Packit Service a04d08
deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted
Packit Service a04d08
deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted
Packit Service a04d08
deb http://testsec.ubuntu.com/ubuntu/ notouched-security main restricted
Packit Service a04d08
""")
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
class TestAptSourceConfigSourceList(t_help.FilesystemMockingTestCase):
Packit Service a04d08
    """TestAptSourceConfigSourceList - Class to test sources list rendering"""
Packit Service a04d08
    def setUp(self):
Packit Service a04d08
        super(TestAptSourceConfigSourceList, self).setUp()
Packit Service 751c4a
        self.subp = subp.subp
Packit Service a04d08
        self.new_root = tempfile.mkdtemp()
Packit Service a04d08
        self.addCleanup(shutil.rmtree, self.new_root)
Packit Service a04d08
Packit Service a04d08
        rpatcher = mock.patch("cloudinit.util.lsb_release")
Packit Service a04d08
        get_rel = rpatcher.start()
Packit Service a04d08
        get_rel.return_value = {'codename': "fakerel"}
Packit Service a04d08
        self.addCleanup(rpatcher.stop)
Packit Service 751c4a
        apatcher = mock.patch("cloudinit.util.get_dpkg_architecture")
Packit Service a04d08
        get_arch = apatcher.start()
Packit Service a04d08
        get_arch.return_value = 'amd64'
Packit Service a04d08
        self.addCleanup(apatcher.stop)
Packit Service a04d08
Packit Service a04d08
    def _get_cloud(self, distro, metadata=None):
Packit Service a04d08
        self.patchUtils(self.new_root)
Packit Service a04d08
        paths = helpers.Paths({})
Packit Service a04d08
        cls = distros.fetch(distro)
Packit Service a04d08
        mydist = cls(distro, {}, paths)
Packit Service a04d08
        myds = DataSourceNone.DataSourceNone({}, mydist, paths)
Packit Service a04d08
        if metadata:
Packit Service a04d08
            myds.metadata.update(metadata)
Packit Service a04d08
        return cloud.Cloud(myds, paths, {}, mydist, None)
Packit Service a04d08
Packit Service a04d08
    def _apt_source_list(self, distro, cfg, cfg_on_empty=False):
Packit Service a04d08
        """_apt_source_list - Test rendering from template (generic)"""
Packit Service a04d08
        # entry at top level now, wrap in 'apt' key
Packit Service a04d08
        cfg = {'apt': cfg}
Packit Service a04d08
        mycloud = self._get_cloud(distro)
Packit Service a04d08
Packit Service a04d08
        with mock.patch.object(util, 'write_file') as mock_writefile:
Packit Service a04d08
            with mock.patch.object(util, 'load_file',
Packit Service a04d08
                                   return_value=MOCKED_APT_SRC_LIST
Packit Service a04d08
                                   ) as mock_loadfile:
Packit Service a04d08
                with mock.patch.object(os.path, 'isfile',
Packit Service a04d08
                                       return_value=True) as mock_isfile:
Packit Service a04d08
                    cfg_func = ('cloudinit.config.cc_apt_configure.' +
Packit Service a04d08
                                '_should_configure_on_empty_apt')
Packit Service a04d08
                    with mock.patch(cfg_func,
Packit Service a04d08
                                    return_value=(cfg_on_empty, "test")
Packit Service a04d08
                                    ) as mock_shouldcfg:
Packit Service a04d08
                        cc_apt_configure.handle("test", cfg, mycloud, LOG,
Packit Service a04d08
                                                None)
Packit Service a04d08
Packit Service a04d08
        return mock_writefile, mock_loadfile, mock_isfile, mock_shouldcfg
Packit Service a04d08
Packit Service a04d08
    def test_apt_v3_source_list_debian(self):
Packit Service a04d08
        """test_apt_v3_source_list_debian - without custom sources or parms"""
Packit Service a04d08
        cfg = {}
Packit Service a04d08
        distro = 'debian'
Packit Service a04d08
        expected = EXPECTED_BASE_CONTENT
Packit Service a04d08
Packit Service a04d08
        mock_writefile, mock_load_file, mock_isfile, mock_shouldcfg = (
Packit Service a04d08
            self._apt_source_list(distro, cfg, cfg_on_empty=True))
Packit Service a04d08
Packit Service a04d08
        template = '/etc/cloud/templates/sources.list.%s.tmpl' % distro
Packit Service a04d08
        mock_writefile.assert_called_once_with('/etc/apt/sources.list',
Packit Service a04d08
                                               expected, mode=0o644)
Packit Service a04d08
        mock_load_file.assert_called_with(template)
Packit Service a04d08
        mock_isfile.assert_any_call(template)
Packit Service a04d08
        self.assertEqual(1, mock_shouldcfg.call_count)
Packit Service a04d08
Packit Service a04d08
    def test_apt_v3_source_list_ubuntu(self):
Packit Service a04d08
        """test_apt_v3_source_list_ubuntu - without custom sources or parms"""
Packit Service a04d08
        cfg = {}
Packit Service a04d08
        distro = 'ubuntu'
Packit Service a04d08
        expected = EXPECTED_BASE_CONTENT
Packit Service a04d08
Packit Service a04d08
        mock_writefile, mock_load_file, mock_isfile, mock_shouldcfg = (
Packit Service a04d08
            self._apt_source_list(distro, cfg, cfg_on_empty=True))
Packit Service a04d08
Packit Service a04d08
        template = '/etc/cloud/templates/sources.list.%s.tmpl' % distro
Packit Service a04d08
        mock_writefile.assert_called_once_with('/etc/apt/sources.list',
Packit Service a04d08
                                               expected, mode=0o644)
Packit Service a04d08
        mock_load_file.assert_called_with(template)
Packit Service a04d08
        mock_isfile.assert_any_call(template)
Packit Service a04d08
        self.assertEqual(1, mock_shouldcfg.call_count)
Packit Service a04d08
Packit Service a04d08
    def test_apt_v3_source_list_ubuntu_snappy(self):
Packit Service a04d08
        """test_apt_v3_source_list_ubuntu_snappy - without custom sources or
Packit Service a04d08
        parms"""
Packit Service a04d08
        cfg = {'apt': {}}
Packit Service a04d08
        mycloud = self._get_cloud('ubuntu')
Packit Service a04d08
Packit Service a04d08
        with mock.patch.object(util, 'write_file') as mock_writefile:
Packit Service a04d08
            with mock.patch.object(util, 'system_is_snappy',
Packit Service a04d08
                                   return_value=True) as mock_issnappy:
Packit Service a04d08
                cc_apt_configure.handle("test", cfg, mycloud, LOG, None)
Packit Service a04d08
Packit Service a04d08
        self.assertEqual(0, mock_writefile.call_count)
Packit Service a04d08
        self.assertEqual(1, mock_issnappy.call_count)
Packit Service a04d08
Packit Service a04d08
    def test_apt_v3_source_list_centos(self):
Packit Service a04d08
        """test_apt_v3_source_list_centos - without custom sources or parms"""
Packit Service a04d08
        cfg = {}
Packit Service a04d08
        distro = 'rhel'
Packit Service a04d08
Packit Service a04d08
        mock_writefile, _, _, _ = self._apt_source_list(distro, cfg)
Packit Service a04d08
Packit Service a04d08
        self.assertEqual(0, mock_writefile.call_count)
Packit Service a04d08
Packit Service a04d08
    def test_apt_v3_source_list_psm(self):
Packit Service a04d08
        """test_apt_v3_source_list_psm - Test specifying prim+sec mirrors"""
Packit Service a04d08
        pm = 'http://test.ubuntu.com/ubuntu/'
Packit Service a04d08
        sm = 'http://testsec.ubuntu.com/ubuntu/'
Packit Service a04d08
        cfg = {'preserve_sources_list': False,
Packit Service a04d08
               'primary': [{'arches': ["default"],
Packit Service a04d08
                            'uri': pm}],
Packit Service a04d08
               'security': [{'arches': ["default"],
Packit Service a04d08
                             'uri': sm}]}
Packit Service a04d08
        distro = 'ubuntu'
Packit Service a04d08
        expected = EXPECTED_PRIMSEC_CONTENT
Packit Service a04d08
Packit Service a04d08
        mock_writefile, mock_load_file, mock_isfile, _ = (
Packit Service a04d08
            self._apt_source_list(distro, cfg, cfg_on_empty=True))
Packit Service a04d08
Packit Service a04d08
        template = '/etc/cloud/templates/sources.list.%s.tmpl' % distro
Packit Service a04d08
        mock_writefile.assert_called_once_with('/etc/apt/sources.list',
Packit Service a04d08
                                               expected, mode=0o644)
Packit Service a04d08
        mock_load_file.assert_called_with(template)
Packit Service a04d08
        mock_isfile.assert_any_call(template)
Packit Service a04d08
Packit Service a04d08
    def test_apt_v3_srcl_custom(self):
Packit Service a04d08
        """test_apt_v3_srcl_custom - Test rendering a custom source template"""
Packit Service a04d08
        cfg = util.load_yaml(YAML_TEXT_CUSTOM_SL)
Packit Service a04d08
        mycloud = self._get_cloud('ubuntu')
Packit Service a04d08
Packit Service a04d08
        # the second mock restores the original subp
Packit Service a04d08
        with mock.patch.object(util, 'write_file') as mockwrite:
Packit Service 751c4a
            with mock.patch.object(subp, 'subp', self.subp):
Packit Service a04d08
                with mock.patch.object(Distro, 'get_primary_arch',
Packit Service a04d08
                                       return_value='amd64'):
Packit Service a04d08
                    cc_apt_configure.handle("notimportant", cfg, mycloud,
Packit Service a04d08
                                            LOG, None)
Packit Service a04d08
Packit Service a04d08
        calls = [call('/etc/apt/sources.list',
Packit Service a04d08
                      EXPECTED_CONVERTED_CONTENT,
Packit Service a04d08
                      mode=0o644)]
Packit Service a04d08
        mockwrite.assert_has_calls(calls)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
# vi: ts=4 expandtab