summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py
blob: b96fd4d40d2591db937d9f85c7c6072767e7f89a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# This file is part of cloud-init. See LICENSE file for license information.

""" test_apt_custom_sources_list
Test templating of custom sources list
"""
import logging
import os
import shutil
import tempfile
from unittest import mock
from unittest.mock import call

from cloudinit import cloud
from cloudinit import distros
from cloudinit import helpers
from cloudinit import subp
from cloudinit import util

from cloudinit.config import cc_apt_configure
from cloudinit.sources import DataSourceNone

from cloudinit.distros.debian import Distro

from cloudinit.tests import helpers as t_help

LOG = logging.getLogger(__name__)

TARGET = "/"

# Input and expected output for the custom template
YAML_TEXT_CUSTOM_SL = """
apt:
  primary:
    - arches: [default]
      uri: http://test.ubuntu.com/ubuntu/
  security:
    - arches: [default]
      uri: http://testsec.ubuntu.com/ubuntu/
  sources_list: |

      # Note, this file is written by cloud-init at install time. It should not
      # end up on the installed system itself.
      # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
      # newer versions of the distribution.
      deb $MIRROR $RELEASE main restricted
      deb-src $MIRROR $RELEASE main restricted
      deb $PRIMARY $RELEASE universe restricted
      deb $SECURITY $RELEASE-security multiverse
      # FIND_SOMETHING_SPECIAL
"""

EXPECTED_CONVERTED_CONTENT = """
# Note, this file is written by cloud-init at install time. It should not
# end up on the installed system itself.
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://test.ubuntu.com/ubuntu/ fakerel main restricted
deb-src http://test.ubuntu.com/ubuntu/ fakerel main restricted
deb http://test.ubuntu.com/ubuntu/ fakerel universe restricted
deb http://testsec.ubuntu.com/ubuntu/ fakerel-security multiverse
# FIND_SOMETHING_SPECIAL
"""

# mocked to be independent to the unittest system
MOCKED_APT_SRC_LIST = """
deb http://test.ubuntu.com/ubuntu/ notouched main restricted
deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted
deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted
deb http://testsec.ubuntu.com/ubuntu/ notouched-security main restricted
"""

EXPECTED_BASE_CONTENT = ("""
deb http://test.ubuntu.com/ubuntu/ notouched main restricted
deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted
deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted
deb http://testsec.ubuntu.com/ubuntu/ notouched-security main restricted
""")

EXPECTED_MIRROR_CONTENT = ("""
deb http://test.ubuntu.com/ubuntu/ notouched main restricted
deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted
deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted
deb http://test.ubuntu.com/ubuntu/ notouched-security main restricted
""")

EXPECTED_PRIMSEC_CONTENT = ("""
deb http://test.ubuntu.com/ubuntu/ notouched main restricted
deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted
deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted
deb http://testsec.ubuntu.com/ubuntu/ notouched-security main restricted
""")


