| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2017, Toshio Kuratomi <[email protected]> |
| 3 |
# Copyright: Contributors to the Ansible project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import sys |
| 9 |
import time |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from ansible.module_utils.facts import timeout |
| 14 |
|
| 15 |
|
| 16 |
@pytest.fixture |
| 17 |
def set_gather_timeout_higher(): |
| 18 |
default_timeout = timeout.GATHER_TIMEOUT |
| 19 |
timeout.GATHER_TIMEOUT = 5 |
| 20 |
yield |
| 21 |
timeout.GATHER_TIMEOUT = default_timeout |
| 22 |
|
| 23 |
|
| 24 |
@pytest.fixture |
| 25 |
def set_gather_timeout_lower(): |
| 26 |
default_timeout = timeout.GATHER_TIMEOUT |
| 27 |
timeout.GATHER_TIMEOUT = 2 |
| 28 |
yield |
| 29 |
timeout.GATHER_TIMEOUT = default_timeout |
| 30 |
|
| 31 |
|
| 32 |
@timeout.timeout |
| 33 |
def sleep_amount_implicit(amount): |
| 34 |
# implicit refers to the lack of argument to the decorator |
| 35 |
time.sleep(amount) |
| 36 |
return 'Succeeded after {0} sec'.format(amount) |
| 37 |
|
| 38 |
|
| 39 |
@timeout.timeout(timeout.DEFAULT_GATHER_TIMEOUT + 5) |
| 40 |
def sleep_amount_explicit_higher(amount): |
| 41 |
# explicit refers to the argument to the decorator |
| 42 |
time.sleep(amount) |
| 43 |
return 'Succeeded after {0} sec'.format(amount) |
| 44 |
|
| 45 |
|
| 46 |
@timeout.timeout(2) |
| 47 |
def sleep_amount_explicit_lower(amount): |
| 48 |
# explicit refers to the argument to the decorator |
| 49 |
time.sleep(amount) |
| 50 |
return 'Succeeded after {0} sec'.format(amount) |
| 51 |
|
| 52 |
|
| 53 |
# |
| 54 |
# Tests for how the timeout decorator is specified |
| 55 |
# |
| 56 |
|
| 57 |
def test_defaults_still_within_bounds(): |
| 58 |
# If the default changes outside of these bounds, some of the tests will |
| 59 |
# no longer test the right thing. Need to review and update the timeouts |
| 60 |
# in the other tests if this fails |
| 61 |
assert timeout.DEFAULT_GATHER_TIMEOUT >= 4 |
| 62 |
|
| 63 |
|
| 64 |
def test_implicit_file_default_succeeds(): |
| 65 |
# amount checked must be less than DEFAULT_GATHER_TIMEOUT |
| 66 |
assert sleep_amount_implicit(1) == 'Succeeded after 1 sec' |
| 67 |
|
| 68 |
|
| 69 |
def test_implicit_file_default_timesout(monkeypatch): |
| 70 |
monkeypatch.setattr(timeout, 'DEFAULT_GATHER_TIMEOUT', 1) |
| 71 |
# sleep_time is greater than the default |
| 72 |
sleep_time = timeout.DEFAULT_GATHER_TIMEOUT + 1 |
| 73 |
with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"): |
| 74 |
sleep_amount_implicit(sleep_time) |
| 75 |
|
| 76 |
|
| 77 |
def test_implicit_file_overridden_succeeds(set_gather_timeout_higher): |
| 78 |
# Set sleep_time greater than the default timeout and less than our new timeout |
| 79 |
sleep_time = 3 |
| 80 |
assert sleep_amount_implicit(sleep_time) == 'Succeeded after {0} sec'.format(sleep_time) |
| 81 |
|
| 82 |
|
| 83 |
def test_implicit_file_overridden_timesout(set_gather_timeout_lower): |
| 84 |
# Set sleep_time greater than our new timeout but less than the default |
| 85 |
sleep_time = 3 |
| 86 |
with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"): |
| 87 |
sleep_amount_implicit(sleep_time) |
| 88 |
|
| 89 |
|
| 90 |
def test_explicit_succeeds(monkeypatch): |
| 91 |
monkeypatch.setattr(timeout, 'DEFAULT_GATHER_TIMEOUT', 1) |
| 92 |
# Set sleep_time greater than the default timeout and less than our new timeout |
| 93 |
sleep_time = 2 |
| 94 |
assert sleep_amount_explicit_higher(sleep_time) == 'Succeeded after {0} sec'.format(sleep_time) |
| 95 |
|
| 96 |
|
| 97 |
def test_explicit_timeout(): |
| 98 |
# Set sleep_time greater than our new timeout but less than the default |
| 99 |
sleep_time = 3 |
| 100 |
with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"): |
| 101 |
sleep_amount_explicit_lower(sleep_time) |
| 102 |
|
| 103 |
|
| 104 |
# |
| 105 |
# Test that exception handling works |
| 106 |
# |
| 107 |
|
| 108 |
@timeout.timeout(1) |
| 109 |
def function_times_out(): |
| 110 |
time.sleep(2) |
| 111 |
|
| 112 |
|
| 113 |
# This is just about the same test as function_times_out but uses a separate process which is where |
| 114 |
# we normally have our timeouts. It's more of an integration test than a unit test. |
| 115 |
@timeout.timeout(1) |
| 116 |
def function_times_out_in_run_command(am): |
| 117 |
am.run_command([sys.executable, '-c', 'import time ; time.sleep(2)']) |
| 118 |
|
| 119 |
|
| 120 |
@timeout.timeout(1) |
| 121 |
def function_other_timeout(): |
| 122 |
raise TimeoutError('Vanilla Timeout') |
| 123 |
|
| 124 |
|
| 125 |
@timeout.timeout(1) |
| 126 |
def function_raises(): |
| 127 |
return 1 / 0 |
| 128 |
|
| 129 |
|
| 130 |
@timeout.timeout(1) |
| 131 |
def function_catches_all_exceptions(): |
| 132 |
try: |
| 133 |
time.sleep(10) |
| 134 |
except BaseException: |
| 135 |
raise RuntimeError('We should not have gotten here') |
| 136 |
|
| 137 |
|
| 138 |
def test_timeout_raises_timeout(): |
| 139 |
with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"): |
| 140 |
function_times_out() |
| 141 |
|
| 142 |
|
| 143 |
@pytest.mark.parametrize('stdin', ({},), indirect=['stdin']) |
| 144 |
def test_timeout_raises_timeout_integration_test(am): |
| 145 |
with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"): |
| 146 |
function_times_out_in_run_command(am) |
| 147 |
|
| 148 |
|
| 149 |
def test_timeout_raises_other_exception(): |
| 150 |
with pytest.raises(ZeroDivisionError, match=r"^division by"): |
| 151 |
function_raises() |
| 152 |
|
| 153 |
|
| 154 |
def test_exception_not_caught_by_called_code(): |
| 155 |
with pytest.raises(timeout.TimeoutError, match=r"^Timer expired after"): |
| 156 |
function_catches_all_exceptions() |