diff --git a/salt/modules/timezone.py b/salt/modules/timezone.py index a01e3cedf6..96a5d0f681 100644 --- a/salt/modules/timezone.py +++ b/salt/modules/timezone.py @@ -109,6 +109,12 @@ def set_zone(timezone): salt '*' timezone.set_zone 'America/Denver' ''' + if salt.utils.which('timedatectl'): + try: + __salt__['cmd.run']('timedatectl set-timezone {0}'.format(timezone)) + except CommandExecutionError: + pass + if 'Solaris' in __grains__['os_family']: zonepath = '/usr/share/lib/zoneinfo/{0}'.format(timezone) else: diff --git a/tests/unit/modules/timezone_test.py b/tests/unit/modules/timezone_test.py index 941e730576..729914e756 100644 --- a/tests/unit/modules/timezone_test.py +++ b/tests/unit/modules/timezone_test.py @@ -90,9 +90,9 @@ class TimezoneTestCase(TestCase): ''' Test to unlinks, then symlinks /etc/localtime to the set timezone. ''' - ret = ('Zone does not exist: /usr/share/lib/zoneinfo/timezone') - mock = MagicMock(side_effect=[False, True, True]) - with patch.dict(timezone.__grains__, {'os_family': 'Solaris'}): + def zone_checking_and_unlinking(): + ret = ('Zone does not exist: /usr/share/lib/zoneinfo/timezone') + mock = MagicMock(side_effect=[False, True, True]) with patch.object(os.path, 'exists', mock): self.assertEqual(timezone.set_zone('timezone'), ret) @@ -102,6 +102,14 @@ class TimezoneTestCase(TestCase): MagicMock(return_value=None)}): self.assertTrue(timezone.set_zone('timezone')) + with patch.dict(timezone.__grains__, {'os_family': 'Solaris'}): + with patch.object(salt.utils, 'which', return_value=False): + zone_checking_and_unlinking() + + with patch.object(salt.utils, 'which', return_value=True): + with patch.dict(timezone.__salt__, {'cmd.run': MagicMock(return_value='')}): + zone_checking_and_unlinking() + def test_zone_compare(self): ''' Test to checks the hash sum between the given timezone, and the