test md5_get on symlinks
[cloudstore.git] / t / Gearman.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 60;
6 use Data::Dump qw(dump);
7
8 use lib 'lib';
9
10 use_ok 'CloudStore::Gearman';
11
12 my $hostname = `hostname -s`;
13 chomp $hostname;
14 $hostname =~ s/-dev// && warn "# fix hostname for development";
15
16 sub gearman {
17         my ( $function, $args ) = @_;
18         ok( my $o = CloudStore::Gearman->gearman_do( @_ ), join(' ',@_) );
19         diag "result = ",dump $o if $ENV{DEBUG};
20         return $o;
21 }
22
23 sub file_exists($) {
24         my ( $file ) = @_;
25         CloudStore::Gearman->gearman_do( file_size => $file );
26 }
27
28 sub test_refresh_file_list {
29         my ( $login ) = @_;
30         ok ! file_exists( "~$login/.meta/files" ), "refresh_file_list removed .meta/files";
31         gearman list_files => "~$login";
32 }
33
34 my $quota = 5 * 1024 * 1024;
35
36 my $u1 = gearman 'create_user' => 'test@example.com secret ' . $quota;
37 like $u1, qr/u\d+/, "login $u1";
38 my $u2 = gearman 'create_user' => 'test2@example.com secret ' . ($quota + 1);
39 like $u2, qr/u\d+/, "login $u2";
40 my $u3 = gearman 'create_user' => 'slice2@example.com secret ' . ($quota + 2);
41 like $u3, qr/u\d+/, "login $u2 (on other slice)";
42
43 foreach my $login ( $u1, $u2, $u3 ) {
44         my $o = gearman $hostname . '_s1_quota_get' => $login;
45         diag dump $o;
46         cmp_ok $o, '=~', qr/^(\d+)\s+(\d+)$/, 'format: used quota';
47 }
48
49 gearman( 'user_usage' => $u1 );
50
51 gearman( $hostname . '_s1_torrent_list' );
52
53 diag "create .meta/files if it doesn't exist";
54 foreach my $login ( $u1, $u2 ) {
55         gearman list_files => "~$login";
56         ok file_exists "~$login/.meta/files", "file_exists ~$login/.meta/files";
57 }
58
59 if ( ! file_exists("~$u1/foo.txt") ) {
60         diag "create ~$u1/foo.txt";
61         $ENV{RSYNC_PASSWORD} ||= 'secret';
62         system('rsync', '-vv', $0, "rsync://$u1\@127.0.0.1:6501/$u1/" );
63         gearman rename_file => "~$u1/Gearman.t#foo.txt";
64 } else {
65         file_exists("~$u1/foo.txt") || BAIL_OUT("~$u1/foo.txt is required for rest of tests");
66 }
67
68 my $md5 = gearman( 'md5_get' => "~$u1/foo.txt" );
69 diag "md5_get = $md5";
70
71 gearman send_file => "~$u1/foo.txt#~$u2/dir with space/file with space";
72 test_refresh_file_list "$u2";
73
74 cmp_ok gearman( 'md5_get' => "~$u2/dir with space/file with space" ), 'eq', $md5, "md5_get on send_file to $u2";
75
76 my $files = gearman list_files => "~$u2";
77 like $files, qr|dir with space/file with space|, 'found sent file in list_files';
78
79 gearman send_file => "~$u2/dir with space/file with space#~$u3/another dir with spaces/and file with spaces";
80 test_refresh_file_list "$u3";
81
82 cmp_ok gearman( 'md5_get' => "~$u3/another dir with spaces/and file with spaces" ), 'eq', $md5, "md5_get on send_file to $u3";
83
84 gearman delete => "~$u2/dir with space/file with space";
85 test_refresh_file_list "$u2";
86
87 gearman delete => "~$u3/another dir with spaces/and file with spaces";
88 test_refresh_file_list "$u3";
89
90 gearman send_file => "~$u1/foo.txt#~$u2/foo.txt";
91 test_refresh_file_list "$u2";
92
93 gearman rename_file => "~$u2/foo.txt#bar.txt";
94 test_refresh_file_list "$u2";
95 gearman rename_file => "~$u2/bar.txt#baz.txt";
96 test_refresh_file_list "$u2";
97
98 cmp_ok  
99 gearman( file_size => "~$u1/foo.txt" ), '==',
100 gearman( file_size => "~$u2/baz.txt" ), 'size_same';
101
102 diag "test symlink to other shard";
103 gearman send_file => "~$u1/foo.txt#~$u3/foo.txt";
104 cmp_ok 
105 gearman( file_size => "~$u1/foo.txt" ), '==',
106 gearman( file_size => "~$u3/foo.txt" ), 'cross-slice symlink size same';
107 test_refresh_file_list "$u3";
108
109 gearman delete => "~$u2/baz.txt";
110 test_refresh_file_list "$u2";
111
112 ok ! file_exists "~$u2/baz.txt", 'deleted file size';
113