[libvirt] [tck PATCH 1/3] hooks: Add systemd detection

Erik Skultety eskultet at redhat.com
Tue Nov 12 15:10:18 UTC 2019


The hooks assume System V. On RPM-based distros, there's the initscripts
package introducing the 'service' command mapping the old style syntax
to the systemd format. However, we can't assume this will be the case
all the time, so some kind of detection of the init system would prevent
test failures originating in the hooks because of the 'service' command
missing.
---
 lib/Sys/Virt/TCK/Hooks.pm | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/lib/Sys/Virt/TCK/Hooks.pm b/lib/Sys/Virt/TCK/Hooks.pm
index 7d35072..ef3403d 100644
--- a/lib/Sys/Virt/TCK/Hooks.pm
+++ b/lib/Sys/Virt/TCK/Hooks.pm
@@ -48,6 +48,13 @@ sub new {
     return $self;
 }
 
+sub have_systemd {
+    my $rc = ((system "command -pv systemctl > /dev/null") >> 8) && 1;
+
+    # shell return codes are inverted, IOW 0 is True, 1 is False
+    return !$rc;
+}
+
 sub log_name {
     my $self = shift;
     my $log_name = shift;
@@ -70,7 +77,13 @@ sub expect_result {
 
 sub libvirtd_status {
     my $self = shift;
-    my $status = `service libvirtd status`;
+    my $status;
+
+    if (have_systemd()) {
+        $status = `systemctl status libvirtd`;
+    } else {
+        $status = `service libvirtd status`;
+    }
 
     if ($status =~ /stopped|unused|inactive/) {
         $self->{libvirtd_status} = 'stopped';
@@ -238,10 +251,17 @@ sub cleanup {
 sub service_libvirtd {
     my $self = shift;
     my $action = $self->{action};
+    my $cmd;
 
     truncate $self->{log_name}, 0 if -f $self->{log_name};
 
-    die "failed on $action daemon" if system "service libvirtd $action";
+    if (have_systemd()) {
+        $cmd = "systemctl $action libvirtd";
+    } else {
+        $cmd = "service libvirtd $action";
+    }
+
+    die "failed on $action daemon" if system $cmd;
 
     $self->libvirtd_status;
 }
-- 
2.23.0




More information about the libvir-list mailing list