Skip to content

Commit

Permalink
Merge pull request #54916 from Oloremo/added_http_request_interval
Browse files Browse the repository at this point in the history
Added request_interval feature to wait_for_successful_query module
  • Loading branch information
dwoz authored Dec 23, 2019
2 parents 9c4def8 + b888db0 commit 303e038
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
5 changes: 4 additions & 1 deletion salt/modules/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def wait_for_successful_query(url, wait_for=300, **kwargs):
.. code-block:: bash
salt '*' http.wait_for_successful_query http://somelink.com/ wait_for=160
salt '*' http.wait_for_successful_query http://somelink.com/ wait_for=160 request_interval=1
'''

starttime = time.time()
Expand All @@ -83,6 +83,9 @@ def wait_for_successful_query(url, wait_for=300, **kwargs):
raise caught_exception # pylint: disable=E0702

return result
elif 'request_interval' in kwargs:
# Space requests out by delaying for an interval
time.sleep(kwargs['request_interval'])


def update_ca_bundle(target=None, source=None, merge_files=None):
Expand Down
7 changes: 3 additions & 4 deletions salt/states/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ def wait_for_successful_query(name, wait_for=300, **kwargs):
# workaround pylint bug https://www.logilab.org/ticket/3207
raise caught_exception # pylint: disable=E0702
return ret
else:
elif 'request_interval' in kwargs:
# Space requests out by delaying for an interval
if 'request_interval' in kwargs:
log.debug('delaying query for %s seconds.', kwargs['request_interval'])
time.sleep(kwargs['request_interval'])
log.debug('delaying query for %s seconds.', kwargs['request_interval'])
time.sleep(kwargs['request_interval'])
25 changes: 25 additions & 0 deletions tests/unit/modules/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase
from tests.support.mock import (
MagicMock,
patch,
)

Expand All @@ -31,3 +32,27 @@ def test_query(self):
'''
with patch.object(salt.utils.http, 'query', return_value='A'):
self.assertEqual(http.query('url'), 'A')

def test_wait_for_with_interval(self):
'''
Test for wait_for_successful_query waits for request_interval
'''

query_mock = MagicMock(side_effect=[{'error': 'error'}, {}])

with patch.object(salt.utils.http, 'query', query_mock):
with patch('time.sleep', MagicMock()) as sleep_mock:
self.assertEqual(http.wait_for_successful_query('url', request_interval=1), {})
sleep_mock.assert_called_once_with(1)

def test_wait_for_without_interval(self):
'''
Test for wait_for_successful_query waits for request_interval
'''

query_mock = MagicMock(side_effect=[{'error': 'error'}, {}])

with patch.object(salt.utils.http, 'query', query_mock):
with patch('time.sleep', MagicMock()) as sleep_mock:
self.assertEqual(http.wait_for_successful_query('url'), {})
sleep_mock.assert_not_called()
25 changes: 25 additions & 0 deletions tests/unit/states/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,28 @@ def test_query(self):
with patch.dict(http.__salt__, {'http.query': mock}):
self.assertDictEqual(http.query("salt", "Dude", "stack"),
ret[1])

def test_wait_for_with_interval(self):
'''
Test for wait_for_successful_query waits for request_interval
'''

query_mock = MagicMock(side_effect=[{'error': 'error'}, {'result': True}])

with patch.object(http, 'query', query_mock):
with patch('time.sleep', MagicMock()) as sleep_mock:
self.assertEqual(http.wait_for_successful_query('url', request_interval=1, status=200),
{'result': True})
sleep_mock.assert_called_once_with(1)

def test_wait_for_without_interval(self):
'''
Test for wait_for_successful_query waits for request_interval
'''

query_mock = MagicMock(side_effect=[{'error': 'error'}, {'result': True}])

with patch.object(http, 'query', query_mock):
with patch('time.sleep', MagicMock()) as sleep_mock:
self.assertEqual(http.wait_for_successful_query('url', status=200), {'result': True})
sleep_mock.assert_not_called()

0 comments on commit 303e038

Please sign in to comment.