1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
# -*- cperl -*-
use ExtUtils::testlib;
use Test::More ;
use Path::Class ;
use Test::Memory::Cycle;
use Config::Model;
use Config;
if ($Config{osname} ne 'linux') {
plan skip_all => "Not a Linux system" ;
}
my @lsmod = eval { `lsmod` ;} ;
if ($@) {
plan skip_all => "Cannot check is fuse module is loaded: $@" ;
}
if (not grep (/fuse/, @lsmod)) {
plan skip_all => "fuse module is not loaded" ;
}
if (not grep (m!/! , `bash -c 'type fusermount'`) ) {
plan skip_all => "fusermount not found" ;
}
my $umount_str = `bash -c 'umount --version'` ;
my ($umount_v) = $umount_str =~ / (\d+\.\d+)/ ;
if ( $umount_v + 0 < 2.18 ) {
plan skip_all => "Did not find umount with version >= 2.18" ;
}
eval { require Config::Model::FuseUI ;} ;
if ( $@ ) {
plan skip_all => "Config::Model::FuseUI or Fuse is not installed";
}
else {
plan tests => 14;
}
use warnings FATAL => qw(all);
use strict;
# required to handle warnings in forked process
local $SIG{__WARN__} = sub { die $_[0] };
use Data::Dumper;
use POSIX ":sys_wait_h";
my $arg = shift || '';
my $log = 0;
my $trace = $arg =~ /t/ ? 1 : 0 ;
my $fuse_debug = $arg =~ /f/ ? 1 : 0 ;
$::debug = 1 if $arg =~ /d/;
$log = 1 if $arg =~ /l/;
Config::Model::Exception::Any->Trace(1) if $arg =~ /e/;
use Log::Log4perl qw(:easy) ;
my $home = $ENV{HOME} || "";
my $log4perl_user_conf_file = "$home/.log4config-model";
if ($log and -e $log4perl_user_conf_file ) {
Log::Log4perl::init($log4perl_user_conf_file);
}
else {
Log::Log4perl->easy_init($WARN);
}
ok(1,"Compilation done");
# pseudo root where config files are written by config-model
my $wr_root = dir('wr_root');
# cleanup before tests
$wr_root->rmtree;
$wr_root->mkpath( { mode => 0755 }) ;
my $fused = $wr_root->subdir('fused') ;
$fused->mkpath( { mode => 0755 }) ;
my $model = Config::Model -> new(legacy => 'ignore') ;
$model->load(Master => 't/big_model.pm') ;
$model->augment_config_class(
name => 'Master',
element => [
'a_boolean' => { type => 'leaf', value_type => 'boolean', default => 0 } ,
],
);
my $inst = $model->instance (root_class_name => 'Master');
ok($inst,"created dummy instance") ;
my $root = $inst -> config_root ;
my $step = 'std_id:ab X=Bv - std_id:bc X=Av - a_string="toto tata"';
ok( $root->load( step => $step, experience => 'advanced' ),
"set up data in tree with '$step'");
my $ui = Config::Model::FuseUI->new(
root => $root ,
mountpoint => "$fused",
);
ok($ui,"Created ui") ;
# now fork
my $pid = fork ;
if (defined $pid and $pid == 0) {
# child process, just run fuse and wait for exit
$ui->run_loop(debug => $fuse_debug) ;
exit ;
}
# WARNING: the child process has its own copy of the config tree
# there's no use in modifying the tree on the parent process.
# wait for fuse to do its job
sleep 1;
# child process, continue tests
my @content = sort map { $_->relative($fused) ; } $fused->children ;
is_deeply( \@content ,[sort $root->get_element_name() ],"check $fused content");
my $std_id = $fused->subdir('std_id') ;
@content = sort map { $_->relative($std_id) ; } $std_id-> children ;
my @std_id_elements = sort $root->fetch_element('std_id')->fetch_all_indexes() ;
is_deeply( \@content , \@std_id_elements ,"check $std_id content (@content)");
is( $fused->file('a_string')->slurp , $root->grab_value('a_string')."\n",
"check a_string content");
my $a_string_fhw = $fused->file('a_string')->openw ;
$a_string_fhw -> print("foo bar") ;
$a_string_fhw->close ;
is( $fused->file('a_string')->slurp , "foo bar\n", "check new a_string content");
$std_id->subdir('cd')->mkpath() ;
@content = sort map { $_->relative($std_id) ; } $std_id-> children ;
is_deeply( \@content , [ @std_id_elements, 'cd' ] ,"check $std_id new content (@content)");
$std_id->subdir('cd')->rmtree() ;
@content = sort map { $_->relative($std_id) ; } $std_id-> children ;
is_deeply( \@content , \@std_id_elements ,"check $std_id content after rmdir (@content)");
is( $fused->file('a_boolean')->slurp , "0\n", "check new a_boolean content");
my $a_boolean_fhw = $fused->file('a_boolean')->openw ;
$a_boolean_fhw -> print("1") ;
$a_boolean_fhw->close ;
is( $fused->file('a_boolean')->slurp , "1\n", "check new a_boolean content (set to 1)");
END {
if ($pid) {
# run this only in parent process
# umount so child process will exit
system("fusermount -u $fused") ;
# inspired from perlipc man page
my $child;
while ( ($child = wait) > 0) {
ok (1,"Process pid $child done");
}
}
exit ;
}
memory_cycle_ok($model);
|