1 package MogileFS
::Test
;
13 our @EXPORT = qw(&find_mogclient_or_skip &temp_store &create_mogstored &create_temp_tracker &try_for &want);
15 sub find_mogclient_or_skip
{
17 # needed for running "make test" from project root directory, with
18 # full svn 'mogilefs' repo checked out, without installing
19 # MogileFS::Client to normal system locations...
21 # then, second path is when running "make disttest", which is another
23 foreach my $dir ("$Bin/../../api/perl/MogileFS-Client/lib",
24 "$Bin/../../../api/perl/MogileFS-Client/lib",
28 $ENV{PERL5LIB
} = $dir . ($ENV{PERL5LIB
} ?
":$ENV{PERL5LIB}" : "");
31 unless (eval "use MogileFS::Client; 1") {
32 warn "Can't find MogileFS::Client: $@\n";
33 Test
::More
::plan
('skip_all' => "Can't find MogileFS::Client library, necessary for testing.");
36 unless (eval { TrackerHandle
::_mogadm_exe
() }) {
37 warn "Can't find mogadm utility $@\n";
38 Test
::More
::plan
('skip_all' => "Can't find mogadm executable, necessary for testing.");
45 my $type = $ENV{MOGTEST_DBTYPE
};
46 my $host = $ENV{MOGTEST_DBHOST
} || '';
47 my $port = $ENV{MOGTEST_DBPORT
} || '';
48 my $user = $ENV{MOGTEST_DBUSER
} || '';
49 my $pass = $ENV{MOGTEST_DBPASS
} || '';
50 my $name = $ENV{MOGTEST_DBNAME
} || '';
51 my $rootuser = $ENV{MOGTEST_DBROOTUSER
} || '';
52 my $rootpass = $ENV{MOGTEST_DBROOTPASS
} || '';
54 # default to mysql, but make sure DBD::MySQL is installed
57 eval "use DBD::mysql; 1" or
58 die "DBD::mysql isn't installed. Please install it or define MOGTEST_DBTYPE env. variable";
61 die "Bogus type" unless $type =~ /^\w+$/;
62 my $store = "MogileFS::Store::$type";
63 eval "use $store; 1;";
65 die "Failed to load $store: $@\n";
67 my %opts = ( dbhost
=> $host, dbport
=> $port,
68 dbuser
=> $user, dbpass
=> $pass,
70 $opts{dbrootuser
} = $rootuser unless $rootuser eq '';
71 $opts{dbrootpass
} = $rootpass unless $rootpass eq '';
72 my $sto = $store->new_temp(%opts);
78 sub create_temp_tracker
{
80 my $opts = shift || [];
83 my $whoami = `whoami`;
87 return IO
::Socket
::INET
->new(PeerAddr
=> "127.0.0.1:7001",
91 my $conn = $connect->();
92 die "Failed: tracker already running on port 7001?\n" if $conn;
95 exec("$Bin/../mogilefsd",
96 ($whoami eq "root" ?
"--user=root" : ()),
100 "--dbuser=" . $sto->user,
101 "--dbpass=" . $sto->pass,
108 return TrackerHandle
->new(pid
=> $pid);
115 sub create_mogstored
{
116 my ($ip, $root, $daemonize) = @_;
119 return IO
::Socket
::INET
->new(PeerAddr
=> "$ip:7500",
123 my $conn = $connect->();
124 die "Failed: tracker already running on port 7500?\n" if $conn;
125 $ENV{PERL5LIB
} .= ":$Bin/../lib";
126 my @args = ("$Bin/../mogstored",
128 "--httplisten=$ip:7500",
129 "--mgmtlisten=$ip:7501",
130 "--maxconns=1000", # because we're not root, put it below 1024
135 # don't set pid. since our fork fid would just
136 # go away, once perlbal daemonized itself.
137 push @args, "--daemonize";
138 system(@args) and die "Failed to start daemonized mogstored.";
141 die "failed to fork: $!" unless defined $pid;
149 return MogstoredHandle
->new(pid
=> $pid, ip
=> $ip, root
=> $root);
151 select undef, undef, undef, 0.25;
157 my ($tries, $code) = @_;
159 return 1 if $code->();
166 my ($admin, $count, $jobclass) = @_;
167 my $req = "!want $count $jobclass\r\n";
169 syswrite($admin, $req) or die "syswrite: $!\n";
172 if ($r =~ /Now desiring $count children doing '$jobclass'/ && <$admin> eq ".\r\n") {
176 syswrite($admin, "!jobs\r\n");
177 MogileFS
::Util
::wait_for_readability
(fileno($admin), 10);
180 if ($line =~ /\A$jobclass count (\d+)/) {
183 last if $line eq ".\r\n";
187 return 1 if $rcount == $count;
188 die "got $jobclass count $rcount (expected=$count)\n";
190 die "got bad response for $req: $r\n";
193 ############################################################################
194 package ProcessHandle
;
196 my ($class, %args) = @_;
197 bless \
%args, $class;
200 sub pid
{ return $_[0]{pid
} }
204 return unless $self->{pid
};
205 kill 15, $self->{pid
};
209 ############################################################################
211 package TrackerHandle
;
212 use base
'ProcessHandle';
216 return "127.0.0.1:7001";
219 my $_mogadm_exe_cache;
221 return $_mogadm_exe_cache if $_mogadm_exe_cache;
222 for my $dir ("$FindBin::Bin/../../utils",
223 "$FindBin::Bin/../../../utils",
224 split(/:/, $ENV{PATH
}),
230 my $exe = $dir . '/mogadm';
231 return $_mogadm_exe_cache = $exe if -x
$exe;
233 die "mogadm executable not found.\n";
238 my $rv = system(_mogadm_exe
(), "--trackers=" . $self->ipport, @_);
242 ############################################################################
243 package MogstoredHandle
;
244 use base
'ProcessHandle';
246 # this space intentionally left blank. all in super class for now.
248 ############################################################################
251 my ($class, $url) = @_;
259 my ($host1) = $self->{url
} =~ m!^http://(.+:\d+)!;
265 my ($dev) = $self->{url
} =~ m!dev(\d+)!;
271 my $path = $self->{url
};
272 $path =~ s!^http://(.+:\d+)!!;