1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#!/usr/local/bin/perl
#
# Test suite for the admin functions of DBD::mSQL and DBD::mysql.
#
#
# Make -w happy
#
$test_dsn = $test_user = $test_password = $verbose = '';
$| = 1;
#
# Include lib.pl
#
$DBI::errstr = ''; # Make -w happy
require DBI;
$mdriver = "";
foreach $file ("lib.pl", "t/lib.pl", "DBD-~DBD_DRIVER~/t/lib.pl") {
do "./$file"; if ($@) { print STDERR "Error while executing lib.pl: $@\n";
exit 10;
}
if ($mdriver ne '') {
last;
}
}
sub ServerError() {
print STDERR ("Cannot connect: ", $DBI::errstr, "\n",
"\tEither your server is not up and running or you have no\n",
"\tpermissions for acessing the DSN $test_dsn.\n",
"\tThis test requires a running server and write permissions.\n",
"\tPlease make sure your server is running and you have\n",
"\tpermissions, then retry.\n");
exit 10;
}
sub InDsnList($@) {
my($dsn, @dsnList) = @_;
my($d);
foreach $d (@dsnList) {
if ($d =~ /^dbi:[^:]+:$dsn\b/i) {
return 1;
}
}
0;
}
#
# Main loop; leave this untouched, put tests after creating
# the new table.
#
while (Testing()) {
# Check if the server is awake.
$dbh = undef;
Test($state or ($dbh = DBI->connect($test_dsn, $test_user,
$test_password)))
or ServerError();
Test($state or (@dsn = DBI->data_sources($mdriver)) >= 0);
if (!$state && $verbose) {
my $d;
print "List of $mdriver data sources:\n";
foreach $d (@dsn) {
print " $d\n";
}
print "List ends.\n";
}
my $drh;
Test($state or ($drh = DBI->install_driver($mdriver)))
or print STDERR ("Cannot obtain drh: " . $DBI::errstr);
#
# Check the ping method.
#
Test($state or $dbh->ping())
or ErrMsgF("Ping failed: %s.\n", $dbh->errstr);
if ($mdriver eq 'mSQL' or $mdriver eq 'mysql') {
my($testdsn) = "testaa";
my($testdsn1, $testdsn2);
my($accessDenied) = 0;
my($warning);
my($warningSub) = sub { $warning = shift };
if (!$state) {
while (InDsnList($testdsn, @dsn)) {
++$testdsn;
}
$testdsn1 = $testdsn;
++$testdsn1;
while (InDsnList($testdsn1, @dsn)) {
++$testdsn1;
}
$testdsn2 = $testdsn1;
++$testdsn2;
while (InDsnList($testdsn2, @dsn)) {
++$testdsn2;
}
$SIG{__WARN__} = $warningSub;
$warning = '';
if (!($result = $drh->func($testdsn, '_CreateDB'))
and ($drh->errstr =~ /(access|permission) denied/i)) {
$accessDenied = 1;
$result = 1;
}
$SIG{__WARN__} = 'DEFAULT';
}
Test($state or $result)
or print STDERR ("Error while executing _CreateDB: "
. $drh->errstr);
Test($state or ($warning =~ /deprecated/))
or print STDERR ("Expected warning, got '$warning'.\n");
Test($state or $accessDenied
or InDsnList($testdsn, DBI->data_sources($mdriver)))
or print STDERR ("New DB not in DSN list\n");
$SIG{__WARN__} = $warningSub;
$warning = '';
Test($state or $accessDenied
or $drh->func($testdsn, '_DropDB'))
or print STDERR ("Error while executing _DropDB: "
. $drh->errstr);
Test($state or $accessDenied or ($warning =~ /deprecated/))
or print STDERR ("Expected warning, got '$warning'\n");
$SIG{__WARN__} = 'DEFAULT';
Test($state or $accessDenied
or !InDsnList($testdsn, DBI->data_sources($mdriver)))
or print STDERR ("New DB not removed from DSN list\n");
my($mayShutdown) = $ENV{'DB_SHUTDOWN_ALLOWED'};
Test($state or $accessDenied
or $drh->func('createdb', $testdsn1, 'admin'))
or printf STDERR ("\$drh->admin('createdb') failed: %s\n",
$drh->errstr);
Test($state or $accessDenied
or InDsnList($testdsn1, DBI->data_sources($mdriver)))
or printf STDERR ("DSN $testdsn1 not in DSN list.\n");
Test($state or $accessDenied
or $drh->func('dropdb', $testdsn1, 'admin'))
or printf STDERR ("\$drh->admin('dropdb') failed: %s\n",
$drh->errstr);
Test($state or $accessDenied
or !InDsnList($testdsn1, DBI->data_sources($mdriver)))
or printf STDERR ("DSN $testdsn1 not removed from DSN list.\n");
Test($state or $accessDenied
or $drh->func('createdb', $testdsn2, 'admin'))
or printf STDERR ("\$drh->admin('createdb') failed: %s\n",
$drh->errstr);
Test($state or $accessDenied
or InDsnList($testdsn2, DBI->data_sources($mdriver)))
or printf STDERR ("DSN $testdsn2 not in DSN list.\n");
Test($state or $accessDenied
or $drh->func('dropdb', $testdsn2, 'admin'))
or printf STDERR ("\$drh->admin('dropdb') failed: %s\n",
$drh->errstr);
Test($state or $accessDenied
or !InDsnList($testdsn2, DBI->data_sources($mdriver)))
or printf STDERR ("DSN $testdsn2 not removed from DSN list.\n");
if ($mdriver eq 'mysql') {
#
# Try to do a shutdown.
#
Test($state or !$mayShutdown or $accessDenied
or $dbh->func("shutdown", "admin"))
or ErrMsgF("Cannot shutdown database: %s.\n", $dbh->errstr);
if (!$state) {
sleep 10;
}
#
# Pinging should fail now.
#
Test($state or !$mayShutdown or $accessDenied or !$dbh->ping())
or print STDERR ("Shutdown failed (ping succeeded)");
#
# Restart the database
#
if (!$state && $mayShutdown && !$accessDenied) {
if (fork() == 0) {
close STDIN;
close STDOUT;
close STDERR;
exec "safe_mysqld &";
}
}
sleep 5;
#
# Try DBD::mysql's automatic reconnect
#
Test($state or $dbh->ping())
or ErrMsgF("Reconnect failed: %s.\n", $dbh->errstr);
}
Test($state or $dbh->disconnect);
}
}
|