summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler/test_handler_locale.py
blob: 15fe7b238f2a78f0fe391a5d476fc9030f3be755 (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
# Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
#
# Author: Juerg Haefliger <juerg.haefliger@hp.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

from cloudinit.config import cc_locale

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

from cloudinit.sources import DataSourceNoCloud

from cloudinit.tests import helpers as t_help

from configobj import ConfigObj

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

LOG = logging.getLogger(__name__)


class TestLocale(t_help.FilesystemMockingTestCase):

    def setUp(self):
        super(TestLocale, self).setUp()
        self.new_root = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.new_root)

    def _get_cloud(self, distro):
        self.patchUtils(self.new_root)
        paths = helpers.Paths({})

        cls = distros.fetch(distro)
        d = cls(distro, {}, paths)
        ds = DataSourceNoCloud.DataSourceNoCloud({}, d, paths)
        cc = cloud.Cloud(ds, paths, {}, d, None)
        return cc

    def test_set_locale_arch(self):
        locale = 'en_GB.UTF-8'
        locale_configfile = '/etc/invalid-locale-path'
        cfg = {
            'locale': locale,
            'locale_configfile': locale_configfile,
        }
        cc = self._get_cloud('arch')

        with mock.patch('cloudinit.distros.arch.subp.subp') as m_subp:
            with mock.patch('cloudinit.distros.arch.LOG.warning') as m_LOG:
                cc_locale.handle('cc_locale', cfg, cc, LOG, [])
                m_LOG.assert_called_with('Invalid locale_configfile %s, '
                                         'only supported value is '
                                         '/etc/locale.conf',
                                         locale_configfile)

                contents = util.load_file(cc.distro.locale_gen_fn)
                self.assertIn('%s UTF-8' % locale, contents)
                m_subp.assert_called_with(['localectl',
                                           'set-locale',
                                           locale], capture=False)

    def test_set_locale_sles(self):

        cfg = {
            'locale': 'My.Locale',
        }
        cc = self._get_cloud('sles')
        cc_locale.handle('cc_locale', cfg, cc, LOG, [])
        if cc.distro.uses_systemd():
            locale_conf = cc.distro.systemd_locale_conf_fn
        else:
            locale_conf = cc.distro.locale_conf_fn
        contents = util.load_file(locale_conf, decode=False)
        n_cfg = ConfigObj(BytesIO(contents))
        if cc.distro.uses_systemd():
            self.assertEqual({'LANG': cfg['locale']}, dict(n_cfg))
        else:
            self.assertEqual({'RC_LANG': cfg['locale']}, dict(n_cfg))

    def test_set_locale_sles_default(self):
        cfg = {}
        cc = self._get_cloud('sles')
        cc_locale.handle('cc_locale', cfg, cc, LOG, [])

        if cc.distro.uses_systemd():
            locale_conf = cc.distro.systemd_locale_conf_fn
            keyname = 'LANG'
        else:
            locale_conf = cc.distro.locale_conf_fn
            keyname = 'RC_LANG'

        contents = util.load_file(locale_conf, decode=False)
        n_cfg = ConfigObj(BytesIO(contents))
        self.assertEqual({keyname: 'en_US.UTF-8'}, dict(n_cfg))

    def test_locale_update_config_if_different_than_default(self):
        """Test cc_locale writes updates conf if different than default"""
        locale_conf = os.path.join(self.new_root, "etc/default/locale")
        util.write_file(locale_conf, 'LANG="en_US.UTF-8"\n')
        cfg = {'locale': 'C.UTF-8'}
        cc = self._get_cloud('ubuntu')
        with mock.patch('cloudinit.distros.debian.subp.subp') as m_subp:
            with mock.patch('cloudinit.distros.debian.LOCALE_CONF_FN',
                            locale_conf):
                cc_locale.handle('cc_locale', cfg, cc, LOG, [])
                m_subp.assert_called_with(['update-locale',
                                           '--locale-file=%s' % locale_conf,
                                           'LANG=C.UTF-8'], capture=False)

    def test_locale_rhel_defaults_en_us_utf8(self):
        """Test cc_locale gets en_US.UTF-8 from distro get_locale fallback"""
        cfg = {}
        cc = self._get_cloud('rhel')
        update_sysconfig = 'cloudinit.distros.rhel_util.update_sysconfig_file'
        with mock.patch.object(cc.distro, 'uses_systemd') as m_use_sd:
            m_use_sd.return_value = True
            with mock.patch(update_sysconfig) as m_update_syscfg:
                cc_locale.handle('cc_locale', cfg, cc, LOG, [])
                m_update_syscfg.assert_called_with('/etc/locale.conf',
                                                   {'LANG': 'en_US.UTF-8'})


# vi: ts=4 expandtab