test_simply.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from nose.tools import assert_equals, assert_true
  2. from tests.asserts import assert_in
  3. import os
  4. from os import path
  5. import json
  6. from ..utils import *
  7. from gixy.core.manager import Manager as Gixy
  8. from gixy.core.plugins_manager import PluginsManager
  9. from gixy.core.config import Config
  10. def setup_module():
  11. pass
  12. def teardown_module():
  13. pass
  14. def test_from_config():
  15. tested_plugins = set()
  16. tested_fp_plugins = set()
  17. conf_dir = path.join(path.dirname(__file__), 'simply')
  18. for plugin in os.listdir(conf_dir):
  19. if plugin in ('.', '..'):
  20. continue
  21. plugin_path = path.join(conf_dir, plugin)
  22. if not path.isdir(plugin_path):
  23. continue
  24. config = {}
  25. if path.exists(path.join(plugin_path, 'config.json')):
  26. with open(path.join(plugin_path, 'config.json'), 'r') as file:
  27. config = json.loads(file.read())
  28. for test_case in os.listdir(plugin_path):
  29. if not test_case.endswith('.conf'):
  30. continue
  31. config_path = path.join(plugin_path, test_case)
  32. if not test_case.endswith('_fp.conf'):
  33. # Not False Positive test
  34. tested_plugins.add(plugin)
  35. test_func = check_configuration
  36. else:
  37. tested_fp_plugins.add(plugin)
  38. test_func = check_configuration_fp
  39. yield test_func, plugin, config_path, config
  40. manager = PluginsManager()
  41. for plugin in manager.plugins:
  42. plugin = plugin.name
  43. assert_true(plugin in tested_plugins,
  44. 'Plugin {name!r} should have at least one simple test config'.format(name=plugin))
  45. assert_true(plugin in tested_fp_plugins,
  46. 'Plugin {name!r} should have at least one simple test config with false positive'.format(name=plugin))
  47. def parse_plugin_options(config_path):
  48. with open(config_path, 'r') as f:
  49. config_line = f.readline()
  50. if config_line.startswith('# Options: '):
  51. return json.loads(config_line[10:])
  52. return None
  53. def yoda_provider(plugin, plugin_options=None):
  54. config = Config(
  55. allow_includes=False,
  56. plugins=[plugin]
  57. )
  58. if plugin_options:
  59. config.set_for(plugin, plugin_options)
  60. return Gixy(config=config)
  61. def check_configuration(plugin, config_path, test_config):
  62. plugin_options = parse_plugin_options(config_path)
  63. with yoda_provider(plugin, plugin_options) as yoda:
  64. yoda.audit(config_path, open(config_path, mode='r'))
  65. formatter = BaseFormatter()
  66. formatter.feed(config_path, yoda)
  67. _, results = formatter.reports.popitem()
  68. assert_equals(len(results), 1, 'Should have one report')
  69. result = results[0]
  70. if 'severity' in test_config:
  71. if not hasattr(test_config['severity'], '__iter__'):
  72. assert_equals(result['severity'], test_config['severity'])
  73. else:
  74. assert_in(result['severity'], test_config['severity'])
  75. assert_equals(result['plugin'], plugin)
  76. assert_true(result['summary'])
  77. assert_true(result['description'])
  78. assert_true(result['config'])
  79. assert_true(result['help_url'].startswith('https://'),
  80. 'help_url must starts with https://. It\'is URL!')
  81. def check_configuration_fp(plugin, config_path, test_config):
  82. with yoda_provider(plugin) as yoda:
  83. yoda.audit(config_path, open(config_path, mode='r'))
  84. assert_equals(len([x for x in yoda.results]), 0,
  85. 'False positive configuration must not trigger any plugins')