Bump to 1.14.1
[platform/upstream/augeas.git] / lenses / tests / test_nginx.aug
1 (*
2 Module: Test_Nginx
3   Provides unit tests and examples for the <Nginx> lens.
4 *)
5 module Test_nginx =
6
7 (* Check for non-recursive ambiguities *)
8 let directive = Nginx.simple
9               | Nginx.block (
10                    Nginx.simple
11                  | Nginx.block Nginx.simple
12               )
13
14 (* Do some limited typechecking on the recursive lens; note that
15    unrolling once more leads to a typecheck error that seems to
16    be spurious, though it's not clear why
17
18    Unrolling once more amounts to adding the clause
19       Nginx.block (Nginx.block Nginx.simple)
20    to unrolled and results in an error
21       overlapping lenses in union.get
22         Example matched by both: 'upstream{}\n'
23 *)
24 let unrolled = Nginx.simple | Nginx.block Nginx.simple
25
26 let lns_unrolled = (Util.comment | Util.empty | unrolled)
27
28 (* Normal unit tests *)
29 let lns = Nginx.lns
30
31 let conf ="user nginx nginx;
32 worker_processes 1;
33 error_log /var/log/nginx/error_log info;
34
35 events {
36         worker_connections 1024;
37         use epoll;
38 }
39
40 # comment1
41 # comment2
42
43 http {
44         # comment3
45         include /etc/nginx/mime.types;
46         default_type application/octet-stream;
47         log_format main
48                 '$remote_addr - $remote_user [$time_local] '
49                 '\"$request\" $status $bytes_sent '
50                 '\"$http_referer\" \"$http_user_agent\" '
51                 '\"$gzip_ratio\"';
52         client_header_timeout 10m;
53         client_body_timeout 10m;
54         send_timeout 10m;
55         connection_pool_size 256;
56         client_header_buffer_size 2k;
57         large_client_header_buffers 4 8k;
58         request_pool_size 4k;
59         gzip on;
60         gzip_min_length 1000;
61         gzip_buffers 4 8k;
62         gzip_types text/plain application/json;
63         output_buffers 1 32k;
64         postpone_output 1460;
65         sendfile on;
66         tcp_nopush on;
67         tcp_nodelay on;
68         keepalive_timeout 75 20;
69         ignore_invalid_headers on;
70         index index.html index.php;
71         include vhosts/*.conf;
72 }
73 "
74
75 test lns get conf =
76    { "user"  = "nginx nginx" }
77    { "worker_processes"      = "1" }
78    { "error_log" = "/var/log/nginx/error_log info" }
79    {}
80    { "events"
81       { "worker_connections"  = "1024" }
82       { "use"      = "epoll" } }
83    {}
84    { "#comment" = "comment1" }
85    { "#comment" = "comment2" }
86    {}
87    { "http"
88           { "#comment" = "comment3" }
89       { "include"  = "/etc/nginx/mime.types" }
90       { "default_type"  = "application/octet-stream" }
91       { "log_format"      = "main
92                 '$remote_addr - $remote_user [$time_local] '
93                 '\"$request\" $status $bytes_sent '
94                 '\"$http_referer\" \"$http_user_agent\" '
95                 '\"$gzip_ratio\"'" }
96       { "client_header_timeout"  = "10m" }
97       { "client_body_timeout"  = "10m" }
98       { "send_timeout"      = "10m" }
99       { "connection_pool_size"      = "256" }
100       { "client_header_buffer_size"      = "2k" }
101       { "large_client_header_buffers"      = "4 8k" }
102       { "request_pool_size"      = "4k" }
103       { "gzip"      = "on" }
104       { "gzip_min_length"      = "1000" }
105       { "gzip_buffers"      = "4 8k" }
106       { "gzip_types"      = "text/plain application/json" }
107       { "output_buffers"      = "1 32k" }
108       { "postpone_output"      = "1460" }
109       { "sendfile"      = "on" }
110       { "tcp_nopush"      = "on" }
111       { "tcp_nodelay"      = "on" }
112       { "keepalive_timeout"      = "75 20" }
113       { "ignore_invalid_headers"      = "on" }
114       { "index"      = "index.html index.php" }
115       { "include"      = "vhosts/*.conf" } }
116
117 (* location blocks *)
118 test lns get "location / { }\n" =
119   { "location"
120     { "#uri" = "/" } }
121
122 test lns get "location = / { }\n" =
123   { "location"
124       { "#comp" = "=" }
125       { "#uri" = "/" } }
126
127 test lns get "location /documents/ { }\n" =
128   { "location"
129     { "#uri" = "/documents/" } }
130
131 test lns get "location ^~ /images/ { }\n" =
132   { "location"
133     { "#comp" = "^~" }
134     { "#uri" = "/images/" } }
135
136 test lns get "location ~* \.(gif|jpg|jpeg)$ { }\n" =
137   { "location"
138     { "#comp" = "~*" }
139     { "#uri" = "\.(gif|jpg|jpeg)$" } }
140
141 test lns get "location @fallback { }\n" =
142  { "location"
143     { "#uri" = "@fallback" } }
144
145 (* if blocks *)
146 test lns get "if ($slow) {
147   tcp_nodelay on;
148 }\n" =
149   { "if"
150     { "#cond" = "($slow)" }
151     { "tcp_nodelay" = "on" } }
152
153 test lns get "if ($request_method = POST)   { }\n" =
154  { "if"
155     { "#cond" = "($request_method = POST)" } }
156
157
158 test lns get "if ($http_cookie ~* \"id=([^;]+)(?:;|$)\") { }\n" =
159  { "if"
160     { "#cond" = "($http_cookie ~* \"id=([^;]+)(?:;|$)\")" } }
161
162 (* geo blocks *)
163 test lns get "geo $geo { }\n" =
164   { "geo"
165     { "#geo" = "$geo" } }
166
167 test lns get "geo $address $geo { }\n" =
168   { "geo"
169     { "#address" = "$address" }
170     { "#geo" = "$geo" } }
171
172 (* map blocks *)
173 test lns get "map $http_host $name { }\n" =
174   { "map"
175     { "#source" = "$http_host" }
176     { "#variable" = "$name" } }
177
178 (* split_clients block *)
179 test lns get "split_clients \"${remote_addr}AAA\" $variable { }\n" =
180   { "split_clients"
181     { "#string" = "\"${remote_addr}AAA\"" }
182     { "#variable" = "$variable" } }
183
184 (* upstream block *)
185 test lns get "upstream backend { }\n" =
186  { "upstream"
187     { "#name" = "backend" } }
188
189 (* GH #179 - recursive blocks *)
190 let http = "http {
191   server {
192     listen 80;
193     location / {
194       root\thtml;
195     }
196   }
197   gzip on;
198 }\n"
199
200 test lns get http =
201   { "http"
202     { "server"
203        { "listen" = "80" }
204        { "location"
205          { "#uri" = "/" }
206          { "root" = "html" } } }
207     { "gzip" = "on" } }
208
209
210 (* GH #335 - server single line entries *)
211 let http_server_single_line_entries = "http {
212   upstream big_server_com {
213     server 127.0.0.3:8000 weight=5;
214     server 127.0.0.3:8001 weight=5;
215     server 192.168.0.1:8000;
216     server 192.168.0.1:8001;
217     server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
218     server backend3.example.com      resolve;
219   }
220 }\n"
221
222 test lns get http_server_single_line_entries =
223   { "http"
224     { "upstream"
225       { "#name" = "big_server_com" }
226       { "@server" { "@address" = "127.0.0.3:8000" } { "weight" = "5" } }
227       { "@server" { "@address" = "127.0.0.3:8001" } { "weight" = "5" } }
228       { "@server" { "@address" = "192.168.0.1:8000" } }
229       { "@server" { "@address" = "192.168.0.1:8001" } }
230       { "@server"
231         { "@address" = "backend2.example.com:8080" }
232         { "fail_timeout" = "5s" }
233         { "slow_start" = "30s" } }
234       { "@server"
235         { "@address" = "backend3.example.com" }
236         { "resolve" } } } }
237
238 (* Make sure we do not screw up the indentation of the file *)
239 test lns put http after set "/http/gzip" "off" =
240 "http {
241   server {
242     listen 80;
243     location / {
244       root\thtml;
245     }
246   }
247   gzip off;
248 }\n"
249
250 (* Test lns
251      GH #260 *)
252 test lns get "http {
253   geo $geo {
254     default        0;
255
256     127.0.0.1      2;
257     192.168.1.0/24 1;
258     10.1.0.0/16    1;
259
260     ::1            2;
261     2001:0db8::/32 1;
262   }
263 }\n" =
264   { "http"
265     { "geo"
266       { "#geo" = "$geo" }
267       { "default" = "0" }
268       {  }
269       { "127.0.0.1" = "2" }
270       { "192.168.1.0" = "1"
271         { "mask" = "24" } }
272       { "10.1.0.0" = "1"
273         { "mask" = "16" } }
274       {  }
275       { "::1" = "2" }
276       { "2001:0db8::" = "1"
277         { "mask" = "32" } } } }
278
279 test lns get "add_header X-XSS-Protection \"1; mode=block\" always;\n" =
280   { "add_header" = "X-XSS-Protection \"1; mode=block\" always" }
281
282 test lns get "location /foo {
283   root /var/www/html;
284   internal;  # only valid in location blocks
285 }\n" =
286   { "location"
287     { "#uri" = "/foo" }
288     { "root" = "/var/www/html" }
289     { "internal"
290       { "#comment" = "only valid in location blocks" } } }
291
292 test lns get "upstream php-handler {
293     server unix:/var/run/php/php7.3-fpm.sock;
294 }\n" =
295   { "upstream"
296     { "#name" = "php-handler" }
297     { "@server"
298       { "@address" = "unix:/var/run/php/php7.3-fpm.sock" } } }
299