blob: 23a98a323fed245a91c6df3f6fbfe69873a1da41 [file] [log] [blame]
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +03001import json
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +00002import os
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00003
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +03004import utils
5
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00006
Oleksii Zhurba17a88482017-10-06 14:29:05 -05007def test_ntp_sync(local_salt_client):
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +00008 testname = os.path.basename(__file__).split('.')[0]
9 active_nodes = utils.get_active_nodes(os.path.basename(__file__))
10 config = utils.get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000011 fail = {}
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000012 saltmaster_time = int(local_salt_client.cmd(
Oleksii Zhurba17a88482017-10-06 14:29:05 -050013 'salt:master',
14 'cmd.run',
15 ['date +%s'],
16 expr_form='pillar').values()[0])
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000017 nodes_time = local_salt_client.cmd(
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000018 utils.list_to_target_string(active_nodes, 'or'),
19 'cmd.run',
20 ['date +%s'],
21 expr_form='compound')
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000022 diff = config.get(testname)["time_deviation"] or 30
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000023 for node, time in nodes_time.iteritems():
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000024 if (int(time) - saltmaster_time) > diff or \
25 (int(time) - saltmaster_time) < -diff:
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000026 fail[node] = time
27
28 assert not fail, 'SaltMaster time: {}\n' \
29 'Nodes with time mismatch:\n {}'.format(saltmaster_time,
30 fail)
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030031
32
33def test_ntp_peers_state(local_salt_client):
34 """Test gets ntpq peers state and check the system peer is declared"""
35
36 active_nodes = utils.get_active_nodes(os.path.basename(__file__))
37 state = local_salt_client.cmd(
38 utils.list_to_target_string(active_nodes, 'or'),
39 'cmd.run',
40 ['ntpq -pn'],
41 expr_form='compound')
42 final_result = {}
43 for node in state:
44 sys_peer_declared = False
45 ntpq_output = state[node].split('\n')
46 # if output has no 'remote' in the head of ntpq output
47 # the 'ntqp -np' command failed and cannot check peers
48 if 'remote' not in ntpq_output[0]:
49 final_result[node] = ntpq_output
50 continue
51
52 # take 3rd+ line of output (the actual peers)
53 try:
54 peers = ntpq_output[2:]
55 except IndexError:
56 final_result[node] = ntpq_output
57 continue
58 for p in peers:
59 if p.split()[0].startswith("*"):
60 sys_peer_declared = True
61 if not sys_peer_declared:
62 final_result[node] = ntpq_output
63 assert not final_result,\
64 "NTP peers state is not expected on some nodes, could not find " \
65 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))