blob: 0210d79076e0edc35aff31e5fe9958554e08a3d1 [file] [log] [blame]
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging, math, re
from autotest_lib.client.bin import test, utils
from autotest_lib.client.common_lib import error
from autotest_lib.client.cros import cros_logging
class platform_AccurateTime(test.test):
version = 1
def __get_offset(self, string):
if (string.find('No time correction needed') > -1) :
return float(0.0)
else :
offset ='Setting (-?[\d+\.]+) seconds', string)
if offset is None:
# If string is empty, check the sys logs dumped later.
raise error.TestError('Unable to find offset in %s' % string)
return float(
def run_once(self):
reader = cros_logging.LogReader()
# Check if htpdate is currently running
if utils.system('pgrep htpdate', ignore_status=True) != 0:
raise error.TestError('htpdate server was not already running')
# Stop it since we cannot start another instance of htpdate
utils.system('initctl stop htpdate')
# Now grab the current time and get its offset
cmd = '/usr/sbin/htpdate -4 -u ntp:ntp -s -t -w'
output = utils.system_output(cmd,retain_output=True)
server_offset = self.__get_offset(output)"server time offset: %f" % server_offset)
self.write_perf_keyval({'seconds_offset': abs(server_offset)})
utils.system('initctl start htpdate')
logging.debug('sys logs emitted: %s' % reader.get_logs())