test_http.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import pytest
  8. # Import Salt Libs
  9. import salt.states.http as 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. Validate the HTTP state
  17. """
  18. def setup_loader_modules(self):
  19. return {http: {}}
  20. def test_query(self):
  21. """
  22. Test to perform an HTTP query and statefully return the result
  23. """
  24. ret = [
  25. {
  26. "changes": {},
  27. "comment": " Either match text (match) or a "
  28. "status code (status) is required.",
  29. "data": {},
  30. "name": "salt",
  31. "result": False,
  32. },
  33. {
  34. "changes": {},
  35. "comment": " (TEST MODE)",
  36. "data": True,
  37. "name": "salt",
  38. "result": None,
  39. },
  40. ]
  41. self.assertDictEqual(http.query("salt"), ret[0])
  42. with patch.dict(http.__opts__, {"test": True}):
  43. mock = MagicMock(return_value=True)
  44. with patch.dict(http.__salt__, {"http.query": mock}):
  45. self.assertDictEqual(http.query("salt", "Dude", "stack"), ret[1])
  46. def test_query_pcre_statustype(self):
  47. """
  48. Test to perform an HTTP query with a regex used to match the status code and statefully return the result
  49. """
  50. testurl = "salturl"
  51. http_result = {"text": "This page returned a 201 status code", "status": "201"}
  52. state_return = {
  53. "changes": {},
  54. "comment": 'Match text "This page returned" was found. Status pattern "200|201" was found.',
  55. "data": {"status": "201", "text": "This page returned a 201 status code"},
  56. "name": testurl,
  57. "result": True,
  58. }
  59. with patch.dict(http.__opts__, {"test": False}):
  60. mock = MagicMock(return_value=http_result)
  61. with patch.dict(http.__salt__, {"http.query": mock}):
  62. self.assertDictEqual(
  63. http.query(
  64. testurl,
  65. match="This page returned",
  66. status="200|201",
  67. status_type="pcre",
  68. ),
  69. state_return,
  70. )
  71. def test_query_stringstatustype(self):
  72. """
  73. Test to perform an HTTP query with a string status code and statefully return the result
  74. """
  75. testurl = "salturl"
  76. http_result = {"text": "This page returned a 201 status code", "status": "201"}
  77. state_return = {
  78. "changes": {},
  79. "comment": 'Match text "This page returned" was found. Status 201 was found.',
  80. "data": {"status": "201", "text": "This page returned a 201 status code"},
  81. "name": testurl,
  82. "result": True,
  83. }
  84. with patch.dict(http.__opts__, {"test": False}):
  85. mock = MagicMock(return_value=http_result)
  86. with patch.dict(http.__salt__, {"http.query": mock}):
  87. self.assertDictEqual(
  88. http.query(
  89. testurl,
  90. match="This page returned",
  91. status="201",
  92. status_type="string",
  93. ),
  94. state_return,
  95. )
  96. def test_query_liststatustype(self):
  97. """
  98. Test to perform an HTTP query with a list of status codes and statefully return the result
  99. """
  100. testurl = "salturl"
  101. http_result = {"text": "This page returned a 201 status code", "status": "201"}
  102. state_return = {
  103. "changes": {},
  104. "comment": 'Match text "This page returned" was found. Status 201 was found.',
  105. "data": {"status": "201", "text": "This page returned a 201 status code"},
  106. "name": testurl,
  107. "result": True,
  108. }
  109. with patch.dict(http.__opts__, {"test": False}):
  110. mock = MagicMock(return_value=http_result)
  111. with patch.dict(http.__salt__, {"http.query": mock}):
  112. self.assertDictEqual(
  113. http.query(
  114. testurl,
  115. match="This page returned",
  116. status=["200", "201"],
  117. status_type="list",
  118. ),
  119. state_return,
  120. )
  121. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  122. def test_wait_for_with_interval(self):
  123. """
  124. Test for wait_for_successful_query waits for request_interval
  125. """
  126. query_mock = MagicMock(side_effect=[{"error": "error"}, {"result": True}])
  127. with patch.object(http, "query", query_mock):
  128. with patch("time.sleep", MagicMock()) as sleep_mock:
  129. self.assertEqual(
  130. http.wait_for_successful_query(
  131. "url", request_interval=1, status=200
  132. ),
  133. {"result": True},
  134. )
  135. sleep_mock.assert_called_once_with(1)
  136. def test_wait_for_without_interval(self):
  137. """
  138. Test for wait_for_successful_query waits for request_interval
  139. """
  140. query_mock = MagicMock(side_effect=[{"error": "error"}, {"result": True}])
  141. with patch.object(http, "query", query_mock):
  142. with patch("time.sleep", MagicMock()) as sleep_mock:
  143. self.assertEqual(
  144. http.wait_for_successful_query("url", status=200), {"result": True}
  145. )
  146. sleep_mock.assert_not_called()