← Back to team overview

cloud-init-dev team mailing list archive

Re: [Merge] ~smoser/cloud-init:feature/pregen-locale into cloud-init:master

 


Diff comments:

> diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py
> index d06d46a..bcf45c6 100644
> --- a/cloudinit/distros/debian.py
> +++ b/cloudinit/distros/debian.py
> @@ -225,4 +217,36 @@ def _maybe_remove_legacy_eth0(path="/etc/network/interfaces.d/eth0.cfg"):
>  
>      LOG.warning(msg)
>  
> +
> +def apply_locale(locale, sys_path=LOCALE_CONF_FN, keyname='LANG'):
> +    """Apply the locale.
> +
> +    Run locale-gen for the provided locale and set the default
> +    system variable 'keyname' appropriately in the provided 'sys_path'.
> +
> +    If sys_path indicates that this value is already the default

ack. will address.

> +    ('keyname=locale') then no changes will be made and locale-gen not called.
> +    """
> +    if not locale:
> +        raise ValueError('Failed to provide locale value.')
> +
> +    if not sys_path:
> +        raise ValueError('Invalid path: %s' % sys_path)
> +
> +    if os.path.exists(sys_path):
> +        locale_content = util.load_file(sys_path)
> +        # if LANG isn't present, regen
> +        sys_defaults = util.load_shell_content(locale_content)
> +        sys_val = sys_defaults.get(keyname, "")
> +        if sys_val.lower() == locale.lower():
> +            LOG.debug(
> +                "System has '%s=%s' requested '%s', skipping regeneration.",
> +                keyname, sys_val, locale)
> +            return
> +
> +    util.subp(['locale-gen', locale], capture=False)
> +    util.subp(
> +        ['update-locale', '--locale-file=' + sys_path,
> +         '%s=%s' % (keyname, locale)], capture=False)
> +
>  # vi: ts=4 expandtab
> diff --git a/tests/unittests/test_distros/test_debian.py b/tests/unittests/test_distros/test_debian.py
> new file mode 100644
> index 0000000..bd7721c
> --- /dev/null
> +++ b/tests/unittests/test_distros/test_debian.py
> @@ -0,0 +1,70 @@
> +# This file is part of cloud-init. See LICENSE file for license information.
> +
> +from ..helpers import (CiTestCase, mock)
> +
> +from cloudinit.distros.debian import apply_locale
> +from cloudinit import util
> +
> +
> +class TestDebianApplyLocale(CiTestCase):
> +    @mock.patch("cloudinit.distros.debian.util.subp")
> +    def test_no_rerun(self, m_subp):

I'll add the first one
The second 'IDONTEXIST' is a bit harder... we are just letting that through to update-locale, and update-locale doesn't care.

# rm -f foo; update-locale --locale-file=foo ASDF=bar; echo $?; cat foo 
0
#  File generated by update-locale
ASDF=bar

> +        """If system has defined locale, no re-run is expected."""
> +        spath = self.tmp_path("default-locale")
> +        m_subp.return_value = (None, None)
> +        locale = 'en_US.UTF-8'
> +        util.write_file(spath, 'LANG=%s\n' % locale, omode="w")
> +        apply_locale(locale, sys_path=spath)
> +        m_subp.assert_not_called()
> +
> +    @mock.patch("cloudinit.distros.debian.util.subp")
> +    def test_rerun_if_different(self, m_subp):
> +        """If system has different locale, locale-gen should be called."""
> +        spath = self.tmp_path("default-locale")
> +        m_subp.return_value = (None, None)
> +        locale = 'en_US.UTF-8'
> +        util.write_file(spath, 'LANG=fr_FR.UTF-8', omode="w")
> +        apply_locale(locale, sys_path=spath)
> +        self.assertEqual(
> +            [['locale-gen', locale],
> +             ['update-locale', '--locale-file=' + spath, 'LANG=%s' % locale]],
> +            [p[0][0] for p in m_subp.call_args_list])
> +
> +    @mock.patch("cloudinit.distros.debian.util.subp")
> +    def test_rerun_if_no_file(self, m_subp):
> +        """If system has different locale, locale-gen should be called."""
> +        spath = self.tmp_path("default-locale")
> +        m_subp.return_value = (None, None)
> +        locale = 'en_US.UTF-8'
> +        apply_locale(locale, sys_path=spath)
> +        self.assertEqual(
> +            [['locale-gen', locale],
> +             ['update-locale', '--locale-file=' + spath, 'LANG=%s' % locale]],
> +            [p[0][0] for p in m_subp.call_args_list])
> +
> +    @mock.patch("cloudinit.distros.debian.util.subp")
> +    def test_rerun_on_unset_system_locale(self, m_subp):
> +        """If system has unset locale, locale-gen should be called."""
> +        m_subp.return_value = (None, None)
> +        spath = self.tmp_path("default-locale")
> +        locale = 'en_US.UTF-8'
> +        util.write_file(spath, 'LANG=', omode="w")
> +        apply_locale(locale, sys_path=spath)
> +        self.assertEqual(
> +            [['locale-gen', locale],
> +             ['update-locale', '--locale-file=' + spath, 'LANG=%s' % locale]],
> +            [p[0][0] for p in m_subp.call_args_list])
> +
> +    @mock.patch("cloudinit.distros.debian.util.subp")
> +    def test_rerun_on_mismatched_keys(self, m_subp):
> +        """If key is LC_ALL and system has only LANG, rerun is expected."""
> +        m_subp.return_value = (None, None)
> +        spath = self.tmp_path("default-locale")
> +        locale = 'en_US.UTF-8'
> +        util.write_file(spath, 'LANG=', omode="w")
> +        apply_locale(locale, sys_path=spath, keyname='LC_ALL')
> +        self.assertEqual(
> +            [['locale-gen', locale],
> +             ['update-locale', '--locale-file=' + spath,
> +              'LC_ALL=%s' % locale]],
> +            [p[0][0] for p in m_subp.call_args_list])


-- 
https://code.launchpad.net/~smoser/cloud-init/+git/cloud-init/+merge/327532
Your team cloud-init commiters is requested to review the proposed merge of ~smoser/cloud-init:feature/pregen-locale into cloud-init:master.


References