[libvirt] [tck PATCH 0/3] A couple of fixes for daemon hooks tests assuming system V only

Erik Skultety (3): hooks: Add systemd detection hooks: Refresh the service status after performing an action hooks: Use internal variable holding the state of the libvirtd service lib/Sys/Virt/TCK/Hooks.pm | 26 +++++++++++++++++++++++--- scripts/hooks/051-daemon-hook.t | 8 ++++---- 2 files changed, 27 insertions(+), 7 deletions(-) -- 2.23.0

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

Presumably, the original intent of the code was to query the status of the service to refresh the internal state variable, but the intended method was never called because of a typo. --- lib/Sys/Virt/TCK/Hooks.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Sys/Virt/TCK/Hooks.pm b/lib/Sys/Virt/TCK/Hooks.pm index ef3403d..a3615e4 100644 --- a/lib/Sys/Virt/TCK/Hooks.pm +++ b/lib/Sys/Virt/TCK/Hooks.pm @@ -263,7 +263,7 @@ sub service_libvirtd { die "failed on $action daemon" if system $cmd; - $self->libvirtd_status; + $self->libvirtd_status(); } sub compare_log { -- 2.23.0

On Tue, Nov 12, 2019 at 04:10:19PM +0100, Erik Skultety wrote:
Presumably, the original intent of the code was to query the status of the service to refresh the internal state variable, but the intended method was never called because of a typo. --- lib/Sys/Virt/TCK/Hooks.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/Sys/Virt/TCK/Hooks.pm b/lib/Sys/Virt/TCK/Hooks.pm index ef3403d..a3615e4 100644 --- a/lib/Sys/Virt/TCK/Hooks.pm +++ b/lib/Sys/Virt/TCK/Hooks.pm @@ -263,7 +263,7 @@ sub service_libvirtd {
die "failed on $action daemon" if system $cmd;
- $self->libvirtd_status; + $self->libvirtd_status();
There's no need for () when invoking a method if it doesn't need any parameters. Actually even if it does need parameters there's some silly ways you can invoke it without round brackets. Welcome to the wonderful world of perl :-) Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Tue, Nov 12, 2019 at 03:44:07PM +0000, Daniel P. Berrangé wrote:
On Tue, Nov 12, 2019 at 04:10:19PM +0100, Erik Skultety wrote:
Presumably, the original intent of the code was to query the status of the service to refresh the internal state variable, but the intended method was never called because of a typo. --- lib/Sys/Virt/TCK/Hooks.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/Sys/Virt/TCK/Hooks.pm b/lib/Sys/Virt/TCK/Hooks.pm index ef3403d..a3615e4 100644 --- a/lib/Sys/Virt/TCK/Hooks.pm +++ b/lib/Sys/Virt/TCK/Hooks.pm @@ -263,7 +263,7 @@ sub service_libvirtd {
die "failed on $action daemon" if system $cmd;
- $self->libvirtd_status; + $self->libvirtd_status();
There's no need for () when invoking a method if it doesn't need any parameters. Actually even if it does need parameters there's some silly ways you can invoke it without round brackets. Welcome to the wonderful world of perl :-)
/o\ Anyhow, I had a bunch of other changes in place and this confused me into thinking that without this patch, the other changes didn't work. Re-running without this patch still works as expected. Thanks for the comments, I'll drop this patch. Erik

Rather than assuming the existence of the 'service' command, use the object variable holding the current state of the libvirtd service which got populated right after we performed the tested action on the service. --- scripts/hooks/051-daemon-hook.t | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/hooks/051-daemon-hook.t b/scripts/hooks/051-daemon-hook.t index 82cedee..b66dde0 100644 --- a/scripts/hooks/051-daemon-hook.t +++ b/scripts/hooks/051-daemon-hook.t @@ -85,7 +85,7 @@ SKIP: { ok($hook->compare_log(), "$hook->{name} is invoked correctly while $hook->{action} libvirtd"); diag "check if libvirtd is stopped"; - ok(`service libvirtd status` =~ /stopped|unused|inactive/, "libvirtd is stopped"); + ok($hook->{libvirtd_status} =~ /stopped|unused|inactive/, "libvirtd is stopped"); # start libvirtd $hook->action('start'); @@ -110,7 +110,7 @@ SKIP: { ok($hook->compare_log(), "$hook->{name} is invoked correctly while $hook->{action} libvirtd"); diag "check if libvirtd is still running"; - ok(`service libvirtd status` =~ /running/, "libvirtd is running"); + ok($hook->{libvirtd_status} =~ /running/, "libvirtd is running"); # restart libvirtd $hook->action('restart'); @@ -135,7 +135,7 @@ SKIP: { ok($hook->compare_log(), "$hook->{name} is invoked correctly while $hook->{action} libvirtd"); diag "check if libvirtd is still running"; - ok(`service libvirtd status` =~ /running/, "libvirtd is running"); + ok($hook->{libvirtd_status} =~ /running/, "libvirtd is running"); # reload libvirtd $hook->action('reload'); @@ -160,7 +160,7 @@ SKIP: { ok($hook->compare_log(), "$hook->{name} is invoked correctly while $hook->{action} libvirtd"); diag "check if libvirtd is still running"; - ok(`service libvirtd status` =~ /running/, "libvirtd is running"); + ok($hook->{libvirtd_status} =~ /running/, "libvirtd is running"); $hook->cleanup(); -- 2.23.0

On Tue, Nov 12, 2019 at 04:10:20PM +0100, Erik Skultety wrote:
Rather than assuming the existence of the 'service' command, use the object variable holding the current state of the libvirtd service which got populated right after we performed the tested action on the service. --- scripts/hooks/051-daemon-hook.t | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
participants (2)
-
Daniel P. Berrangé
-
Erik Skultety