summaryrefslogtreecommitdiff
path: root/tests/unittests/config/test_cc_ubuntu_drivers.py
blob: 822c79cea0af99c91d9240fcadea7f02afdc23a2 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# This file is part of cloud-init. See LICENSE file for license information.

import copy
import os
import re

import pytest

from cloudinit import log
from cloudinit.config import cc_ubuntu_drivers as drivers
from cloudinit.config.schema import (
    SchemaValidationError,
    get_schema,
    validate_cloudconfig_schema,
)
from cloudinit.subp import ProcessExecutionError
from tests.unittests.helpers import mock, skipUnlessJsonSchema

MPATH = "cloudinit.config.cc_ubuntu_drivers."
M_TMP_PATH = MPATH + "temp_utils.mkdtemp"
OLD_UBUNTU_DRIVERS_ERROR_STDERR = (
    "ubuntu-drivers: error: argument <command>: invalid choice: 'install' "
    "(choose from 'list', 'autoinstall', 'devices', 'debug')\n"
)


# The tests in this module call helper methods which are decorated with
# mock.patch.  pylint doesn't understand that mock.patch passes parameters to
# the decorated function, so it incorrectly reports that we aren't passing
# values for all parameters.  Instead of annotating every single call, we
# disable it for the entire module:
#  pylint: disable=no-value-for-parameter


