"""
Simple test that executes date command in a sanbox and verifies it is correct
"""
import datetime
from virttest.lvsb 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:
for result in results:
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
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:
if failed > 0:
return True
return False
def run(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
"""
start_time = datetime.datetime.now()
status_error = bool('yes' == params.get('status_error', 'no'))
sb_agg_list = make_sandboxes(params, env)
agg_count = [agg.count for agg in sb_agg_list]
result_list = [agg.results() for agg in sb_agg_list]
stop_time = datetime.datetime.now()
failed_list = [agg.are_failed() for agg in sb_agg_list]
if status_error:
if not some_failed(failed_list) and verify_datetime(start_time,
stop_time,
result_list) < 1:
test.fail("Error test failed on only %s of %s sandboxes"
% (failed_list, agg_count))
else:
if some_failed(failed_list):
test.fail("Some sandboxes had non-zero exit codes")
if verify_datetime(start_time, stop_time, result_list) > 0:
test.fail("Some sandboxes reported invalid date/time")