eliminate variable name requirement from query()
[scpubgit/DKit.git] / t / dot_ssh.t
1 use strictures 1;
2 use Test::More;
3 use DX::Solver;
4 use DX::SetOver;
5 use DX::Observer::FromCode;
6 use DX::Action::FromCode;
7 use File::Spec;
8 use Test::Exception;
9
10 {
11   package My::PathStatus;
12
13   use Moo;
14
15   with 'DX::Role::Fact';
16
17   has path => (is => 'ro', required => 1);
18   has info => (is => 'ro', predicate => 1);
19
20   package My::PathStatusInfo;
21
22   use Moo;
23
24   with 'DX::Role::Fact';
25
26   has is_directory => (is => 'ro', default => 0);
27   has is_file => (is => 'ro', default => 0);
28   has mode => (is => 'ro', required => 1);
29 }
30
31 @INC{qw(My/PathStatus.pm My/PathStatusInfo.pm)} = (__FILE__,__FILE__);
32
33 my %protos = (
34   '.ssh' => My::PathStatus->new(
35     path => '.ssh',
36     info => My::PathStatusInfo->new(is_directory => 1, mode => '0755')
37   ),
38   '.ssh/authorized_keys' => My::PathStatus->new(
39     path => '.ssh/authorized_keys',
40     info => My::PathStatusInfo->new(is_file => 1, mode => '0644')
41   ),
42 );
43
44 my %empty = (
45   '.ssh' => My::PathStatus->new(
46     path => '.ssh'
47   ),
48   '.ssh/authorized_keys' => My::PathStatus->new(
49     path => '.ssh/authorized_keys'
50   )
51 );
52
53 my %path_status;
54
55 my $solver = DX::Solver->new(
56   facts => { path_status => DX::SetOver->new(
57                over => 'path',
58                values => \%path_status,
59              ) },
60 );
61
62 $solver->add_rule(@$_) for (
63   [ path_status => [ qw(PS) ],
64     [ member_of => 'PS', \'path_status' ] ],
65   [ path => [ qw(PS P) ],
66     [ prop => 'PS', \'path', 'P' ] ],
67   [ info_prop => [ qw(PS N V) ],
68     [ prop => 'PS', \'info', 'PSI' ],
69     [ prop => 'PSI', 'N', 'V' ] ],
70   [ mode => [ qw(PS M) ],
71     [ info_prop => 'PS', \'mode', 'M' ] ],
72   [ exists_path => [ qw(PS) ],
73     [ info_prop => 'PS', \'is_directory', \1 ] ],
74   [ exists_path => [ qw(PS) ],
75     [ info_prop => 'PS', \'is_file', \1 ] ],
76   [ is_directory => [ qw(PS) ],
77     [ info_prop => 'PS', \'is_directory', \1 ] ],
78   [ is_file => [ qw(PS) ],
79     [ info_prop => 'PS', \'is_file', \1 ] ],
80 );
81
82 %path_status = %protos;
83
84 sub paths_for_simple {
85   join ' ', map $_->value_for('PS')->path, $solver->query(
86     [ path_status => 'PS' ], @_
87   )->results;
88 }
89
90 is(paths_for_simple(), '.ssh .ssh/authorized_keys');
91
92 is(paths_for_simple([ is_directory => 'PS' ]), '.ssh');
93
94 is(paths_for_simple([ is_file => 'PS' ]), '.ssh/authorized_keys');
95
96 is(paths_for_simple([ mode => 'PS', [ value => '0755' ] ]), '.ssh');
97
98 $solver->add_rule(
99   path_status_at => [ 'PS', 'P' ],
100     [ path_status => 'PS' ],
101     [ path => qw(PS P) ],
102 );
103 $solver->add_rule(
104   path_status_at => [ 'PS', 'P' ],
105     [ constrain => [] => sub { die "ARGH" } ]
106 );
107
108 throws_ok {
109   $solver->query(
110     [ path_status_at => 'PS', [ value => '.ssh' ] ]
111   )->results
112 } qr/ARGH/;
113
114 delete $solver->rule_set->rules->{'path_status_at/2'};
115
116 $solver->add_rule(
117   path_status_at => [ 'PS', 'P' ],
118     [ path_status => 'PS' ],
119     [ path => qw(PS P) ],
120     [ 'cut' ],
121 );
122 $solver->add_rule(
123   path_status_at => [ 'PS', 'P' ],
124     [ constrain => [] => sub { die "ARGH" } ]
125 );
126
127 my @res;
128
129 lives_ok {
130   @res = $solver->query(
131     [ path_status_at => 'PS', [ value => '.ssh' ] ]
132   )->results
133 };
134
135 is(join(' ', map $_->value_for('PS')->path, @res), '.ssh');
136
137 delete $solver->rule_set->rules->{'path_status_at/2'};
138
139 $solver->add_rule(
140   path_status_at => [ 'PS', 'P' ],
141     [ path_status => 'PS' ],
142     [ path => qw(PS P) ],
143     [ 'cut' ],
144 );
145
146 my %ob_res;
147
148 $solver->add_rule(
149   path_status_at => [ 'PS', 'P' ],
150     [ observe => [ 'P' ],
151         sub {
152           my ($path) = $_[0];
153           DX::Observer::FromCode->new(
154             code => sub { (path_status => $ob_res{$path}) }
155           )
156         }
157     ],
158     [ path_status => 'PS' ],
159     [ path => qw(PS P) ],
160 );
161
162 %path_status = ();
163
164 $ob_res{'.ssh'} = $protos{'.ssh'};
165
166 sub paths_for {
167   join ' ', map $_->value_for('PS')->path, $solver->query(@_)->results;
168 }
169
170 is(
171   paths_for([ path_status => 'PS' ], [ path => 'PS', [ value => '.ssh' ] ]),
172   '',
173   'no .ssh entry'
174 );
175
176 throws_ok { paths_for([ path_status_at => 'PS', [ value => '.ssh' ] ]) }
177   qr/refused/;
178
179 $solver->{observation_policy} = sub { 1 };
180
181 is(
182   paths_for([ path_status_at => 'PS', [ value => '.ssh' ] ]),
183   '.ssh',
184   'observation'
185 );
186
187 is($path_status{'.ssh'}, $ob_res{'.ssh'});
188
189 delete $solver->{observation_policy};
190
191 lives_ok { paths_for([ path_status_at => 'PS', [ value => '.ssh' ] ]) }
192   'No observation required anymore';
193
194 $path_status{'.ssh/authorized_keys'} = $protos{'.ssh/authorized_keys'};
195
196 is(
197   paths_for([ path_status => 'PS' ], [ not => [ is_directory => 'PS' ] ]),
198   '.ssh/authorized_keys',
199   'Negation'
200 );
201
202 $solver->add_rule(@$_) for (
203   [ directory_at => [ qw(PS P) ],
204     [ path_status_at => qw(PS P) ],
205     [ is_directory => 'PS' ] ],
206   [ file_at => [ qw(PS P) ],
207     [ path_status_at => qw(PS P) ],
208     [ is_file => 'PS' ] ],
209 );
210
211 %path_status = ();
212
213 $ob_res{'.ssh'} = $empty{'.ssh'};
214
215 #%path_status = %protos;
216
217 $solver->{observation_policy} = sub { 1 };
218
219 sub dot_ssh_query {
220   $solver->query([ directory_at => 'PS' => [ value => '.ssh' ] ]);
221 }
222
223 is_deeply(
224   [ dot_ssh_query()->results ],
225   []
226 );
227
228 #::Dwarn(paths_for([ directory_at => 'PS', [ value => '.ssh' ] ]));
229
230 $solver->add_rule(@$_) for (
231   [ is_directory => [ qw(PS) ],
232     [ not => [ exists_path => 'PS' ] ],
233     [ act => [ 'PS' ],
234         sub {
235           my ($value) = @_;
236           DX::Action::FromCode->new(
237             expect => sub {
238               (path_status => My::PathStatus->new(
239                 path => $value->path,
240                 info => My::PathStatusInfo->new(
241                   is_directory => 1, mode => ''
242                 )
243               ))
244             },
245             perform => sub {
246               $ob_res{$value->path} = $protos{$value->path};
247               (path_status => $value);
248             }
249           )
250         } ] ]
251 );
252
253 %path_status = ();
254
255 @res = dot_ssh_query()->results;
256
257 is(scalar(@res),1,'Single result');
258
259 is($path_status{'.ssh'}, $empty{'.ssh'}, 'Empty observed');
260
261 is(
262   scalar(my ($action) = $res[0]->actions), 1
263 );
264
265 $solver->run_action($action);
266
267 ok(!$path_status{'.ssh'}, 'Empty retracted');
268
269 @res = dot_ssh_query()->results;
270
271 is(scalar(@res),1,'Single result');
272
273 is($path_status{'.ssh'}, $protos{'.ssh'}, 'Created observed');
274
275 ok(!$res[0]->actions, 'No action');
276
277 $solver->add_predicate(
278   catfile => [ qw(DirPath FileName FilePath) ],
279     [ qw(+ + -) ] => sub {
280       +(FilePath => [ value => File::Spec->catfile($_{DirPath}, $_{FileName}) ])
281     },
282 );
283
284 $solver->add_rule(@$_) for (
285   [ file_in => [ qw(DirStatus FileName FileStatus) ],
286     [ is_directory => qw(DirStatus) ],
287     [ path => qw(DirStatus DirPath) ],
288     [ catfile => qw(DirPath FileName FilePath) ],
289     [ file_at => qw(FileStatus FilePath) ] ],
290   [ is_file => [ qw(PS) ],
291     [ not => [ exists_path => 'PS' ] ],
292     [ act => [ 'PS' ],
293         sub {
294           my ($value) = @_;
295           DX::Action::FromCode->new(
296             expect => sub {
297               (path_status => My::PathStatus->new(
298                 path => $value->path,
299                 info => My::PathStatusInfo->new(
300                   is_file => 1, mode => ''
301                 )
302               ))
303             },
304             perform => sub {
305               $ob_res{$value->path} = $protos{$value->path};
306               (path_status => $value);
307             }
308           )
309         } ] ]
310 );
311
312 %path_status = ();
313 %ob_res = %empty;
314
315 sub keys_file {
316   $solver->query(
317      [ directory_at => 'D' => \'.ssh' ],
318      [ file_in => 'D' => \'authorized_keys' => 'F' ],
319    );
320 }
321
322 @res = keys_file()->results;
323
324 is(scalar @res, 1, 'One result');
325
326 is(scalar(my @act = $res[0]->actions), 2, 'Two actions');
327
328 #::Dwarn(\@act);
329
330 is(scalar(my ($poss) = grep !@{$_->dependencies}, @act), 1, 'One possible');
331
332 $solver->run_action($poss);
333
334 @res = keys_file()->results;
335
336 is(scalar @res, 1, 'One result');
337
338 is(
339   scalar(($poss) = grep !@{$_->dependencies}, $res[0]->actions), 1,
340   'One possible'
341 );
342
343 $solver->run_action($poss);
344
345 @res = keys_file()->results;
346
347 is(scalar @res, 1, 'One result');
348
349 is(scalar($res[0]->actions), 0, 'No actions');
350
351 done_testing;