@pytest.mark.parametrize(
    "cfg_accepted,install_gpgpu",
    [
        pytest.param(
            {"drivers": {"nvidia": {"license-accepted": True}}},
            ["ubuntu-drivers", "install", "--gpgpu", "nvidia"],
            id="without_version",
        ),
        pytest.param(
            {
                "drivers": {
                    "nvidia": {"license-accepted": True, "version": "123"}
                }
            },
            ["ubuntu-drivers", "install", "--gpgpu", "nvidia:123"],
            id="with_version",
        ),
    ],
)
@mock.patch(MPATH + "debconf")
@mock.patch(MPATH + "HAS_DEBCONF", True)
class TestUbuntuDrivers:
    install_gpgpu = ["ubuntu-drivers", "install", "--gpgpu", "nvidia"]

    @pytest.mark.parametrize(
        "true_value",
        [
            True,
            "yes",
            "true",
            "on",
            "1",
        ],
    )
    @mock.patch(M_TMP_PATH)
    @mock.patch(MPATH + "subp.subp", return_value=("", ""))
    @mock.patch(MPATH + "subp.which", return_value=False)
    def test_happy_path_taken(
        self,
        m_which,
        m_subp,
        m_tmp,
        m_debconf,
        tmpdir,
        cfg_accepted,
        install_gpgpu,
        true_value,
    ):
        """Positive path test through handle. Package should be installed."""
        new_config: dict = copy.deepcopy(cfg_accepted)
        new_config["drivers"]["nvidia"]["license-accepted"] = true_value

        tdir = tmpdir
        debconf_file = tdir.join("nvidia.template")
        m_tmp.return_value = tdir
        myCloud = mock.MagicMock()
        drivers.handle("ubuntu_drivers", new_config, myCloud, None)
        assert [
            mock.call(drivers.X_LOADTEMPLATEFILE, debconf_file)
        ] == m_debconf.DebconfCommunicator().__enter__().command.call_args_list
        assert [
            mock.call(["ubuntu-drivers-common"])
        ] == myCloud.distro.install_packages.call_args_list
        assert [mock.call(install_gpgpu)] == m_subp.call_args_list

    @mock.patch(M_TMP_PATH)
    @mock.patch(MPATH + "subp.subp")
    @mock.patch(MPATH + "subp.which", return_value=False)
    def test_handle_raises_error_if_no_drivers_found(
        self,
        m_which,
        m_subp,
        m_tmp,
        m_debconf,
        caplog,
        tmpdir,
        cfg_accepted,
        install_gpgpu,
    ):
        """If ubuntu-drivers doesn't install any drivers, raise an error."""
        tdir = tmpdir
        debconf_file = os.path.join(tdir, "nvidia.template")
        m_tmp.return_value = tdir
        myCloud = mock.MagicMock()

        m_subp.side_effect = ProcessExecutionError(
            stdout="No drivers found for installation.\n", exit_code=1
        )

        with pytest.raises(Exception):
            drivers.handle("ubuntu_drivers", cfg_accepted, myCloud, None)
        assert [
            mock.call(drivers.X_LOADTEMPLATEFILE, debconf_file)
        ] == m_debconf.DebconfCommunicator().__enter__().command.call_args_list
        assert [
            mock.call(["ubuntu-drivers-common"])
        ] == myCloud.distro.install_packages.call_args_list
        assert [mock.call(install_gpgpu)] == m_subp.call_args_list
        assert (
            "ubuntu-drivers found no drivers for installation" in caplog.text
        )

    @pytest.mark.parametrize(
        "config",
        [
            pytest.param(
                {"drivers": {"nvidia": {"license-accepted": False}}},
                id="license_not_accepted",
            ),
            pytest.param(
                {"drivers": {"nvidia": {"license-accepted": "garbage"}}},
                id="garbage_in_license_field",
            ),
            pytest.param({"drivers": {"nvidia": {}}}, id="no_license_key"),
            pytest.param(
                {"drivers": {"acme": {"license-accepted": True}}},
                id="no_nvidia_key",
            ),
            # ensure we don't do anything if string refusal given
            pytest.param(
                {"drivers": {"nvidia": {"license-accepted": "no"}}},
                id="string_given_no",
            ),
            pytest.param(
                {"drivers": {"nvidia": {"license-accepted": "false"}}},
                id="string_given_false",
            ),
            pytest.param(
                {"drivers": {"nvidia": {"license-accepted": "off"}}},
                id="string_given_off",
            ),
            pytest.param(
                {"drivers": {"nvidia": {"license-accepted": "0"}}},
                id="string_given_0",
            ),
            # specifying_a_version_doesnt_override_license_acceptance
            pytest.param(
                {
                    "drivers": {
                        "nvidia": {"license-accepted": False, "version": "123"}
                    }
                },
                id="with_version",
            ),
        ],
    )
    @mock.patch(MPATH + "subp.subp", return_value=("", ""))
    @mock.patch(MPATH + "subp.which", return_value=False)
    def test_handle_inert(
        self, m_which, m_subp, m_debconf, cfg_accepted, install_gpgpu, config
    ):
        """Helper to reduce repetition when testing negative cases"""
        myCloud = mock.MagicMock()
        drivers.handle("ubuntu_drivers", config, myCloud, None)
        assert 0 == myCloud.distro.install_packages.call_count
        assert 0 == m_subp.call_count

    @mock.patch(MPATH + "install_drivers")
    @mock.patch(MPATH + "LOG")
    def test_handle_no_drivers_does_nothing(
        self, m_log, m_install_drivers, m_debconf, cfg_accepted, install_gpgpu
    ):
        """If no 'drivers' key in the config, nothing should be done."""
        myCloud = mock.MagicMock()
        drivers.handle("ubuntu_drivers", {"foo": "bzr"}, myCloud, None)
        assert "Skipping module named" in m_log.debug.call_args_list[0][0][0]
        assert 0 == m_install_drivers.call_count

    @mock.patch(M_TMP_PATH)
    @mock.patch(MPATH + "subp.subp", return_value=("", ""))
    @mock.patch(MPATH + "subp.which", return_value=True)
    def test_install_drivers_no_install_if_present(
        self,
        m_which,
        m_subp,
        m_tmp,
        m_debconf,
        tmpdir,
        cfg_accepted,
        install_gpgpu,
    ):
        """If 'ubuntu-drivers' is present, no package install should occur."""
        tdir = tmpdir
        debconf_file = tmpdir.join("nvidia.template")
        m_tmp.return_value = tdir
        pkg_install = mock.MagicMock()
        distro = mock.Mock()
        drivers.install_drivers(
            cfg_accepted["drivers"],
            pkg_install_func=pkg_install,
            distro=distro,
        )
        assert 0 == pkg_install.call_count
        assert [mock.call("ubuntu-drivers")] == m_which.call_args_list
        assert [
            mock.call(drivers.X_LOADTEMPLATEFILE, debconf_file)
        ] == m_debconf.DebconfCommunicator().__enter__().command.call_args_list
        assert [mock.call(install_gpgpu)] == m_subp.call_args_list

    def test_install_drivers_rejects_invalid_config(
        self, m_debconf, cfg_accepted, install_gpgpu
    ):
        """install_drivers should raise TypeError if not given a config dict"""
        pkg_install = mock.MagicMock()
        distro = mock.Mock()
        with pytest.raises(TypeError, match=".*expected dict.*"):
            drivers.install_drivers(
                "mystring", pkg_install_func=pkg_install, distro=distro
            )
        assert 0 == pkg_install.call_count

    @mock.patch(M_TMP_PATH)
    @mock.patch(MPATH + "subp.subp")
    @mock.patch(MPATH + "subp.which", return_value=False)
    def test_install_drivers_handles_old_ubuntu_drivers_gracefully(
        self,
        m_which,
        m_subp,
        m_tmp,
        m_debconf,
        caplog,
        tmpdir,
        cfg_accepted,
        install_gpgpu,
    ):
        """Older ubuntu-drivers versions should emit message and raise error"""
        debconf_file = tmpdir.join("nvidia.template")
        m_tmp.return_value = tmpdir
        myCloud = mock.MagicMock()

        m_subp.side_effect = ProcessExecutionError(
            stderr=OLD_UBUNTU_DRIVERS_ERROR_STDERR, exit_code=2
        )

        with pytest.raises(Exception):
            drivers.handle("ubuntu_drivers", cfg_accepted, myCloud, None)
        assert [
            mock.call(drivers.X_LOADTEMPLATEFILE, debconf_file)
        ] == m_debconf.DebconfCommunicator().__enter__().command.call_args_list
        assert [
            mock.call(["ubuntu-drivers-common"])
        ] == myCloud.distro.install_packages.call_args_list
        assert [mock.call(install_gpgpu)] == m_subp.call_args_list
        assert (
            MPATH[:-1],
            log.WARNING,
            (
                "the available version of ubuntu-drivers is"
                " too old to perform requested driver installation"
            ),
        ) == caplog.record_tuples[-1]

    @mock.patch(M_TMP_PATH)
    @mock.patch(MPATH + "subp.subp", return_value=("", ""))
    @mock.patch(MPATH + "subp.which", return_value=False)
    def test_debconf_not_installed_does_nothing(
        self,
        m_which,
        m_subp,
        m_tmp,
        m_debconf,
        tmpdir,
        cfg_accepted,
        install_gpgpu,
    ):
        m_debconf.DebconfCommunicator.side_effect = AttributeError
        m_tmp.return_value = tmpdir
        myCloud = mock.MagicMock()
        version_none_cfg = {
            "drivers": {"nvidia": {"license-accepted": True, "version": None}}
        }
        with pytest.raises(AttributeError):
            drivers.handle("ubuntu_drivers", version_none_cfg, myCloud, None)
        assert (
            0 == m_debconf.DebconfCommunicator.__enter__().command.call_count
        )
        assert 0 == m_subp.call_count


