blob: 6751f1af5b300c24689f2ec0d5899bd084ab6e98 [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]
28 assert len(result) <= 1, 'Not all nodes have the same time:\n {}'.format(
29 json.dumps(result, indent=4))
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030030
31
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -050032@pytest.mark.smoke
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030033def test_ntp_peers_state(local_salt_client):
Oleksii Zhurba9b744862019-02-26 17:33:43 -060034 """Test gets ntpq peers state and checks the system peer is declared"""
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030035 state = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050036 tgt='*',
37 param='ntpq -pn',
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030038 expr_form='compound')
39 final_result = {}
40 for node in state:
41 sys_peer_declared = False
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050042 if not state[node]:
43 # TODO: do not skip
Hanna Arhipova56eab942019-05-06 20:14:18 +030044 logging.warning("Node {} is skipped".format(node))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050045 continue
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030046 ntpq_output = state[node].split('\n')
47 # if output has no 'remote' in the head of ntpq output
48 # the 'ntqp -np' command failed and cannot check peers
49 if 'remote' not in ntpq_output[0]:
50 final_result[node] = ntpq_output
51 continue
52
53 # take 3rd+ line of output (the actual peers)
54 try:
55 peers = ntpq_output[2:]
56 except IndexError:
57 final_result[node] = ntpq_output
58 continue
59 for p in peers:
60 if p.split()[0].startswith("*"):
61 sys_peer_declared = True
62 if not sys_peer_declared:
63 final_result[node] = ntpq_output
64 assert not final_result,\
65 "NTP peers state is not expected on some nodes, could not find " \
66 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))