Sparrow/Commands/Task.pm untested

filter: Statementstmt 0.0% 0 / 385 Branchbran 0.0% 0 / 156 Conditioncond 0.0% 0 / 143 MC/DCmcdc 0.0% 0 / 77 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 806 CC 79 SCAR 58.8
CC
79
Cov
0%
task_run
1056.0
task_add
182.0
task_save
156.0
CRAP 356.8
1 package Sparrow::Commands::Task;
2  
3 use strict;
4  
5 use base 'Exporter';
6  
7 use Sparrow::Constants;
8 use Sparrow::Misc;
9 use Sparrow::Commands::Plugin;
10  
11 use Carp;
12 use File::Basename;
13 use File::Path;
14  
15 use JSON;
16 use YAML;
17 use Config::General ;
18  
19 use Data::Dumper;
20 use File::Copy;
21  
22 use Term::ANSIColor;
23  
24 use Getopt::Long qw(GetOptionsFromArray);
25  
26 use File::Copy;
27  
28 use File::Path qw(rmtree make_path);
29  
30 our @EXPORT = qw{
31  
32 task_list
33  
34 task_add
35 task_show
36 task_remove
37  
38 task_ini
39 task_load_ini
40  
41 task_run
42  
43 task_get
44 task_set
45  
46 task_copy
47  
48 task_save
49 task_restore
50  
51 };
52  
53 sub task_list {
54  
55  
56 my @args = @_;
57 my $nocolor;
58 my @search_pattern;
59  
60 my $args_st = GetOptionsFromArray(
61 \@args,
62 "nocolor" => \$nocolor,
63 "search=s" => \@search_pattern,
64 );
65  
66  
67 if (nocolor() || $nocolor){
68 print "[sparrow task list]\n";
69 } else {
70 print "[",colored(['bold green on_black'],'sparrow task list'), "]\n";
71 }
72  
73 my $root_dir = sparrow_root.'/projects/';
74  
75 opendir(my $dh, $root_dir) || confess "can't opendir $root_dir: $!";
76  
77 for my $p (sort { $a cmp $b } grep { ! /^\.{1,2}$/ } readdir($dh)){
78 next unless -d "$root_dir/$p/tasks";
79 my $project = basename($p);
80 opendir(my $th, "$root_dir/$p/tasks") || confess "can't opendir $root_dir/$p: $!";
81 TASK: for my $t (sort { $a cmp $b } grep { ! /^\.{1,2}$/ } readdir($th)) {
82 my $task = basename($t);
83 if (scalar @search_pattern){ # filter tasks by --search patterns
84 for my $pt (@search_pattern){
85 $project=~/$pt/ or $task=~/$pt/ or next TASK;
86 }
87 }
88 if (nocolor() || $nocolor){
89 print "$project/$task\n";
90 } else {
91 print colored(['bold yellow'],$project), colored(['white'],"/"), colored(['bold cyan'],$task), "\n";
92 }
93 }
94 closedir $th;
95 }
96 print "\n\n";
97 closedir $dh;
98  
99 }
100  
101 sub task_add {
102  
103 my $project = shift or confess "usage: task_add(*project,task,plugin,opts)";
104 my $tid = shift or confess "usage: task_add(project,*task,plugin,opts)";
105 my $pid = shift or confess "usage: task_add(project,task,*plugin,opts)";
106 my %opts = @_;
107  
108 confess "unknown project" unless -d sparrow_root."/projects/$project";
109  
110 $project=~/^[\w\d-\._]+$/ or confess 'project parameter does not meet naming requirements - /^[\w\d-\._]+$/';
111  
112 $tid=~/^[\w\d-\._]+$/ or confess 'task parameter does not meet naming requirements - /^[\w\d-\._]+$/';
113  
114 if (-d sparrow_root."/projects/$project/tasks/$tid") {
115 print "task $project/$tid already exists, update task parameters\n" unless $opts{'--quiet'};
116 } else {
117 mkdir sparrow_root."/projects/$project/tasks";
118 mkdir sparrow_root."/projects/$project/tasks/$tid" or confess "can't create task directory: $!";
119 }
120  
121 my $ptype;
122  
123  
124 if ($pid=~/(public|private)@/){
125 $ptype = $1;
126 $pid=~s/(public|private)@//;
127 }
128
129 if (! $ptype and -f sparrow_root."/plugins/public/$pid/sparrow.json" and -d sparrow_root."/plugins/private/$pid" ){
130 warn "both public and private $pid plugin exists; choose `public\@$pid` or `private\@$pid` to overcome this ambiguity";
131 return;
132 }elsif( -f sparrow_root."/plugins/public/$pid/sparrow.json" and $ptype ne 'private' ){
133 my @task_args = ($project,$tid,'plugin',"public\@$pid");
134 push @task_args, ('host', $opts{'--host'}) if $opts{'--host'};
135 push @task_args, ('task_desc', $opts{task_desc} || $tid);
136 task_set(@task_args);
137 print "task - set plugin to public\@$pid\n" unless $opts{'--quiet'};
138 }elsif( -d sparrow_root."/plugins/private/$pid/" and $ptype ne '--public' ){
139 my @task_args = ($project,$tid,'plugin',"private\@$pid");
140 push @task_args, ('host', $opts{'--host'}) if $opts{'--host'};
141 push @task_args, ('task_desc', $opts{task_desc} || $tid);
142 task_set(@task_args);
143 print "task - set plugin to private\@$pid\n" unless $opts{'--quiet'};
144 }else{
145 confess "plugin is not installed, you need to install it first to use in task";
146 }
147  
148 print "task $project/$tid successfully created\n" unless $opts{'--quiet'};
149  
150 }
151  
152 sub task_show {
153  
154 my $project = shift or confess "usage: task_show(*project,task)";
155 my $tid = shift or confess "usage: task_show(project,*task)";
156  
157 confess "unknown project" unless -d sparrow_root."/projects/$project";
158 confess "unknown task" unless -d sparrow_root."/projects/$project/tasks/$tid";
159  
160  
161 print "[task $project/$tid]\n\n";
162  
163 local $Data::Dumper::Terse=1;
164 print Dumper(task_get($project,$tid)), "\n\n";
165  
166 my $ini_file = sparrow_root."/projects/$project/tasks/$tid/suite.cfg";
167 my $ini_file_old = sparrow_root."/projects/$project/tasks/$tid/suite.ini";
168  
169 if (-f $ini_file){
170 print "[configuration file - $ini_file]\n\n";
171 open F, $ini_file or confess "can't open $ini_file to read: $!";
172 print join "", <F>;
173 close F;
174 }elsif ( -f $ini_file_old ) {
175 print "[configuration file - $ini_file_old]\n\n";
176 open F, $ini_file_old or confess "can't open $ini_file_old to read: $!";
177 print join "", <F>;
178 close F;
179 } else{
180 print "configuration file does not exist\n"
181 }
182  
183  
184 }
185  
186 sub task_remove {
187  
188 my $project = shift or confess('usage: task_remove(*project,task)');
189 my $tid = shift or confess('usage: task_remove(project,*task)');
190  
191 $project=~/^[\w\d-\._]+$/ or confess 'project parameter does not meet naming requirements - /^[\w\d-\._]+$/';
192 $tid=~/^[\w\d-\._]+$/ or confess 'task parameter does not meet naming requirements - /^[\w\d-\._]+$/';
193  
194 if (-d sparrow_root."/projects/$project" and -d sparrow_root."/projects/$project/tasks/$tid" ){
195 rmtree( sparrow_root."/projects/$project/tasks/$tid" );
196 print "task $project/$tid successfully removed\n";
197 }else{
198 warn "unknown task";
199 }
200  
201 }
202  
203 sub task_ini {
204  
205 my $project = shift or confess "usage: task_ini(*project,task)";
206 my $tid = shift or confess "usage: task_ini(project,*task)";
207  
208 confess "unknown project" unless -d sparrow_root."/projects/$project";
209 confess "unknown task" unless -d sparrow_root."/projects/$project/tasks/$tid";
210 confess "please setup your preferable editor via EDITOR environment variable\n" unless editor;
211  
212 my $cfg_file_path = sparrow_root."/projects/$project/tasks/$tid/suite.cfg";
213 my $cfg_ini_file_path = sparrow_root."/projects/$project/tasks/$tid/suite.ini";
214  
215 if ( ! -f $cfg_file_path and -f $cfg_ini_file_path ){
216 copy($cfg_ini_file_path,$cfg_file_path) or confess "Copy failed: $!";
217 }
218  
219 exec editor.' '.$cfg_file_path;
220  
221 }
222  
223 sub task_load_ini {
224  
225 my $project = shift or confess "usage: task_load_ini(*project,task,path)";
226 my $tid = shift or confess "usage: task_load_ini(project,*task,path)";
227 my $ini_file_path = shift or confess "usage: task_load_ini(project,*task,path)";
228  
229 confess "unknown project" unless -d sparrow_root."/projects/$project";
230 confess "unknown task" unless -d sparrow_root."/projects/$project/tasks/$tid";
231  
232 my $dest_path = sparrow_root."/projects/$project/tasks/$tid/suite.cfg";
233 copy($ini_file_path,$dest_path) or confess "Copy failed: $!";
234  
235 print "loaded test suite ini from $ini_file_path OK \n";
236  
237 }
238  
239  
240 sub task_run {
241  
242 my $project = shift or confess "usage: task_run(*project,task,parameters)";
243 my $tid = shift or confess "usage: task_run(project,*task,parameters)";
244 my @args = @_;
245  
246 my $cli_args;
247  
248 my $i=0;
249 for my $a (@args) {
250 if ($a eq '--') {
251 delete $args[$i];
252 $cli_args = join ' ', delete @args[$i .. $#args];
253 last;
254 }
255 $i++;
256 }
257  
258 my $verbose_mode = 0;
259 my $noexec_mode = 0;
260 my $cron_mode = 0;
261  
262 my $dump_config_arg;
263 my $format_arg = $ENV{OUTTHENTIC_FORMAT} || sparrow_config->{'format'} || 'default';
264 my $debug_arg;
265 my $purge_cache_arg;
266 my $match_l_arg;
267 my $story_arg;
268 my $ini_arg;
269 my $yaml_arg;
270 my $json_arg;
271 my $nocolor_arg;
272 my $args_file_arg;
273 my $cwd_arg;
274  
275 my @runtime_params;
276  
277 my $args_st = GetOptionsFromArray(
278  
279 \@args,
280  
281 "verbose" => \$verbose_mode,
282 "no-exec" => \$noexec_mode,
283 "cron" => \$cron_mode,
284 "args-file=s" => \$args_file_arg,
285 "param=s" => \@runtime_params,
286 "dump-config" => \$dump_config_arg,
287 "purge-cache" => \$purge_cache_arg,
288 "format=s" => \$format_arg,
289 "debug=i" => \$debug_arg,
290 "match_l=i" => \$match_l_arg,
291 "cwd=s" => \$cwd_arg,
292 "story=s" => \$story_arg,
293 "ini=s" => \$ini_arg,
294 "yaml=s" => \$yaml_arg,
295 "json=s" => \$json_arg,
296 "nocolor" => \$nocolor_arg,
297  
298 );
299  
300 confess "unknown project" unless -d sparrow_root."/projects/$project";
301 confess "unknown task: ".(sparrow_root."/projects/$project/tasks/$tid")
302 unless -d sparrow_root."/projects/$project/tasks/$tid";
303  
304 my $task_set = task_get($project,$tid);
305  
306 confess "plugin not set" unless $task_set->{'plugin'};
307  
308 my $pdir = sparrow_root."/plugins/".($task_set->{'install_dir'});
309  
310 confess 'plugin not installed' unless -d $pdir;
311  
312 my $spj = plugin_meta($pdir);
313
314 my $cmd;
315  
316 if ($^O =~ 'MSWin') {
317 $cmd = "cd $pdir && set PATH=%PATH%;%cd%/local/bin && set PERL5LIB=%cd%/local/lib/perl5;\%PERL5LIB% && set PYTHONPATH=%cd%/python-lib;%PYTHONPATH% && ";
318 } else {
319 $cmd = "cd $pdir && export PATH=\$PATH:\$PWD/local/bin && export PERL5LIB=\$PWD/local/lib/perl5:\$PERL5LIB && export PYTHONPATH=\$PWD/python-lib:\$PYTHONPATH && ";
320 }
321  
322 if ($spj->{plugin_type} eq 'outthentic'){
323  
324 if ($^O =~ 'MSWin') {
325 $cmd.=" strun --root ./ --task \"[task] ".($task_set->{task_desc})."\""
326 } else {
327 $cmd.=" strun --root ./ --task '[task] ".($task_set->{task_desc})."'"
328 }
329  
330 } elsif ( $spj->{plugin_type} eq 'swat' ) {
331 #warn $task_set->{host};
332 $cmd.=" swat ./ ". ($task_set->{host}).' ';
333 } else {
334 confess "unsupported plugin type: $spj->{plugin_type}"
335 }
336  
337 if ($yaml_arg){
338 $cmd.=" --yaml $yaml_arg";
339 } elsif ($json_arg){
340 $cmd.=" --json $json_arg";
341 } else {
342 my $path = sparrow_root."/projects/$project/tasks/$tid/suite.cfg";
343 if (-f $path and -s $path){
344 OK: {
345  
346 open CFG, $path or die "cannot open file $path to read: $!";
347 my $str = join "", <CFG>;
348 close CFG;
349  
350 eval { decode_json($str) };
351 $cmd.=" --json $path", last OK unless $@;
352
353 eval { Load($str) };
354 $cmd.=" --yaml $path", last OK unless $@;
355  
356 eval {
357  
358 Config::General->new(
359 -InterPolateVars => 1 ,
360 -InterPolateEnv => 1 ,
361 -ConfigFile => $path
362 )->getall or confess "file $path is not valid config file";
363
364 };
365  
366 $cmd.=" --ini $path", last OK unless $@;
367  
368 confess "bad configuration found at $path";
369  
370 }
371  
372 }else {
373 my $path = sparrow_root."/projects/$project/tasks/$tid/suite.ini";
374 $cmd.=" --ini $path" if -f $path and -s $path;
375 }
376 }
377  
378 for my $rp (@runtime_params){
379 $rp=~/(\S+?)=(.*)/;
380 #warn $1; warn $2;
381 my $k = $1; my $v = $2;
382 if ($^O =~ 'MSWin') {
383 $cmd.= " --param $k=$v";
384 } else {
385 $cmd.= " --param $k='$v'";
386 }
387 }
388  
389  
390 $cmd.= " --nocolor" if $nocolor_arg;
391 $cmd.= " --dump-config" if $dump_config_arg;
392 $cmd.= " --purge-cache" if $purge_cache_arg;
393 unless ( $spj->{plugin_type} eq 'swat' ){
394 $cmd.= " --format $format_arg" if $format_arg;
395 }
396 $cmd.= " --debug $debug_arg" if $debug_arg;
397 $cmd.= " --match_l $match_l_arg" if $match_l_arg;
398  
399 $cmd.= " --cwd $cwd_arg" if $cwd_arg;
400 $cmd.= " --story $story_arg" if $story_arg;
401 $cmd.= " --args-file $args_file_arg" if $args_file_arg;
402  
403 $cmd.= " -- $cli_args" if $cli_args;
404  
405 if ($cron_mode) {
406 my $repo_file = sparrow_root.'/cache/report-'.$project.'-'.$tid.'-'.$$.'.txt';
407 exec "( $cmd 1>$repo_file 2>\&1 && rm $repo_file ) || ( cat $repo_file ; rm -v $repo_file; exit 1; )";
408 } else {
409  
410 if ($verbose_mode){
411 print map {"# $_\n"} split /&&\s+/, $cmd;
412 print "\n";
413 }
414  
415 if ($noexec_mode or $^O =~ 'MSWin'){
416 execute_shell_command($cmd);
417 } else {
418 exec $cmd;
419 }
420 }
421  
422 }
423  
424  
425 sub task_get {
426  
427 my $project = shift or confess "usage: task_get(*project,task)";
428 my $tid = shift or confess "usage: task_get(project,*task)";
429  
430 my $data;
431
432 if (open F, sparrow_root."/projects/$project/tasks/$tid/settings.json") {
433 my $str = join "", <F>;
434 close F;
435 $data = decode_json($str);
436 $data->{task_desc} ||= $tid;
437 if ($data->{plugin}) {
438 my ($t,$name) = split '@' , $data->{plugin};
439 $data->{install_dir} = "$t/$name";
440 $data->{type} = $t;
441 }
442 } else {
443 $data = {};
444 }
445 return $data;
446  
447 }
448  
449 sub task_set {
450  
451 my $project = shift or confess "usage: task_set(*project,task,args)";
452 my $tid = shift or confess "usage: task_set(project,*task,args)";
453 my %args = @_;
454  
455 my $task_settings;
456  
457 open F, ">", sparrow_root."/projects/$project/tasks/$tid/settings.json" or
458 confess "can't open file to write: projects/$project/tasks/$tid/settings.json";
459  
460 for my $f (keys %args){
461 $task_settings->{$f} = $args{$f};
462 }
463  
464 print F encode_json($task_settings);
465 close F;
466  
467 }
468  
469 sub task_save {
470  
471 my $dir = shift or confess "usage: task_save(*/path/to/dir)";
472 my @opts = @_;
473  
474 my $quiet_mode;
475 my $ignore_path;
476 my $merge_mode;
477  
478 my $args_st = GetOptionsFromArray(
479 \@opts,
480 "quiet" => \$quiet_mode,
481 "merge" => \$merge_mode,
482 "ignore=s" => \$ignore_path,
483 );
484  
485  
486 die "directory $dir does not exist" unless -d $dir;
487  
488 if ( -d "$dir/projects" and ! $merge_mode ) {
489 rmtree("$dir/projects") or die "can't remove dir: $dir/projects, error: $!";
490 }
491  
492 my @ignore;
493  
494 if ($ignore_path){
495 print "read task ignore file from $ignore_path ...\n";
496 @ignore = parse_task_ignore_file($ignore_path);
497 } elsif ( -f default_task_ignore_file() ) {
498 print "read task ignore file from ".(default_task_ignore_file())." ...\n";
499 @ignore = parse_task_ignore_file(default_task_ignore_file());
500 } else {
501 @ignore = ();
502 }
503  
504 print "save current tasks to [$dir] ...\n=========================\n";
505  
506 my $spr = sparrow_root()."/projects";
507 opendir(my $dh, $spr) || confess "can't opendir $spr: $!";
508  
509 for my $p (sort { $a cmp $b } grep { ! /^\.{1,2}$/ } readdir($dh)){
510 next unless -d "$spr/$p/tasks";
511 my $project = basename($p);
512 opendir(my $th, "$spr/$p/tasks") || confess "can't opendir $spr/$p: $!";
513 for my $t (sort { $a cmp $b } grep { ! /^\.{1,2}$/ } readdir($th)){
514 my $task = basename($t);
515 my $skip = 0;
516 for my $i (@ignore){
517 $skip=1 if "$project:$task" =~ /^$i$/;
518 };
519 if ($skip){
520 print "SKIP $project/$task ...\n" unless $quiet_mode;
521 } else {
522 print "$project/$task ...\n" unless $quiet_mode;
523 make_path("$dir/projects/$project/tasks/$task/");
524 if (-f "$spr/$project/tasks/$task/settings.json" ){
525 copy("$spr/$project/tasks/$task/settings.json", "$dir/projects/$project/tasks/$task/");
526 } else {
527 warn "broken task $spr/$project/tasks/$task/ , settings.json file not found";
528 }
529 if (-f "$spr/$project/tasks/$task/suite.cfg" ){
530 copy("$spr/$project/tasks/$task/suite.cfg", "$dir/projects/$project/tasks/$task/");
531 };
532 }
533 }
534 closedir $th;
535 }
536 closedir $dh;
537  
538 }
539  
540 sub task_restore {
541  
542 my $dir = shift or confess "usage: task_restore(*/path/to/dir)";
543  
544 die "directory $dir does not exist" unless -d $dir;
545  
546 my @opts = @_;
547  
548 my $quiet_mode;
549  
550 my $args_st = GetOptionsFromArray(
551 \@opts,
552 "quiet" => \$quiet_mode,
553 );
554  
555  
556 print "restore tasks from [$dir] ...\n=========================\n";
557  
558 my $spr = sparrow_root()."/projects";
559  
560 opendir(my $dh, "$dir/projects") || confess "can't opendir $dir/projects: $!";
561  
562 for my $p (sort { $a cmp $b } grep { ! /^\.{1,2}$/ } readdir($dh)){
563  
564 my $project = basename($p);
565  
566 make_path("$spr/$project");
567  
568 next unless -d "$dir/projects/$p/tasks";
569  
570 opendir(my $th, "$dir/projects/$p/tasks") || confess "can't opendir $dir/projects/$p/tasks : $!";
571  
572 for my $t (sort { $a cmp $b } grep { ! /^\.{1,2}$/ } readdir($th)){
573 my $task = basename($t);
574 print "restore $project/$task ...\n" unless $quiet_mode;
575 make_path("$spr/$project/tasks/$task/");
576  
577 if (-f "$dir/projects/$project/tasks/$task/settings.json" ){
578 copy("$dir/projects/$project/tasks/$task/settings.json", "$spr/$project/tasks/$task/");
579 my $task_set = task_get($project,$task);
580 my $plg = $task_set->{plugin};
581 install_plugin($plg);
582 } else {
583 warn "broken task $dir/projects/$project/tasks/$task/ , settings.json file not found";
584 }
585 if (-f "$dir/projects/$project/tasks/$task/suite.cfg" ){
586 copy("$dir/projects/$project/tasks/$task/suite.cfg", "$spr/$project/tasks/$task/");
587 };
588 }
589 closedir $th;
590 }
591 closedir $dh;
592  
593 }
594  
595 sub parse_task_ignore_file {
596  
597 my $path = shift;
598 my @ignore;
599  
600 open my $fh, $path or die "can't open file [$path] to read: $!";
601 while( my $l = <$fh>){
602 chomp $l;
603 $l=~s/(.*)#.*/$1/;
604 $l=~s/\s+//g;
605 $l=~s{/}[:];
606 push @ignore, $l;
607 }
608 close $fh;
609 return @ignore;
610 }
611  
612 sub task_copy {
613  
614 my $project_from = shift or confess "usage: task_copy(*project_from,task_from,project_to,task_to)";
615 my $task_from = shift or confess "usage: task_copy(project_from,*task_from,project_to,task_to)";
616 my $project_to = shift or confess "usage: task_copy(project_from,task_from,*project_to,task_to)";
617 my $task_to = shift or confess "usage: task_copy(project_from,task_from,project_to,*task_to)";
618  
619 print "copy task $project_from/$task_from] to $project_to/$task_to ...\n=========================\n";
620  
621 my $spr = sparrow_root()."/projects";
622  
623 make_path("$spr/$project_to/tasks/$task_to/");
624  
625 if (-f "$spr/$project_from/tasks/$task_from/settings.json" ){
626 copy("$spr/$project_from/tasks/$task_from/settings.json", "$spr/$project_to/tasks/$task_to/");
627 } else {
628 warn "broken task $spr/$project_from/tasks/$task_from, settings.json file not found";
629 }
630 if (-f "$spr/$project_from/tasks/$task_from/suite.cfg" ){
631 copy("$spr/$project_from/tasks/$task_from/suite.cfg", "$spr/$project_to/tasks/$task_to/");
632 }
633 }
634  
635 sub nocolor { $ENV{SPARROW_NO_COLOR} }
636  
637 1;
638