@mock.patch(MPATH + "debconf")
@mock.patch(MPATH + "HAS_DEBCONF", True)
class TestUbuntuDriversWithVersion:
    """With-version specific tests"""

    cfg_accepted = {
        "drivers": {"nvidia": {"license-accepted": True, "version": "123"}}
    }
    install_gpgpu = ["ubuntu-drivers", "install", "--gpgpu", "nvidia:123"]

    @mock.patch(M_TMP_PATH)
    @mock.patch(MPATH + "subp.subp", return_value=("", ""))
    @mock.patch(MPATH + "subp.which", return_value=False)
    def test_version_none_uses_latest(
        self, m_which, m_subp, m_tmp, m_debconf, tmpdir
    ):
        debconf_file = tmpdir.join("nvidia.template")
        m_tmp.return_value = tmpdir
        myCloud = mock.MagicMock()
        version_none_cfg = {
            "drivers": {"nvidia": {"license-accepted": True, "version": None}}
        }
        drivers.handle("ubuntu_drivers", version_none_cfg, myCloud, None)
        assert [
            mock.call(drivers.X_LOADTEMPLATEFILE, debconf_file)
        ] == m_debconf.DebconfCommunicator().__enter__().command.call_args_list
        assert [
            mock.call(["ubuntu-drivers", "install", "--gpgpu", "nvidia"]),
        ] == m_subp.call_args_list


