Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 1 | import json |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 2 | import utils |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 3 | import pytest |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 4 | import logging |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 5 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 6 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 7 | @pytest.mark.smoke |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 8 | # move to sl? |
Oleksii Zhurba | 17a8848 | 2017-10-06 14:29:05 -0500 | [diff] [blame] | 9 | def test_ntp_sync(local_salt_client): |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame] | 10 | """Test checks that system time is the same across all nodes""" |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 11 | config = utils.get_configuration() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 12 | nodes_time = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 13 | tgt='*', |
| 14 | param='date +%s', |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 15 | expr_form='compound') |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame] | 16 | result = {} |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 17 | for node, time in nodes_time.items(): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 18 | if isinstance(nodes_time[node], bool): |
| 19 | time = 'Cannot access node(-s)' |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame] | 20 | 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] |
Sergey Galkin | aa4f939 | 2019-12-03 14:16:30 +0400 | [diff] [blame] | 27 | for time in result: |
| 28 | time_diff = abs(int(time)-int(list(result)[0])) |
| 29 | assert time_diff <= config.get("maximum_time_diff"), ( |
| 30 | 'Time is out of sync on the following nodes:\n{}'.format( |
| 31 | json.dumps(result, indent=4)) |
| 32 | ) |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 33 | |
| 34 | |
Ievgeniia Zadorozhna | dbb042e | 2019-11-06 15:46:26 +0300 | [diff] [blame] | 35 | @pytest.mark.flaky(reruns=5, reruns_delay=60) |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 36 | @pytest.mark.smoke |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 37 | def test_ntp_peers_state(local_salt_client): |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame] | 38 | """Test gets ntpq peers state and checks the system peer is declared""" |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 39 | state = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 40 | tgt='*', |
| 41 | param='ntpq -pn', |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 42 | expr_form='compound') |
| 43 | final_result = {} |
| 44 | for node in state: |
| 45 | sys_peer_declared = False |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 46 | if not state[node]: |
| 47 | # TODO: do not skip |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 48 | logging.warning("Node {} is skipped".format(node)) |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 49 | continue |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 50 | ntpq_output = state[node].split('\n') |
| 51 | # if output has no 'remote' in the head of ntpq output |
| 52 | # the 'ntqp -np' command failed and cannot check peers |
| 53 | if 'remote' not in ntpq_output[0]: |
| 54 | final_result[node] = ntpq_output |
| 55 | continue |
| 56 | |
| 57 | # take 3rd+ line of output (the actual peers) |
| 58 | try: |
| 59 | peers = ntpq_output[2:] |
| 60 | except IndexError: |
| 61 | final_result[node] = ntpq_output |
| 62 | continue |
| 63 | for p in peers: |
| 64 | if p.split()[0].startswith("*"): |
| 65 | sys_peer_declared = True |
| 66 | if not sys_peer_declared: |
| 67 | final_result[node] = ntpq_output |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 68 | assert not final_result, ( |
| 69 | "NTP peers state is not as expected on some nodes; could not find " |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 70 | "declared system peer:\n{}".format(json.dumps(final_result, indent=4)) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 71 | ) |