blob: dd199d1567254a2048d4df757ee63de33466e5f3 [file] [log] [blame]
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +03001import json
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +03002import utils
3
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00004
Oleksii Zhurba17a88482017-10-06 14:29:05 -05005def test_ntp_sync(local_salt_client):
Oleksii Zhurba9b744862019-02-26 17:33:43 -06006 """Test checks that system time is the same across all nodes"""
7
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +00008 config = utils.get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00009 nodes_time = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050010 tgt='*',
11 param='date +%s',
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000012 expr_form='compound')
Oleksii Zhurba9b744862019-02-26 17:33:43 -060013 result = {}
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000014 for node, time in nodes_time.iteritems():
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050015 if isinstance(nodes_time[node], bool):
16 time = 'Cannot access node(-s)'
Oleksii Zhurba9b744862019-02-26 17:33:43 -060017 if node in config.get("ntp_skipped_nodes"):
18 continue
19 if time in result:
20 result[time].append(node)
21 result[time].sort()
22 else:
23 result[time] = [node]
24 assert len(result) <= 1, 'Not all nodes have the same time:\n {}'.format(
25 json.dumps(result, indent=4))
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030026
27
28def test_ntp_peers_state(local_salt_client):
Oleksii Zhurba9b744862019-02-26 17:33:43 -060029 """Test gets ntpq peers state and checks the system peer is declared"""
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030030 state = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050031 tgt='*',
32 param='ntpq -pn',
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030033 expr_form='compound')
34 final_result = {}
35 for node in state:
36 sys_peer_declared = False
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050037 if not state[node]:
38 # TODO: do not skip
39 print ("Node {} is skipped".format(node))
40 continue
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030041 ntpq_output = state[node].split('\n')
42 # if output has no 'remote' in the head of ntpq output
43 # the 'ntqp -np' command failed and cannot check peers
44 if 'remote' not in ntpq_output[0]:
45 final_result[node] = ntpq_output
46 continue
47
48 # take 3rd+ line of output (the actual peers)
49 try:
50 peers = ntpq_output[2:]
51 except IndexError:
52 final_result[node] = ntpq_output
53 continue
54 for p in peers:
55 if p.split()[0].startswith("*"):
56 sys_peer_declared = True
57 if not sys_peer_declared:
58 final_result[node] = ntpq_output
59 assert not final_result,\
60 "NTP peers state is not expected on some nodes, could not find " \
61 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))