comparison config_dump.t @ 621:884b2f0c173f

Tests: tests for dumped nginx configuration ("nginx -T").
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 01 Jun 2015 14:34:36 +0300
parents
children 1dd0e909fe94
comparison
equal deleted inserted replaced
620:7497adad1a50 621:884b2f0c173f
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for dumped nginx configuration (nginx -T).
7 # Among other things, test that configuration blocks are properly processed.
8
9 ###############################################################################
10
11 use warnings;
12 use strict;
13
14 use Test::More;
15
16 BEGIN { use FindBin; chdir($FindBin::Bin); }
17
18 use lib 'lib';
19 use Test::Nginx;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new()->has(qw/http map/);
27
28 plan(skip_all => 'no config dump') unless $t->has_version('1.9.2');
29
30 $t->plan(10)->write_file_expand('nginx.conf', <<'EOF');
31
32 %%TEST_GLOBALS%%
33
34 daemon off;
35
36 include %%TESTDIR%%/inc.conf;
37
38 events {
39 }
40
41 http {
42 %%TEST_GLOBALS_HTTP%%
43
44 map $args $x {
45 default 0;
46 foo bar;
47 include map.conf;
48 }
49
50 upstream u {
51 server 127.0.0.1:8081;
52 }
53
54 server {
55 listen 127.0.0.1:8080;
56 server_name localhost;
57
58 location / { }
59 }
60 }
61
62 EOF
63
64 $t->write_file('inc.conf', 'include inc2.conf;');
65 $t->write_file('inc2.conf', '#inc2.conf');
66 $t->write_file('map.conf', '#map.conf;');
67
68 $t->run();
69
70 ###############################################################################
71
72 my $d = $t->testdir;
73
74 my $dump = $t->dump_config();
75 like($dump, qr!^# configuration file $d/nginx.conf:$!m, 'nginx.conf found');
76 like($dump, qr!^# configuration file $d/inc.conf:$!m, 'inc.conf found');
77 like($dump, qr!^# configuration file $d/inc2.conf:$!m, 'inc2.conf found');
78 like($dump, qr!^# configuration file $d/map.conf:$!m, 'map.conf found');
79
80 is(getconf($t, $dump, 'nginx.conf'), $t->read_file('nginx.conf'), 'content');
81 is(getconf($t, $dump, 'inc.conf'), $t->read_file('inc.conf'), 'content inc');
82 is(getconf($t, $dump, 'map.conf'), $t->read_file('map.conf'), 'content inc 2');
83
84 unlink($t->testdir . "/inc.conf");
85 unlink($t->testdir . "/map.conf");
86
87 $dump = $t->dump_config();
88 unlike($dump, qr!file $d/inc.conf!, 'missing inc.conf');
89 unlike($dump, qr!file $d/map.conf!, 'missing map.conf');
90 like($dump, qr!file $d/nginx.conf test failed!, 'test failed');
91
92 $t->write_file('inc.conf', 'include inc2.conf;');
93 $t->write_file('inc2.conf', '#inc2.conf');
94 $t->write_file('map.conf', '#map.conf;');
95
96 ###############################################################################
97
98 sub getconf {
99 my ($t, $string, $conf) = @_;
100 my $prefix = "# configuration file $d/$conf:\n";
101 my $offset = index($string, $prefix) + length($prefix);
102 my $len = length($t->read_file($conf));
103 return substr($string, $offset, $len) =~ tr/\r//rd;
104 }