summaryrefslogtreecommitdiff
path: root/tests/unittests
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2017-06-08 15:42:12 -0500
committerScott Moser <smoser@brickies.net>2017-07-25 13:56:10 -0400
commit0ef61b289472665f4e3059a24a8b9b91246f06ee (patch)
tree4ff0d8911a5ce87fbc00d46b8de728fc7774d4e5 /tests/unittests
parent85c984c3c74b0a8751698e20915b89756580b09f (diff)
downloadvyos-cloud-init-0ef61b289472665f4e3059a24a8b9b91246f06ee.tar.gz
vyos-cloud-init-0ef61b289472665f4e3059a24a8b9b91246f06ee.zip
locale: Do not re-run locale-gen if provided locale is system default.
If the system configure default in /etc/default/locale is set to the same value that is provided for cloud-init's "locale" setting, then do not re-run locale-gen. This allows images built with a locale already generated to not re-run locale-gen (which can be very heavy). Also here is a fix to invoke update-locale correctly and remove the internal writing of /etc/default/locale. We were calling update-locale <locale> This ends up having no affect. The more correct invocation is: update-locale LANG=<locale> Also added some support here should we ever want to change setting LANG to setting LC_ALL (or any other key). Lastly, a test change to allow us to use assert_not_called from mock. Versions of mock in CentOS 6 do not have assert_not_called.
Diffstat (limited to 'tests/unittests')
-rw-r--r--tests/unittests/helpers.py12
-rw-r--r--tests/unittests/test_distros/test_debian.py82
2 files changed, 94 insertions, 0 deletions
diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py
index 6691cf82..08c5c469 100644
--- a/tests/unittests/helpers.py
+++ b/tests/unittests/helpers.py
@@ -376,4 +376,16 @@ except AttributeError:
return wrapper
return decorator
+
+# older versions of mock do not have the useful 'assert_not_called'
+if not hasattr(mock.Mock, 'assert_not_called'):
+ def __mock_assert_not_called(mmock):
+ if mmock.call_count != 0:
+ msg = ("[citest] Expected '%s' to not have been called. "
+ "Called %s times." %
+ (mmock._mock_name or 'mock', mmock.call_count))
+ raise AssertionError(msg)
+ mock.Mock.assert_not_called = __mock_assert_not_called
+
+
# 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 00000000..2330ad52
--- /dev/null
+++ b/tests/unittests/test_distros/test_debian.py
@@ -0,0 +1,82 @@
+# 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
+
+
+@mock.patch("cloudinit.distros.debian.util.subp")
+class TestDebianApplyLocale(CiTestCase):
+ def test_no_rerun(self, m_subp):
+ """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()
+
+ 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])
+
+ def test_rerun_if_no_file(self, m_subp):
+ """If system has no locale file, 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])
+
+ 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])
+
+ 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])
+
+ def test_falseish_locale_raises_valueerror(self, m_subp):
+ """locale as None or "" is invalid and should raise ValueError."""
+
+ with self.assertRaises(ValueError) as ctext_m:
+ apply_locale(None)
+ m_subp.assert_not_called()
+
+ self.assertEqual(
+ 'Failed to provide locale value.', str(ctext_m.exception))
+
+ with self.assertRaises(ValueError) as ctext_m:
+ apply_locale("")
+ m_subp.assert_not_called()
+ self.assertEqual(
+ 'Failed to provide locale value.', str(ctext_m.exception))