Migrate parameterized tests to pytest

pull/146/head
Emily 2024-07-28 19:32:34 +01:00
parent 80cc1edc9c
commit b5124787fb
3 changed files with 275 additions and 337 deletions

View File

@ -1,3 +1,4 @@
import pytest
from gixy.core.regexp import Regexp
'''
@ -12,8 +13,7 @@ CATEGORIES:
'''
def test_positive_contains():
cases = (
@pytest.mark.parametrize('regexp,char', (
(r'[a-z]', 'a'),
(r'[a-z]*', 'a'),
(r'[a-z]*?', 'a'),
@ -35,14 +35,17 @@ def test_positive_contains():
(r'\W', '\n'),
(r'[^\W]', 'a'),
(r'.', 'a')
)
for case in cases:
regexp, char = case
yield check_positive_contain, regexp, char
))
def test_positive_contains(regexp, char):
reg = Regexp(regexp, case_sensitive=True)
assert reg.can_contain(char), '{reg!r} should contain {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False)
char = char.upper()
assert reg.can_contain(char), '{reg!r} (case insensitive) should contain {chr!r}'.format(reg=regexp, chr=char)
def test_negative_contains():
cases = (
@pytest.mark.parametrize('regexp,char', (
('[a-z]', '1'),
('[a-z]*', '2'),
('[a-z]*?', '3'),
@ -63,27 +66,29 @@ def test_negative_contains():
(r'\W', 'a'),
(r'[^\W]', '\n'),
(r'.', '\n')
)
for case in cases:
regexp, char = case
yield check_negative_contain, regexp, char
))
def test_negative_contains(regexp, char):
reg = Regexp(regexp, case_sensitive=True)
assert not reg.can_contain(char), '{reg!r} should not contain {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False)
char = char.upper()
assert not reg.can_contain(char), '{reg!r} (case insensitive) should not contain {chr!r}'.format(reg=regexp, chr=char)
def test_groups_names():
cases = (
@pytest.mark.parametrize('regexp,groups', (
('foo', [0]),
('(1)(2)(?:3)', [0, 1, 2]),
('(1)((2)|(?:3))', [0, 1, 2, 3]),
("(?'pcre_7'1as)(?P<outer>(?<inner>2)|(?:3))", [0, 1, 2, 3, 'pcre_7', 'outer', 'inner']),
('/proxy/(?<proxy>.*)$', [0, 1, 'proxy'])
)
for case in cases:
regexp, groups = case
yield check_groups_names, regexp, groups
))
def test_groups_names(regexp, groups):
reg = Regexp(regexp)
assert set(reg.groups.keys()) == set(groups)
def test_to_string():
cases = (
@pytest.mark.parametrize('regexp,string', (
(r'foo', 'foo'),
(r'(1)(2)(?:3)', '(1)(2)(?:3)'),
(r'(1)((2)|(?:3))', '(1)((?:(2)|(?:3)))'),
@ -96,14 +101,13 @@ def test_to_string():
(r'1*', '1*'),
(r'1*?', '1*?'),
(r'1+', '1+'),
)
for case in cases:
regexp, string = case
yield check_to_string, regexp, string
))
def test_to_string(regexp, string):
reg = Regexp(regexp)
assert str(reg) == string
def test_positive_startswith():
cases = (
@pytest.mark.parametrize('regexp,char,strict', (
(r'foo', 'q', False),
(r'foo', 'f', True),
(r'^foo', 'f', False),
@ -118,14 +122,17 @@ def test_positive_startswith():
(r'((a))', 'b', False),
(r'^[a-z]{0}0', '0', False),
(r'^[a-z]{1}0', 'a', False),
)
for case in cases:
regexp, check, strict = case
yield check_positive_startswith, regexp, check, strict
))
def test_positive_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert reg.can_startswith(char), '{reg!r} can start\'s with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper()
assert reg.can_startswith(char), '{reg!r} (case insensitive) can start\'s with {chr!r}'.format(reg=regexp, chr=char)
def test_negative_startswith():
cases = (
@pytest.mark.parametrize('regexp,char,strict', (
(r'foo', '\n', False),
(r'foo', 'o', True),
(r'^foo', 'o', False),
@ -139,14 +146,17 @@ def test_negative_startswith():
(r'((abc)|(ss))', 'b', True),
(r'^[a-z]{0}0', 'a', False),
(r'^[a-z]{0}0', 'g', False),
)
for case in cases:
regexp, check, strict = case
yield check_negative_startswith, regexp, check, strict
))
def test_negative_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert not reg.can_startswith(char), '{reg!r} can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper()
assert not reg.can_startswith(char), '{reg!r} (case insensitive) can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char)
def test_positive_must_contain():
cases = (
@pytest.mark.parametrize('regexp,char', (
(r'abc', 'a'),
(r'abc', 'b'),
(r'abc', 'c'),
@ -155,14 +165,17 @@ def test_positive_must_contain():
(r'([0])', '0'),
(r'(?:[0])', '0'),
(r'(?:[0])|0|((((0))))', '0'),
)
for case in cases:
regexp, char = case
yield check_positive_must_contain, regexp, char
))
def test_positive_must_contain(regexp, char):
reg = Regexp(regexp, case_sensitive=True)
assert reg.must_contain(char), '{reg!r} must contain with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False)
char = char.upper()
assert reg.must_contain(char), '{reg!r} (case insensitive) must contain with {chr!r}'.format(reg=regexp, chr=char)
def test_negative_must_contain():
cases = (
@pytest.mark.parametrize('regexp,char', (
(r'[a-z]', '1'),
(r'2{0}1', '2'),
(r'3?', '3'),
@ -187,14 +200,17 @@ def test_negative_must_contain():
(r'\W', '\n'),
(r'[^\W]', 'a'),
(r'.', '\n')
)
for case in cases:
regexp, char = case
yield check_negative_must_contain, regexp, char
))
def test_negative_must_contain(regexp, char):
reg = Regexp(regexp, case_sensitive=True)
assert not reg.must_contain(char), '{reg!r} must NOT contain with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False)
char = char.upper()
assert not reg.must_contain(char), '{reg!r} (case insensitive) must NOT contain with {chr!r}'.format(reg=regexp, chr=char)
def test_positive_must_startswith():
cases = (
@pytest.mark.parametrize('regexp,char,strict', (
(r'foo', 'f', True),
(r'^foo', 'f', False),
(r'(^foo)', 'f', True),
@ -202,14 +218,17 @@ def test_positive_must_startswith():
(r'((a))', 'a', True),
(r'^[a-z]{0}0', '0', False),
(r'^a{1}0', 'a', False),
)
for case in cases:
regexp, check, strict = case
yield check_positive_must_startswith, regexp, check, strict
))
def test_positive_must_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert reg.must_startswith(char), '{reg!r} MUST start\'s with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper()
assert reg.must_startswith(char), '{reg!r} (case insensitive) MUST start\'s with {chr!r}'.format(reg=regexp, chr=char)
def test_negative_must_startswith():
cases = (
@pytest.mark.parametrize('regexp,char,strict', (
(r'foo', 'o', False),
(r'^foo', 'o', False),
(r'(^foo)', 'o', False),
@ -223,14 +242,17 @@ def test_negative_must_startswith():
(r'^((a))', 'b', False),
(r'((a))', 'a', False),
(r'^a{0}0', 'a', False),
)
for case in cases:
regexp, check, strict = case
yield check_negative_must_startswith, regexp, check, strict
))
def test_negative_must_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert not reg.must_startswith(char), '{reg!r} MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper()
assert not reg.must_startswith(char), '{reg!r} (case insensitive) MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char)
def test_generate():
cases = (
@pytest.mark.parametrize('regexp,values', (
(r'foo', ['foo']),
(r'^sss', ['^sss']),
(r'(1)(2)(3)', ['123']),
@ -249,10 +271,10 @@ def test_generate():
(r'[^\x00-\x7b|\x7e-\xff]', ['\x7d']),
(r'(a|b|c)', ['a', 'b', 'c']),
(r'[xyz]', ['x', 'y', 'z'])
)
for case in cases:
regexp, values = case
yield check_generate, regexp, values
))
def test_generate(regexp, values):
reg = Regexp(regexp)
assert sorted(reg.generate('|', anchored=True)) == sorted(values)
def test_strict_generate():
@ -291,90 +313,3 @@ def test_group_can_contains():
assert reg.group(1).can_contain('\n'), 'Group 1 from regex "{src}" can contains {sym!r}'.format(src=source, sym='\\n')
assert not reg.group('action').can_contain('/'), 'Group "action" from regex "{src}" CAN\'T (!) contain {sym!r}'.format(src=source, sym='/')
def check_positive_contain(regexp, char):
reg = Regexp(regexp, case_sensitive=True)
assert reg.can_contain(char), '{reg!r} should contain {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False)
char = char.upper()
assert reg.can_contain(char), '{reg!r} (case insensitive) should contain {chr!r}'.format(reg=regexp, chr=char)
def check_negative_contain(regexp, char):
reg = Regexp(regexp, case_sensitive=True)
assert not reg.can_contain(char), '{reg!r} should not contain {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False)
char = char.upper()
assert not reg.can_contain(char), '{reg!r} (case insensitive) should not contain {chr!r}'.format(reg=regexp, chr=char)
def check_positive_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert reg.can_startswith(char), '{reg!r} can start\'s with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper()
assert reg.can_startswith(char), '{reg!r} (case insensitive) can start\'s with {chr!r}'.format(reg=regexp, chr=char)
def check_negative_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert not reg.can_startswith(char), '{reg!r} can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper()
assert not reg.can_startswith(char), '{reg!r} (case insensitive) can\'t start\'s with {chr!r}'.format(reg=regexp, chr=char)
def check_groups_names(regexp, groups):
reg = Regexp(regexp)
assert set(reg.groups.keys()) == set(groups)
def check_to_string(regexp, string):
reg = Regexp(regexp)
assert str(reg) == string
def check_positive_must_contain(regexp, char):
reg = Regexp(regexp, case_sensitive=True)
assert reg.must_contain(char), '{reg!r} must contain with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False)
char = char.upper()
assert reg.must_contain(char), '{reg!r} (case insensitive) must contain with {chr!r}'.format(reg=regexp, chr=char)
def check_negative_must_contain(regexp, char):
reg = Regexp(regexp, case_sensitive=True)
assert not reg.must_contain(char), '{reg!r} must NOT contain with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False)
char = char.upper()
assert not reg.must_contain(char), '{reg!r} (case insensitive) must NOT contain with {chr!r}'.format(reg=regexp, chr=char)
def check_positive_must_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert reg.must_startswith(char), '{reg!r} MUST start\'s with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper()
assert reg.must_startswith(char), '{reg!r} (case insensitive) MUST start\'s with {chr!r}'.format(reg=regexp, chr=char)
def check_negative_must_startswith(regexp, char, strict):
reg = Regexp(regexp, case_sensitive=True, strict=strict)
assert not reg.must_startswith(char), '{reg!r} MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char)
reg = Regexp(regexp, case_sensitive=False, strict=strict)
char = char.upper()
assert not reg.must_startswith(char), '{reg!r} (case insensitive) MUST NOT start\'s with {chr!r}'.format(reg=regexp, chr=char)
def check_generate(regexp, values):
reg = Regexp(regexp)
assert sorted(reg.generate('|', anchored=True)) == sorted(values)

