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""" |
| 11 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 12 | config = utils.get_configuration() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 13 | nodes_time = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 14 | tgt='*', |
| 15 | param='date +%s', |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 16 | expr_form='compound') |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame] | 17 | result = {} |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 18 | for node, time in nodes_time.iteritems(): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 19 | if isinstance(nodes_time[node], bool): |
| 20 | time = 'Cannot access node(-s)' |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame] | 21 | 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 Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 28 | assert len(result) <= 1, ( |
| 29 | 'Time is out of sync on the following nodes:\n{}'.format( |
| 30 | json.dumps(result, indent=4)) |
| 31 | ) |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 32 | |
| 33 | |
Ievgeniia Zadorozhna | dbb042e | 2019-11-06 15:46:26 +0300 | [diff] [blame^] | 34 | @pytest.mark.flaky(reruns=5, reruns_delay=60) |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 35 | @pytest.mark.smoke |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 36 | def test_ntp_peers_state(local_salt_client): |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame] | 37 | """Test gets ntpq peers state and checks the system peer is declared""" |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 38 | state = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 39 | tgt='*', |
| 40 | param='ntpq -pn', |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 41 | expr_form='compound') |
| 42 | final_result = {} |
| 43 | for node in state: |
| 44 | sys_peer_declared = False |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 45 | if not state[node]: |
| 46 | # TODO: do not skip |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 47 | logging.warning("Node {} is skipped".format(node)) |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 48 | continue |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 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 Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 67 | assert not final_result, ( |
| 68 | "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] | 69 | "declared system peer:\n{}".format(json.dumps(final_result, indent=4)) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 70 | ) |