2016-06-14 20:47:17 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
2016-06-15 19:34:54 +00:00
|
|
|
import datetime
|
2016-06-14 20:47:17 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting import skipIf
|
|
|
|
from salttesting.helpers import (
|
|
|
|
destructiveTest,
|
2016-06-15 19:34:54 +00:00
|
|
|
ensure_in_syspath
|
2016-06-14 20:47:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
import integration
|
2016-06-21 19:24:49 +00:00
|
|
|
import salt.utils
|
2016-06-14 20:47:17 +00:00
|
|
|
|
2016-06-30 15:46:35 +00:00
|
|
|
|
2016-06-21 19:24:49 +00:00
|
|
|
@skipIf(not salt.utils.is_linux(), 'These tests can only be run on linux')
|
2016-06-14 20:47:17 +00:00
|
|
|
class SystemModuleTest(integration.ModuleCase):
|
|
|
|
'''
|
|
|
|
Validate the date/time functions in the system module
|
|
|
|
'''
|
|
|
|
fmt_str = "%Y-%m-%d %H:%M:%S"
|
|
|
|
|
2016-06-21 19:24:49 +00:00
|
|
|
def __init__(self, arg):
|
|
|
|
super(self.__class__, self).__init__(arg)
|
|
|
|
self._orig_time = None
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(SystemModuleTest, self).setUp()
|
|
|
|
os_grain = self.run_function('grains.item', ['kernel'])
|
2016-06-30 15:46:35 +00:00
|
|
|
if os_grain['kernel'] not in 'Linux':
|
2016-06-21 19:24:49 +00:00
|
|
|
self.skipTest(
|
|
|
|
'Test not applicable to \'{kernel}\' kernel'.format(
|
|
|
|
**os_grain
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if self._orig_time is not None:
|
|
|
|
self._restore_time()
|
|
|
|
self._orig_time = None
|
|
|
|
|
2016-06-14 20:47:17 +00:00
|
|
|
def _save_time(self):
|
2016-06-21 19:24:49 +00:00
|
|
|
self._orig_time = datetime.datetime.utcnow()
|
2016-06-14 20:47:17 +00:00
|
|
|
|
2016-06-21 19:24:49 +00:00
|
|
|
def _set_time(self, new_time, offset=None):
|
2016-06-14 20:47:17 +00:00
|
|
|
t = new_time.timetuple()[:6]
|
2016-06-21 19:24:49 +00:00
|
|
|
t += (offset,)
|
|
|
|
return self.run_function('system.set_system_date_time', t)
|
|
|
|
|
|
|
|
def _restore_time(self):
|
|
|
|
result = self._set_time(self._orig_time, "+0000")
|
|
|
|
self.assertTrue(result, msg="Unable to restore time properly")
|
|
|
|
|
|
|
|
def _same_times(self, t1, t2, seconds_diff=2):
|
|
|
|
'''
|
|
|
|
Helper function to check if two datetime objects
|
|
|
|
are close enough to the same time.
|
|
|
|
'''
|
2016-06-14 20:47:17 +00:00
|
|
|
return abs(t1 - t2) < datetime.timedelta(seconds=seconds_diff)
|
|
|
|
|
|
|
|
def test_get_system_date_time(self):
|
|
|
|
'''
|
|
|
|
Test we are able to get the correct time
|
|
|
|
'''
|
|
|
|
t1 = datetime.datetime.now()
|
|
|
|
res = self.run_function('system.get_system_date_time')
|
|
|
|
t2 = datetime.datetime.strptime(res, self.fmt_str)
|
2016-06-15 19:34:54 +00:00
|
|
|
msg = ("Difference in times is too large. Now: {0} Fake: {1}"
|
2016-06-20 17:05:05 +00:00
|
|
|
.format(t1, t2))
|
2016-06-15 19:34:54 +00:00
|
|
|
self.assertTrue(self._same_times(t1, t2), msg=msg)
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
def test_get_system_date_time_utc(self):
|
|
|
|
'''
|
|
|
|
Test we are able to get the correct time with utc
|
|
|
|
'''
|
|
|
|
t1 = datetime.datetime.utcnow()
|
2016-06-21 19:24:49 +00:00
|
|
|
res = self.run_function('system.get_system_date_time',
|
|
|
|
utc_offset="+0000")
|
2016-06-14 20:47:17 +00:00
|
|
|
t2 = datetime.datetime.strptime(res, self.fmt_str)
|
2016-06-15 19:34:54 +00:00
|
|
|
msg = ("Difference in times is too large. Now: {0} Fake: {1}"
|
2016-06-20 17:05:05 +00:00
|
|
|
.format(t1, t2))
|
2016-06-14 20:47:17 +00:00
|
|
|
self.assertTrue(self._same_times(t1, t2), msg=msg)
|
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
|
|
|
def test_set_system_date_time(self):
|
|
|
|
'''
|
|
|
|
Test changing the system clock. We are only able to set it up to a
|
|
|
|
resolution of a second so this test may appear to run in negative time.
|
|
|
|
'''
|
2016-06-21 19:24:49 +00:00
|
|
|
cmp_time = datetime.datetime.strptime("1981-02-03 04:05:06",
|
|
|
|
self.fmt_str)
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
self._save_time()
|
2016-06-21 19:24:49 +00:00
|
|
|
result = self._set_time(cmp_time)
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
time_now = datetime.datetime.now()
|
2016-06-15 19:34:54 +00:00
|
|
|
msg = ("Difference in times is too large. Now: {0} Fake: {1}"
|
2016-06-21 19:24:49 +00:00
|
|
|
.format(time_now, cmp_time))
|
|
|
|
self.assertTrue(result and self._same_times(time_now, cmp_time),
|
|
|
|
msg=msg)
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
|
|
|
def test_set_system_date_time_utc(self):
|
|
|
|
'''
|
|
|
|
Test changing the system clock. We are only able to set it up to a
|
|
|
|
resolution of a second so this test may appear to run in negative time.
|
|
|
|
'''
|
2016-06-21 19:24:49 +00:00
|
|
|
cmp_time = datetime.datetime.strptime("1981-02-03 04:05:06", self.fmt_str)
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
self._save_time()
|
2016-06-21 19:24:49 +00:00
|
|
|
result = self._set_time(cmp_time, offset="+0000")
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
time_now = datetime.datetime.utcnow()
|
2016-06-15 19:34:54 +00:00
|
|
|
msg = ("Difference in times is too large. Now: {0} Fake: {1}"
|
2016-06-21 19:24:49 +00:00
|
|
|
.format(time_now, cmp_time))
|
|
|
|
self.assertTrue(result)
|
|
|
|
self.assertTrue(self._same_times(time_now, cmp_time), msg=msg)
|
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
|
|
|
def test_set_system_date_time_utcoffset_east(self):
|
|
|
|
'''
|
|
|
|
Test changing the system clock. We are only able to set it up to a
|
|
|
|
resolution of a second so this test may appear to run in negative time.
|
|
|
|
'''
|
|
|
|
cmp_time = datetime.datetime.strptime("1981-02-03 11:05:06",
|
|
|
|
self.fmt_str)
|
|
|
|
offset_str = "-0700"
|
|
|
|
time_to_set = datetime.datetime.strptime("1981-02-03 04:05:06",
|
|
|
|
self.fmt_str)
|
|
|
|
|
|
|
|
self._save_time()
|
|
|
|
|
|
|
|
result = self._set_time(time_to_set, offset=offset_str)
|
2016-06-14 20:47:17 +00:00
|
|
|
|
2016-06-21 19:24:49 +00:00
|
|
|
time_now = datetime.datetime.utcnow()
|
|
|
|
msg = ("Difference in times is too large. Now: {0} Fake: {1}"
|
|
|
|
.format(time_now, cmp_time))
|
|
|
|
self.assertTrue(result)
|
|
|
|
self.assertTrue(self._same_times(time_now, cmp_time), msg=msg)
|
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
|
|
|
def test_set_system_date_time_utcoffset_west(self):
|
|
|
|
'''
|
|
|
|
Test changing the system clock. We are only able to set it up to a
|
|
|
|
resolution of a second so this test may appear to run in negative time.
|
|
|
|
'''
|
|
|
|
cmp_time = datetime.datetime.strptime("1981-02-03 02:05:06",
|
|
|
|
self.fmt_str)
|
|
|
|
offset_str = "+0200"
|
|
|
|
time_to_set = datetime.datetime.strptime("1981-02-03 04:05:06",
|
|
|
|
self.fmt_str)
|
|
|
|
|
|
|
|
self._save_time()
|
|
|
|
result = self._set_time(time_to_set, offset=offset_str)
|
|
|
|
|
|
|
|
time_now = datetime.datetime.utcnow()
|
|
|
|
msg = ("Difference in times is too large. Now: {0} Fake: {1}"
|
|
|
|
.format(time_now, cmp_time))
|
|
|
|
self.assertTrue(result)
|
|
|
|
self.assertTrue(self._same_times(time_now, cmp_time), msg=msg)
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
|
|
|
def test_set_system_time(self):
|
|
|
|
'''
|
|
|
|
Test setting the system time without adjusting the date.
|
|
|
|
'''
|
2016-06-21 19:24:49 +00:00
|
|
|
cmp_time = datetime.datetime.now().replace(hour=10, minute=5, second=0)
|
2016-06-14 20:47:17 +00:00
|
|
|
self._save_time()
|
|
|
|
|
2016-06-21 19:24:49 +00:00
|
|
|
result = self.run_function('system.set_system_time', ["10:05:00"])
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
time_now = datetime.datetime.now()
|
2016-06-15 19:34:54 +00:00
|
|
|
msg = ("Difference in times is too large. Now: {0} Fake: {1}"
|
2016-06-21 19:24:49 +00:00
|
|
|
.format(time_now, cmp_time))
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
self.assertTrue(result)
|
2016-06-21 19:24:49 +00:00
|
|
|
self.assertTrue(self._same_times(time_now, cmp_time), msg=msg)
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
|
|
|
def test_set_system_date(self):
|
|
|
|
'''
|
|
|
|
Test setting the system date without adjusting the time.
|
|
|
|
'''
|
2016-06-21 19:24:49 +00:00
|
|
|
cmp_time = datetime.datetime.now().replace(year=2000, month=12, day=25)
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
self._save_time()
|
|
|
|
result = self.run_function('system.set_system_date', ["2000-12-25"])
|
|
|
|
|
|
|
|
time_now = datetime.datetime.now()
|
2016-06-15 19:34:54 +00:00
|
|
|
msg = ("Difference in times is too large. Now: {0} Fake: {1}"
|
2016-06-21 19:24:49 +00:00
|
|
|
.format(time_now, cmp_time))
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
self.assertTrue(result)
|
2016-06-21 19:24:49 +00:00
|
|
|
self.assertTrue(self._same_times(time_now, cmp_time), msg=msg)
|
2016-06-14 20:47:17 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(SystemModuleTest)
|