File Coverage

lib/Rex/Virtualization/LibVirt/destroy.pm
Criterion Covered Total %
statement 11 23 47.8
branch 0 8 0.0
condition 0 3 0.0
subroutine 4 5 80.0
pod 0 1 0.0
total 15 40 37.5


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Virtualization::LibVirt::destroy;
6              
7 1     1   18 use v5.12.5;
  1         4  
8 1     1   6 use warnings;
  1         2  
  1         41  
9              
10             our $VERSION = '1.14.2.2'; # TRIAL VERSION
11              
12 1     1   5 use Rex::Logger;
  1         2  
  1         7  
13 1     1   45 use Rex::Helper::Run;
  1         2  
  1         270  
14              
15             sub execute {
16 0     0 0   my ( $class, $arg1, %opt ) = @_;
17 0           my $virt_settings = Rex::Config->get("virtualization");
18             chomp( my $uri =
19 0 0         ref($virt_settings) ? $virt_settings->{connect} : i_run "virsh uri" );
20              
21 0 0         unless ($arg1) {
22 0           die("You have to define the vm name!");
23             }
24              
25 0           my $dom = $arg1;
26 0           Rex::Logger::debug("destroying domain: $dom");
27              
28 0 0         unless ($dom) {
29 0           die("VM $dom not found.");
30             }
31              
32             # virsh must distinguish between a not-running VM and failed to destroy via exit code!
33 0           my $out = i_run "virsh -c $uri destroy '$dom' 2>&1", fail_ok => 1;
34 0 0 0       if ( $? != 0 && $out !~ /domain is not running/ ) {
35 0           die("Error destroying vm $dom");
36             }
37              
38             }
39              
40             1;