blob: 21528ca73a814d8b69eb887ea2119d80c750be75 [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
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -050034@pytest.mark.smoke
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030035def test_ntp_peers_state(local_salt_client):
Oleksii Zhurba9b744862019-02-26 17:33:43 -060036 """Test gets ntpq peers state and checks the system peer is declared"""
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030037 state = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050038 tgt='*',
39 param='ntpq -pn',
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030040 expr_form='compound')
41 final_result = {}
42 for node in state:
43 sys_peer_declared = False
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050044 if not state[node]:
45 # TODO: do not skip
Hanna Arhipova56eab942019-05-06 20:14:18 +030046 logging.warning("Node {} is skipped".format(node))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050047 continue
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030048 ntpq_output = state[node].split('\n')
49 # if output has no 'remote' in the head of ntpq output
50 # the 'ntqp -np' command failed and cannot check peers
51 if 'remote' not in ntpq_output[0]:
52 final_result[node] = ntpq_output
53 continue
54
55 # take 3rd+ line of output (the actual peers)
56 try:
57 peers = ntpq_output[2:]
58 except IndexError:
59 final_result[node] = ntpq_output
60 continue
61 for p in peers:
62 if p.split()[0].startswith("*"):
63 sys_peer_declared = True
64 if not sys_peer_declared:
65 final_result[node] = ntpq_output
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020066 assert not final_result, (
67 "NTP peers state is not as expected on some nodes; could not find "
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030068 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020069 )