5 use DX::Observer::FromCode;
6 use DX::Action::FromCode;
10 package My::PathStatus;
14 has path => (is => 'ro', required => 1);
15 has info => (is => 'ro');
17 package My::PathStatusInfo;
21 has is_directory => (is => 'ro', default => 0);
22 has is_file => (is => 'ro', default => 0);
23 has mode => (is => 'ro', required => 1);
26 @INC{qw(My/PathStatus.pm My/PathStatusInfo.pm)} = (__FILE__,__FILE__);
29 '.ssh' => My::PathStatus->new(
31 info => My::PathStatusInfo->new(is_directory => 1, mode => '0755')
33 '.ssh/authorized_keys' => My::PathStatus->new(
34 path => '.ssh/authorized_keys',
35 info => My::PathStatusInfo->new(is_file => 1, mode => '0644')
40 '.ssh' => My::PathStatus->new(
47 my $solver = DX::Solver->new(
48 facts => { path_status => DX::SetOver->new(
50 values => \%path_status,
54 $solver->add_rule(@$_) for (
55 [ path_status => [ qw(PS) ],
56 [ member_of => 'PS', [ value => 'path_status' ] ] ],
57 [ path => [ qw(PS P) ],
58 [ prop => 'PS', [ value => 'path' ], 'P' ] ],
59 [ mode => [ qw(PS M) ],
60 [ constrain => [ qw(PS M) ],
61 sub { $_[0]->info and $_[0]->info->mode eq $_[1] } ] ],
62 [ exists_path => [ qw(PS) ],
63 [ constrain => [ qw(PS) ],
65 $_[0]->info and ($_[0]->info->is_directory or $_[0]->info->is_file)
67 [ is_directory => [ qw(PS) ],
68 [ constrain => [ qw(PS) ],
69 sub { $_[0]->info and $_[0]->info->is_directory } ] ],
70 [ is_file => [ qw(PS) ],
71 [ constrain => [ qw(PS) ],
72 sub { $_[0]->info and $_[0]->info->is_file } ] ],
75 %path_status = %protos;
77 sub paths_for_simple {
78 join ' ', map $_->{PS}->bound_value->path, $solver->query(
79 [ qw(PS) ], [ path_status => 'PS' ], @_
83 is(paths_for_simple(), '.ssh .ssh/authorized_keys');
85 is(paths_for_simple([ is_directory => 'PS' ]), '.ssh');
87 is(paths_for_simple([ is_file => 'PS' ]), '.ssh/authorized_keys');
89 is(paths_for_simple([ mode => 'PS', [ value => '0755' ] ]), '.ssh');
92 path_status_at => [ 'PS', 'P' ],
93 [ path_status => 'PS' ],
97 path_status_at => [ 'PS', 'P' ],
98 [ constrain => [] => sub { die "ARGH" } ]
104 [ path_status_at => 'PS', [ value => '.ssh' ] ]
108 delete $solver->rule_set->rules->{'path_status_at/2'};
111 path_status_at => [ 'PS', 'P' ],
112 [ path_status => 'PS' ],
113 [ path => qw(PS P) ],
117 path_status_at => [ 'PS', 'P' ],
118 [ constrain => [] => sub { die "ARGH" } ]
124 @res = $solver->query(
126 [ path_status_at => 'PS', [ value => '.ssh' ] ]
130 is(join(' ', map $_->{PS}->bound_value->path, @res), '.ssh');
132 delete $solver->rule_set->rules->{'path_status_at/2'};
135 path_status_at => [ 'PS', 'P' ],
136 [ path_status => 'PS' ],
137 [ path => qw(PS P) ],
144 path_status_at => [ 'PS', 'P' ],
145 [ observe => [ 'P' ],
148 DX::Observer::FromCode->new(
149 code => sub { (path_status => $ob_res{$path}) }
153 [ path_status => 'PS' ],
154 [ path => qw(PS P) ],
159 $ob_res{'.ssh'} = $protos{'.ssh'};
162 join ' ', map $_->{PS}->bound_value->path, $solver->query([ 'PS' ], @_)->results;
166 paths_for([ path_status => 'PS' ], [ path => 'PS', [ value => '.ssh' ] ]),
171 throws_ok { paths_for([ path_status_at => 'PS', [ value => '.ssh' ] ]) }
174 $solver->{observation_policy} = sub { 1 };
177 paths_for([ path_status_at => 'PS', [ value => '.ssh' ] ]),
182 is($path_status{'.ssh'}, $ob_res{'.ssh'});
184 delete $solver->{observation_policy};
186 lives_ok { paths_for([ path_status_at => 'PS', [ value => '.ssh' ] ]) }
187 'No observation required anymore';
189 $path_status{'.ssh/authorized_keys'} = $protos{'.ssh/authorized_keys'};
192 paths_for([ path_status => 'PS' ], [ not => [ is_directory => 'PS' ] ]),
193 '.ssh/authorized_keys',
197 $solver->add_rule(@$_) for (
198 [ directory_at => [ qw(PS P) ],
199 [ path_status_at => qw(PS P) ],
200 [ is_directory => 'PS' ] ],
205 $ob_res{'.ssh'} = $empty{'.ssh'};
207 #%path_status = %protos;
209 $solver->{observation_policy} = sub { 1 };
212 $solver->query([ 'PS' ], [ directory_at => 'PS' => [ value => '.ssh' ] ]);
216 [ dot_ssh_query()->results ],
220 #::Dwarn(paths_for([ directory_at => 'PS', [ value => '.ssh' ] ]));
222 $solver->add_rule(@$_) for (
223 [ is_directory => [ qw(PS) ],
224 [ not => [ exists_path => 'PS' ] ],
228 my ($id, $value) = ($ps_var->id, $ps_var->bound_value);
229 DX::Action::FromCode->new(
231 ($id => My::PathStatus->new(
232 path => $value->path,
233 info => My::PathStatusInfo->new(
234 is_directory => 1, mode => ''
239 $ob_res{$value->path} = $protos{$value->path};
240 (path_status => $value);
248 @res = dot_ssh_query()->results;
250 is(scalar(@res),1,'Single result');
252 is($path_status{'.ssh'}, $empty{'.ssh'}, 'Empty observed');
254 ok(my $action = $res[0]->{PS}->action);
256 my ($type, $value) = $action->run;
258 $solver->facts->{$type}->remove_value($value);
260 ok(!$path_status{'.ssh'}, 'Empty retracted');
262 @res = dot_ssh_query()->results;
264 is(scalar(@res),1,'Single result');
266 is($path_status{'.ssh'}, $protos{'.ssh'}, 'Created observed');
268 ok(!$res[0]->{PS}->action, 'No action');