test_http.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.http as http
  9. import salt.utils.http
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. class HttpTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.modules.http
  17. """
  18. def setup_loader_modules(self):
  19. return {http: {}}
  20. def test_query(self):
  21. """
  22. Test for Query a resource, and decode the return data
  23. """
  24. with patch.object(salt.utils.http, "query", return_value="A"):
  25. self.assertEqual(http.query("url"), "A")
  26. def test_wait_for_with_interval(self):
  27. """
  28. Test for wait_for_successful_query waits for request_interval
  29. """
  30. query_mock = MagicMock(side_effect=[{"error": "error"}, {}])
  31. with patch.object(salt.utils.http, "query", query_mock):
  32. with patch("time.sleep", MagicMock()) as sleep_mock:
  33. self.assertEqual(
  34. http.wait_for_successful_query("url", request_interval=1), {}
  35. )
  36. sleep_mock.assert_called_once_with(1)
  37. def test_wait_for_without_interval(self):
  38. """
  39. Test for wait_for_successful_query waits for request_interval
  40. """
  41. query_mock = MagicMock(side_effect=[{"error": "error"}, {}])
  42. with patch.object(salt.utils.http, "query", query_mock):
  43. with patch("time.sleep", MagicMock()) as sleep_mock:
  44. self.assertEqual(http.wait_for_successful_query("url"), {})
  45. sleep_mock.assert_not_called()