/
test_analyze.py
59 lines (45 loc) · 1.74 KB
/
test_analyze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
import crossplane
def test_state_directive():
fname = '/path/to/nginx.conf'
stmt = {
'directive': 'state',
'args': ['/path/to/state/file.conf'],
'line': 5 # this is arbitrary
}
# the state directive should not cause errors if it's in these contexts
good_contexts = set([
('http', 'upstream'),
('stream', 'upstream'),
('some_third_party_context',)
])
for ctx in good_contexts:
crossplane.analyzer.analyze(fname, stmt, term=';', ctx=ctx)
# the state directive should not be in any of these contexts
bad_contexts = set(crossplane.analyzer.CONTEXTS) - good_contexts
for ctx in bad_contexts:
try:
crossplane.analyzer.analyze(fname, stmt, term=';', ctx=ctx)
raise Exception("bad context for 'state' passed: " + repr(ctx))
except crossplane.errors.NgxParserDirectiveContextError:
continue
def test_flag_directive_args():
fname = '/path/to/nginx.conf'
ctx = ('events',)
# an NGINX_CONF_FLAG directive
stmt = {
'directive': 'accept_mutex',
'line': 2 # this is arbitrary
}
good_args = [['on'], ['off'], ['On'], ['Off'], ['ON'], ['OFF']]
for args in good_args:
stmt['args'] = args
crossplane.analyzer.analyze(fname, stmt, term=';', ctx=ctx)
bad_args = [['1'], ['0'], ['true'], ['okay'], ['']]
for args in bad_args:
stmt['args'] = args
try:
crossplane.analyzer.analyze(fname, stmt, term=';', ctx=ctx)
raise Exception('bad args for flag directive: ' + repr(args))
except crossplane.errors.NgxParserDirectiveArgumentsError as e:
assert e.strerror.endswith('it must be "on" or "off"')