blob: abf0d8aff0268947f7421b399ade4c5eba534de6 [file] [log] [blame]
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03001import json
2import utils
3import pytest
4
5@pytest.mark.xfail
6def test_ntp_sync(local_salt_client):
7 """Test checks that system time is the same across all nodes"""
8
9 config = utils.get_configuration()
10 nodes_time = local_salt_client.cmd(
11 tgt='*',
12 param='date +%s',
13 expr_form='compound')
14 result = {}
15 for node, time in nodes_time.iteritems():
16 if isinstance(nodes_time[node], bool):
17 time = 'Cannot access node(-s)'
18 if node in config.get("ntp_skipped_nodes"):
19 continue
20 if time in result:
21 result[time].append(node)
22 result[time].sort()
23 else:
24 result[time] = [node]
25 assert len(result) <= 1, 'Not all nodes have the same time:\n {}'.format(
26 json.dumps(result, indent=4))
27
28
29def test_ntp_peers_state(local_salt_client):
30 """Test gets ntpq peers state and checks the system peer is declared"""
31 state = local_salt_client.cmd(
32 tgt='*',
33 param='ntpq -pn',
34 expr_form='compound')
35 final_result = {}
36 for node in state:
37 sys_peer_declared = False
38 if not state[node]:
39 # TODO: do not skip
40 print ("Node {} is skipped".format(node))
41 continue
42 ntpq_output = state[node].split('\n')
43 # if output has no 'remote' in the head of ntpq output
44 # the 'ntqp -np' command failed and cannot check peers
45 if 'remote' not in ntpq_output[0]:
46 final_result[node] = ntpq_output
47 continue
48
49 # take 3rd+ line of output (the actual peers)
50 try:
51 peers = ntpq_output[2:]
52 except IndexError:
53 final_result[node] = ntpq_output
54 continue
55 for p in peers:
56 if p.split()[0].startswith("*"):
57 sys_peer_declared = True
58 if not sys_peer_declared:
59 final_result[node] = ntpq_output
60 assert not final_result,\
61 "NTP peers state is not expected on some nodes, could not find " \
62 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))