2015-01-30 12:52:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Rupesh Tare <rupesht@saltstack.com>
|
2015-01-30 12:52:27 +00:00
|
|
|
'''
|
|
|
|
|
2015-01-30 23:37:00 +00:00
|
|
|
# Import Python libs
|
2018-01-20 18:13:46 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2015-01-30 23:37:00 +00:00
|
|
|
|
2015-01-30 12:52:27 +00:00
|
|
|
# Import Salt Testing Libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2015-01-30 12:52:27 +00:00
|
|
|
MagicMock,
|
2016-02-29 18:17:52 +00:00
|
|
|
Mock,
|
2015-01-30 12:52:27 +00:00
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
2018-01-30 16:03:58 +00:00
|
|
|
try:
|
|
|
|
import pytest
|
|
|
|
except ImportError as import_error:
|
|
|
|
pytest = None
|
2015-01-30 12:52:27 +00:00
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.localemod as localemod
|
2016-01-25 19:30:09 +00:00
|
|
|
from salt.exceptions import CommandExecutionError
|
2018-01-30 16:32:14 +00:00
|
|
|
from salt.ext import six
|
2015-01-30 12:52:27 +00:00
|
|
|
|
2018-01-31 09:17:04 +00:00
|
|
|
|
2018-01-30 16:03:58 +00:00
|
|
|
@skipIf(not pytest, False)
|
2015-01-30 12:52:27 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 15:35:30 +00:00
|
|
|
class LocalemodTestCase(TestCase, LoaderModuleMockMixin):
|
2015-01-30 12:52:27 +00:00
|
|
|
'''
|
|
|
|
Test cases for salt.modules.localemod
|
|
|
|
'''
|
2018-01-30 19:36:17 +00:00
|
|
|
locale_ctl_out = '''
|
|
|
|
System Locale: LANG=de_DE.utf8
|
|
|
|
LANGUAGE=de_DE.utf8
|
|
|
|
VC Keymap: n/a
|
|
|
|
X11 Layout: us
|
|
|
|
X11 Model: pc105
|
|
|
|
'''
|
2018-04-04 14:58:21 +00:00
|
|
|
locale_ctl_notset = '''
|
2018-10-29 19:31:36 +00:00
|
|
|
System Locale: n/a
|
|
|
|
|
2018-04-04 14:58:21 +00:00
|
|
|
VC Keymap: n/a
|
2018-10-29 19:45:23 +00:00
|
|
|
X11 Layout: n/a
|
|
|
|
X11 Model: n/a
|
2018-04-04 14:58:21 +00:00
|
|
|
'''
|
2018-01-30 19:47:04 +00:00
|
|
|
locale_ctl_out_empty = ''
|
2018-01-30 19:51:23 +00:00
|
|
|
locale_ctl_out_broken = '''
|
|
|
|
System error:Recursive traversal of loopback mount points
|
|
|
|
'''
|
2018-01-30 19:59:23 +00:00
|
|
|
locale_ctl_out_structure = '''
|
|
|
|
Main: printers=We're upgrading /dev/null
|
|
|
|
racks=hardware stress fractures
|
|
|
|
failure=Ionisation from the air-conditioning
|
|
|
|
Cow say: errors=We're out of slots on the server
|
|
|
|
hardware=high pressure system failure
|
|
|
|
Reason: The vendor put the bug there.
|
|
|
|
'''
|
2018-01-31 09:17:44 +00:00
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {localemod: {}}
|
2017-02-19 15:35:30 +00:00
|
|
|
|
2015-01-30 12:52:27 +00:00
|
|
|
def test_list_avail(self):
|
|
|
|
'''
|
|
|
|
Test for Lists available (compiled) locales
|
|
|
|
'''
|
|
|
|
with patch.dict(localemod.__salt__,
|
|
|
|
{'cmd.run': MagicMock(return_value='A\nB')}):
|
2018-01-31 12:24:47 +00:00
|
|
|
assert localemod.list_avail() == ['A', 'B']
|
2015-01-30 12:52:27 +00:00
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2018-01-30 19:36:17 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock(return_value=locale_ctl_out)})
|
|
|
|
def test_localectl_status_parser(self):
|
|
|
|
'''
|
|
|
|
Test localectl status parser.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
out = localemod._localectl_status()
|
|
|
|
assert isinstance(out, dict)
|
|
|
|
for key in ['system_locale', 'vc_keymap', 'x11_layout', 'x11_model']:
|
|
|
|
assert key in out
|
|
|
|
assert isinstance(out['system_locale'], dict)
|
|
|
|
assert 'LANG' in out['system_locale']
|
|
|
|
assert 'LANGUAGE' in out['system_locale']
|
|
|
|
assert out['system_locale']['LANG'] == out['system_locale']['LANGUAGE'] == 'de_DE.utf8'
|
2018-04-04 14:58:21 +00:00
|
|
|
assert isinstance(out['vc_keymap'], dict)
|
|
|
|
assert 'data' in out['vc_keymap']
|
2018-04-25 13:59:25 +00:00
|
|
|
assert out['vc_keymap']['data'] is None
|
2018-04-04 14:58:21 +00:00
|
|
|
assert isinstance(out['x11_layout'], dict)
|
|
|
|
assert 'data' in out['x11_layout']
|
2018-04-04 20:13:59 +00:00
|
|
|
assert out['x11_layout']['data'] == 'us'
|
2018-04-04 14:58:21 +00:00
|
|
|
assert isinstance(out['x11_model'], dict)
|
|
|
|
assert 'data' in out['x11_model']
|
2018-04-04 20:13:59 +00:00
|
|
|
assert out['x11_model']['data'] == 'pc105'
|
2018-04-04 14:58:21 +00:00
|
|
|
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock(return_value=locale_ctl_notset)})
|
|
|
|
def test_localectl_status_parser_notset(self):
|
|
|
|
'''
|
|
|
|
Test localectl status parser.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
out = localemod._localectl_status()
|
|
|
|
assert isinstance(out, dict)
|
|
|
|
for key in ['system_locale', 'vc_keymap', 'x11_layout']:
|
|
|
|
assert key in out
|
|
|
|
assert isinstance(out['system_locale'], dict)
|
|
|
|
assert 'data' in out['system_locale']
|
2018-04-25 13:59:25 +00:00
|
|
|
assert out['system_locale']['data'] is None
|
2018-04-04 14:58:21 +00:00
|
|
|
assert isinstance(out['vc_keymap'], dict)
|
|
|
|
assert 'data' in out['vc_keymap']
|
2018-04-25 13:59:25 +00:00
|
|
|
assert out['vc_keymap']['data'] is None
|
2018-04-04 14:58:21 +00:00
|
|
|
assert isinstance(out['x11_layout'], dict)
|
|
|
|
assert 'data' in out['x11_layout']
|
2018-04-25 13:59:25 +00:00
|
|
|
assert out['x11_layout']['data'] is None
|
2018-01-30 19:36:17 +00:00
|
|
|
|
2018-01-31 14:36:51 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', MagicMock())
|
|
|
|
def test_dbus_locale_parser_matches(self):
|
|
|
|
'''
|
|
|
|
Test dbus locale status parser matching the results.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
i_dbus = MagicMock()
|
|
|
|
i_dbus.Get = MagicMock(return_value=['LANG=de_DE.utf8'])
|
|
|
|
dbus = MagicMock(return_value=i_dbus)
|
|
|
|
|
|
|
|
with patch('salt.modules.localemod.dbus.Interface', dbus):
|
|
|
|
out = localemod._parse_dbus_locale()
|
|
|
|
assert isinstance(out, dict)
|
|
|
|
assert 'LANG' in out
|
|
|
|
assert out['LANG'] == 'de_DE.utf8'
|
|
|
|
|
2018-01-31 14:42:32 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', MagicMock())
|
|
|
|
@patch('salt.modules.localemod.log', MagicMock())
|
|
|
|
def test_dbus_locale_parser_doesnot_matches(self):
|
|
|
|
'''
|
|
|
|
Test dbus locale status parser does not matching the results.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
i_dbus = MagicMock()
|
|
|
|
i_dbus.Get = MagicMock(return_value=['Fatal error right in front of screen'])
|
|
|
|
dbus = MagicMock(return_value=i_dbus)
|
|
|
|
|
|
|
|
with patch('salt.modules.localemod.dbus.Interface', dbus):
|
|
|
|
out = localemod._parse_dbus_locale()
|
|
|
|
assert isinstance(out, dict)
|
|
|
|
assert 'LANG' not in out
|
|
|
|
assert localemod.log.error.called
|
|
|
|
msg = localemod.log.error.call_args[0][0] % localemod.log.error.call_args[0][1]
|
|
|
|
assert msg == ('Odd locale parameter "Fatal error right in front of screen" detected in dbus locale output.'
|
|
|
|
' This should not happen. You should probably investigate what caused this.')
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 19:47:26 +00:00
|
|
|
@patch('salt.modules.localemod.log', MagicMock())
|
2018-01-30 19:38:33 +00:00
|
|
|
def test_localectl_status_parser_no_systemd(self):
|
|
|
|
'''
|
|
|
|
Test localectl status parser raises an exception if no systemd installed.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with pytest.raises(CommandExecutionError) as err:
|
|
|
|
localemod._localectl_status()
|
|
|
|
assert 'Unable to find "localectl"' in six.text_type(err)
|
2018-01-30 19:47:26 +00:00
|
|
|
assert not localemod.log.debug.called
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2018-01-30 19:47:04 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock(return_value=locale_ctl_out_empty)})
|
|
|
|
def test_localectl_status_parser_empty(self):
|
|
|
|
with pytest.raises(CommandExecutionError) as err:
|
|
|
|
localemod._localectl_status()
|
|
|
|
assert 'Unable to parse result of "localectl"' in six.text_type(err)
|
2018-01-30 19:38:33 +00:00
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2018-01-30 19:51:23 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock(return_value=locale_ctl_out_broken)})
|
|
|
|
def test_localectl_status_parser_broken(self):
|
|
|
|
with pytest.raises(CommandExecutionError) as err:
|
|
|
|
localemod._localectl_status()
|
|
|
|
assert 'Unable to parse result of "localectl"' in six.text_type(err)
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2018-01-30 19:59:23 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock(return_value=locale_ctl_out_structure)})
|
|
|
|
def test_localectl_status_parser_structure(self):
|
|
|
|
out = localemod._localectl_status()
|
|
|
|
assert isinstance(out, dict)
|
|
|
|
for key in ['main', 'cow_say']:
|
|
|
|
assert isinstance(out[key], dict)
|
|
|
|
for in_key in out[key]:
|
2018-01-31 09:19:04 +00:00
|
|
|
assert isinstance(out[key][in_key], six.text_type)
|
2018-04-04 14:58:21 +00:00
|
|
|
assert isinstance(out['reason']['data'], six.text_type)
|
2018-01-30 19:59:23 +00:00
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2018-01-30 16:04:37 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Ubuntu', 'osmajorrelease': 42})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 16:04:37 +00:00
|
|
|
@patch('salt.modules.localemod._parse_dbus_locale', MagicMock(return_value={'LANG': 'en_US.utf8'}))
|
|
|
|
@patch('salt.modules.localemod._localectl_status', MagicMock(return_value={'system_locale': {'LANG': 'de_DE.utf8'}}))
|
2018-01-30 16:23:40 +00:00
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=True))
|
2018-01-30 16:04:37 +00:00
|
|
|
def test_get_locale_with_systemd_nodbus(self):
|
|
|
|
'''
|
2018-01-30 16:17:46 +00:00
|
|
|
Test getting current system locale with systemd but no dbus available.
|
2018-01-30 16:04:37 +00:00
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
assert localemod.get_locale() == 'de_DE.utf8'
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2018-01-30 16:06:10 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Ubuntu', 'osmajorrelease': 42})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', True)
|
2018-01-30 16:06:10 +00:00
|
|
|
@patch('salt.modules.localemod._parse_dbus_locale', MagicMock(return_value={'LANG': 'en_US.utf8'}))
|
|
|
|
@patch('salt.modules.localemod._localectl_status', MagicMock(return_value={'system_locale': {'LANG': 'de_DE.utf8'}}))
|
2018-01-30 16:23:40 +00:00
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=True))
|
2018-01-30 16:06:10 +00:00
|
|
|
def test_get_locale_with_systemd_and_dbus(self):
|
|
|
|
'''
|
|
|
|
Test getting current system locale with systemd and dbus available.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
assert localemod.get_locale() == 'en_US.utf8'
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2018-01-30 16:18:13 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Suse', 'osmajorrelease': 12})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', True)
|
2018-01-30 16:18:13 +00:00
|
|
|
@patch('salt.modules.localemod._parse_dbus_locale', MagicMock(return_value={'LANG': 'en_US.utf8'}))
|
|
|
|
@patch('salt.modules.localemod._localectl_status', MagicMock(return_value={'system_locale': {'LANG': 'de_DE.utf8'}}))
|
2018-01-30 16:23:40 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock()})
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=True))
|
2018-01-30 16:18:13 +00:00
|
|
|
def test_get_locale_with_systemd_and_dbus_sle12(self):
|
|
|
|
'''
|
|
|
|
Test getting current system locale with systemd and dbus available on SLE12.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
localemod.get_locale()
|
|
|
|
assert localemod.__salt__['cmd.run'].call_args[0][0] == 'grep "^RC_LANG" /etc/sysconfig/language'
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 16:24:04 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'RedHat', 'osmajorrelease': 12})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 16:24:04 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock()})
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
|
|
|
def test_get_locale_with_no_systemd_redhat(self):
|
|
|
|
'''
|
2018-01-30 16:26:05 +00:00
|
|
|
Test getting current system locale with systemd and dbus available on RedHat.
|
2018-01-30 16:24:04 +00:00
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
localemod.get_locale()
|
|
|
|
assert localemod.__salt__['cmd.run'].call_args[0][0] == 'grep "^LANG=" /etc/sysconfig/i18n'
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 16:26:26 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Debian', 'osmajorrelease': 12})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 16:26:26 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock()})
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
|
|
|
def test_get_locale_with_no_systemd_debian(self):
|
|
|
|
'''
|
|
|
|
Test getting current system locale with systemd and dbus available on Debian.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
localemod.get_locale()
|
|
|
|
assert localemod.__salt__['cmd.run'].call_args[0][0] == 'grep "^LANG=" /etc/default/locale'
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 16:27:26 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Gentoo', 'osmajorrelease': 12})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 16:27:26 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock()})
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
|
|
|
def test_get_locale_with_no_systemd_gentoo(self):
|
|
|
|
'''
|
|
|
|
Test getting current system locale with systemd and dbus available on Gentoo.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
localemod.get_locale()
|
|
|
|
assert localemod.__salt__['cmd.run'].call_args[0][0] == 'eselect --brief locale show'
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 16:28:28 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Solaris', 'osmajorrelease': 12})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 16:28:28 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock()})
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
2018-02-09 14:40:22 +00:00
|
|
|
def test_get_locale_with_no_systemd_solaris(self):
|
2018-01-30 16:28:28 +00:00
|
|
|
'''
|
|
|
|
Test getting current system locale with systemd and dbus available on Solaris.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
localemod.get_locale()
|
|
|
|
assert localemod.__salt__['cmd.run'].call_args[0][0] == 'grep "^LANG=" /etc/default/init'
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 16:32:14 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'BSD', 'osmajorrelease': 8, 'oscodename': 'DrunkDragon'})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 16:32:14 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run': MagicMock()})
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
|
|
|
def test_get_locale_with_no_systemd_unknown(self):
|
2015-01-30 12:52:27 +00:00
|
|
|
'''
|
2018-01-30 16:32:14 +00:00
|
|
|
Test getting current system locale with systemd and dbus available on Gentoo.
|
|
|
|
:return:
|
2015-01-30 12:52:27 +00:00
|
|
|
'''
|
2018-01-30 16:32:14 +00:00
|
|
|
with pytest.raises(CommandExecutionError) as err:
|
|
|
|
localemod.get_locale()
|
|
|
|
assert '"DrunkDragon" is unsupported' in six.text_type(err)
|
2015-01-30 12:52:27 +00:00
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2018-01-30 17:31:44 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Ubuntu', 'osmajorrelease': 42})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 17:31:44 +00:00
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=True))
|
|
|
|
@patch('salt.modules.localemod._localectl_set', MagicMock())
|
|
|
|
def test_set_locale_with_systemd_nodbus(self):
|
|
|
|
'''
|
|
|
|
Test setting current system locale with systemd but no dbus available.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
loc = 'de_DE.utf8'
|
|
|
|
localemod.set_locale(loc)
|
|
|
|
assert localemod._localectl_set.call_args[0][0] == 'de_DE.utf8'
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2018-01-30 17:34:06 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Ubuntu', 'osmajorrelease': 42})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', True)
|
2018-01-30 17:34:06 +00:00
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=True))
|
|
|
|
@patch('salt.modules.localemod._localectl_set', MagicMock())
|
|
|
|
def test_set_locale_with_systemd_and_dbus(self):
|
|
|
|
'''
|
|
|
|
Test setting current system locale with systemd and dbus available.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
loc = 'de_DE.utf8'
|
|
|
|
localemod.set_locale(loc)
|
|
|
|
assert localemod._localectl_set.call_args[0][0] == 'de_DE.utf8'
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2018-01-30 17:43:01 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Suse', 'osmajorrelease': 12})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', True)
|
2018-01-30 17:43:01 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', MagicMock())
|
|
|
|
@patch('salt.modules.localemod._localectl_set', MagicMock())
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=True))
|
|
|
|
def test_set_locale_with_systemd_and_dbus_sle12(self):
|
|
|
|
'''
|
|
|
|
Test setting current system locale with systemd and dbus available on SLE12.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
loc = 'de_DE.utf8'
|
|
|
|
localemod.set_locale(loc)
|
|
|
|
assert not localemod._localectl_set.called
|
|
|
|
assert localemod.__salt__['file.replace'].called
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][0] == '/etc/sysconfig/language'
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][1] == '^RC_LANG=.*'
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][2] == 'RC_LANG="{}"'.format(loc)
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 17:45:55 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'RedHat', 'osmajorrelease': 42})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 17:45:55 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', MagicMock())
|
|
|
|
@patch('salt.modules.localemod._localectl_set', MagicMock())
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
|
|
|
def test_set_locale_with_no_systemd_redhat(self):
|
|
|
|
'''
|
|
|
|
Test setting current system locale with systemd and dbus available on RedHat.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
loc = 'de_DE.utf8'
|
|
|
|
localemod.set_locale(loc)
|
|
|
|
assert not localemod._localectl_set.called
|
|
|
|
assert localemod.__salt__['file.replace'].called
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][0] == '/etc/sysconfig/i18n'
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][1] == '^LANG=.*'
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][2] == 'LANG="{}"'.format(loc)
|
|
|
|
|
2018-01-31 14:52:36 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value='/usr/sbin/update-locale'))
|
2018-01-30 17:48:14 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Debian', 'osmajorrelease': 42})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 17:48:14 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', MagicMock())
|
|
|
|
@patch('salt.modules.localemod._localectl_set', MagicMock())
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
|
|
|
def test_set_locale_with_no_systemd_debian(self):
|
|
|
|
'''
|
|
|
|
Test setting current system locale with systemd and dbus available on Debian.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
loc = 'de_DE.utf8'
|
|
|
|
localemod.set_locale(loc)
|
|
|
|
assert not localemod._localectl_set.called
|
|
|
|
assert localemod.__salt__['file.replace'].called
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][0] == '/etc/default/locale'
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][1] == '^LANG=.*'
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][2] == 'LANG="{}"'.format(loc)
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-31 14:55:34 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Debian', 'osmajorrelease': 42})
|
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
|
|
|
@patch('salt.modules.localemod.__salt__', MagicMock())
|
|
|
|
@patch('salt.modules.localemod._localectl_set', MagicMock())
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
|
|
|
def test_set_locale_with_no_systemd_debian_no_update_locale(self):
|
|
|
|
'''
|
|
|
|
Test setting current system locale with systemd and dbus available on Debian but update-locale is not installed.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
loc = 'de_DE.utf8'
|
|
|
|
with pytest.raises(CommandExecutionError) as err:
|
|
|
|
localemod.set_locale(loc)
|
|
|
|
assert not localemod._localectl_set.called
|
|
|
|
assert 'Cannot set locale: "update-locale" was not found.' in six.text_type(err)
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 17:52:37 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Gentoo', 'osmajorrelease': 42})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 17:52:37 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', MagicMock())
|
|
|
|
@patch('salt.modules.localemod._localectl_set', MagicMock())
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
|
|
|
def test_set_locale_with_no_systemd_gentoo(self):
|
|
|
|
'''
|
|
|
|
Test setting current system locale with systemd and dbus available on Gentoo.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
loc = 'de_DE.utf8'
|
|
|
|
localemod.set_locale(loc)
|
|
|
|
assert not localemod._localectl_set.called
|
|
|
|
assert localemod.__salt__['cmd.retcode'].call_args[0][0] == 'eselect --brief locale set de_DE.utf8'
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 17:58:44 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Solaris', 'osmajorrelease': 42})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 17:58:44 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'locale.list_avail': MagicMock(return_value=['de_DE.utf8']),
|
|
|
|
'file.replace': MagicMock()})
|
|
|
|
@patch('salt.modules.localemod._localectl_set', MagicMock())
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
2018-02-09 14:40:22 +00:00
|
|
|
def test_set_locale_with_no_systemd_solaris_with_list_avail(self):
|
2018-01-30 17:58:44 +00:00
|
|
|
'''
|
2018-02-09 14:40:22 +00:00
|
|
|
Test setting current system locale with systemd and dbus available on Solaris.
|
2018-01-30 17:58:44 +00:00
|
|
|
The list_avail returns the proper locale.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
loc = 'de_DE.utf8'
|
|
|
|
localemod.set_locale(loc)
|
|
|
|
assert not localemod._localectl_set.called
|
|
|
|
assert localemod.__salt__['file.replace'].called
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][0] == '/etc/default/init'
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][1] == '^LANG=.*'
|
|
|
|
assert localemod.__salt__['file.replace'].call_args[0][2] == 'LANG="{}"'.format(loc)
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 18:07:12 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Solaris', 'osmajorrelease': 42})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 18:07:12 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'locale.list_avail': MagicMock(return_value=['en_GB.utf8']),
|
|
|
|
'file.replace': MagicMock()})
|
|
|
|
@patch('salt.modules.localemod._localectl_set', MagicMock())
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
2018-02-09 14:40:22 +00:00
|
|
|
def test_set_locale_with_no_systemd_solaris_without_list_avail(self):
|
2018-01-30 18:07:12 +00:00
|
|
|
'''
|
2018-02-09 14:40:22 +00:00
|
|
|
Test setting current system locale with systemd and dbus is not available on Solaris.
|
2018-01-30 18:07:12 +00:00
|
|
|
The list_avail does not return the proper locale.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
loc = 'de_DE.utf8'
|
|
|
|
assert not localemod.set_locale(loc)
|
|
|
|
assert not localemod._localectl_set.called
|
|
|
|
assert not localemod.__salt__['file.replace'].called
|
|
|
|
|
2018-02-08 19:38:25 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
2018-01-30 18:16:15 +00:00
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'BSD', 'osmajorrelease': 42})
|
2018-01-30 20:05:00 +00:00
|
|
|
@patch('salt.modules.localemod.dbus', None)
|
2018-01-30 18:16:15 +00:00
|
|
|
@patch('salt.modules.localemod.__salt__', {'locale.list_avail': MagicMock(return_value=['en_GB.utf8']),
|
|
|
|
'file.replace': MagicMock()})
|
|
|
|
@patch('salt.modules.localemod._localectl_set', MagicMock())
|
|
|
|
@patch('salt.utils.systemd.booted', MagicMock(return_value=False))
|
|
|
|
def test_set_locale_with_no_systemd_unknown(self):
|
|
|
|
'''
|
|
|
|
Test setting current system locale without systemd on unknown system.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with pytest.raises(CommandExecutionError) as err:
|
|
|
|
localemod.set_locale('de_DE.utf8')
|
|
|
|
assert 'Unsupported platform' in six.text_type(err)
|
2015-09-03 01:22:42 +00:00
|
|
|
|
2018-01-31 12:20:34 +00:00
|
|
|
@patch('salt.utils.locales.normalize_locale', MagicMock(return_value='en_US.UTF-8 UTF-8'))
|
|
|
|
@patch('salt.modules.localemod.__salt__', {'locale.list_avail': MagicMock(return_value=['A', 'B'])})
|
2015-01-30 12:52:27 +00:00
|
|
|
def test_avail(self):
|
|
|
|
'''
|
|
|
|
Test for Check if a locale is available
|
|
|
|
'''
|
2018-01-31 12:20:34 +00:00
|
|
|
assert localemod.avail('locale')
|
2015-01-30 12:52:27 +00:00
|
|
|
|
2018-01-30 20:14:04 +00:00
|
|
|
@patch('salt.modules.localemod.log', MagicMock())
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path'))
|
|
|
|
@patch('salt.modules.localemod.__grains__', {'os': 'Debian'})
|
|
|
|
@patch('salt.modules.localemod.__salt__', {'file.search': MagicMock(return_value=False)})
|
2015-01-31 00:23:32 +00:00
|
|
|
def test_gen_locale_not_valid(self):
|
|
|
|
'''
|
|
|
|
Tests the return of gen_locale when the provided locale is not found
|
|
|
|
'''
|
2018-01-30 20:14:04 +00:00
|
|
|
assert not localemod.gen_locale('foo')
|
|
|
|
assert localemod.log.error.called
|
|
|
|
msg = localemod.log.error.call_args[0][0] % (localemod.log.error.call_args[0][1],
|
|
|
|
localemod.log.error.call_args[0][2])
|
|
|
|
assert msg == 'The provided locale "foo" is not found in /usr/share/i18n/SUPPORTED'
|
2015-01-31 00:23:32 +00:00
|
|
|
|
2018-01-31 11:54:42 +00:00
|
|
|
@patch('salt.modules.localemod.log', MagicMock())
|
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Suse'})
|
|
|
|
@patch('os.listdir', MagicMock(return_value=[]))
|
|
|
|
@patch('salt.utils.locales.join_locale', MagicMock(return_value='en_GB.utf8'))
|
|
|
|
def test_gen_locale_suse_invalid(self):
|
|
|
|
'''
|
|
|
|
Tests the location where gen_locale is searching for generated paths.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
assert not localemod.gen_locale('de_DE.utf8')
|
|
|
|
assert localemod.log.error.called
|
|
|
|
msg = localemod.log.error.call_args[0][0] % (localemod.log.error.call_args[0][1],
|
|
|
|
localemod.log.error.call_args[0][2])
|
2018-01-31 12:00:12 +00:00
|
|
|
assert localemod.os.listdir.call_args[0][0] == '/usr/share/locale'
|
2018-01-31 11:54:42 +00:00
|
|
|
assert msg == 'The provided locale "en_GB.utf8" is not found in /usr/share/locale'
|
|
|
|
|
2018-01-31 12:14:35 +00:00
|
|
|
@patch('salt.modules.localemod.log', MagicMock())
|
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Suse'})
|
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run_all': MagicMock(return_value={'retcode': 0})})
|
|
|
|
@patch('os.listdir', MagicMock(return_value=['de_DE']))
|
|
|
|
@patch('os.path.exists', MagicMock(return_value=False))
|
|
|
|
@patch('salt.utils.locales.join_locale', MagicMock(return_value='de_DE.utf8'))
|
|
|
|
@patch('salt.utils.path.which', MagicMock(side_effect=[None, '/usr/bin/localedef']))
|
|
|
|
def test_gen_locale_suse_valid(self):
|
|
|
|
'''
|
|
|
|
Tests the location where gen_locale is calling localedef on Suse os-family.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
localemod.gen_locale('de_DE.utf8')
|
|
|
|
assert localemod.__salt__['cmd.run_all'].call_args[0][0] == ['localedef', '--force', '-i', 'de_DE',
|
|
|
|
'-f', 'utf8', 'de_DE.utf8', '--quiet']
|
|
|
|
|
2018-01-31 12:17:53 +00:00
|
|
|
@patch('salt.modules.localemod.log', MagicMock())
|
|
|
|
@patch('salt.modules.localemod.__grains__', {'os_family': 'Suse'})
|
|
|
|
@patch('salt.modules.localemod.__salt__', {'cmd.run_all': MagicMock(return_value={'retcode': 0})})
|
|
|
|
@patch('os.listdir', MagicMock(return_value=['de_DE']))
|
|
|
|
@patch('os.path.exists', MagicMock(return_value=False))
|
|
|
|
@patch('salt.utils.locales.join_locale', MagicMock(return_value='de_DE.utf8'))
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=None))
|
|
|
|
def test_gen_locale_suse_localedef_error_handling(self):
|
|
|
|
'''
|
|
|
|
Tests the location where gen_locale is handling error while calling not installed localedef on Suse os-family.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with pytest.raises(CommandExecutionError) as err:
|
|
|
|
localemod.gen_locale('de_DE.utf8')
|
|
|
|
assert 'Command "locale-gen" or "localedef" was not found on this system.' in six.text_type(err)
|
|
|
|
|
2015-03-17 21:13:23 +00:00
|
|
|
def test_gen_locale_debian(self):
|
2015-01-31 00:23:32 +00:00
|
|
|
'''
|
|
|
|
Tests the return of successful gen_locale on Debian system
|
|
|
|
'''
|
2015-04-18 17:57:34 +00:00
|
|
|
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337}
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(localemod.__grains__, {'os': 'Debian'}), \
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch.dict(localemod.__salt__,
|
|
|
|
{'file.search': MagicMock(return_value=True),
|
|
|
|
'file.replace': MagicMock(return_value=True),
|
|
|
|
'cmd.run_all': MagicMock(return_value=ret)}):
|
2018-01-31 12:24:47 +00:00
|
|
|
assert localemod.gen_locale('en_US.UTF-8 UTF-8')
|
2015-01-31 00:23:32 +00:00
|
|
|
|
2015-05-22 16:48:30 +00:00
|
|
|
def test_gen_locale_debian_no_charmap(self):
|
|
|
|
'''
|
|
|
|
Tests the return of successful gen_locale on Debian system without a charmap
|
|
|
|
'''
|
|
|
|
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337}
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(localemod.__grains__, {'os': 'Debian'}), \
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch.dict(localemod.__salt__,
|
2018-01-31 12:28:07 +00:00
|
|
|
{'file.search': lambda s, p, flags: not len(p.split()) == 1,
|
2017-04-10 13:00:57 +00:00
|
|
|
'file.replace': MagicMock(return_value=True),
|
|
|
|
'cmd.run_all': MagicMock(return_value=ret)}):
|
2018-01-31 12:24:47 +00:00
|
|
|
assert localemod.gen_locale('en_US.UTF-8')
|
2015-05-22 16:48:30 +00:00
|
|
|
|
2015-01-31 00:23:32 +00:00
|
|
|
def test_gen_locale_ubuntu(self):
|
|
|
|
'''
|
|
|
|
Test the return of successful gen_locale on Ubuntu system
|
|
|
|
'''
|
2015-04-18 17:57:34 +00:00
|
|
|
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337}
|
2015-01-30 12:52:27 +00:00
|
|
|
with patch.dict(localemod.__salt__,
|
|
|
|
{'file.replace': MagicMock(return_value=True),
|
|
|
|
'file.touch': MagicMock(return_value=None),
|
|
|
|
'file.append': MagicMock(return_value=None),
|
2017-04-10 13:00:57 +00:00
|
|
|
'cmd.run_all': MagicMock(return_value=ret)}), \
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch('os.listdir', MagicMock(return_value=['en_US'])), \
|
|
|
|
patch.dict(localemod.__grains__, {'os': 'Ubuntu'}):
|
2018-01-31 12:24:47 +00:00
|
|
|
assert localemod.gen_locale('en_US.UTF-8')
|
2015-01-30 12:52:27 +00:00
|
|
|
|
2015-01-31 00:23:32 +00:00
|
|
|
def test_gen_locale_gentoo(self):
|
|
|
|
'''
|
|
|
|
Tests the return of successful gen_locale on Gentoo system
|
|
|
|
'''
|
2015-04-18 17:57:34 +00:00
|
|
|
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337}
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(localemod.__grains__, {'os_family': 'Gentoo'}), \
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch('os.listdir', MagicMock(return_value=['en_US.UTF-8'])), \
|
|
|
|
patch.dict(localemod.__salt__,
|
|
|
|
{'file.search': MagicMock(return_value=True),
|
|
|
|
'file.replace': MagicMock(return_value=True),
|
|
|
|
'cmd.run_all': MagicMock(return_value=ret)}):
|
2018-01-31 12:24:47 +00:00
|
|
|
assert localemod.gen_locale('en_US.UTF-8 UTF-8')
|
2015-01-30 12:52:27 +00:00
|
|
|
|
2015-05-22 16:48:30 +00:00
|
|
|
def test_gen_locale_gentoo_no_charmap(self):
|
|
|
|
'''
|
|
|
|
Tests the return of successful gen_locale on Gentoo system without a charmap
|
|
|
|
'''
|
2015-07-23 13:17:18 +00:00
|
|
|
def file_search(search, pattern, flags):
|
2015-05-22 16:48:30 +00:00
|
|
|
'''
|
|
|
|
mock file.search
|
|
|
|
'''
|
|
|
|
if len(pattern.split()) == 1:
|
|
|
|
return False
|
|
|
|
else: # charmap was supplied
|
|
|
|
return True
|
|
|
|
|
|
|
|
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337}
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch.dict(localemod.__grains__, {'os_family': 'Gentoo'}), \
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch('os.listdir', MagicMock(return_value=['en_US.UTF-8'])), \
|
|
|
|
patch.dict(localemod.__salt__,
|
|
|
|
{'file.search': file_search,
|
|
|
|
'file.replace': MagicMock(return_value=True),
|
|
|
|
'cmd.run_all': MagicMock(return_value=ret)}):
|
2018-01-31 12:24:47 +00:00
|
|
|
assert localemod.gen_locale('en_US.UTF-8')
|
2015-05-22 16:48:30 +00:00
|
|
|
|
2015-01-31 00:23:32 +00:00
|
|
|
def test_gen_locale(self):
|
|
|
|
'''
|
|
|
|
Tests the return of successful gen_locale
|
|
|
|
'''
|
2015-04-18 17:57:34 +00:00
|
|
|
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337}
|
2015-01-31 00:23:32 +00:00
|
|
|
with patch.dict(localemod.__salt__,
|
2015-05-15 18:50:55 +00:00
|
|
|
{'cmd.run_all': MagicMock(return_value=ret),
|
2017-04-10 13:00:57 +00:00
|
|
|
'file.replace': MagicMock()}), \
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch('os.listdir', MagicMock(return_value=['en_US'])):
|
2018-01-31 12:24:47 +00:00
|
|
|
assert localemod.gen_locale('en_US.UTF-8')
|
2015-03-17 21:13:23 +00:00
|
|
|
|
|
|
|
def test_gen_locale_verbose(self):
|
|
|
|
'''
|
|
|
|
Tests the return of successful gen_locale
|
|
|
|
'''
|
2015-04-18 17:57:34 +00:00
|
|
|
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337}
|
2015-03-17 21:13:23 +00:00
|
|
|
with patch.dict(localemod.__salt__,
|
2015-05-15 18:50:55 +00:00
|
|
|
{'cmd.run_all': MagicMock(return_value=ret),
|
2017-04-10 13:00:57 +00:00
|
|
|
'file.replace': MagicMock()}), \
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')), \
|
2017-04-10 13:00:57 +00:00
|
|
|
patch('os.listdir', MagicMock(return_value=['en_US'])):
|
2018-01-31 12:24:47 +00:00
|
|
|
assert localemod.gen_locale('en_US.UTF-8', verbose=True) == ret
|
2015-01-30 12:52:27 +00:00
|
|
|
|
2018-02-16 20:26:06 +00:00
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value="/usr/bin/localctl"))
|
2016-02-29 18:17:52 +00:00
|
|
|
def test_parse_localectl(self):
|
|
|
|
localectl_out = (' System Locale: LANG=en_US.UTF-8\n'
|
|
|
|
' LANGUAGE=en_US:en\n'
|
|
|
|
' VC Keymap: n/a')
|
|
|
|
mock_cmd = Mock(return_value=localectl_out)
|
|
|
|
with patch.dict(localemod.__salt__, {'cmd.run': mock_cmd}):
|
2018-01-30 16:32:37 +00:00
|
|
|
ret = localemod._localectl_status()['system_locale']
|
2018-01-31 12:24:47 +00:00
|
|
|
assert {'LANG': 'en_US.UTF-8', 'LANGUAGE': 'en_US:en'} == ret
|