1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Test::More; |
7 |
use File::Find; |
8 |
use blib; |
9 |
|
10 |
eval "use DBD::Pg"; |
11 |
plan skip_all => "DBD::Pg required for testing" if $@; |
12 |
plan tests => 47; |
13 |
|
14 |
use_ok('DBI'); |
15 |
use_ok('Fuse::DBI'); |
16 |
|
17 |
my $test_db = 'test'; |
18 |
my $dsn = "DBI:Pg:dbname=$test_db"; |
19 |
my $mount = '/tmp/fuse_dbi_mnt'; |
20 |
|
21 |
ok((! -e $mount || rmdir $mount), "mount point $mount"); |
22 |
|
23 |
mkdir $mount || die "mkdir $mount: $!"; |
24 |
ok(-d $mount, "mkdir $mount"); |
25 |
|
26 |
ok(my $dbh = DBI->connect($dsn, , '', '', { RaiseError => 1 }), |
27 |
"connect fusedbi test database"); |
28 |
|
29 |
my $drop = eval { $dbh->do(qq{ drop table files }) }; |
30 |
diag "drop table files" if ($drop); |
31 |
|
32 |
ok($dbh->do(qq{ |
33 |
create table files ( |
34 |
name text primary key, |
35 |
data text |
36 |
) |
37 |
}), "create table files"); |
38 |
|
39 |
ok(my $sth_insert = $dbh->prepare(qq{ |
40 |
insert into files (name,data) values (?,?) |
41 |
}), "prepare insert"); |
42 |
|
43 |
ok(my $sth_select = $dbh->prepare(qq{ |
44 |
select data from files where name = ? |
45 |
}), "prepare select"); |
46 |
|
47 |
my @files = qw(file dir/file dir/subdir/file); |
48 |
my %file_data; |
49 |
|
50 |
foreach my $file (@files) { |
51 |
$file_data{$file} = ("this is test data on ".localtime()."\n") x length($file); |
52 |
ok($sth_insert->execute($file,$file_data{$file}), "insert $file"); |
53 |
} |
54 |
|
55 |
my $sql_filenames = qq{ |
56 |
select |
57 |
name as id, |
58 |
name as filename, |
59 |
length(data) as size, |
60 |
1 as writable |
61 |
from files |
62 |
}; |
63 |
|
64 |
my $sql_read = qq{ |
65 |
select data |
66 |
from files |
67 |
where name = ?; |
68 |
}; |
69 |
|
70 |
my $sql_update = qq{ |
71 |
update files |
72 |
set data = ? |
73 |
where name = ?; |
74 |
}; |
75 |
|
76 |
system "fusermount -q -u $mount" || diag "nothing mounted at $mount, ok"; |
77 |
|
78 |
my $mnt = Fuse::DBI->mount({ |
79 |
filenames => $sql_filenames, |
80 |
read => $sql_read, |
81 |
update => $sql_update, |
82 |
dsn => $dsn, |
83 |
mount => $mount, |
84 |
fork => 1, |
85 |
}); |
86 |
|
87 |
ok($mnt, "mount"); |
88 |
|
89 |
sub test_file { |
90 |
my $f = $File::Find::name; |
91 |
|
92 |
ok($f, "file $f"); |
93 |
|
94 |
return unless (-f $f); |
95 |
|
96 |
ok(open(F, $f), "open read $f"); |
97 |
my $tmp = ''; |
98 |
while(<F>) { |
99 |
$tmp .= $_; |
100 |
} |
101 |
ok(close(F), "close"); |
102 |
|
103 |
# strip mountpoint |
104 |
$f =~ s#^\Q$mount\E/##; |
105 |
|
106 |
ok($file_data{$f}, "$f exists"); |
107 |
|
108 |
cmp_ok(length($file_data{$f}), '==', length($tmp), "size"); |
109 |
cmp_ok($file_data{$f}, 'eq', $tmp, "content"); |
110 |
|
111 |
$tmp =~ tr/a-z/A-Z/; |
112 |
$tmp .= $f; |
113 |
|
114 |
ok(open(F, "> $mount/$f"), "open write $mount/$f"); |
115 |
print F $tmp; |
116 |
ok(close(F), "close"); |
117 |
|
118 |
ok($sth_select->execute($f), "select $f"); |
119 |
cmp_ok($sth_select->fetchrow_array(), 'eq', $tmp, "updated content"); |
120 |
} |
121 |
|
122 |
# small delay so that filesystem could mount |
123 |
sleep(1); |
124 |
|
125 |
find({ wanted => \&test_file, no_chdir => 1 }, $mount); |
126 |
|
127 |
ok($mnt->umount,"umount"); |
128 |
|
129 |
undef $sth_select; |
130 |
undef $sth_insert; |
131 |
|
132 |
ok($dbh->disconnect, "disconnect"); |