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

from cloudinit.config import cc_resolv_conf

from cloudinit import cloud
from cloudinit import distros
from cloudinit import helpers
from cloudinit import util
from copy import deepcopy

from cloudinit.tests import helpers as t_help

import logging
import os
import shutil
import tempfile
from unittest import mock

LOG = logging.getLogger(__name__)


class TestResolvConf(t_help.FilesystemMockingTestCase):
    with_logs = True
    cfg = {'manage_resolv_conf': True, 'resolv_conf': {}}

    def setUp(self):
        super(TestResolvConf, self).setUp()
        self.tmp = tempfile.mkdtemp()
        util.ensure_dir(os.path.join(self.tmp, 'data'))
        self.addCleanup(shutil.rmtree, self.tmp)

    def _fetch_distro(self, kind, conf=None):
        cls = distros.fetch(kind)
        paths = helpers.Paths({'cloud_dir': self.tmp})
        conf = {} if conf is None else conf
        return cls(kind, conf, paths)

    def call_resolv_conf_handler(self, distro_name, conf, cc=None):
        if not cc:
            ds = None
            distro = self._fetch_distro(distro_name, conf)
            paths = helpers.Paths({'cloud_dir': self.tmp})
            cc = cloud.Cloud(ds, paths, {}, distro, None)
        cc_resolv_conf.handle('cc_resolv_conf', conf, cc, LOG, [])

    @mock.patch("cloudinit.config.cc_resolv_conf.templater.render_to_file")
    def test_resolv_conf_systemd_resolved(self, m_render_to_file):
        self.call_resolv_conf_handler('photon', self.cfg)

        assert [
            mock.call(mock.ANY, '/etc/systemd/resolved.conf', mock.ANY)
        ] == m_render_to_file.call_args_list

    @mock.patch("cloudinit.config.cc_resolv_conf.templater.render_to_file")
    def test_resolv_conf_no_param(self, m_render_to_file):
        tmp = deepcopy(self.cfg)
        self.logs.truncate(0)
        tmp.pop('resolv_conf')
        self.call_resolv_conf_handler('photon', tmp)

        self.assertIn('manage_resolv_conf True but no parameters provided',
                      self.logs.getvalue())
        assert [
            mock.call(mock.ANY, '/etc/systemd/resolved.conf', mock.ANY)
        ] not in m_render_to_file.call_args_list

    @mock.patch("cloudinit.config.cc_resolv_conf.templater.render_to_file")
    def test_resolv_conf_manage_resolv_conf_false(self, m_render_to_file):
        tmp = deepcopy(self.cfg)
        self.logs.truncate(0)
        tmp['manage_resolv_conf'] = False
        self.call_resolv_conf_handler('photon', tmp)
        self.assertIn("'manage_resolv_conf' present but set to False",
                      self.logs.getvalue())
        assert [
            mock.call(mock.ANY, '/etc/systemd/resolved.conf', mock.ANY)
        ] not in m_render_to_file.call_args_list

    @mock.patch("cloudinit.config.cc_resolv_conf.templater.render_to_file")
    def test_resolv_conf_etc_resolv_conf(self, m_render_to_file):
        self.call_resolv_conf_handler('rhel', self.cfg)

        assert [
            mock.call(mock.ANY, '/etc/resolv.conf', mock.ANY)
        ] == m_render_to_file.call_args_list

    @mock.patch("cloudinit.config.cc_resolv_conf.templater.render_to_file")
    def test_resolv_conf_invalid_resolve_conf_fn(self, m_render_to_file):
        ds = None
        distro = self._fetch_distro('rhel', self.cfg)
        paths = helpers.Paths({'cloud_dir': self.tmp})
        cc = cloud.Cloud(ds, paths, {}, distro, None)
        cc.distro.resolve_conf_fn = 'bla'

        self.logs.truncate(0)
        self.call_resolv_conf_handler('rhel', self.cfg, cc)

        self.assertIn('No template found, not rendering resolve configs',
                      self.logs.getvalue())

        assert [
            mock.call(mock.ANY, '/etc/resolv.conf', mock.ANY)
        ] not in m_render_to_file.call_args_list

# vi: ts=4 expandtab