test more of Gearman API
[cloudstore.git] / t / Gearman.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 43;
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
15 my $user = $ARGV[0] || 2001;
16
17 sub gearman {
18         my ( $function, $args ) = @_;
19         ok( my $o = CloudStore::Gearman->gearman_do( @_ ), join(' ',@_) );
20         diag "result = ",dump $o if $ENV{DEBUG};
21         return $o;
22 }
23
24 sub file_exists($) {
25         my ( $file ) = @_;
26         CloudStore::Gearman->gearman_do( file_size => $file );
27 }
28
29 sub test_refresh_file_list {
30         my ( $login ) = @_;
31         ok ! file_exists( "~$login/.meta/files" ), "refresh_file_list removed .meta/files";
32         gearman list_files => "~$login";
33 }
34
35 my $u1 = gearman 'create_user' => 'test@example.com secret 0';
36 like $u1, qr/u\d+/, "login $u1";
37 my $u2 = gearman 'create_user' => 'test2@example.com secret 0';
38 like $u2, qr/u\d+/, "login $u2";
39 my $u3 = gearman 'create_user' => 'slice2@example.com secret 0';
40 like $u3, qr/u\d+/, "login $u2 (on other slice)";
41
42 my $o = gearman $hostname . '_s1_quota_get' => $user;
43 diag dump $o;
44 cmp_ok $o, '=~', qr/^(\d+)\s+(\d+)$/, 'format: used quota';
45
46 gearman( 'user_usage' => $user );
47
48 gearman( $hostname . '_s1_torrent_list' );
49
50 diag "create .meta/files if it doesn't exist";
51 foreach my $login ( $u1, $u2 ) {
52         gearman list_files => "~$login";
53         ok file_exists "~$login/.meta/files", "file_exists ~$login/.meta/files";
54 }
55
56 file_exists "~$u1/foo.txt" || BAIL_OUT("~$u1/foo.txt is required for rest of tests");
57
58 gearman send_file => "~$u1/foo.txt#~$u2/dir with space/file with space";
59 test_refresh_file_list "$u2";
60
61 gearman delete => "~$u2/dir with space/file with space";
62 test_refresh_file_list "$u2";
63
64 gearman send_file => "~$u1/foo.txt#~$u2/foo.txt";
65 test_refresh_file_list "$u2";
66
67 gearman rename_file => "~$u2/foo.txt#bar.txt";
68 test_refresh_file_list "$u2";
69 gearman rename_file => "~$u2/bar.txt#baz.txt";
70 test_refresh_file_list "$u2";
71
72 cmp_ok  
73 gearman( file_size => "~$u1/foo.txt" ), '==',
74 gearman( file_size => "~$u2/baz.txt" ), 'size_same';
75
76 diag "test symlink to other shard";
77 gearman send_file => "~$u1/foo.txt#~$u3/foo.txt";
78 cmp_ok 
79 gearman( file_size => "~$u1/foo.txt" ), '==',
80 gearman( file_size => "~$u3/foo.txt" ), 'cross-slice symlink size same';
81 test_refresh_file_list "$u3";
82
83 gearman delete => "~$u2/baz.txt";
84 test_refresh_file_list "$u2";
85
86 ok ! file_exists "~$u2/baz.txt", 'deleted file size';
87