better check of md5sum files
[cloudstore.git] / t / API.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 29;
6 use Data::Dump qw(dump);
7 use File::Path;
8
9 use lib 'lib';
10
11 use_ok 'CloudStore::API';
12
13 BEGIN { $ENV{SLICE} = '/rsync1/s1' }
14
15 ok my $o = CloudStore::API->new('s1'), 'new';
16
17 die 'You need to run this test as root' unless $> == 0;
18
19 cmp_ok $o->create_user('md5@example.com','md5sum',0), 'eq', 'md5', 'create_user md5';
20
21 ok my $info = $o->user_info( 'md5' ), 'user_info';
22 diag dump $info;
23
24 cmp_ok $info->{uid}, '==', 2000, 'uid';
25
26 ok my $uid = $o->create_user('test@example.com','password',100_000_000), 'create_user test';
27 diag "test: $uid";
28 ok my $test = $o->user_info( $uid ), "user_info $uid";
29 diag dump($test);
30
31 ok open(my $fh, ">", $test->{dir} . '/foo.txt'), 'open';
32 ok print($fh "test pid: $$\n"), 'print';
33 ok close($fh) , 'close';
34 ok chown $test->{uid}, $test->{gid}, "$test->{dir}/foo.txt", 'chown';
35
36 ok my $uid2 = $o->create_user('test2@example.com','password',100_000_000), 'create_user test2';
37 ok my $test2 = $o->user_info( $uid2 ), "user_info $uid2";
38
39 foreach ( $test->{dir}, $test2->{dir} ) {
40         $_ .= "/.log";
41         unlink $_ if -e $_;
42 }
43
44 sub usage {
45         ok my $u = $o->usage( $_[0] ), 'usage';
46 #       diag dump $u;
47 }
48
49 ok $o->send_file( $uid => '/foo.txt', $uid2 => 'dir1/dir2/bar.txt' ), 'send_file';
50 usage $uid;
51 usage $uid2;
52
53 ok $o->send_file( $uid2 => 'dir1/dir2/bar.txt', $uid => 'bar.txt' ), 'send_file back';
54 usage $uid;
55 usage $uid2;
56
57 ok $o->send_file( $uid2 => 'dir1/dir2/bar.txt', 2004 => 'bar.txt' ), 'send_file cross-slice';
58
59 ok $o->delete( $uid, 'foo.txt' );
60 usage $uid;
61
62 ok $o->delete( $uid2, 'dir1' );
63 usage $uid2;
64
65 ok $o->delete( $uid, 'bar.txt' );
66 usage $uid;
67
68 my $path = "/tmp/test.$$/base/dir/path";
69 ok $o->mkbasepath( $path, { uid => $info->{uid} } ), "mkbasepath $path";
70
71 ok ! $o->mkbasepath( $path, { uid => $info->{uid} } ), "exists $path";
72
73 $path =~ s{/path}{}; # strip file
74 my $uid = (stat($path))[4];
75 diag "uid $path = $uid";
76 cmp_ok $uid, '==', $info->{uid}, "owner $info->{uid}";
77
78 File::Path::remove_tree("/tmp/test.$$"); # cleanup
79