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:
# The timeout has been reached, check the jid to see if the
# timeout needs to be increased
jinfo = self.gather_job_info(jid, tgt, tgt_type, minions - found, **kwargs)
still_running = [id_ for id_, jdat in jinfo.iteritems()
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()
)
)
if timeout > 0:
last_time = True
continue
else:
last_time = True
log.debug('jid {0} not running on any minions last time'.format(jid))
continue
jinfo = self.gather_job_info(jid, tgt, tgt_type, minions - found, **kwargs)
more_time = [id_ for id_, jdat in jinfo.iteritems()
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)
def get_returns(
@ -1090,17 +1094,37 @@ class LocalClient(object):
# All minions have returned, break out of the loop
break
if int(time.time()) > timeout_at:
if verbose or show_timeout:
if self.opts.get('minion_data_cache', False) \
or tgt_type in ('glob', 'pcre', 'list'):
if len(found) < len(minions):
fail = sorted(list(minions.difference(found)))
for minion in fail:
ret[minion] = {
'out': 'no_return',
'ret': 'Minion did not return'
}
break
if timeout > 0:
if verbose or show_timeout:
if self.opts.get('minion_data_cache', False) \
or tgt_type in ('glob', 'pcre', 'list'):
if len(found) < len(minions):
fail = sorted(list(minions.difference(found)))
for minion in fail:
ret[minion] = {
'out': 'no_return',
'ret': 'Minion did not return'
}
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)
return ret
@ -1233,11 +1257,10 @@ class LocalClient(object):
if 'retcode' in cache_jinfo[id_]:
ret[id_]['retcode'] = cache_jinfo[id_]['retcode']
yield ret
if more_time:
last_time = True
else:
timeout_at = time.time() + timeout
continue
else:
last_time = True
time.sleep(0.01)
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:
self.assertTrue(ret['minion'])
def test_cli_timeout(self):
'''
Test cli timeouts. A timeout > 0 should timeout, and a timeout of 0 means
wait until all returns complete
'''
# verify that timeouts work
# Test timeouts, a timeout of > 0 should timeout
cmd_iter = self.client.cmd_cli(
'minion',
'test.sleep',
@ -39,7 +34,7 @@ class StdTest(integration.ModuleCase):
self.assertRaises(StopIteration,
cmd_iter.next)
# verify that timeout of 0 waits
# A timeout of 0 means wait until done
cmd_iter = self.client.cmd_cli(
'minion',
'test.sleep',
@ -63,6 +58,29 @@ class StdTest(integration.ModuleCase):
for ret in cmd_iter:
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):
'''
test cmd_iter_no_block
@ -76,6 +94,35 @@ class StdTest(integration.ModuleCase):
continue
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):
'''
test cmd_iter
@ -108,6 +155,24 @@ class StdTest(integration.ModuleCase):
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__':
from integration import run_tests
run_tests(StdTest)