@mock.patch(MPATH + "debconf")
class TestUbuntuDriversNotRun:
    @mock.patch(MPATH + "HAS_DEBCONF", True)
    @mock.patch(M_TMP_PATH)
    @mock.patch(MPATH + "install_drivers")
    @mock.patch(MPATH + "LOG")
    def test_no_cfg_drivers_does_nothing(
        self,
        m_log,
        m_install_drivers,
        m_tmp,
        m_debconf,
        tmpdir,
    ):
        m_tmp.return_value = tmpdir
        myCloud = mock.MagicMock()
        version_none_cfg = {}
        drivers.handle("ubuntu_drivers", version_none_cfg, myCloud, None)
        assert 0 == m_install_drivers.call_count
        assert (
            mock.call(
                "Skipping module named %s, no 'drivers' key in config",
                "ubuntu_drivers",
            )
            == m_log.debug.call_args_list[-1]
        )

    @mock.patch(MPATH + "HAS_DEBCONF", False)
    @mock.patch(M_TMP_PATH)
    @mock.patch(MPATH + "install_drivers")
    @mock.patch(MPATH + "LOG")
    def test_has_not_debconf_does_nothing(
        self,
        m_log,
        m_install_drivers,
        m_tmp,
        m_debconf,
        tmpdir,
    ):
        m_tmp.return_value = tmpdir
        myCloud = mock.MagicMock()
        version_none_cfg = {"drivers": {"nvidia": {"license-accepted": True}}}
        drivers.handle("ubuntu_drivers", version_none_cfg, myCloud, None)
        assert 0 == m_install_drivers.call_count
        assert (
            mock.call(
                "Skipping module named %s, 'python3-debconf' is not installed",
                "ubuntu_drivers",
            )
            == m_log.warning.call_args_list[-1]
        )


class TestUbuntuAdvantageSchema:
    @pytest.mark.parametrize(
        "config, error_msg",
        [
            # Strict boolean license-accepted
            (
                {"drivers": {"nvidia": {"license-accepted": "TRUE"}}},
                "drivers.nvidia.license-accepted: 'TRUE' is not of type"
                " 'boolean'",
            ),
            # Additional properties disallowed
            (
                {"drivers": {"bogus": {"license-accepted": True}}},
                re.escape(
                    "drivers: Additional properties are not allowed ('bogus'"
                ),
            ),
            (
                {"drivers": {"nvidia": {"bogus": True}}},
                re.escape(
                    "drivers.nvidia: Additional properties are not allowed"
                    " ('bogus' "
                ),
            ),
        ],
    )
    @skipUnlessJsonSchema()
    def test_schema_validation(self, config, error_msg):
        if error_msg is None:
            validate_cloudconfig_schema(config, get_schema(), strict=True)
        else:
            with pytest.raises(SchemaValidationError, match=error_msg):
                validate_cloudconfig_schema(config, get_schema(), strict=True)


# vi: ts=4 expandtab