blob: e5bed2f0dcbe98b3b89adbd4b41148d313b57b9f [file] [log] [blame]
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +03001import json
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +03002import utils
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -05003import pytest
Hanna Arhipova56eab942019-05-06 20:14:18 +03004import logging
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +03005
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00006
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -05007@pytest.mark.smoke
Hanna Arhipova56eab942019-05-06 20:14:18 +03008# move to sl?
Oleksii Zhurba17a88482017-10-06 14:29:05 -05009def test_ntp_sync(local_salt_client):
Oleksii Zhurba9b744862019-02-26 17:33:43 -060010 """Test checks that system time is the same across all nodes"""
11
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000012 config = utils.get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000013 nodes_time = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050014 tgt='*',
15 param='date +%s',
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000016 expr_form='compound')
Oleksii Zhurba9b744862019-02-26 17:33:43 -060017 result = {}
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000018 for node, time in nodes_time.iteritems():
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050019 if isinstance(nodes_time[node], bool):
20 time = 'Cannot access node(-s)'
Oleksii Zhurba9b744862019-02-26 17:33:43 -060021 if node in config.get("ntp_skipped_nodes"):
22 continue
23 if time in result:
24 result[time].append(node)
25 result[time].sort()
26 else:
27 result[time] = [node]
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020028 assert len(result) <= 1, (
29 'Time is out of sync on the following nodes:\n{}'.format(
30 json.dumps(result, indent=4))
31 )
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030032
33
Ievgeniia Zadorozhnadbb042e2019-11-06 15:46:26 +030034@pytest.mark.flaky(reruns=5, reruns_delay=60)
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -050035@pytest.mark.smoke
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030036def test_ntp_peers_state(local_salt_client):
Oleksii Zhurba9b744862019-02-26 17:33:43 -060037 """Test gets ntpq peers state and checks the system peer is declared"""
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030038 state = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050039 tgt='*',
40 param='ntpq -pn',
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030041 expr_form='compound')
42 final_result = {}
43 for node in state:
44 sys_peer_declared = False
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050045 if not state[node]:
46 # TODO: do not skip
Hanna Arhipova56eab942019-05-06 20:14:18 +030047 logging.warning("Node {} is skipped".format(node))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050048 continue
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030049 ntpq_output = state[node].split('\n')
50 # if output has no 'remote' in the head of ntpq output
51 # the 'ntqp -np' command failed and cannot check peers
52 if 'remote' not in ntpq_output[0]:
53 final_result[node] = ntpq_output
54 continue
55
56 # take 3rd+ line of output (the actual peers)
57 try:
58 peers = ntpq_output[2:]
59 except IndexError:
60 final_result[node] = ntpq_output
61 continue
62 for p in peers:
63 if p.split()[0].startswith("*"):
64 sys_peer_declared = True
65 if not sys_peer_declared:
66 final_result[node] = ntpq_output
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020067 assert not final_result, (
68 "NTP peers state is not as expected on some nodes; could not find "
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030069 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020070 )