OSDN Git Service

Fixed chkconfig option
[ultramonkey-l7/ultramonkey-l7-v2.git] / l7directord / t / 32_getid.t
1 use strict;
2 use warnings;
3 no warnings qw(redefine once);
4 use lib qw(t/lib lib);
5 use subs qw(print);
6 use Cwd;
7 use L7lib;
8 use Test::More tests => 41;
9
10 L7lib::chdir();
11 L7lib::comment_out();
12 require './l7directord';
13 override();
14
15 #...............................................
16 # test start
17 #   - get_ip_port
18 {
19     my $host = undef;
20     my $checkport = undef;
21     my $got = get_ip_port($host, $checkport);
22     is $got, '', 'get_ip_port - host and checkport is undef';
23 }
24 {
25     my $host = { server => { ip => '127.0.0.1', port => undef } };
26     my $checkport = undef;
27     my $got = get_ip_port($host, $checkport);
28     is $got, '', 'get_ip_port - host port and checkport is undef';
29 }
30 {
31     my $host = { server => { ip => undef, port => undef } };
32     my $checkport = 100;
33     my $got = get_ip_port($host, $checkport);
34     is $got, '', 'get_ip_port - host ip is undef';
35 }
36 {
37     my $host = { server => { ip => '127.0.0.1', port => 80 } };
38     my $checkport = undef;
39     my $got = get_ip_port($host, $checkport);
40     is $got, '127.0.0.1:80', 'get_ip_port - get host ip and port';
41 }
42 {
43     my $host = { server => { ip => '127.0.0.1', port => 80 } };
44     my $checkport = 8080;
45     my $got = get_ip_port($host, $checkport);
46     is $got, '127.0.0.1:8080', 'get_ip_port - get host ip and checkport';
47 }
48 {
49     my $host = { server => { ip => '127.0.0.1', port => 0 } };
50     my $checkport = undef;
51     my $got = get_ip_port($host, $checkport);
52     is $got, '127.0.0.1:0', 'get_ip_port - get host ip and port zero';
53 }
54 #   - get_health_check_id_str
55 {
56     my $v = undef;
57     my $r = { server => {} };
58     my $got = get_health_check_id_str($v, $r);
59     is $got, undef, 'get_health_check_id_str - v is undef';
60 }
61 {
62     my $v = {};
63     my $r = undef;
64     my $got = get_health_check_id_str($v, $r);
65     is $got, undef, 'get_health_check_id_str - r is undef';
66 }
67 {
68     my $v = {};
69     my $r = {};
70     my $got = get_health_check_id_str($v, $r);
71     is $got, undef, 'get_health_check_id_str - r->{server} is undef';
72 }
73 {
74     my $v = {};
75     my $r = { server => {} };
76     my $got = get_health_check_id_str($v, $r);
77     is $got, '::::::::::::::::::', 'get_health_check_id_str - all args is ok(empty)';
78 }
79 {
80     my $v = {};
81     my $r = { server => { ip => 1 } };
82     my $got = get_health_check_id_str($v, $r);
83     is $got, '1::::::::::::::::::', 'get_health_check_id_str - set real ip';
84 }
85 {
86     my $v = {};
87     my $r = { server => { port => 1 } };
88     my $got = get_health_check_id_str($v, $r);
89     is $got, ':1:::::::::::::::::', 'get_health_check_id_str - set real port';
90 }
91 {
92     my $v = { checkport => 1 };
93     my $r = { server => {} };
94     my $got = get_health_check_id_str($v, $r);
95     is $got, ':1:::::::::::::::::', 'get_health_check_id_str - set checkport';
96 }
97 {
98     my $v = { checktype => 'connect' };
99     my $r = { server => {} };
100     my $got = get_health_check_id_str($v, $r);
101     is $got, '::connect::::::::::::::::', 'get_health_check_id_str - set checktype';
102 }
103 {
104     my $v = { service => 'http' };
105     my $r = { server => {} };
106     my $got = get_health_check_id_str($v, $r);
107     is $got, ':::http:::::::::::::::', 'get_health_check_id_str - set service';
108 }
109 {
110     my $v = { protocol => 'tcp' };
111     my $r = { server => {} };
112     my $got = get_health_check_id_str($v, $r);
113     is $got, '::::tcp::::::::::::::', 'get_health_check_id_str - set protocol';
114 }
115 {
116     my $v = { num_connects => 1 };
117     my $r = { server => {} };
118     my $got = get_health_check_id_str($v, $r);
119     is $got, ':::::1:::::::::::::', 'get_health_check_id_str - set num_connects';
120 }
121 {
122     my $v = {};
123     my $r = { server => {}, request => '/' };
124     my $got = get_health_check_id_str($v, $r);
125     is $got, '::::::/::::::::::::', 'get_health_check_id_str - set real request';
126 }
127 {
128     my $v = {};
129     my $r = { server => {}, receive => 'ok' };
130     my $got = get_health_check_id_str($v, $r);
131     is $got, ':::::::ok:::::::::::', 'get_health_check_id_str - set real receive';
132 }
133 {
134     my $v = { httpmethod => 'GET' };
135     my $r = { server => {} };
136     my $got = get_health_check_id_str($v, $r);
137     is $got, '::::::::GET::::::::::', 'get_health_check_id_str - set httpmethod';
138 }
139 {
140     my $v = { virtualhost => 'virtual' };
141     my $r = { server => {} };
142     my $got = get_health_check_id_str($v, $r);
143     is $got, ':::::::::virtual:::::::::', 'get_health_check_id_str - set virtualhost';
144 }
145 {
146     my $v = { login => 'ftp' };
147     my $r = { server => {} };
148     my $got = get_health_check_id_str($v, $r);
149     is $got, '::::::::::ftp::::::::', 'get_health_check_id_str - set login';
150 }
151 {
152     my $v = { passwd => 'ftp' };
153     my $r = { server => {} };
154     my $got = get_health_check_id_str($v, $r);
155     is $got, ':::::::::::ftp:::::::', 'get_health_check_id_str - set passwd';
156 }
157 {
158     my $v = { database => 'template' };
159     my $r = { server => {} };
160     my $got = get_health_check_id_str($v, $r);
161     is $got, '::::::::::::template::::::', 'get_health_check_id_str - set database';
162 }
163 {
164     my $v = { customcheck => '/bin/echo' };
165     my $r = { server => {} };
166     my $got = get_health_check_id_str($v, $r);
167     is $got, ':::::::::::::/bin/echo:::::', 'get_health_check_id_str - set customcheck';
168 }
169 {
170     my $v = { checkinterval => 1 };
171     my $r = { server => {} };
172     my $got = get_health_check_id_str($v, $r);
173     is $got, '::::::::::::::1::::', 'get_health_check_id_str - set checkinterval';
174 }
175 {
176     my $v = { checkcount => 1 };
177     my $r = { server => {} };
178     my $got = get_health_check_id_str($v, $r);
179     is $got, ':::::::::::::::1:::', 'get_health_check_id_str - set checkcount';
180 }
181 {
182     my $v = { checktimeout => 1 };
183     my $r = { server => {} };
184     my $got = get_health_check_id_str($v, $r);
185     is $got, '::::::::::::::::1::', 'get_health_check_id_str - set checktimeout';
186 }
187 {
188     my $v = { negotiatetimeout => 1 };
189     my $r = { server => {} };
190     my $got = get_health_check_id_str($v, $r);
191     is $got, ':::::::::::::::::1:', 'get_health_check_id_str - set negotiatetimeout';
192 }
193 {
194     my $v = { retryinterval => 1 };
195     my $r = { server => {} };
196     my $got = get_health_check_id_str($v, $r);
197     is $got, '::::::::::::::::::1', 'get_health_check_id_str - set retryinterval';
198 }
199 {
200     my $v = {
201         checkport        => 10,
202         checktype        => 'connect',
203         service          => 'http',
204         protocol         => 'tcp',
205         num_connects     => 2,
206         httpmethod       => 'GET',
207         virtualhost      => 'virtual',
208         login            => 'ftp',
209         passwd           => 'ftp',
210         database         => 'template',
211         customcheck      => '/bin/echo',
212         checkinterval    => 3,
213         checkcount       => 4,
214         checktimeout     => 5,
215         negotiatetimeout => 6,
216         retryinterval    => 7,
217     };
218     my $r = {
219         server  => { ip => 1, port => 1 },
220         request => '/',
221         receive => 'ok',
222     };
223     my $got = get_health_check_id_str($v, $r);
224     is $got, '1:10:connect:http:tcp:2:/:ok:GET:virtual:ftp:ftp:template:/bin/echo:3:4:5:6:7',
225             'get_health_check_id_str - set all';
226 }
227 #   - get_virtual_id_str
228 {
229     my $v = undef;
230     my $got = get_virtual_id_str($v);
231     is $got, undef, 'get_virtual_id_str - v is undef';
232 }
233 {
234     my $v = {};
235     my $got = get_virtual_id_str($v);
236     is $got, undef, 'get_virtual_id_str - v->{module} is undef';
237 }
238 {
239     my $v = { module => {} };
240     my $got = get_virtual_id_str($v);
241     is $got, '::', 'get_virtual_id_str - arg is ok(empty)';
242 }
243 {
244     my $v = { module => {}, protocol => 'tcp' };
245     my $got = get_virtual_id_str($v);
246     is $got, 'tcp::', 'get_virtual_id_str - set protocol';
247 }
248 {
249     my $v = { module => {}, server => { ip => '127.0.0.1', port => 80 } };
250     my $got = get_virtual_id_str($v);
251     is $got, ':127.0.0.1:80:', 'get_virtual_id_str - set server';
252 }
253 {
254     my $v = { module => { name => 'cinsert', key => '--cookie-name monkey' } };
255     my $got = get_virtual_id_str($v);
256     is $got, '::cinsert --cookie-name monkey', 'get_virtual_id_str - set module';
257 }
258 {
259     my $v = {
260         server   => { ip => '127.0.0.1', port => 80 },
261         module   => { name => 'cinsert', key => '--cookie-name monkey' },
262         protocol => 'tcp',
263     };
264     my $got = get_virtual_id_str($v);
265     is $got, 'tcp:127.0.0.1:80:cinsert --cookie-name monkey', 'get_virtual_id_str - set all';
266 }
267 #   - get_forward_flag
268 {
269     my $got = get_forward_flag();
270     is $got, '', 'get_forward_flag - forward is undef';
271 }
272 {
273     my $got = get_forward_flag('MAsq');
274     is $got, '-m', 'get_forward_flag - forward is masq';
275 }
276 {
277     my $got = get_forward_flag('MAsqs');
278     is $got, '', 'get_forward_flag - forward is otherwise';
279 }
280 # test end
281 #...............................................
282
283 L7lib::comment_in();
284
285 sub set_default {
286 }
287 sub override {
288     *ld_log = \&__ld_log;
289 }
290 sub __ld_log {
291 }