lib/Rex/Helper/Run.pm

filter: Statementstmt 57.3% 66 / 115 Branchbran 52.9% 18 / 34 Conditioncond 63.6% 7 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 4 totaltotal 56.2% 103 / 183 CC 23 SCAR 33.7
CC
23
Cov
57%
i_run
46.2
upload_and_run
12.0
i_exec
2.0
CRAP 29.1
« blib/lib/Rex/Template.pm ↑ Summary lib/Rex/TaskList/Base.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Helper::Run;
6  
7 94 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 94 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 94 our $VERSION = '1.16.1'; # VERSION
11  
12 94 require Exporter;
13 94 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 94 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 94 ▶use Net::OpenSSH::ShellQuoter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 94 ▶use Rex::Interface::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 94 ▶use Rex::Interface::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 94 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 94 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 94 require Rex::Commands;
22 94 require Rex::Config;
23  
24 94 @EXPORT = qw(upload_and_run i_run i_exec i_exec_nohup);
25  
26 0 ▶sub upload_and_run {
Subroutine upload_and_run: not called CC=3 SCAR=24.8
Pod undocumented
27 0 my ( $template, %option ) = @_;
28  
29 0 my $rnd_file = get_tmp_file;
30  
31 0 my $fh = Rex::Interface::File->create;
32 0 $fh->open( ">", $rnd_file );
33 0 $fh->write($template);
34 0 $fh->close;
35  
36 0 my $fs = Rex::Interface::Fs->create;
37 0 $fs->chmod( 755, $rnd_file );
38  
39 0 my @argv;
40 0 my $command = $rnd_file;
41  
42 0 ▶ if ( exists $option{with} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"with"}) 0 0 0
43 0 $command = Rex::Config->get_executor_for( $option{with} ) . " $command";
44 }
45  
46 0 ▶ if ( exists $option{args} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"args"}) 0 0 0
47 0 $command .= join( " ", @{ $option{args} } );
48 }
49  
50 0 return i_run("$command 2>&1");
51 }
52  
53 # internal run command, doesn't get reported
54 971 ▶sub i_run {
Subroutine i_run: called CC=21 SCAR=38.3
Pod undocumented
55 971 my $cmd = shift;
56 971 my ( $code, $option );
57 971 $option = {};
58 971 ▶ if ( ref $_[0] eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "CODE") 0 971 971
59 0 $code = shift;
60 }
61 971 ▶ if ( scalar @_ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 0) 884 87 971
62 884 $option = {@_};
63 }
64  
65 971 ▶ $option->{valid_retval} ||= [0];
Condition50%1/2
$option->{"valid_retval"} ||= [0]
l!l
0971
MC/DC0%0/1
$option->{"valid_retval"} ||= [0]
$option->{"valid_retval"}
Truth table50%1/2
A
A= $option->{"valid_retval"}
Aresult
11
00
66 971 ▶ $option->{fail_ok} //= 0;
Condition100%2/2
$option->{"fail_ok"} //= 0
l!l
826145
MC/DC100%1/1
$option->{"fail_ok"} //= 0
$option->{"fail_ok"}
Truth table100%2/2
A
A= $option->{"fail_ok"}
Aresult
11
00
67  
68 971 ▶ if ( $option->{no_stderr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{"no_stderr"}) 0 971 971
69 0 $cmd = "$cmd 2>/dev/null";
70 }
71  
72 971 ▶ if ( $option->{stderr_to_stdout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{"stderr_to_stdout"}) 0 971 971
73 0 $cmd = "$cmd 2>&1";
74 }
75  
76 971 ▶ if ( ref $option->{valid_retval} ne "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $option->{"valid_retval"} ne "ARRAY") 0 971 971
77 0 $option->{valid_retval} = [ $option->{valid_retval} ];
78 }
79  
80 971 my $is_no_hup = 0;
81 971 my $tmp_output_file = get_tmp_file();
82 971 ▶ if ( exists $option->{nohup} && $option->{nohup} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"nohup"} and $option->{"nohup"}) 0 971 971
Condition33%1/3
exists $option->{"nohup"} and $option->{"nohup"}
!ll&&!rl&&r
97100
MC/DC0%0/2
exists $option->{"nohup"} and $option->{"nohup"}
exists $option->{"nohup"} $option->{"nohup"}
Truth table33%1/3
A and B
A= exists $option->{"nohup"}B= $option->{"nohup"}
ABresult
0X0
100
111
83 0 $cmd = "nohup $cmd >$tmp_output_file";
84 0 delete $option->{nohup};
85 0 $is_no_hup = 1;
86 }
87  
88 971 my $path;
89  
90 971 ▶ if ( !Rex::Config->get_no_path_cleanup() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Rex::Config"->get_no_path_cleanup) 971 0 971
91 971 $path = join( ":", Rex::Config->get_path() );
92 }
93  
94 971 my $exec = Rex::Interface::Exec->create;
95 971 my ( $out, $err ) = $exec->exec( $cmd, $path, $option );
96 971 my $ret_val = $?;
97  
98 971 ▶ chomp $out if $out;
Branch: 1 branch
BranchTrueFalseTotal
if $out 589 382 971
99 971 ▶ chomp $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 971 971
100  
101 971 $Rex::Commands::Run::LAST_OUTPUT = [ $out, $err ];
102  
103 971 ▶ $out ||= "";
Condition100%2/2
$out ||= ""
l!l
589382
MC/DC100%1/1
$out ||= ""
$out
Truth table100%2/2
A
A= $out
Aresult
11
00
104 971 ▶ $err ||= "";
Condition50%1/2
$err ||= ""
l!l
0971
MC/DC0%0/1
$err ||= ""
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
105  
106 971 ▶ if ( scalar( grep { $_ == $ret_val } @{ $option->{valid_retval} } ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ == $ret_val;} @{$$option{"valid_retval"};}) == 0) 382 589 971
107 382 ▶ if ( !$option->{fail_ok} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($option->{"fail_ok"}) 0 382 382
108 0 Rex::Logger::debug("Error executing `$cmd`: ");
109 0 Rex::Logger::debug("STDOUT:");
110 0 Rex::Logger::debug($out);
111 0 Rex::Logger::debug("STDERR:");
112 0 Rex::Logger::debug($err);
113  
114 0 ▶ if ($is_no_hup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_no_hup) 0 0 0
115 0 $out = $exec->exec("cat $tmp_output_file ; rm -f $tmp_output_file");
116 0 $Rex::Commands::Run::LAST_OUTPUT = [$out];
117 0 $? = $ret_val;
118 }
119  
120 0 confess("Error during `i_run`");
121 }
122 }
123  
124 971 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 0 971 971
125 0 return &$code( $out, $err );
126 }
127  
128 971 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 680 291 971
129 680 return split( /\r?\n/, $out );
130 }
131  
132 291 ▶ if ($is_no_hup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_no_hup) 0 291 291
133 0 $out = $exec->exec("cat $tmp_output_file ; rm -f $tmp_output_file");
134 0 $Rex::Commands::Run::LAST_OUTPUT = [$out];
135 0 $? = $ret_val;
136 }
137  
138 291 return $out;
139 }
140  
141 0 ▶sub i_exec {
Subroutine i_exec: not called CC=1 SCAR=6.9
Pod undocumented
142 0 my ( $cmd, @args ) = @_;
143  
144 0 my $exec = Rex::Interface::Exec->create;
145 0 my $quoter = Net::OpenSSH::ShellQuoter->quoter( $exec->shell->name );
146  
147 0 my $_cmd_str = "$cmd " . join( " ", map { $quoter->quote($_) } @args );
148  
149 0 i_run $_cmd_str;
150 }
151  
152 0 ▶sub i_exec_nohup {
Subroutine i_exec_nohup: not called CC=1 SCAR=6.9
Pod undocumented
153 0 my ( $cmd, @args ) = @_;
154  
155 0 my $exec = Rex::Interface::Exec->create;
156 0 my $quoter = Net::OpenSSH::ShellQuoter->quoter( $exec->shell->name );
157  
158 0 my $_cmd_str = "$cmd " . join( " ", map { $quoter->quote($_) } @args );
159 0 i_run $_cmd_str, nohup => 1;
160 }
161  
162 94 1;
« blib/lib/Rex/Template.pm ↑ Summary lib/Rex/TaskList/Base.pm »