Add tests for timeouts to client tests. In addition this makes all of the client functions have the same functionality

This commit is contained in:
Thomas Jackson 2014-09-18 14:38:13 -07:00
parent c8f15230f5
commit 6b91d539b8
2 changed files with 123 additions and 35 deletions

View File

@ -888,22 +888,26 @@ class LocalClient(object):
if int(time.time()) > timeout_at: if int(time.time()) > timeout_at:
# The timeout has been reached, check the jid to see if the # The timeout has been reached, check the jid to see if the
# timeout needs to be increased # timeout needs to be increased
jinfo = self.gather_job_info(jid, tgt, tgt_type, minions - found, **kwargs) if timeout > 0:
still_running = [id_ for id_, jdat in jinfo.iteritems() last_time = True
if jdat
]
if still_running:
timeout_at = int(time.time()) + timeout
log.debug(
'jid {0} still running on {1} will now timeout at {2}'.format(
jid, still_running, datetime.fromtimestamp(timeout_at).time()
)
)
continue continue
else: else:
last_time = True jinfo = self.gather_job_info(jid, tgt, tgt_type, minions - found, **kwargs)
log.debug('jid {0} not running on any minions last time'.format(jid)) more_time = [id_ for id_, jdat in jinfo.iteritems()
continue if jdat
]
if more_time:
timeout_at = int(time.time()) + timeout
log.debug(
'jid {0} still running on {1} will now timeout at {2}'.format(
jid, more_time, datetime.fromtimestamp(timeout_at).time()
)
)
continue
else:
last_time = True
log.debug('jid {0} not running on any minions last time'.format(jid))
continue
time.sleep(0.01) time.sleep(0.01)
def get_returns( def get_returns(
@ -1090,17 +1094,37 @@ class LocalClient(object):
# All minions have returned, break out of the loop # All minions have returned, break out of the loop
break break
if int(time.time()) > timeout_at: if int(time.time()) > timeout_at:
if verbose or show_timeout: if timeout > 0:
if self.opts.get('minion_data_cache', False) \ if verbose or show_timeout:
or tgt_type in ('glob', 'pcre', 'list'): if self.opts.get('minion_data_cache', False) \
if len(found) < len(minions): or tgt_type in ('glob', 'pcre', 'list'):
fail = sorted(list(minions.difference(found))) if len(found) < len(minions):
for minion in fail: fail = sorted(list(minions.difference(found)))
ret[minion] = { for minion in fail:
'out': 'no_return', ret[minion] = {
'ret': 'Minion did not return' 'out': 'no_return',
} 'ret': 'Minion did not return'
break }
break
else:
jinfo = self.gather_job_info(jid,
tgt,
tgt_type,
minions - found,
)
more_time = False
for id_ in jinfo:
if jinfo[id_]:
if verbose:
print(
'Execution is still running on {0}'.format(id_)
)
more_time = True
if not more_time:
break
else:
timeout_at = time.time() + timeout
continue
time.sleep(0.01) time.sleep(0.01)
return ret return ret
@ -1233,11 +1257,10 @@ class LocalClient(object):
if 'retcode' in cache_jinfo[id_]: if 'retcode' in cache_jinfo[id_]:
ret[id_]['retcode'] = cache_jinfo[id_]['retcode'] ret[id_]['retcode'] = cache_jinfo[id_]['retcode']
yield ret yield ret
if more_time: last_time = True
else:
timeout_at = time.time() + timeout timeout_at = time.time() + timeout
continue continue
else:
last_time = True
time.sleep(0.01) time.sleep(0.01)
def get_event_iter_returns(self, jid, minions, timeout=None): def get_event_iter_returns(self, jid, minions, timeout=None):

View File

@ -24,12 +24,7 @@ class StdTest(integration.ModuleCase):
for ret in cmd_iter: for ret in cmd_iter:
self.assertTrue(ret['minion']) self.assertTrue(ret['minion'])
def test_cli_timeout(self): # Test timeouts, a timeout of > 0 should timeout
'''
Test cli timeouts. A timeout > 0 should timeout, and a timeout of 0 means
wait until all returns complete
'''
# verify that timeouts work
cmd_iter = self.client.cmd_cli( cmd_iter = self.client.cmd_cli(
'minion', 'minion',
'test.sleep', 'test.sleep',
@ -39,7 +34,7 @@ class StdTest(integration.ModuleCase):
self.assertRaises(StopIteration, self.assertRaises(StopIteration,
cmd_iter.next) cmd_iter.next)
# verify that timeout of 0 waits # A timeout of 0 means wait until done
cmd_iter = self.client.cmd_cli( cmd_iter = self.client.cmd_cli(
'minion', 'minion',
'test.sleep', 'test.sleep',
@ -63,6 +58,29 @@ class StdTest(integration.ModuleCase):
for ret in cmd_iter: for ret in cmd_iter:
self.assertTrue(ret['minion']) self.assertTrue(ret['minion'])
# Test timeouts, a timeout of > 0 should timeout
cmd_iter = self.client.cmd_iter(
'minion',
'test.sleep',
arg=[5],
timeout=2
)
self.assertRaises(StopIteration,
cmd_iter.next)
# A timeout of 0 means wait until done
cmd_iter = self.client.cmd_iter(
'minion',
'test.sleep',
arg=[5],
timeout=0
)
num_ret = 0
for ret in cmd_iter:
num_ret += 1
self.assertTrue(ret['minion'])
assert num_ret > 0
def test_iter_no_block(self): def test_iter_no_block(self):
''' '''
test cmd_iter_no_block test cmd_iter_no_block
@ -76,6 +94,35 @@ class StdTest(integration.ModuleCase):
continue continue
self.assertTrue(ret['minion']) self.assertTrue(ret['minion'])
# Test timeouts, a timeout of > 0 should timeout
cmd_iter = self.client.cmd_iter_no_block(
'minion',
'test.sleep',
arg=[5],
timeout=2
)
num_ret = 0
for ret in cmd_iter:
if ret is None:
continue
num_ret += 1
assert num_ret == 0
# A timeout of 0 means wait until done
cmd_iter = self.client.cmd_iter_no_block(
'minion',
'test.sleep',
arg=[5],
timeout=0
)
num_ret = 0
for ret in cmd_iter:
if ret is None:
continue
num_ret += 1
self.assertTrue(ret['minion'])
assert num_ret > 0
def test_full_returns(self): def test_full_returns(self):
''' '''
test cmd_iter test cmd_iter
@ -108,6 +155,24 @@ class StdTest(integration.ModuleCase):
ret['minion'] ret['minion']
) )
# Test timeouts, a timeout of > 0 should timeout
ret = self.client.cmd_full_return(
'minion',
'test.sleep',
arg=[5],
timeout=2
)
assert len(ret) == 0
# A timeout of 0 means wait until done
ret = self.client.cmd_full_return(
'minion',
'test.sleep',
arg=[5],
timeout=0
)
assert len(ret) > 0
if __name__ == '__main__': if __name__ == '__main__':
from integration import run_tests from integration import run_tests
run_tests(StdTest) run_tests(StdTest)