test_http.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Salt Libs
  8. import salt.states.http as http
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase
  12. from tests.support.mock import (
  13. MagicMock,
  14. patch
  15. )
  16. class HttpTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Validate the HTTP state
  19. '''
  20. def setup_loader_modules(self):
  21. return {http: {}}
  22. def test_query(self):
  23. '''
  24. Test to perform an HTTP query and statefully return the result
  25. '''
  26. ret = [{'changes': {},
  27. 'comment': ' Either match text (match) or a '
  28. 'status code (status) is required.', 'data': {},
  29. 'name': 'salt', 'result': False},
  30. {'changes': {}, 'comment': ' (TEST MODE)', 'data': True, 'name': 'salt',
  31. 'result': None}]
  32. self.assertDictEqual(http.query("salt"), ret[0])
  33. with patch.dict(http.__opts__, {'test': True}):
  34. mock = MagicMock(return_value=True)
  35. with patch.dict(http.__salt__, {'http.query': mock}):
  36. self.assertDictEqual(http.query("salt", "Dude", "stack"),
  37. ret[1])
  38. def test_query_pcre_statustype(self):
  39. '''
  40. Test to perform an HTTP query with a regex used to match the status code and statefully return the result
  41. '''
  42. testurl = "salturl"
  43. http_result = {
  44. "text": "This page returned a 201 status code",
  45. "status": "201"
  46. }
  47. state_return = {'changes': {},
  48. 'comment': 'Match text "This page returned" was found. Status pattern "200|201" was found.',
  49. 'data': {'status': '201', 'text': 'This page returned a 201 status code'},
  50. 'name': testurl,
  51. 'result': True}
  52. with patch.dict(http.__opts__, {'test': False}):
  53. mock = MagicMock(return_value=http_result)
  54. with patch.dict(http.__salt__, {'http.query': mock}):
  55. self.assertDictEqual(http.query(testurl,
  56. match="This page returned",
  57. status="200|201",
  58. status_type='pcre'
  59. ), state_return)
  60. def test_query_stringstatustype(self):
  61. '''
  62. Test to perform an HTTP query with a string status code and statefully return the result
  63. '''
  64. testurl = "salturl"
  65. http_result = {
  66. "text": "This page returned a 201 status code",
  67. "status": "201"
  68. }
  69. state_return = {'changes': {},
  70. 'comment': 'Match text "This page returned" was found. Status 201 was found.',
  71. 'data': {'status': '201', 'text': 'This page returned a 201 status code'},
  72. 'name': testurl,
  73. 'result': True}
  74. with patch.dict(http.__opts__, {'test': False}):
  75. mock = MagicMock(return_value=http_result)
  76. with patch.dict(http.__salt__, {'http.query': mock}):
  77. self.assertDictEqual(http.query(testurl,
  78. match="This page returned",
  79. status="201",
  80. status_type='string'
  81. ), state_return)
  82. def test_query_liststatustype(self):
  83. '''
  84. Test to perform an HTTP query with a list of status codes and statefully return the result
  85. '''
  86. testurl = "salturl"
  87. http_result = {
  88. "text": "This page returned a 201 status code",
  89. "status": "201"
  90. }
  91. state_return = {'changes': {},
  92. 'comment': 'Match text "This page returned" was found. Status 201 was found.',
  93. 'data': {'status': '201', 'text': 'This page returned a 201 status code'},
  94. 'name': testurl,
  95. 'result': True}
  96. with patch.dict(http.__opts__, {'test': False}):
  97. mock = MagicMock(return_value=http_result)
  98. with patch.dict(http.__salt__, {'http.query': mock}):
  99. self.assertDictEqual(http.query(testurl,
  100. match="This page returned",
  101. status=["200", "201"],
  102. status_type='list'
  103. ), state_return)
  104. def test_wait_for_with_interval(self):
  105. '''
  106. Test for wait_for_successful_query waits for request_interval
  107. '''
  108. query_mock = MagicMock(side_effect=[{'error': 'error'}, {'result': True}])
  109. with patch.object(http, 'query', query_mock):
  110. with patch('time.sleep', MagicMock()) as sleep_mock:
  111. self.assertEqual(http.wait_for_successful_query('url', request_interval=1, status=200),
  112. {'result': True})
  113. sleep_mock.assert_called_once_with(1)
  114. def test_wait_for_without_interval(self):
  115. '''
  116. Test for wait_for_successful_query waits for request_interval
  117. '''
  118. query_mock = MagicMock(side_effect=[{'error': 'error'}, {'result': True}])
  119. with patch.object(http, 'query', query_mock):
  120. with patch('time.sleep', MagicMock()) as sleep_mock:
  121. self.assertEqual(http.wait_for_successful_query('url', status=200), {'result': True})
  122. sleep_mock.assert_not_called()