test_nginx_parser.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from nose.tools import assert_equal
  2. from tests.asserts import assert_is_instance
  3. from gixy.parser.nginx_parser import NginxParser
  4. from gixy.directives.directive import *
  5. from gixy.directives.block import *
  6. def _parse(config):
  7. return NginxParser(cwd='', allow_includes=False).parse(config)
  8. def test_directive():
  9. configs = [
  10. 'access_log syslog:server=127.0.0.1,tag=nginx_sentry toolsformat;',
  11. 'user http;',
  12. 'internal;',
  13. 'set $foo "bar";',
  14. "set $foo 'bar';",
  15. 'proxy_pass http://unix:/run/sock.socket;',
  16. 'rewrite ^/([a-zA-Z0-9]+)$ /$1/${arg_v}.pb break;'
  17. ]
  18. expected = [
  19. [Directive],
  20. [Directive],
  21. [Directive],
  22. [Directive, SetDirective],
  23. [Directive],
  24. [Directive, RewriteDirective]
  25. ]
  26. for i, config in enumerate(configs):
  27. return assert_config, config, expected[i]
  28. def test_blocks():
  29. configs = [
  30. 'if (-f /some) {}',
  31. 'location / {}'
  32. ]
  33. expected = [
  34. [Directive, Block, IfBlock],
  35. [Directive, Block, LocationBlock],
  36. ]
  37. for i, config in enumerate(configs):
  38. yield assert_config, config, expected[i]
  39. def test_dump_simple():
  40. config = '''
  41. # configuration file /etc/nginx/nginx.conf:
  42. http {
  43. include sites/*.conf;
  44. }
  45. # configuration file /etc/nginx/conf.d/listen:
  46. listen 80;
  47. # configuration file /etc/nginx/sites/default.conf:
  48. server {
  49. include conf.d/listen;
  50. }
  51. '''
  52. tree = _parse(config)
  53. assert_is_instance(tree, Directive)
  54. assert_is_instance(tree, Block)
  55. assert_is_instance(tree, Root)
  56. assert_equal(len(tree.children), 1)
  57. http = tree.children[0]
  58. assert_is_instance(http, Directive)
  59. assert_is_instance(http, Block)
  60. assert_is_instance(http, HttpBlock)
  61. assert_equal(len(http.children), 1)
  62. include_server = http.children[0]
  63. assert_is_instance(include_server, Directive)
  64. assert_is_instance(include_server, IncludeBlock)
  65. assert_equal(include_server.file_path, '/etc/nginx/sites/default.conf')
  66. assert_equal(len(include_server.children), 1)
  67. server = include_server.children[0]
  68. assert_is_instance(server, Directive)
  69. assert_is_instance(server, Block)
  70. assert_is_instance(server, ServerBlock)
  71. assert_equal(len(server.children), 1)
  72. include_listen = server.children[0]
  73. assert_is_instance(include_listen, Directive)
  74. assert_is_instance(include_listen, IncludeBlock)
  75. assert_equal(include_listen.file_path, '/etc/nginx/conf.d/listen')
  76. assert_equal(len(include_listen.children), 1)
  77. listen = include_listen.children[0]
  78. assert_is_instance(listen, Directive)
  79. assert_equal(listen.args, ['80'])
  80. def assert_config(config, expected):
  81. tree = _parse(config)
  82. assert_is_instance(tree, Directive)
  83. assert_is_instance(tree, Block)
  84. assert_is_instance(tree, Root)
  85. child = tree.children[0]
  86. for ex in expected:
  87. assert_is_instance(child, ex)