blob: 3636f1410413eba78c060e94d535542929ca66e1 [file] [log] [blame]
import json
import utils
def test_ntp_sync(local_salt_client):
"""Test checks that system time is the same across all nodes"""
active_nodes = utils.get_active_nodes()
config = utils.get_configuration()
nodes_time = local_salt_client.cmd(
utils.list_to_target_string(active_nodes, 'or'),
'cmd.run',
['date +%s'],
expr_form='compound')
result = {}
for node, time in nodes_time.iteritems():
if node in config.get("ntp_skipped_nodes"):
continue
if time in result:
result[time].append(node)
result[time].sort()
else:
result[time] = [node]
assert len(result) <= 1, 'Not all nodes have the same time:\n {}'.format(
json.dumps(result, indent=4))
def test_ntp_peers_state(local_salt_client):
"""Test gets ntpq peers state and checks the system peer is declared"""
active_nodes = utils.get_active_nodes()
state = local_salt_client.cmd(
utils.list_to_target_string(active_nodes, 'or'),
'cmd.run',
['ntpq -pn'],
expr_form='compound')
final_result = {}
for node in state:
sys_peer_declared = False
ntpq_output = state[node].split('\n')
# if output has no 'remote' in the head of ntpq output
# the 'ntqp -np' command failed and cannot check peers
if 'remote' not in ntpq_output[0]:
final_result[node] = ntpq_output
continue
# take 3rd+ line of output (the actual peers)
try:
peers = ntpq_output[2:]
except IndexError:
final_result[node] = ntpq_output
continue
for p in peers:
if p.split()[0].startswith("*"):
sys_peer_declared = True
if not sys_peer_declared:
final_result[node] = ntpq_output
assert not final_result,\
"NTP peers state is not expected on some nodes, could not find " \
"declared system peer:\n{}".format(json.dumps(final_result, indent=4))