blob: bf51290bc33adeb95c650563fe91d257fdb80da1 [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
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +03004
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00005
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -05006@pytest.mark.smoke
7#move to sl?
Oleksii Zhurba17a88482017-10-06 14:29:05 -05008def test_ntp_sync(local_salt_client):
Oleksii Zhurba9b744862019-02-26 17:33:43 -06009 """Test checks that system time is the same across all nodes"""
10
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]
27 assert len(result) <= 1, 'Not all nodes have the same time:\n {}'.format(
28 json.dumps(result, indent=4))
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030029
30
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -050031@pytest.mark.smoke
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030032def test_ntp_peers_state(local_salt_client):
Oleksii Zhurba9b744862019-02-26 17:33:43 -060033 """Test gets ntpq peers state and checks the system peer is declared"""
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030034 state = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050035 tgt='*',
36 param='ntpq -pn',
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030037 expr_form='compound')
38 final_result = {}
39 for node in state:
40 sys_peer_declared = False
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050041 if not state[node]:
42 # TODO: do not skip
43 print ("Node {} is skipped".format(node))
44 continue
Ievgeniia Zadorozhnac48585f2019-02-04 19:38:54 +030045 ntpq_output = state[node].split('\n')
46 # if output has no 'remote' in the head of ntpq output
47 # the 'ntqp -np' command failed and cannot check peers
48 if 'remote' not in ntpq_output[0]:
49 final_result[node] = ntpq_output
50 continue
51
52 # take 3rd+ line of output (the actual peers)
53 try:
54 peers = ntpq_output[2:]
55 except IndexError:
56 final_result[node] = ntpq_output
57 continue
58 for p in peers:
59 if p.split()[0].startswith("*"):
60 sys_peer_declared = True
61 if not sys_peer_declared:
62 final_result[node] = ntpq_output
63 assert not final_result,\
64 "NTP peers state is not expected on some nodes, could not find " \
65 "declared system peer:\n{}".format(json.dumps(final_result, indent=4))