View File

@ -1,3 +1,4 @@
import pytest
from gixy.parser.nginx_parser import NginxParser
from gixy.directives.directive import *
from gixy.directives.block import *
@ -7,8 +8,8 @@ def _parse(config):
return NginxParser(cwd='', allow_includes=False).parse(config)
def test_directive():
configs = [
@pytest.mark.parametrize('config,expected', zip(
[
'access_log syslog:server=127.0.0.1,tag=nginx_sentry toolsformat;',
'user http;',
'internal;',
@ -16,34 +17,35 @@ def test_directive():
"set $foo 'bar';",
'proxy_pass http://unix:/run/sock.socket;',
'rewrite ^/([a-zA-Z0-9]+)$ /$1/${arg_v}.pb break;'
]
],
expected = [
[
[Directive],
[Directive],
[Directive],
[Directive, SetDirective],
[Directive, SetDirective],
[Directive],
[Directive, RewriteDirective]
]
for i, config in enumerate(configs):
return assert_config, config, expected[i]
))
def test_directive(config, expected):
assert_config(config, expected)
def test_blocks():
configs = [
@pytest.mark.parametrize('config,expected', zip(
[
'if (-f /some) {}',
'location / {}'
]
],
expected = [
[
[Directive, Block, IfBlock],
[Directive, Block, LocationBlock],
]
for i, config in enumerate(configs):
yield assert_config, config, expected[i]
))
def test_blocks(config, expected):
assert_config(config, expected)
def test_dump_simple():

View File

@ -1,6 +1,7 @@
import os
from os import path
import json
import pytest
from ..utils import *
from gixy.core.manager import Manager as Gixy
@ -8,18 +9,13 @@ from gixy.core.plugins_manager import PluginsManager
from gixy.core.config import Config
def setup_module():
pass
def teardown_module():
pass
def test_from_config():
def generate_config_test_cases():
tested_plugins = set()
tested_fp_plugins = set()
config_cases = []
config_fp_cases = []
conf_dir = path.join(path.dirname(__file__), 'simply')
for plugin in os.listdir(conf_dir):
if plugin in ('.', '..'):
@ -42,12 +38,10 @@ def test_from_config():
if not test_case.endswith('_fp.conf'):
# Not False Positive test
tested_plugins.add(plugin)
test_func = check_configuration
config_cases.append((plugin, config_path, config))
else:
tested_fp_plugins.add(plugin)
test_func = check_configuration_fp
yield test_func, plugin, config_path, config
config_fp_cases.append((plugin, config_path, config))
manager = PluginsManager()
for plugin in manager.plugins:
@ -55,6 +49,11 @@ def test_from_config():
assert plugin in tested_plugins, 'Plugin {name!r} should have at least one simple test config'.format(name=plugin)
assert plugin in tested_fp_plugins, 'Plugin {name!r} should have at least one simple test config with false positive'.format(name=plugin)
return config_cases, config_fp_cases
all_config_cases, all_config_fp_cases = generate_config_test_cases()
def parse_plugin_options(config_path):
with open(config_path, 'r') as f:
@ -74,7 +73,8 @@ def yoda_provider(plugin, plugin_options=None):
return Gixy(config=config)
def check_configuration(plugin, config_path, test_config):
@pytest.mark.parametrize('plugin,config_path,test_config', all_config_cases)
def test_configuration(plugin, config_path, test_config):
plugin_options = parse_plugin_options(config_path)
with yoda_provider(plugin, plugin_options) as yoda:
yoda.audit(config_path, open(config_path, mode='r'))
@ -97,7 +97,8 @@ def check_configuration(plugin, config_path, test_config):
assert result['help_url'].startswith('https://'), 'help_url must starts with https://. It\'is URL!'
def check_configuration_fp(plugin, config_path, test_config):
@pytest.mark.parametrize('plugin,config_path,test_config', all_config_fp_cases)
def test_configuration_fp(plugin, config_path, test_config):
with yoda_provider(plugin) as yoda:
yoda.audit(config_path, open(config_path, mode='r'))
assert len([x for x in yoda.results]) == 0, 'False positive configuration must not trigger any plugins'