summaryrefslogtreecommitdiff
path: root/tests/unittests/config/test_cc_ssh_import_id.py
blob: ffeee92e589742afd54f0fa6dce73d9c9d428586 (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
# This file is part of cloud-init. See LICENSE file for license information.

import logging
from unittest import mock

import pytest

from cloudinit.config import cc_ssh_import_id
from tests.unittests.util import get_cloud

LOG = logging.getLogger(__name__)

MODPATH = "cloudinit.config.cc_ssh_import_ids."


class TestIsKeyInNestedDict:
    @pytest.mark.parametrize(
        "cfg,expected",
        (
            ({}, False),
            ({"users": [{"name": "bob"}]}, False),
            ({"ssh_import_id": ["yep"]}, True),
            ({"ssh_import_id": ["yep"], "users": [{"name": "bob"}]}, True),
            (
                {
                    "apt": {"preserve_sources_list": True},
                    "ssh_import_id": ["yep"],
                    "users": [{"name": "bob"}],
                },
                True,
            ),
            (
                {
                    "apt": [{}],
                    "ssh_import_id": ["yep"],
                    "users": [{"name": "bob"}],
                },
                True,
            ),
            (
                {
                    "apt": {"preserve_sources_list": True},
                    "users": [
                        {"name": "bob"},
                        {"name": "judy", "ssh_import_id": ["yep"]},
                    ],
                },
                True,
            ),
        ),
    )
    def test_find_ssh_import_id_directives(self, cfg, expected):
        assert expected is cc_ssh_import_id.is_key_in_nested_dict(
            cfg, "ssh_import_id"
        )


class TestHandleSshImportIDs:
    """Test cc_ssh_import_id handling of config."""

    @pytest.mark.parametrize(
        "cfg,log",
        (
            ({}, "no 'ssh_import_id' directives found"),
            (
                {"users": [{"name": "bob"}]},
                "no 'ssh_import_id' directives found",
            ),
            ({"ssh_import_id": ["bobkey"]}, "ssh-import-id is not installed"),
        ),
    )
    @mock.patch("cloudinit.subp.which")
    def test_skip_inapplicable_configs(self, m_which, cfg, log, caplog):
        """Skip config without ssh_import_id"""
        m_which.return_value = None
        cloud = get_cloud("ubuntu")
        cc_ssh_import_id.handle("name", cfg, cloud, LOG, [])
        assert log in caplog.text