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 |
| 3 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 4 | |
Oleksii Zhurba | 17a8848 | 2017-10-06 14:29:05 -0500 | [diff] [blame] | 5 | def test_ntp_sync(local_salt_client): |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame^] | 6 | """Test checks that system time is the same across all nodes""" |
| 7 | |
| 8 | active_nodes = utils.get_active_nodes() |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 9 | config = utils.get_configuration() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 10 | nodes_time = local_salt_client.cmd( |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 11 | utils.list_to_target_string(active_nodes, 'or'), |
| 12 | 'cmd.run', |
| 13 | ['date +%s'], |
| 14 | expr_form='compound') |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame^] | 15 | result = {} |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 16 | for node, time in nodes_time.iteritems(): |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame^] | 17 | if node in config.get("ntp_skipped_nodes"): |
| 18 | continue |
| 19 | if time in result: |
| 20 | result[time].append(node) |
| 21 | result[time].sort() |
| 22 | else: |
| 23 | result[time] = [node] |
| 24 | assert len(result) <= 1, 'Not all nodes have the same time:\n {}'.format( |
| 25 | json.dumps(result, indent=4)) |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def test_ntp_peers_state(local_salt_client): |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame^] | 29 | """Test gets ntpq peers state and checks the system peer is declared""" |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 30 | |
Oleksii Zhurba | 9b74486 | 2019-02-26 17:33:43 -0600 | [diff] [blame^] | 31 | active_nodes = utils.get_active_nodes() |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 32 | state = local_salt_client.cmd( |
| 33 | utils.list_to_target_string(active_nodes, 'or'), |
| 34 | 'cmd.run', |
| 35 | ['ntpq -pn'], |
| 36 | expr_form='compound') |
| 37 | final_result = {} |
| 38 | for node in state: |
| 39 | sys_peer_declared = False |
| 40 | ntpq_output = state[node].split('\n') |
| 41 | # if output has no 'remote' in the head of ntpq output |
| 42 | # the 'ntqp -np' command failed and cannot check peers |
| 43 | if 'remote' not in ntpq_output[0]: |
| 44 | final_result[node] = ntpq_output |
| 45 | continue |
| 46 | |
| 47 | # take 3rd+ line of output (the actual peers) |
| 48 | try: |
| 49 | peers = ntpq_output[2:] |
| 50 | except IndexError: |
| 51 | final_result[node] = ntpq_output |
| 52 | continue |
| 53 | for p in peers: |
| 54 | if p.split()[0].startswith("*"): |
| 55 | sys_peer_declared = True |
| 56 | if not sys_peer_declared: |
| 57 | final_result[node] = ntpq_output |
| 58 | assert not final_result,\ |
| 59 | "NTP peers state is not expected on some nodes, could not find " \ |
| 60 | "declared system peer:\n{}".format(json.dumps(final_result, indent=4)) |