class TestAptSourceConfigSourceList(t_help.FilesystemMockingTestCase):
    """TestAptSourceConfigSourceList - Class to test sources list rendering"""
    def setUp(self):
        super(TestAptSourceConfigSourceList, self).setUp()
        self.subp = subp.subp
        self.new_root = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.new_root)

        rpatcher = mock.patch("cloudinit.util.lsb_release")
        get_rel = rpatcher.start()
        get_rel.return_value = {'codename': "fakerel"}
        self.addCleanup(rpatcher.stop)
        apatcher = mock.patch("cloudinit.util.get_dpkg_architecture")
        get_arch = apatcher.start()
        get_arch.return_value = 'amd64'
        self.addCleanup(apatcher.stop)

    def _get_cloud(self, distro, metadata=None):
        self.patchUtils(self.new_root)
        paths = helpers.Paths({})
        cls = distros.fetch(distro)
        mydist = cls(distro, {}, paths)
        myds = DataSourceNone.DataSourceNone({}, mydist, paths)
        if metadata:
            myds.metadata.update(metadata)
        return cloud.Cloud(myds, paths, {}, mydist, None)

    def _apt_source_list(self, distro, cfg, cfg_on_empty=False):
        """_apt_source_list - Test rendering from template (generic)"""
        # entry at top level now, wrap in 'apt' key
        cfg = {'apt': cfg}
        mycloud = self._get_cloud(distro)

        with mock.patch.object(util, 'write_file') as mock_writefile:
            with mock.patch.object(util, 'load_file',
                                   return_value=MOCKED_APT_SRC_LIST
                                   ) as mock_loadfile:
                with mock.patch.object(os.path, 'isfile',
                                       return_value=True) as mock_isfile:
                    cfg_func = ('cloudinit.config.cc_apt_configure.' +
                                '_should_configure_on_empty_apt')
                    with mock.patch(cfg_func,
                                    return_value=(cfg_on_empty, "test")
                                    ) as mock_shouldcfg:
                        cc_apt_configure.handle("test", cfg, mycloud, LOG,
                                                None)

        return mock_writefile, mock_loadfile, mock_isfile, mock_shouldcfg

    def test_apt_v3_source_list_debian(self):
        """test_apt_v3_source_list_debian - without custom sources or parms"""
        cfg = {}
        distro = 'debian'
        expected = EXPECTED_BASE_CONTENT

        mock_writefile, mock_load_file, mock_isfile, mock_shouldcfg = (
            self._apt_source_list(distro, cfg, cfg_on_empty=True))

        template = '/etc/cloud/templates/sources.list.%s.tmpl' % distro
        mock_writefile.assert_called_once_with('/etc/apt/sources.list',
                                               expected, mode=0o644)
        mock_load_file.assert_called_with(template)
        mock_isfile.assert_any_call(template)
        self.assertEqual(1, mock_shouldcfg.call_count)

    def test_apt_v3_source_list_ubuntu(self):
        """test_apt_v3_source_list_ubuntu - without custom sources or parms"""
        cfg = {}
        distro = 'ubuntu'
        expected = EXPECTED_BASE_CONTENT

        mock_writefile, mock_load_file, mock_isfile, mock_shouldcfg = (
            self._apt_source_list(distro, cfg, cfg_on_empty=True))

        template = '/etc/cloud/templates/sources.list.%s.tmpl' % distro
        mock_writefile.assert_called_once_with('/etc/apt/sources.list',
                                               expected, mode=0o644)
        mock_load_file.assert_called_with(template)
        mock_isfile.assert_any_call(template)
        self.assertEqual(1, mock_shouldcfg.call_count)

    def test_apt_v3_source_list_ubuntu_snappy(self):
        """test_apt_v3_source_list_ubuntu_snappy - without custom sources or
        parms"""
        cfg = {'apt': {}}
        mycloud = self._get_cloud('ubuntu')

        with mock.patch.object(util, 'write_file') as mock_writefile:
            with mock.patch.object(util, 'system_is_snappy',
                                   return_value=True) as mock_issnappy:
                cc_apt_configure.handle("test", cfg, mycloud, LOG, None)

        self.assertEqual(0, mock_writefile.call_count)
        self.assertEqual(1, mock_issnappy.call_count)

    def test_apt_v3_source_list_centos(self):
        """test_apt_v3_source_list_centos - without custom sources or parms"""
        cfg = {}
        distro = 'rhel'

        mock_writefile, _, _, _ = self._apt_source_list(distro, cfg)

        self.assertEqual(0, mock_writefile.call_count)

    def test_apt_v3_source_list_psm(self):
        """test_apt_v3_source_list_psm - Test specifying prim+sec mirrors"""
        pm = 'http://test.ubuntu.com/ubuntu/'
        sm = 'http://testsec.ubuntu.com/ubuntu/'
        cfg = {'preserve_sources_list': False,
               'primary': [{'arches': ["default"],
                            'uri': pm}],
               'security': [{'arches': ["default"],
                             'uri': sm}]}
        distro = 'ubuntu'
        expected = EXPECTED_PRIMSEC_CONTENT

        mock_writefile, mock_load_file, mock_isfile, _ = (
            self._apt_source_list(distro, cfg, cfg_on_empty=True))

        template = '/etc/cloud/templates/sources.list.%s.tmpl' % distro
        mock_writefile.assert_called_once_with('/etc/apt/sources.list',
                                               expected, mode=0o644)
        mock_load_file.assert_called_with(template)
        mock_isfile.assert_any_call(template)

    def test_apt_v3_srcl_custom(self):
        """test_apt_v3_srcl_custom - Test rendering a custom source template"""
        cfg = util.load_yaml(YAML_TEXT_CUSTOM_SL)
        mycloud = self._get_cloud('ubuntu')

        # the second mock restores the original subp
        with mock.patch.object(util, 'write_file') as mockwrite:
            with mock.patch.object(subp, 'subp', self.subp):
                with mock.patch.object(Distro, 'get_primary_arch',
                                       return_value='amd64'):
                    cc_apt_configure.handle("notimportant", cfg, mycloud,
                                            LOG, None)

        calls = [call('/etc/apt/sources.list',
                      EXPECTED_CONVERTED_CONTENT,
                      mode=0o644)]
        mockwrite.assert_has_calls(calls)


# vi: ts=4 expandtab