提交 40cdd82e 编写于 作者: C Chris Evich 提交者: Lucas Meneghel Rodrigues

virt-sandbox: Implement date test

Signed-off-by: NChris Evich <cevich@redhat.com>
上级 3dad115d
- sandbox_date:
type = sandbox_date
virt_test_type = sandbox
- virt_sandbox_date:
type = virt_sandbox_date
virt_test_type = sandbox
status_error = 'no'
# Common key used by all TestSandboxes subclasses
sandbox_command = '/usr/bin/date +%s.%N'
variants:
- positive_testing:
variants:
- one_sandbox:
# Default parameters at work here
- two_sandboxes:
# Can override the default
sandbox_count = 2
- three_sandboxes:
# Can override TestSandboxes subclass instance param
sandbox_count_TestSimpleSandboxes = 3
- negative_testing:
status_error = 'yes'
variants:
- broken_sandbox:
# The command can be changed per-class also
sandbox_command_TestSimpleSandboxes = '/bin/false'
- broken_date:
sandbox_command = '/usr/bin/date +9%s.%N'
import logging, datetime
from autotest.client.shared import error, utils
def run_sandbox_date(test, params, env):
cmdresult = utils.run('virt-sandbox -c lxc:/// -- /usr/bin/date +%s')
test_dt = datetime.datetime.fromtimestamp(float(cmdresult.stdout))
local_dt = datetime.datetime.now()
delta = local_dt - test_dt
if delta.days < 0:
delta = test_dt - local_dt
tenseconds = datetime.timedelta(seconds=5)
if delta > tenseconds:
raise error.TestFail("Time difference greater than %s" % tenseconds)
"""
Simple test that executes date command in a sanbox and verifies it is correct
"""
import datetime
from autotest.client.shared import error
from virttest.sandbox import make_sandboxes
def verify_datetime(start_time, stop_time, result_list):
"""
Return the number of sandboxes which reported incorrect date
"""
bad_dt = 0
for results in result_list: # list of aggregate managers
for result in results: # list of sandbox stdouts
try:
test_dt = datetime.datetime.fromtimestamp(float(result.strip()))
except (TypeError,ValueError):
bad_dt += 1
else:
if test_dt >= start_time and test_dt <= stop_time:
continue # good result, check next
else:
bad_dt += 1
return bad_dt
def some_failed(failed_list):
"""
Return True if any single sandbox reported a non-zero exit code
"""
for failed in failed_list: # list of sandboxes w/ non-zero exit codes
if failed > 0:
return True
return False
def run_virt_sandbox_date(test, params, env):
"""
Executes date command in a sanbox and verifies it is correct
1) Gather parameters
2) Create configured sandbox aggregater(s)
3) Run and stop all sandboxes in all agregators
4) Handle results
"""
# Record time for comparison when finished
start_time = datetime.datetime.now()
status_error = bool('yes' == params.get('status_error', 'no'))
# list of sandbox agregate managers (containing list of sandboxes)
sb_agg_list = make_sandboxes(params, env)
# Number of sandboxes for each aggregate type
agg_count = [agg.count for agg in sb_agg_list]
# Run all sandboxes until timeout or finished w/ output
# store list of stdout's for each sandbox in each agregate type
result_list = [agg.results() for agg in sb_agg_list]
# Timeouts throw SandboxException, if normal exit, record ending time
stop_time = datetime.datetime.now()
# Number of sandboxs with non-zero exit codes for each aggregate type
failed_list = [agg.are_failed() for agg in sb_agg_list]
# handle results
if status_error: # Negative test
if not some_failed(failed_list) and verify_datetime(start_time,
stop_time,
result_list) < 1:
raise error.TestFail("Error test failed on only %s of %s sandboxes"
% (failed_list, agg_count))
else: # Positive test
if some_failed(failed_list):
raise error.TestFail("Some sandboxes had non-zero exit codes")
if verify_datetime(start_time, stop_time, result_list) > 0:
raise error.TestFail("Some sandboxes reported invalid date/time")
# Otherwise test passed
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册