Speed up units using time.sleep by mocking or shortening sleep time (#61064)

* Speed up units using time.sleep by mocking or shortening sleep time

* Only use durations when not a collection
This commit is contained in:
Matt Martz 2019-08-21 16:52:58 -05:00 committed by GitHub
parent c1547f10d6
commit adb886e4ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 5 deletions

View file

@ -101,6 +101,9 @@ def command_units(args):
'--junit-xml', os.path.join(data_context().results, 'junit', 'python%s-units.xml' % version),
]
if not data_context().content.collection:
cmd.append('--durations=25')
if version != '2.6':
# added in pytest 4.5.0, which requires python 2.7+
cmd.append('--strict-markers')

View file

@ -31,7 +31,7 @@ from ansible.module_utils.facts import timeout
@pytest.fixture
def set_gather_timeout_higher():
default_timeout = timeout.GATHER_TIMEOUT
timeout.GATHER_TIMEOUT = timeout.DEFAULT_GATHER_TIMEOUT + 5
timeout.GATHER_TIMEOUT = 5
yield
timeout.GATHER_TIMEOUT = default_timeout
@ -81,7 +81,8 @@ def test_implicit_file_default_succeeds():
assert sleep_amount_implicit(1) == 'Succeeded after 1 sec'
def test_implicit_file_default_timesout():
def test_implicit_file_default_timesout(monkeypatch):
monkeypatch.setattr(timeout, 'DEFAULT_GATHER_TIMEOUT', 1)
# sleep_time is greater than the default
sleep_time = timeout.DEFAULT_GATHER_TIMEOUT + 1
with pytest.raises(timeout.TimeoutError):
@ -90,7 +91,7 @@ def test_implicit_file_default_timesout():
def test_implicit_file_overridden_succeeds(set_gather_timeout_higher):
# Set sleep_time greater than the default timeout and less than our new timeout
sleep_time = timeout.DEFAULT_GATHER_TIMEOUT + 1
sleep_time = 3
assert sleep_amount_implicit(sleep_time) == 'Succeeded after {0} sec'.format(sleep_time)
@ -101,9 +102,10 @@ def test_implicit_file_overridden_timesout(set_gather_timeout_lower):
assert sleep_amount_implicit(sleep_time) == '(Not expected to Succeed)'
def test_explicit_succeeds():
def test_explicit_succeeds(monkeypatch):
monkeypatch.setattr(timeout, 'DEFAULT_GATHER_TIMEOUT', 1)
# Set sleep_time greater than the default timeout and less than our new timeout
sleep_time = timeout.DEFAULT_GATHER_TIMEOUT + 1
sleep_time = 2
assert sleep_amount_explicit_higher(sleep_time) == 'Succeeded after {0} sec'.format(sleep_time)

View file

@ -64,9 +64,13 @@ class TestCnosModule(unittest.TestCase):
self.test_log = tempfile.mkstemp(prefix='ansible-test-cnos-module-', suffix='.log')[1]
self.mock_sleep = patch('time.sleep')
self.mock_sleep.start()
def tearDown(self):
super(TestCnosModule, self).tearDown()
self.mock_sleep.stop()
os.remove(self.test_log)
def execute_module(self, failed=False, changed=False, commands=None,

View file

@ -59,6 +59,13 @@ class AnsibleFailJson(Exception):
class TestEnosModule(unittest.TestCase):
def setUp(self):
self.mock_sleep = patch('time.sleep')
self.mock_sleep.start()
def tearDown(self):
self.mock_sleep.stop()
def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False):

View file

@ -40,5 +40,8 @@ class ModuleTestCase(unittest.TestCase):
def setUp(self):
self.mock_module = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
self.mock_module.start()
self.mock_sleep = patch('time.sleep')
self.mock_sleep.start()
set_module_args({})
self.addCleanup(self.mock_module.stop)
self.addCleanup(self.mock_sleep.stop)