blob: f4161f2880a6745bd350cc9361a576b3d4fd4a03 [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"""
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000011 config = utils.get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000012 nodes_time = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050013 tgt='*',
14 param='date +%s',
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000015 expr_form='compound')
Oleksii Zhurba9b744862019-02-26 17:33:43 -060016 result = {}
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000017 for node, time in nodes_time.iteritems():
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050018 if isinstance(nodes_time[node], bool):
19 time = 'Cannot access node(-s)'
Oleksii Zhurba9b744862019-02-26 17:33:43 -060020 if node in config.get("ntp_skipped_nodes"):
21 continue
22 if time in result:
23 result[time].append(node)
24 result[time].sort()
25 else:
26 result[time] = [node]
Sergey Galkinaa4f9392019-12-03 14:16:30 +040027 for time in result:
28 time_diff = abs(int(time)-int(list(result)[0]))
29 assert time_diff <= config.get("maximum_time_diff"), (
30 'Time is out of sync on the following nodes:\n{}'.format(
31 json.dumps(result, indent=4))
32 )
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030033
34
Ievgeniia Zadorozhnadbb042e2019-11-06 15:46:26 +030035@pytest.mark.flaky(reruns=5, reruns_delay=60)
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -050036@pytest.mark.smoke
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030037def test_ntp_peers_state(local_salt_client):
Oleksii Zhurba9b744862019-02-26 17:33:43 -060038 """Test gets ntpq peers state and checks the system peer is declared"""
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030039 state = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050040 tgt='*',
41 param='ntpq -pn',
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030042 expr_form='compound')
43 final_result = {}
44 for node in state:
45 sys_peer_declared = False
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050046 if not state[node]:
47 # TODO: do not skip
Hanna Arhipova56eab942019-05-06 20:14:18 +030048 logging.warning("Node {} is skipped".format(node))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050049 continue
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030050 ntpq_output = state[node].split('\n')
51 # if output has no 'remote' in the head of ntpq output
52 # the 'ntqp -np' command failed and cannot check peers
53 if 'remote' not in ntpq_output[0]:
54 final_result[node] = ntpq_output
55 continue
56
57 # take 3rd+ line of output (the actual peers)
58 try:
59 peers = ntpq_output[2:]
60 except IndexError:
61 final_result[node] = ntpq_output
62 continue
63 for p in peers:
64 if p.split()[0].startswith("*"):
65 sys_peer_declared = True
66 if not sys_peer_declared:
67 final_result[node] = ntpq_output
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020068 assert not final_result, (
69 "NTP peers state is not as expected on some nodes; could not find "
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030070 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020071 )