1
0

test_tomcat.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. # Import future libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import 3rd-party libs
  5. from io import BytesIO, StringIO
  6. # Import salt module
  7. import salt.modules.tomcat as tomcat
  8. from salt.ext.six import string_types
  9. from salt.ext.six.moves.urllib.request import (
  10. HTTPBasicAuthHandler as _HTTPBasicAuthHandler,
  11. )
  12. from salt.ext.six.moves.urllib.request import (
  13. HTTPDigestAuthHandler as _HTTPDigestAuthHandler,
  14. )
  15. from salt.ext.six.moves.urllib.request import build_opener as _build_opener
  16. # Import Salt Testing libs
  17. from tests.support.mixins import LoaderModuleMockMixin
  18. from tests.support.mock import MagicMock, patch
  19. from tests.support.unit import TestCase
  20. class TomcatTestCasse(TestCase, LoaderModuleMockMixin):
  21. """
  22. Tests cases for salt.modules.tomcat
  23. """
  24. def setup_loader_modules(self):
  25. return {tomcat: {}}
  26. def test_tomcat_wget_no_bytestring(self):
  27. responses = {
  28. "string": StringIO("Best response ever\r\nAnd you know it!"),
  29. "bytes": BytesIO(b"Best response ever\r\nAnd you know it!"),
  30. }
  31. string_mock = MagicMock(return_value=responses["string"])
  32. bytes_mock = MagicMock(return_value=responses["bytes"])
  33. with patch(
  34. "salt.modules.tomcat._auth",
  35. MagicMock(
  36. return_value=_build_opener(
  37. _HTTPBasicAuthHandler(), _HTTPDigestAuthHandler()
  38. )
  39. ),
  40. ):
  41. with patch("salt.modules.tomcat._urlopen", string_mock):
  42. response = tomcat._wget(
  43. "tomcat.wait", url="http://localhost:8080/nofail"
  44. )
  45. for line in response["msg"]:
  46. self.assertIsInstance(line, string_types)
  47. with patch("salt.modules.tomcat._urlopen", bytes_mock):
  48. try:
  49. response = tomcat._wget(
  50. "tomcat.wait", url="http://localhost:8080/nofail"
  51. )
  52. except TypeError as type_error:
  53. if (
  54. type_error.args[0]
  55. == "startswith first arg must be bytes or a tuple of bytes, not str"
  56. ):
  57. self.fail("Got back a byte string, should've been a string")
  58. else:
  59. raise type_error
  60. for line in response["msg"]:
  61. self.assertIsInstance(line, string_types)