1
0

test_block.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. from nose.tools import assert_equals, assert_true, assert_false
  2. from tests.asserts import assert_is_instance, assert_is_none, assert_is_not_none
  3. from gixy.parser.nginx_parser import NginxParser
  4. from gixy.directives.block import *
  5. # TODO(buglloc): what about include block?
  6. def _get_parsed(config):
  7. root = NginxParser(cwd='', allow_includes=False).parse(config)
  8. return root.children[0]
  9. def test_block():
  10. config = 'some {some;}'
  11. directive = _get_parsed(config)
  12. assert_is_instance(directive, Block)
  13. assert_true(directive.is_block)
  14. assert_true(directive.self_context)
  15. assert_false(directive.provide_variables)
  16. def test_http():
  17. config = '''
  18. http {
  19. default_type application/octet-stream;
  20. sendfile on;
  21. keepalive_timeout 65;
  22. }
  23. '''
  24. directive = _get_parsed(config)
  25. assert_is_instance(directive, HttpBlock)
  26. assert_true(directive.is_block)
  27. assert_true(directive.self_context)
  28. assert_false(directive.provide_variables)
  29. def test_server():
  30. config = '''
  31. server {
  32. listen 80;
  33. server_name _;
  34. server_name cool.io;
  35. }
  36. '''
  37. directive = _get_parsed(config)
  38. assert_is_instance(directive, ServerBlock)
  39. assert_true(directive.is_block)
  40. assert_true(directive.self_context)
  41. assert_equals([d.args[0] for d in directive.get_names()], ['_', 'cool.io'])
  42. assert_false(directive.provide_variables)
  43. def test_location():
  44. config = '''
  45. location / {
  46. }
  47. '''
  48. directive = _get_parsed(config)
  49. assert_is_instance(directive, LocationBlock)
  50. assert_true(directive.is_block)
  51. assert_true(directive.self_context)
  52. assert_true(directive.provide_variables)
  53. assert_is_none(directive.modifier)
  54. assert_equals(directive.path, '/')
  55. assert_false(directive.is_internal)
  56. def test_location_internal():
  57. config = '''
  58. location / {
  59. internal;
  60. }
  61. '''
  62. directive = _get_parsed(config)
  63. assert_is_instance(directive, LocationBlock)
  64. assert_true(directive.is_internal)
  65. def test_location_modifier():
  66. config = '''
  67. location = / {
  68. }
  69. '''
  70. directive = _get_parsed(config)
  71. assert_is_instance(directive, LocationBlock)
  72. assert_equals(directive.modifier, '=')
  73. assert_equals(directive.path, '/')
  74. def test_if():
  75. config = '''
  76. if ($some) {
  77. }
  78. '''
  79. directive = _get_parsed(config)
  80. assert_is_instance(directive, IfBlock)
  81. assert_true(directive.is_block)
  82. assert_false(directive.self_context)
  83. assert_false(directive.provide_variables)
  84. assert_equals(directive.variable, '$some')
  85. assert_is_none(directive.operand)
  86. assert_is_none(directive.value)
  87. def test_if_modifier():
  88. config = '''
  89. if (-f /some) {
  90. }
  91. '''
  92. directive = _get_parsed(config)
  93. assert_is_instance(directive, IfBlock)
  94. assert_equals(directive.operand, '-f')
  95. assert_equals(directive.value, '/some')
  96. assert_is_none(directive.variable)
  97. def test_if_variable():
  98. config = '''
  99. if ($http_some = '/some') {
  100. }
  101. '''
  102. directive = _get_parsed(config)
  103. assert_is_instance(directive, IfBlock)
  104. assert_equals(directive.variable, '$http_some')
  105. assert_equals(directive.operand, '=')
  106. assert_equals(directive.value, '/some')
  107. def test_block_some_flat():
  108. config = '''
  109. some {
  110. default_type application/octet-stream;
  111. sendfile on;
  112. if (-f /some/) {
  113. keepalive_timeout 65;
  114. }
  115. }
  116. '''
  117. directive = _get_parsed(config)
  118. for d in ['default_type', 'sendfile', 'keepalive_timeout']:
  119. c = directive.some(d, flat=True)
  120. assert_is_not_none(c)
  121. assert_equals(c.name, d)
  122. def test_block_some_not_flat():
  123. config = '''
  124. some {
  125. default_type application/octet-stream;
  126. sendfile on;
  127. if (-f /some/) {
  128. keepalive_timeout 65;
  129. }
  130. }
  131. '''
  132. directive = _get_parsed(config)
  133. c = directive.some('keepalive_timeout', flat=False)
  134. assert_is_none(c)
  135. def test_block_find_flat():
  136. config = '''
  137. some {
  138. directive 1;
  139. if (-f /some/) {
  140. directive 2;
  141. }
  142. }
  143. '''
  144. directive = _get_parsed(config)
  145. finds = directive.find('directive', flat=True)
  146. assert_equals(len(finds), 2)
  147. assert_equals([x.name for x in finds], ['directive', 'directive'])
  148. assert_equals([x.args[0] for x in finds], ['1', '2'])
  149. def test_block_find_not_flat():
  150. config = '''
  151. some {
  152. directive 1;
  153. if (-f /some/) {
  154. directive 2;
  155. }
  156. }
  157. '''
  158. directive = _get_parsed(config)
  159. finds = directive.find('directive', flat=False)
  160. assert_equals(len(finds), 1)
  161. assert_equals([x.name for x in finds], ['directive'])
  162. assert_equals([x.args[0] for x in finds], ['1'])