-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathtests.py
executable file
·172 lines (145 loc) · 4.4 KB
/
tests.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Testing module for python-nginx.
python-nginx
(c) 2016 Jacob Cook
Licensed under GPLv3, see LICENSE.md
"""
# flake8: noqa
import nginx
import unittest
TESTBLOCK_CASE_1 = """
include conf.d/pre/*.cfg;
upstream php {
server unix:/tmp/php-fcgi.socket;
}
server {
listen 80; # This comment should be present;
# And this one
server_name localhost 127.0.0.1;
root /srv/http; # And also this one
mykey "myvalue; #notme myothervalue";
# This one too
index index.php;
location ~ \.php(?:$|/) {
fastcgi_pass php;
}
}
"""
TESTBLOCK_CASE_2 = """
upstream php
{
server unix:/tmp/php-fcgi.socket;
}
server
{
listen 80; # This comment should be present;
# And this one
server_name localhost 127.0.0.1;
root /srv/http; # And also this one
mykey "myvalue; #notme myothervalue";
# This one too
index index.php;
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~ \.php(?:$|/) {
fastcgi_pass php;
}
# location from the issue #10
location / {
return 301 $scheme://$host:$server_port${request_uri}bitbucket/;
}
}
"""
TESTBLOCK_CASE_3="""
upstream test0 {
ip_hash;
server 127.0.0.1:8080;
keepalive 16;
}
upstream test1{
server 127.0.0.2:8080;
keepalive 16;
}
upstream test2
{
server 127.0.0.3:8080;
keepalive 16;
}
server {
listen 80;
server_name example.com;
location = /
{
root html;
}
}
"""
TESTBLOCK_CASE_4 = """
# This is an example of a messy config
upstream php { server unix:/tmp/php-cgi.socket; }
server { server_name localhost; #this is the server server_name
location /{ test_key test_value; }}
"""
class TestPythonNginx(unittest.TestCase):
def test_basic_load(self):
self.assertTrue(nginx.loads(TESTBLOCK_CASE_1) is not None)
def test_messy_load(self):
data = nginx.loads(TESTBLOCK_CASE_4)
self.assertTrue(data is not None)
self.assertTrue(len(data.server.comments), 1)
self.assertTrue(len(data.server.locations), 1)
def test_comment_parse(self):
data = nginx.loads(TESTBLOCK_CASE_1)
self.assertEqual(len(data.server.comments), 4)
self.assertEqual(data.server.comments[2].comment, 'And also this one')
def test_key_parse(self):
data = nginx.loads(TESTBLOCK_CASE_1)
self.assertEqual(len(data.server.keys), 5)
firstKey = data.server.keys[0]
thirdKey = data.server.keys[3]
self.assertEqual(firstKey.name, 'listen')
self.assertEqual(firstKey.value, '80')
self.assertEqual(thirdKey.name, 'mykey')
self.assertEqual(thirdKey.value, '"myvalue; #notme myothervalue"')
def test_key_parse_complex(self):
data = nginx.loads(TESTBLOCK_CASE_2)
self.assertEqual(len(data.server.keys), 5)
firstKey = data.server.keys[0]
thirdKey = data.server.keys[3]
self.assertEqual(firstKey.name, 'listen')
self.assertEqual(firstKey.value, '80')
self.assertEqual(thirdKey.name, 'mykey')
self.assertEqual(thirdKey.value, '"myvalue; #notme myothervalue"')
self.assertEqual(
data.server.locations[-1].keys[0].value,
"301 $scheme://$host:$server_port${request_uri}bitbucket/"
)
def test_location_parse(self):
data = nginx.loads(TESTBLOCK_CASE_1)
self.assertEqual(len(data.server.locations), 1)
firstLoc = data.server.locations[0]
self.assertEqual(firstLoc.value, '~ \.php(?:$|/)')
self.assertEqual(len(firstLoc.keys), 1)
def test_brace_position(self):
data = nginx.loads(TESTBLOCK_CASE_3)
self.assertEqual(len(data.filter('Upstream')), 3)
def test_single_value_keys(self):
data = nginx.loads(TESTBLOCK_CASE_3)
single_value_key = data.filter('Upstream')[0].keys[0]
self.assertEqual(single_value_key.name, 'ip_hash')
self.assertEqual(single_value_key.value, '')
def test_reflection(self):
inp_data = nginx.loads(TESTBLOCK_CASE_1)
out_data = '\n' + nginx.dumps(inp_data)
self.assertEqual(TESTBLOCK_CASE_1, out_data)
def test_filtering(self):
data = nginx.loads(TESTBLOCK_CASE_1)
self.assertEqual(len(data.server.filter('Key', 'mykey')), 1)
self.assertEqual(data.server.filter('Key', 'nothere'), [])
if __name__ == '__main__':
unittest.main()