Debian Codegraph @

mysql-test/suite/rpl/extension/bhs.pl

raw
1 #!/usr/bin/perl
2
3 use File::Basename;
4 use File::Copy qw(copy);
5 use File::Spec qw(catdir);
6 use File::Path;
7 use IO::File;
8 use strict;
9
10 # Constants
11 my $case_header= "###############################################################################\n"
12 . "# Note! The test case updated for running under blackhole slave configuration #\n"
13 . "###############################################################################\n\n";
14 my $before_replace= "# *** BHS ***\n";
15 my $after_replace= "# *** /BHS ***\n";
16 my %copy_dirs= (
17 "include" => "include",
18 "extra" => "extra"
19 );
20
21 # Variables
22 my %test_dirs;
23 my @update_test_cases;
24 my %rules;
25 my $opt_trans_test_list;
26
27 print "Creating suite rpl_bhs\n";
28
29 # *** Set platform-independent pathes ***
30
31 # Set extension directory
32 my $ext_dir= dirname(File::Spec->rel2abs($0));
33 # Set bhs directory
34 my $bhs_dir= File::Spec->catdir($ext_dir, "bhs");
35 # Set mysql-test directory
36 my $mysql_test_dir= $ext_dir;
37 $mysql_test_dir =~ s/(\/|\\)suite(\/|\\)rpl(\/|\\)extension$//;
38 # Set path to mtr
39 my $mtr_script = File::Spec->catdir($mysql_test_dir, "mysql-test-run.pl");
40 # Set directory of rpl suite
41 my $suite_rpl_dir = File::Spec->catdir($mysql_test_dir, "suite", "rpl");
42 # Set directory of rpl_bhs suite
43 my $suite_rpl_bhs_dir = File::Spec->catdir($mysql_test_dir, "suite", "rpl_bhs");
44 # Set test cases mask with path
45 my $suite_rpl_bhs_cases_dir = File::Spec->catdir($suite_rpl_bhs_dir, "t");
46
47 # Check first argument
48 if ($ARGV[0] =~ m/\-\-trans\-test\-list=(.+)/i)
49 {
50 $opt_trans_test_list= File::Spec->catdir($suite_rpl_bhs_dir, $1);
51 shift @ARGV;
52 $mtr_script= "perl " . $mtr_script . " " . join(" ", @ARGV);
53 }
54 else
55 {
56 die("First argument of bhs.pl must be --trans-test-list with path to test case list");
57 }
58
59 # *** Copy files ***
60
61 # Copy rpl suite into rpl_bhs
62 print "copying:\n $suite_rpl_dir\n --> $suite_rpl_bhs_dir\n";
63 dircopy($suite_rpl_dir, $suite_rpl_bhs_dir);
64
65 # Copy additional dirs outside of rpl suite
66 foreach my $cur_dir (keys %copy_dirs)
67 {
68 my $from_dir= File::Spec->catdir($mysql_test_dir, $cur_dir);
69 my $to_dir= File::Spec->catdir($suite_rpl_bhs_dir, $copy_dirs{$cur_dir});
70 print " $from_dir\n --> $to_dir\n";
71 dircopy($from_dir, $to_dir);
72 }
73
74 # Copy server config files
75 print " configuration files\n";
76 copy(File::Spec->catdir($ext_dir, "bhs", "my.cnf"), $suite_rpl_bhs_dir);
77 copy(File::Spec->catdir($ext_dir, "bhs", "rpl_1slave_base.cnf"), $suite_rpl_bhs_dir);
78
79 # Add BHS disabled.def
80 print "updating disabled.def\n";
81 my $fh = new IO::File File::Spec->catdir($bhs_dir, "disabled.def"), "r";
82 if (defined $fh) {
83 my @disabled = <$fh>;
84 undef $fh;
85 my $fh = new IO::File File::Spec->catdir($suite_rpl_bhs_dir, "t", "disabled.def"), O_WRONLY|O_APPEND;
86 if (defined $fh) {
87 print $fh join ("", @disabled);
88 undef $fh;
89 }
90 }
91
92
93 # *** Update test cases
94
95 # Read update_rules
96 my $fh = new IO::File File::Spec->catdir($bhs_dir, "update_test_cases"), "r";
97 if (defined $fh) {
98 @update_test_cases = <$fh>;
99 undef $fh;
100 }
101
102 foreach my $update (@update_test_cases)
103 {
104 $update =~ s/\s//g;
105 my ($tmpl, $file)= split(/\:/, $update);
106 $file= File::Spec->catdir($bhs_dir, $file);
107 $fh = new IO::File $file, "r";
108 if (defined $fh)
109 {
110 my @lines= <$fh>;
111 undef $fh;
112 my $found= "";
113 my $replace= "";
114 my $line_num= 0;
115 foreach my $line (@lines)
116 {
117 if ($line =~ m/^\s*\[(.+)\]\s*$/ && $found eq "")
118 {
119 $found= $1;
120 }
121 elsif ($line =~ m/^\s*\[(.+)\]\s*$/ && $found ne "")
122 {
123 $rules{$tmpl}{$found} = $replace;
124 chomp $rules{$tmpl}{$found};
125 $found= $1;
126 $replace= "";
127 $line_num= 0;
128 }
129 elsif ($line !~ m/^\s*$/)
130 {
131 $replace .= $line;
132 $line_num++;
133 }
134 }
135 if ($found ne "")
136 {
137 $rules{$tmpl}{$found}= $replace;
138 }
139 }
140 }
141
142 for (my $i = 0; $i < scalar(@update_test_cases); $i++)
143 {
144 if ($update_test_cases[$i] =~ m/(.+)\:.+/)
145 {
146 $update_test_cases[$i]= $1;
147 my @cur_path= split(/\//, $update_test_cases[$i]);
148 $update_test_cases[$i]= File::Spec->catdir(@cur_path);
149 # Collect directories with test cases
150 pop(@cur_path);
151 $test_dirs{File::Spec->catdir(@cur_path)}= 1;
152 }
153 }
154
155 # Updating test cases
156 my $case_num= 0;
157 foreach my $test_dir (keys %test_dirs)
158 {
159 # Read list of test cases
160 my $cur_path= File::Spec->catdir($suite_rpl_bhs_dir, $test_dir);
161 opendir(my $dh, $cur_path) or exit(1);
162 my @cases = grep(/\.(test|inc)$/,readdir($dh));
163 closedir($dh);
164 foreach my $case (sort @cases)
165 {
166 my $case2= File::Spec->catdir($test_dir, $case);
167 foreach my $update_case (@update_test_cases)
168 {
169 my @paths= split(/\//, $update_case);
170 my $update_case2= File::Spec->catdir(@paths);
171 if (compare_names($case2, $update_case2) == 1)
172 {
173 $fh = new IO::File File::Spec->catdir($cur_path, $case), "r";
174 my @lines;
175 if (defined $fh)
176 {
177 @lines = <$fh>;
178 undef $fh;
179 }
180 my $content= "";
181 foreach my $line (@lines)
182 {
183 foreach my $cmd (keys %{$rules{$update_case}})
184 {
185 if ($line =~ m/$cmd/i)
186 {
187 my $orig_line= "# Replaced command: " . $line;
188 $line =~ s/$cmd/$rules{$update_case}{$cmd}/;
189 $line =~ s/\n\n$/\n/;
190 $line = $before_replace . $orig_line . $line . $after_replace;
191 last;
192 }
193 }
194 $content .= $line;
195 }
196 $fh = new IO::File File::Spec->catdir($cur_path, $case), "w";
197 if (defined $fh)
198 {
199 print $fh $case_header . $content;
200 undef $fh;
201 }
202 $case_num++;
203 last;
204 }
205 }
206 }
207 }
208
209 print "updated $case_num files\n";
210
211 print "Run $mtr_script\n";
212
213 system( $mtr_script );
214
215 sub compare_names
216 {
217 my ($test, $rule)= @_;
218 my $res= 0;
219 $res= 1 if ($test eq $rule);
220 if ($rule =~ m/\*/)
221 {
222 $rule =~ s/(\\|\/)+/\ /g;
223 $rule =~ s/\*/\.\*/g;
224 $test =~ s/(\\|\/)+/\ /g;
225 $res= 1 if ($test =~ m/^$rule$/i)
226 }
227 return $res;
228 }
229
230 sub dircopy
231 {
232 my ($from_dir, $to_dir)= @_;
233 mkdir $to_dir if (! -e $to_dir);
234 opendir my($dh), $from_dir or die "Could not open dir '$from_dir': $!";
235 for my $entry (readdir $dh)
236 {
237 next if $entry =~ /^(\.|\.\.)$/;
238 my $source = File::Spec->catdir($from_dir, $entry);
239 my $destination = File::Spec->catdir($to_dir, $entry);
240 if (-d $source)
241 {
242 mkdir $destination or die "mkdir '$destination' failed: $!" if not -e $destination;
243 dircopy($source, $destination);
244 }
245 else
246 {
247 copy($source, $destination) or die "copy '$source' to '$destination' failed: $!";
248 }
249 }
250 closedir $dh;
251 return;
252 }