blob: 998aa314fff6a6986f57b4a04e33cedd977ed196 [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"""
12
13 config = utils.get_configuration()
14 nodes_time = local_salt_client.cmd(
15 tgt='*',
16 param='date +%s',
17 expr_form='compound')
18 result = {}
19 for node, time in nodes_time.iteritems():
20 if isinstance(nodes_time[node], bool):
21 time = 'Cannot access node(-s)'
22 if node in config.get("ntp_skipped_nodes"):
23 continue
24 if time in result:
25 result[time].append(node)
26 result[time].sort()
27 else:
28 result[time] = [node]
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +020029 assert len(result) <= 1, (
30 'Time is out of sync on the following nodes:\n{}'.format(
31 json.dumps(result, indent=4))
32 )
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030033
34
Oleksii Zhurba23c18332019-05-09 18:53:40 -050035@pytest.mark.smoke
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030036def test_ntp_peers_state(local_salt_client):
37 """Test gets ntpq peers state and checks the system peer is declared"""
38 state = local_salt_client.cmd(
39 tgt='*',
40 param='ntpq -pn',
41 expr_form='compound')
42 final_result = {}
43 for node in state:
44 sys_peer_declared = False
45 if not state[node]:
46 # TODO: do not skip
Hanna Arhipova1eef8312019-05-06 20:14:18 +030047 logging.warning("Node {} is skipped".format(node))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030048 continue
49 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 Kruglovbc0a88b2019-08-20 11:45:35 +020067 assert not final_result, (
68 "NTP peers state is not as expected on some nodes; could not find "
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030069 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))
Dmitriy Kruglovbc0a88b2019-08-20 11:45:35 +020070 )