blob: ebe0c2fb0b0296dc474fb6ae6af87cf66a17ae22 [file] [log] [blame]
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03001import json
2import utils
3import pytest
Hanna Arhipova1eef8312019-05-06 20:14:18 +03004import logging
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03005
Oleksii Zhurba23c18332019-05-09 18:53:40 -05006
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03007@pytest.mark.xfail
Oleksii Zhurba23c18332019-05-09 18:53:40 -05008@pytest.mark.smoke
9#move to sl?
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030010def test_ntp_sync(local_salt_client):
11 """Test checks that system time is the same across all nodes"""
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030012 config = utils.get_configuration()
13 nodes_time = local_salt_client.cmd(
14 tgt='*',
15 param='date +%s',
16 expr_form='compound')
17 result = {}
Ekaterina Chernovac73bc4e2019-11-12 14:56:03 +030018 for node, time in nodes_time.items():
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030019 if isinstance(nodes_time[node], bool):
20 time = 'Cannot access node(-s)'
21 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]
Sergey Galkin9daa27d2019-12-03 14:16:30 +040028 for time in result:
29 time_diff = abs(int(time)-int(list(result)[0]))
30 assert time_diff <= config.get("maximum_time_diff"), (
31 'Time is out of sync on the following nodes:\n{}'.format(
32 json.dumps(result, indent=4))
33 )
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030034
35
Ievgeniia Zadorozhnab7b00b52019-11-06 15:46:26 +030036@pytest.mark.flaky(reruns=5, reruns_delay=60)
Oleksii Zhurba23c18332019-05-09 18:53:40 -050037@pytest.mark.smoke
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030038def test_ntp_peers_state(local_salt_client):
39 """Test gets ntpq peers state and checks the system peer is declared"""
40 state = local_salt_client.cmd(
41 tgt='*',
42 param='ntpq -pn',
43 expr_form='compound')
44 final_result = {}
45 for node in state:
46 sys_peer_declared = False
47 if not state[node]:
48 # TODO: do not skip
Hanna Arhipova1eef8312019-05-06 20:14:18 +030049 logging.warning("Node {} is skipped".format(node))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030050 continue
51 ntpq_output = state[node].split('\n')
52 # if output has no 'remote' in the head of ntpq output
53 # the 'ntqp -np' command failed and cannot check peers
54 if 'remote' not in ntpq_output[0]:
55 final_result[node] = ntpq_output
56 continue
57
58 # take 3rd+ line of output (the actual peers)
59 try:
60 peers = ntpq_output[2:]
61 except IndexError:
62 final_result[node] = ntpq_output
63 continue
64 for p in peers:
65 if p.split()[0].startswith("*"):
66 sys_peer_declared = True
67 if not sys_peer_declared:
68 final_result[node] = ntpq_output
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +020069 assert not final_result, (
70 "NTP peers state is not as expected on some nodes; could not find "
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030071 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +020072 )