test_parser.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.protocols.haproxy._parser}.
  5. """
  6. from twisted.trial.unittest import SynchronousTestCase as TestCase
  7. from twisted.test.proto_helpers import MemoryReactor
  8. from twisted.internet.endpoints import (
  9. _WrapperServerEndpoint, TCP4ServerEndpoint, TCP6ServerEndpoint,
  10. UNIXServerEndpoint, serverFromString, _parse as parseEndpoint
  11. )
  12. from .._wrapper import HAProxyWrappingFactory
  13. from .._parser import unparseEndpoint
  14. class UnparseEndpointTests(TestCase):
  15. """
  16. Tests to ensure that un-parsing an endpoint string round trips through
  17. escaping properly.
  18. """
  19. def check(self, input):
  20. """
  21. Check that the input unparses into the output, raising an assertion
  22. error if it doesn't.
  23. @param input: an input in endpoint-string-description format. (To
  24. ensure determinism, keyword arguments should be in alphabetical
  25. order.)
  26. @type input: native L{str}
  27. """
  28. self.assertEqual(unparseEndpoint(*parseEndpoint(input)), input)
  29. def test_basicUnparse(self):
  30. """
  31. An individual word.
  32. """
  33. self.check("word")
  34. def test_multipleArguments(self):
  35. """
  36. Multiple arguments.
  37. """
  38. self.check("one:two")
  39. def test_keywords(self):
  40. """
  41. Keyword arguments.
  42. """
  43. self.check("aleph=one:bet=two")
  44. def test_colonInArgument(self):
  45. """
  46. Escaped ":".
  47. """
  48. self.check("hello\\:colon\\:world")
  49. def test_colonInKeywordValue(self):
  50. """
  51. Escaped ":" in keyword value.
  52. """
  53. self.check("hello=\\:")
  54. def test_colonInKeywordName(self):
  55. """
  56. Escaped ":" in keyword name.
  57. """
  58. self.check("\\:=hello")
  59. class HAProxyServerParserTests(TestCase):
  60. """
  61. Tests that the parser generates the correct endpoints.
  62. """
  63. def onePrefix(self, description, expectedClass):
  64. """
  65. Test the C{haproxy} enpdoint prefix against one sub-endpoint type.
  66. @param description: A string endpoint description beginning with
  67. C{haproxy}.
  68. @type description: native L{str}
  69. @param expectedClass: the expected sub-endpoint class given the
  70. description.
  71. @type expectedClass: L{type}
  72. @return: the parsed endpoint
  73. @rtype: L{IStreamServerEndpoint}
  74. @raise twisted.trial.unittest.Failtest: if the parsed endpoint doesn't
  75. match expectations.
  76. """
  77. reactor = MemoryReactor()
  78. endpoint = serverFromString(reactor, description)
  79. self.assertIsInstance(endpoint, _WrapperServerEndpoint)
  80. self.assertIsInstance(endpoint._wrappedEndpoint, expectedClass)
  81. self.assertIs(endpoint._wrapperFactory, HAProxyWrappingFactory)
  82. return endpoint
  83. def test_tcp4(self):
  84. """
  85. Test if the parser generates a wrapped TCP4 endpoint.
  86. """
  87. self.onePrefix('haproxy:tcp:8080', TCP4ServerEndpoint)
  88. def test_tcp6(self):
  89. """
  90. Test if the parser generates a wrapped TCP6 endpoint.
  91. """
  92. self.onePrefix('haproxy:tcp6:8080', TCP6ServerEndpoint)
  93. def test_unix(self):
  94. """
  95. Test if the parser generates a wrapped UNIX endpoint.
  96. """
  97. self.onePrefix('haproxy:unix:address=/tmp/socket', UNIXServerEndpoint)