blob: 3636f1410413eba78c060e94d535542929ca66e1 [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
8 active_nodes = utils.get_active_nodes()
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +00009 config = utils.get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000010 nodes_time = local_salt_client.cmd(
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000011 utils.list_to_target_string(active_nodes, 'or'),
12 'cmd.run',
13 ['date +%s'],
14 expr_form='compound')
Oleksii Zhurba9b744862019-02-26 17:33:43 -060015 result = {}
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000016 for node, time in nodes_time.iteritems():
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
Oleksii Zhurba9b744862019-02-26 17:33:43 -060031 active_nodes = utils.get_active_nodes()
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030032 state = local_salt_client.cmd(
33 utils.list_to_target_string(active_nodes, 'or'),
34 'cmd.run',
35 ['ntpq -pn'],
36 expr_form='compound')
37 final_result = {}
38 for node in state:
39 sys_peer_declared = False
40 ntpq_output = state[node].split('\n')
41 # if output has no 'remote' in the head of ntpq output
42 # the 'ntqp -np' command failed and cannot check peers
43 if 'remote' not in ntpq_output[0]:
44 final_result[node] = ntpq_output
45 continue
46
47 # take 3rd+ line of output (the actual peers)
48 try:
49 peers = ntpq_output[2:]
50 except IndexError:
51 final_result[node] = ntpq_output
52 continue
53 for p in peers:
54 if p.split()[0].startswith("*"):
55 sys_peer_declared = True
56 if not sys_peer_declared:
57 final_result[node] = ntpq_output
58 assert not final_result,\
59 "NTP peers state is not expected on some nodes, could not find " \
60 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))