9 use MogileFS::Util qw(error_code);
12 find_mogclient_or_skip();
14 # use mogadm to init it,
15 # mogstored on temp dir,
16 # register mogstored temp dir,
21 my $sto = eval { temp_store(); };
25 plan skip_all => "Can't create temporary test database: $@";
32 my ($hostA_ip, $hostB_ip, $hostC_ip) = (qw/127.0.1.1 127.0.1.2 127.0.1.3/);
36 $mogroot{1} = File::Temp::tempdir( CLEANUP => 1 );
37 $mogroot{2} = File::Temp::tempdir( CLEANUP => 1 );
38 $mogroot{3} = File::Temp::tempdir( CLEANUP => 1 );
39 my $dev2host = { 1 => 1, 2 => 1,
42 foreach (sort { $a <=> $b } keys %$dev2host) {
43 my $root = $mogroot{$dev2host->{$_}};
44 mkdir("$root/dev$_") or die "Failed to create dev$_ dir: $!";
47 my $ms1 = create_mogstored($hostA_ip, $mogroot{1});
48 ok($ms1, "got mogstored1");
49 my $ms2 = create_mogstored($hostB_ip, $mogroot{2});
50 ok($ms2, "got mogstored2");
52 while (! -e "$mogroot{1}/dev1/usage" &&
53 ! -e "$mogroot{2}/dev4/usage") {
54 print "Waiting on usage...\n";
58 my $tmptrack = create_temp_tracker($sto);
61 my $mogc = MogileFS::Client->new(
63 hosts => [ "127.0.0.1:7001" ],
65 my $be = $mogc->{backend}; # gross, reaching inside of MogileFS::Client
67 my $lasttime = 1167609600; # Mon Jan 1 00:00:00 UTC 2007
69 my $timestamp = $dbh->selectrow_array("SELECT ".$sto->unix_timestamp);
70 # FIXME: Some databases might be pedantic about the FROM
71 # but having it on others means that if the table has no rows
72 # we won't get any results!
73 my $rv = $timestamp > $lasttime;
74 $lasttime = $timestamp;
76 }), "Store provides sane unix_timestamp");
78 # test some basic commands to backend
79 ok($be->do_request("test", {}), "test ping worked");
80 ok(!$be->do_request("test", {crash => 1}), "crash didn't");
81 ok($be->do_request("test", {}), "test ping again worked");
84 ok($tmptrack->mogadm("domain", "add", "todie"), "created todie domain");
85 ok($tmptrack->mogadm("domain", "delete", "todie"), "delete todie domain");
86 ok(!$tmptrack->mogadm("domain", "delete", "todie"), "didn't delete todie domain again");
88 ok($tmptrack->mogadm("domain", "add", "testdom"), "created test domain");
89 ok($tmptrack->mogadm("class", "add", "testdom", "1copy", "--mindevcount=1"), "created 1copy class in testdom");
90 ok($tmptrack->mogadm("class", "add", "testdom", "2copies", "--mindevcount=2"), "created 2copies class in testdom");
91 ok($tmptrack->mogadm("class", "add", "testdom", "poltest", "--replpolicy=MultipleHosts(3)"),
92 "created a specific policy class");
94 ok($tmptrack->mogadm("host", "add", "hostA", "--ip=$hostA_ip", "--status=alive"), "created hostA");
95 ok($tmptrack->mogadm("host", "add", "hostB", "--ip=$hostB_ip", "--status=alive"), "created hostB");
97 ok($tmptrack->mogadm("device", "add", "hostA", 1), "created dev1 on hostA");
98 ok($tmptrack->mogadm("device", "add", "hostA", 2), "created dev2 on hostA");
99 ok($tmptrack->mogadm("device", "add", "hostB", 3), "created dev3 on hostB");
100 ok($tmptrack->mogadm("device", "add", "hostB", 4), "created dev4 on hostB");
102 #ok($tmptrack->mogadm("device", "mark", "hostA", 1, "alive"), "dev1 alive");
103 #ok($tmptrack->mogadm("device", "mark", "hostA", 2, "alive"), "dev2 alive");
104 #ok($tmptrack->mogadm("device", "mark", "hostB", 3, "alive"), "dev3 alive");
105 #ok($tmptrack->mogadm("device", "mark", "hostB", 4, "alive"), "dev4 alive");
109 my $was = $be->{timeout}; # can't use local on phash :(
111 ok($be->do_request("do_monitor_round", {}), "waited for monitor")
112 or die "Failed to wait for monitor";
113 ok($be->do_request("do_monitor_round", {}), "waited for monitor")
114 or die "Failed to wait for monitor";
115 $be->{timeout} = $was;
119 my $fh = $mogc->new_file('no_content', "2copies");
120 die "Error: " . $mogc->errstr unless $fh;
121 ok(close($fh), "closed file");
125 my $fh = $mogc->new_file('no_content', "2copies");
126 die "Error: " . $mogc->errstr unless $fh;
127 ok(close($fh), "closed file");
130 # wait for it to replicate
132 my @urls = $mogc->get_paths("no_content");
135 diag("no_content still only on $nloc devices");
139 }), "replicated to 2 paths");
142 my $to_repl_rows = $dbh->selectrow_array("SELECT COUNT(*) FROM file_to_replicate");
143 return $to_repl_rows == 0;
144 }), "no more files to replicate");
147 ok($mogc->delete("no_content"), "deleted no_content")
148 or die "Error: " . $mogc->errstr;
150 # create two sample files
151 my $data = "My test file.\n" x 1024;
152 foreach my $k (qw(file1 file2)) {
153 my $fh = $mogc->new_file($k, "2copies");
154 ok($fh, "got filehandle") or
155 die "Error: " . $mogc->errstr;
157 ok(close($fh), "closed file");
161 ok($mogc->delete("file2"), "deleted file2")
162 or die "Error: " . $mogc->errstr;
164 # verify we can't delete the domain now
165 ok(!$tmptrack->mogadm("domain", "delete", "testdom"), "can't delete domain in use");
167 # wait for it to replicate
170 @urls = $mogc->get_paths("file1");
173 diag("file1 still only on $nloc devices");
177 }), "replicated to 2 paths");
180 my $to_repl_rows = $dbh->selectrow_array("SELECT COUNT(*) FROM file_to_replicate");
181 return $to_repl_rows == 0;
182 }), "no more files to replicate");
184 my $p1 = MogPath->new($urls[0]);
185 my $p2 = MogPath->new($urls[1]);
186 isnt($p1->host, $p2->host, "host1 and host2 are different");
187 my $path1 = $mogroot{$dev2host->{$p1->device}} . $p1->path;
188 my $path2 = $mogroot{$dev2host->{$p2->device}} . $p2->path;
189 is(-s $path1, length($data), "right length on disk for path1");
190 is(-s $path2, length($data), "right length on disk for path2");
192 ok(unlink($path1), "deleted path $path1");
193 my $dead_url = $urls[0];
195 @urls = $mogc->get_paths("file1");
196 isnt($urls[0], $dead_url, "didn't return dead url first (try $_)");
199 # Tests for updateclass command
201 my $fh = $mogc->new_file('file1copy', "1copy");
202 ok($fh, "got filehandle") or
203 die "Error: " . $mogc->errstr;
204 print $fh 'EXAMPLE DATA';
205 ok(close($fh), "closed file");
207 is scalar($mogc->get_paths("file1copy")), 1, 'File is on 1 device';
209 $mogc->update_class('2copies');
211 # wait for it to replicate
213 my @urls = $mogc->get_paths("file1copy");
216 diag("no_content still only on $nloc devices");
220 }), "replicated to 2 paths");
222 ok($mogc->delete("file1copy"), "deleted updateclass testfile file1copy")
223 or die "Error: " . $mogc->errstr;
226 ok($be->do_request("rename", {
228 to_key => "file1renamed",
230 }), "renamed file1 to file1renamed");
232 ok($be->do_request("delete", {
233 key => "file1renamed",
235 }), "deleted file1renamed");
237 # create a couple hundred files now
239 diag("Creating $n_files files...");
240 for my $n (1..$n_files) {
241 my $fh = $mogc->new_file("manyhundred_$n", "2copies")
242 or die "Failed to create manyhundred_$n: " . $mogc->errstr;
243 my $data = "File number $n.\n" x 512;
245 close($fh) or die "Failed to close manyhundred_$n";
246 diag("created $n/$n_files") if $n % 10 == 0;
248 pass("Created a ton of files");
250 # wait for replication to go down
256 $to_repl_rows = $dbh->selectrow_array("SELECT COUNT(*) FROM file_to_replicate");
257 last if ! $to_repl_rows;
258 diag("Files to replicate: $to_repl_rows");
261 die "Failed to replicate all $n_files files" if $to_repl_rows;
262 pass("Replicated all $n_files files");
265 # now let's delete a host, which should fail hard, because there are still devices attached to it
267 die "Can't delete an active host" if
268 $tmptrack->mogadm("host", "delete", "hostB");
269 pass("didn't delete hostB");
272 # create a new host and device, for when we start killing some devices
273 my $ms3 = create_mogstored($hostC_ip, $mogroot{3});
274 ok($ms3, "got mogstored3");
275 ok($tmptrack->mogadm("host", "add", "hostC", "--ip=$hostC_ip", "--status=alive"), "created hostC");
276 ok($tmptrack->mogadm("device", "add", "hostC", 5), "created dev5 on hostC");
277 ok($tmptrack->mogadm("device", "add", "hostC", 6), "created dev6 on hostC");
279 # let it be discovered
280 sleep(5); # FIXME: make an explicit "rescan" or "remonitor" job to mogilefsd, just for test suite
282 ok($tmptrack->mogadm("device", "mark", "hostB", 3, "dead"), "marked device B/3 dead");
283 ok($tmptrack->mogadm("device", "mark", "hostB", 4, "dead"), "marked device B/4 dead");
287 my $sth = $dbh->prepare("SELECT devid, COUNT(*) FROM file_on GROUP BY devid");
289 while (my ($devid, $ct) = $sth->fetchrow_array) {
292 diag("Replication update: " . join(", ", map { "dev$_: " . sprintf("%3d", ($has{$_}||0)) } (1..6)));
293 return 0 if $has{3} || $has{4};
294 return $has{1} && $has{1} && $has{5} && $has{6};
295 }), "files replicated to hostC from hostB");
298 # hosts are no longer able to be nuked even if they have deleted devices.
299 # this saves us from some subtle bugs.
300 #ok($tmptrack->mogadm("host", "delete", "hostB"), "killed hostB");
302 # delete them all, see if they go away.
303 for my $n (1..$n_files) {
304 my $rv = $mogc->delete("manyhundred_$n")
305 or die "Failed to delete manyhundred_$n";
307 pass("deleted all $n_files files");
311 foreach my $hn (1, 3) {
312 my @lfiles = `find $mogroot{$hn} -type f -name '*.fid'`;
313 push @files, @lfiles;
314 diag("files on host $hn = " . scalar(@lfiles));
317 }), "and they're gone from filesystem");
319 foreach my $t (qw(file file_on file_to_delete)) {
321 return $dbh->selectrow_array("SELECT COUNT(*) FROM $t") == 0;
322 }), "table $t is empty");
325 # Test some broken client modes.
327 my $c = IO::Socket::INET->new(PeerAddr => '127.0.0.1:7001',
329 die "Failed to connect to test tracker" unless $c;
330 # Pretend to upload a file, then tell the server weird things.
331 # Not trying to be defensable to all sorts of things, but ensuring we're
332 # safe against double close, bad destdev, etc.
333 print $c "create_open "
334 . "domain=testdom&fid=0&class=&multi_dest=1&key=fufufu\n";
337 ok($res =~ m/fid=(\d+)/, "bare create_open worked");
339 # Pretend we uploaded something.
340 print $c "create_close "
341 . "domain=testdom&fid=$fidid&devid=4&size=0&key=fufufu"
342 . "&path=http://127.0.1.2:7500/dev4/0/000/000/0000000$fidid.fid\n";
344 ok($res2 =~ m/invalid_destdev/, "cannot upload to unlisted destdev");
346 # TODO: test double closing, etc.
350 my ($tries, $code) = @_;
352 return 1 if $code->();