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
pull/61083/head
Matt Martz 5 years ago committed by GitHub
parent c1547f10d6
commit adb886e4ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -101,6 +101,9 @@ def command_units(args):
'--junit-xml', os.path.join(data_context().results, 'junit', 'python%s-units.xml' % version), '--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': if version != '2.6':
# added in pytest 4.5.0, which requires python 2.7+ # added in pytest 4.5.0, which requires python 2.7+
cmd.append('--strict-markers') cmd.append('--strict-markers')

@ -31,7 +31,7 @@ from ansible.module_utils.facts import timeout
@pytest.fixture @pytest.fixture
def set_gather_timeout_higher(): def set_gather_timeout_higher():
default_timeout = timeout.GATHER_TIMEOUT default_timeout = timeout.GATHER_TIMEOUT
timeout.GATHER_TIMEOUT = timeout.DEFAULT_GATHER_TIMEOUT + 5 timeout.GATHER_TIMEOUT = 5
yield yield
timeout.GATHER_TIMEOUT = default_timeout timeout.GATHER_TIMEOUT = default_timeout
@ -81,7 +81,8 @@ def test_implicit_file_default_succeeds():
assert sleep_amount_implicit(1) == 'Succeeded after 1 sec' 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 is greater than the default
sleep_time = timeout.DEFAULT_GATHER_TIMEOUT + 1 sleep_time = timeout.DEFAULT_GATHER_TIMEOUT + 1
with pytest.raises(timeout.TimeoutError): 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): def test_implicit_file_overridden_succeeds(set_gather_timeout_higher):
# Set sleep_time greater than the default timeout and less than our new timeout # 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) 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)' 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 # 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) assert sleep_amount_explicit_higher(sleep_time) == 'Succeeded after {0} sec'.format(sleep_time)

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

@ -59,6 +59,13 @@ class AnsibleFailJson(Exception):
class TestEnosModule(unittest.TestCase): 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, def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False): sort=True, defaults=False):

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

Loading…
Cancel
Save