Move _validate_job_timeout() to utils

Let's make this function available for others.
Signed-off-by: NAmador Pahim <apahim@redhat.com>
上级 6bf9e774
......@@ -26,6 +26,7 @@ from avocado.core import loader
from avocado.core import multiplexer
from avocado.core.plugin_interfaces import CLICmd
from avocado.core.settings import settings
from avocado.utils.data_structures import time_to_seconds
class Run(CLICmd):
......@@ -159,34 +160,13 @@ class Run(CLICmd):
node = args.default_avocado_params.get_node(value[0], True)
node.value[value[1]] = value[2]
def _validate_job_timeout(self, raw_timeout):
units = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}
mult = 1
if raw_timeout is not None:
try:
unit = raw_timeout[-1].lower()
if unit in units:
mult = units[unit]
timeout = int(raw_timeout[:-1]) * mult
else:
timeout = int(raw_timeout)
if timeout < 1:
raise ValueError()
except (ValueError, TypeError):
log = logging.getLogger("avocado.app")
log.error("Invalid number '%s' for job timeout. Use an "
"integer number greater than 0", raw_timeout)
sys.exit(exit_codes.AVOCADO_FAIL)
else:
timeout = 0
return timeout
def run(self, args):
"""
Run test modules or simple tests.
:param args: Command line args received from the run subparser.
"""
log = logging.getLogger("avocado.app")
self._activate(args)
if args.unique_job_id is not None:
try:
......@@ -194,9 +174,12 @@ class Run(CLICmd):
if len(args.unique_job_id) != 40:
raise ValueError
except ValueError:
log = logging.getLogger("avocado.app")
log.error('Unique Job ID needs to be a 40 digit hex number')
sys.exit(exit_codes.AVOCADO_FAIL)
args.job_timeout = self._validate_job_timeout(args.job_timeout)
try:
args.job_timeout = time_to_seconds(args.job_timeout)
except ValueError as e:
log.error(e.message)
sys.exit(exit_codes.AVOCADO_FAIL)
job_instance = job.Job(args)
return job_instance.run()
......@@ -218,3 +218,26 @@ class CallbackRegister(object):
to be executed!
"""
self.run()
def time_to_seconds(time):
"""
Convert time in minutes, hours and days to seconds.
:param time: Time, optionally including the unit (i.e. '10d')
"""
units = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}
if time is not None:
try:
unit = time[-1].lower()
if unit in units:
mult = units[unit]
seconds = int(time[:-1]) * mult
else:
seconds = int(time)
except (ValueError, TypeError) as e:
raise ValueError("Invalid value '%s' for time. Use a string with "
"the number and optionally the time unit (s, m, "
"h or d)." % time)
else:
seconds = 0
return seconds
......@@ -135,15 +135,15 @@ class JobTimeOutTest(unittest.TestCase):
def test_invalid_values(self):
cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
'--job-timeout=0 examples/tests/passtest.py' % self.tmpdir)
'--job-timeout=1,5 examples/tests/passtest.py' % self.tmpdir)
result = process.run(cmd_line, ignore_status=True)
self.assertEqual(result.exit_status, exit_codes.AVOCADO_FAIL)
self.assertIn('Invalid number', result.stderr)
self.assertIn('Invalid value', result.stderr)
cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
'--job-timeout=123x examples/tests/passtest.py' % self.tmpdir)
result = process.run(cmd_line, ignore_status=True)
self.assertEqual(result.exit_status, exit_codes.AVOCADO_FAIL)
self.assertIn('Invalid number', result.stderr)
self.assertIn('Invalid value', result.stderr)
def test_valid_values(self):
cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册