Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 1 | import json |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 2 | import os |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 3 | |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 4 | import utils |
| 5 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 6 | |
Oleksii Zhurba | 17a8848 | 2017-10-06 14:29:05 -0500 | [diff] [blame] | 7 | def test_ntp_sync(local_salt_client): |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 8 | testname = os.path.basename(__file__).split('.')[0] |
| 9 | active_nodes = utils.get_active_nodes(os.path.basename(__file__)) |
| 10 | config = utils.get_configuration() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 11 | fail = {} |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 12 | saltmaster_time = int(local_salt_client.cmd( |
Oleksii Zhurba | 17a8848 | 2017-10-06 14:29:05 -0500 | [diff] [blame] | 13 | 'salt:master', |
| 14 | 'cmd.run', |
| 15 | ['date +%s'], |
| 16 | expr_form='pillar').values()[0]) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 17 | nodes_time = local_salt_client.cmd( |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 18 | utils.list_to_target_string(active_nodes, 'or'), |
| 19 | 'cmd.run', |
| 20 | ['date +%s'], |
| 21 | expr_form='compound') |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 22 | diff = config.get(testname)["time_deviation"] or 30 |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 23 | for node, time in nodes_time.iteritems(): |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 24 | if (int(time) - saltmaster_time) > diff or \ |
| 25 | (int(time) - saltmaster_time) < -diff: |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 26 | fail[node] = time |
| 27 | |
| 28 | assert not fail, 'SaltMaster time: {}\n' \ |
| 29 | 'Nodes with time mismatch:\n {}'.format(saltmaster_time, |
| 30 | fail) |
Ievgeniia Zadorozhna | c48585f | 2019-02-04 19:38:54 +0300 | [diff] [blame] | 31 | |
| 32 | |
| 33 | def test_ntp_peers_state(local_salt_client): |
| 34 | """Test gets ntpq peers state and check the system peer is declared""" |
| 35 | |
| 36 | active_nodes = utils.get_active_nodes(os.path.basename(__file__)) |
| 37 | state = local_salt_client.cmd( |
| 38 | utils.list_to_target_string(active_nodes, 'or'), |
| 39 | 'cmd.run', |
| 40 | ['ntpq -pn'], |
| 41 | expr_form='compound') |
| 42 | final_result = {} |
| 43 | for node in state: |
| 44 | sys_peer_declared = False |
| 45 | 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)) |