1# ==== Purpose ====
2#
3# Grep a file for a pattern, produce a single string out of the
4# matching lines, and assert that the string matches a given regular
5# expression.
6#
7# ==== Usage ====
8#
9# --let $assert_text= TEXT
10# --let $assert_file= FILE
11# --let $assert_select= REGEX
12# [--let $assert_match= REGEX | --let $assert_count= NUMBER]
13# [--let $assert_only_after= REGEX]
14# --source include/assert_grep.inc
15#
16# Parameters:
17#
18#   $assert_text
19#     Text that describes what is being checked. This text is written to
20#     the query log so it should not contain non-deterministic elements.
21#
22#   $assert_file
23#     File to search.
24#
25#   $assert_select
26#     All lines matching this text will be checked.
27#
28#   $assert_match
29#     The script will find all lines that match $assert_select,
30#     concatenate them to a long string, and assert that it matches
31#     $assert_match.
32#
33#   $assert_count
34#     Instead of asserting that the selected lines match
35#     $assert_match, assert that there were exactly $assert_count
36#     matching lines.
37#
38#   $assert_only_after
39#     Reset all the lines matched and the counter when finding this pattern.
40#     It is useful for searching things in the mysqld.err log file just
41#     after the last server restart for example (discarding the log content
42#     of previous server executions).
43
44
45if (!$assert_text)
46{
47  --die !!!ERROR IN TEST: you must set $assert_text
48}
49if (!$assert_file)
50{
51  --die !!!ERROR IN TEST: you must set $assert_file
52}
53if (!$assert_select)
54{
55  --die !!!ERROR IN TEST: you must set $assert_select
56}
57if ($assert_match == '')
58{
59  if ($assert_count == '')
60  {
61    --die !!!ERROR IN TEST: you must set either $assert_match or $assert_count
62  }
63}
64if ($assert_match != '')
65{
66  if ($assert_count != '')
67  {
68    --echo assert_text='$assert_text' assert_count='$assert_count'
69    --die !!!ERROR IN TEST: you must set only one of $assert_match or $assert_count
70  }
71}
72
73
74--let $include_filename= assert_grep.inc [$assert_text]
75--source include/begin_include_file.inc
76
77
78--let _AG_ASSERT_TEXT= $assert_text
79--let _AG_ASSERT_FILE= $assert_file
80--let _AG_ASSERT_SELECT= $assert_select
81--let _AG_ASSERT_MATCH= $assert_match
82--let _AG_ASSERT_COUNT= $assert_count
83--let _AG_OUT= `SELECT CONCAT('$MYSQLTEST_VARDIR/tmp/_ag_', UUID())`
84--let _AG_ASSERT_ONLY_AFTER= $assert_only_after
85
86
87--perl
88  use strict;
89  use warnings;
90  my $file= $ENV{'_AG_ASSERT_FILE'};
91  my $assert_select= $ENV{'_AG_ASSERT_SELECT'};
92  my $assert_match= $ENV{'_AG_ASSERT_MATCH'};
93  my $assert_count= $ENV{'_AG_ASSERT_COUNT'};
94  my $assert_only_after= $ENV{'_AG_ASSERT_ONLY_AFTER'};
95  my $out= $ENV{'_AG_OUT'};
96
97  my $result= '';
98  my $count= 0;
99  open(FILE, "$file") or die("Error $? opening $file: $!\n");
100  while (<FILE>) {
101    my $line = $_;
102    if ($assert_only_after && $line =~ /$assert_only_after/) {
103      $result = "";
104      $count = 0;
105    }
106    if ($line =~ /$assert_select/) {
107      if ($assert_count ne '') {
108        $count++;
109      }
110      else {
111        $result .= $line;
112      }
113    }
114  }
115  close(FILE) or die("Error $? closing $file: $!");
116  open OUT, "> $out" or die("Error $? opening $out: $!");
117  if ($assert_count ne '' && ($count != $assert_count)) {
118    print OUT ($count) or die("Error $? writing $out: $!");
119  }
120  elsif ($assert_count eq '' && $result !~ /$assert_match/) {
121    print OUT ($result) or die("Error $? writing $out: $!");
122  }
123  else {
124    print OUT ("assert_grep.inc ok");
125  }
126  close OUT or die("Error $? closing $out: $!");
127EOF
128
129
130--let $_ag_outcome= `SELECT LOAD_FILE('$_AG_OUT')`
131if ($_ag_outcome != 'assert_grep.inc ok')
132{
133  --source include/show_rpl_debug_info.inc
134  --echo include/assert_grep.inc failed!
135  --echo assert_text: '$assert_text'
136  --echo assert_file: '$assert_file'
137  --echo assert_select: '$assert_select'
138  --echo assert_match: '$assert_match'
139  --echo assert_count: '$assert_count'
140  --echo assert_only_after: '$assert_only_after'
141  if ($assert_match != '')
142  {
143    --echo matching lines: '$_ag_outcome'
144  }
145  if ($assert_count != '')
146  {
147    --echo number of matching lines: $_ag_outcome
148  }
149  --die assert_grep.inc failed.
150}
151
152
153--let $include_filename= include/assert_grep.inc [$assert_text]
154--source include/end_include_file.inc
155