File: fuse_ui.t

package info (click to toggle)
libconfig-model-perl 2.155-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,172 kB
  • sloc: perl: 15,117; makefile: 19
file content (166 lines) | stat: -rw-r--r-- 4,711 bytes parent folder | download | duplicates (3)
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
# -*- cperl -*-

use ExtUtils::testlib;
use List::MoreUtils qw/any/;
use Test::More;
use Path::Tiny;

use Test::Memory::Cycle;
use Config::Model;
use Config::Model::Tester::Setup qw/init_test  setup_test_dir/;
use Config;

use warnings;
use strict;
use lib "t/lib";

# Config::Model::FuseUI is loaded later within an eval

if ( $Config{osname} ne 'linux' ) {
    plan skip_all => "Not a Linux system";
}

my @lsmod = eval { `lsmod`; };

if ($@) {
    plan skip_all => "Cannot check is fuse kernel module is loaded: $@";
}

if ( not any {/fuse/} @lsmod ) {
    plan skip_all => "fuse kernel module is not loaded";
}

if ( system(q!bash -c 'type -p fusermount' > /dev/null!) != 0 ) {
    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 {
    # the forked process prints an ok, hence done_testing cannot be used
    plan tests => 17;
}


# required to handle warnings in forked process
#local $SIG{__WARN__} = sub { die $_[0] unless $_[0] =~ /deprecated/ };

use Data::Dumper;
use POSIX ":sys_wait_h";

my ($model, $trace, $args) = init_test('fuse_debug');
my $wr_root = setup_test_dir();


my $fused = $wr_root->child('fused');
$fused->mkpath( { mode => oct(755) } );

$model->load( Master => 'Config/Model/models/Master.pl' );

$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 - std_id:"a/c" X=Av - a_string="toto tata"';
ok( $root->load( step => $step ), "set up data in tree with '$step'" );

my $ui = Config::Model::FuseUI->new(
    root            => $root,
    mountpoint      => "$fused",
    dir_char_mockup => '\\',
);
my $dir_char_mockup = $ui->dir_char_mockup;

ok( $ui, "Created ui (dir mockup is $dir_char_mockup)" );

# now fork
my $pid = fork;

if ( defined $pid and $pid == 0 ) {

    # child process, just run fuse and wait for exit
    $ui->run_loop( debug => $args->{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->child('std_id');
@content = sort map { $_->relative($std_id); } $std_id->children;
my @std_id_elements = sort $root->fetch_element('std_id')->fetch_all_indexes();
for ( @std_id_elements ) { s(/){$dir_char_mockup}g; };
is_deeply( \@content, \@std_id_elements, "check $std_id content (@content)" );

is(
    $fused->child('a_string')->slurp,
    $root->grab_value('a_string') . "\n",
    "check a_string content"
);
my $a_string_fhw = $fused->child('a_string')->openw;
$a_string_fhw->print("foo bar");
$a_string_fhw->close;

is( $fused->child('a_string')->slurp, "foo bar\n", "check new a_string content" );

$std_id->child('cd')->mkpath();
ok( 1, "mkpath on cd dir done" );
@content = sort map { $_->relative($std_id); } $std_id->children;
is_deeply( \@content, [ @std_id_elements, 'cd' ], "check $std_id new content (@content)" );

$std_id->child('cd')->remove_tree();
ok( 1, "remove_tree on cd dir done" );
@content = sort map { $_->relative($std_id); } $std_id->children;
is_deeply( \@content, \@std_id_elements, "check $std_id content after rmdir (@content)" );

is( $fused->child('a_boolean')->slurp, "0\n", "check new a_boolean content" );
my $a_boolean_fhw = $fused->child('a_boolean')->openw;
$a_boolean_fhw->print("1");
$a_boolean_fhw->close;
is( $fused->child('a_boolean')->slurp, "1\n", "check new a_boolean content (set to 1)" );

$a_boolean_fhw = $fused->child('a_boolean')->openw;
$a_boolean_fhw->print("a");
$a_boolean_fhw->close;
is( $fused->child('a_boolean')->slurp, "\n", "check new a_boolean content (blank after error)" );

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, "memory cycles" );