Skip to content

Commit

Permalink
Merge pull request #1059 from joseivanlopez/fix_service_actions
Browse files Browse the repository at this point in the history
Fix system service actions
  • Loading branch information
joseivanlopez committed Jun 8, 2020
2 parents 0ea870b + 1d538e2 commit e7e11c8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 112 deletions.
44 changes: 16 additions & 28 deletions library/systemd/src/lib/yast2/system_service.rb
@@ -1,6 +1,4 @@
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
# Copyright (c) [2018-2020] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -181,7 +179,7 @@ def found?
#
# @return [String] all possible active_state values of systemd
def state
return socket.active_state if socket_active? && !service.active?
return socket.active_state if socket? && socket.active? && !service.active?

service.active_state
end
Expand All @@ -192,7 +190,7 @@ def state
#
# @return [String] all possible sub_state values of systemd
def substate
return socket.sub_state if socket_active? && !service.active?
return socket.sub_state if socket? && socket.active? && !service.active?

service.sub_state
end
Expand Down Expand Up @@ -220,7 +218,7 @@ def current_start_mode
#
# @return [Boolean]
def currently_active?
service.active? || socket_active?
service.active? || (socket? && socket.active?)
end

# Returns the list of supported start modes for this service (if a socket
Expand Down Expand Up @@ -476,15 +474,7 @@ def perform_action
#
# @return [Boolean] true if the service was correctly started
def perform_start
result = true

if socket && start_mode == :on_demand
result &&= socket.start unless socket_active?
else
result &&= service.start unless service.active?
end

result
(socket? && start_mode == :on_demand) ? socket.start : service.start
end

# Stops the service in the underlying system
Expand All @@ -495,8 +485,8 @@ def perform_start
def perform_stop
result = true

result &&= service.stop if service.active?
result &&= socket.stop if socket_active?
result &&= socket.stop if socket?
result &&= service.stop

result
end
Expand All @@ -522,7 +512,7 @@ def perform_reload

result = true

result &&= socket.stop if socket_active? && start_mode != :on_demand
result &&= socket.stop if socket? && start_mode != :on_demand
result &&= service.active? ? service.reload : perform_start

result
Expand All @@ -542,20 +532,18 @@ def clear_errors
@errors.clear
end

# Returns the associated socket
# Whether there is a socket associated to the service
#
# @return [Yast2::Systemd::Socket]
def socket
service && service.socket
# @return [Boolean]
def socket?
!socket.nil?
end

# Whether the associated socket (if any) is actived
# Returns the associated socket
#
# @return [Boolean]
def socket_active?
return false unless socket

socket.active?
# @return [Yast2::Systemd::Socket, nil]
def socket
service.socket
end

# Registers change for a given key
Expand Down
125 changes: 42 additions & 83 deletions library/systemd/test/yast2/system_service_test.rb
@@ -1,7 +1,5 @@
#!/usr/bin/env rspec
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
# Copyright (c) [2018-2020] SUSE LLC
#
# All Rights Reserved.
#
Expand Down Expand Up @@ -744,64 +742,35 @@
context "and the action is start" do
let(:action) { :start }

context "and neither the service nor the socket are active" do
let(:service_active) { false }
let(:socket_active) { false }

context "and the start mode is set to :on_demand" do
let(:start_mode) { :on_demand }

it "tries to start the socket" do
expect(socket).to receive(:start).and_return(true)

system_service.save
end
context "and the start mode is set to :on_demand" do
let(:start_mode) { :on_demand }

it "does not try to start the service" do
allow(socket).to receive(:start).and_return(true)
expect(service).to_not receive(:start)
it "tries to start the socket" do
expect(socket).to receive(:start).and_return(true)

system_service.save
end
system_service.save
end

context "and the start mode is set to :on_boot or :manual" do
let(:start_mode) { :manual }

it "tries to start the service" do
expect(service).to receive(:start).and_return(true)

system_service.save
end

it "does not try to start the socket" do
allow(service).to receive(:start).and_return(true)
expect(socket).to_not receive(:start)
it "does not try to start the service" do
allow(socket).to receive(:start).and_return(true)
expect(service).to_not receive(:start)

system_service.save
end
system_service.save
end
end

context "and the service is active" do
let(:service_active) { true }
let(:socket_active) { false }
context "and the start mode is set to :on_boot or :manual" do
let(:start_mode) { :manual }

it "does not try to start neither the socket nor the service" do
expect(socket).to_not receive(:start)
expect(service).to_not receive(:start)
it "tries to start the service" do
expect(service).to receive(:start).and_return(true)

system_service.save
end
end

context "and the socket is active" do
let(:socket_active) { true }
let(:service_active) { true }

it "does not try to start neither the socket nor the service" do
it "does not try to start the socket" do
allow(service).to receive(:start).and_return(true)
expect(socket).to_not receive(:start)
expect(service).to_not receive(:start)

system_service.save
end
Expand All @@ -816,61 +785,51 @@
allow(socket).to receive(:stop).and_return(true)
end

context "and the service is active" do
let(:service_active) { true }

it "tries to stop the service" do
expect(service).to receive(:stop).and_return(true)
it "tries to stop the service" do
expect(service).to receive(:stop).and_return(true)

system_service.save
end
system_service.save
end

context "and the socket is active" do
let(:socket_active) { true }

it "tries to stop the socket" do
expect(socket).to receive(:stop).and_return(true)
it "tries to stop the socket" do
expect(socket).to receive(:stop).and_return(true)

system_service.save
end
system_service.save
end
end

context "and the service is not active" do
let(:service_active) { false }

it "does not try to stop the service again" do
expect(service).to_not receive(:stop)
system_service.save
end
end
context "and the action is restart" do
let(:action) { :restart }

context "and the socket is not active" do
let(:socket_active) { false }
before do
allow(system_service).to receive(:perform_stop).and_return(stop_success)

it "does not try to stop the socket again" do
expect(socket).to_not receive(:stop)
system_service.save
end
allow(system_service).to receive(:perform_start).and_return(true)
end
end

context "and the action is restart" do
let(:action) { :restart }
let(:stop_success) { true }

it "performs the stop action (see above)" do
expect(system_service).to receive(:perform_stop).and_return(true)
expect(system_service).to receive(:perform_stop)

system_service.save
end

context "and the system service is correctly stopped" do
before do
allow(system_service).to receive(:perform_stop).and_return(true)
end
let(:stop_success) { true }

it "performs the start action (see above)" do
expect(system_service).to receive(:perform_start).and_return(true)
expect(system_service).to receive(:perform_start)

system_service.save
end
end

context "and the system service is not correctly stopped" do
let(:stop_success) { false }

it "does not try to perform the start action" do
expect(system_service).to_not receive(:perform_start)

system_service.save
end
Expand Down
7 changes: 7 additions & 0 deletions package/yast2.changes
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Fri Jun 5 15:13:42 UTC 2020 - José Iván López González <jlopez@suse.com>

- Improve actions to stop and start a system service.
- Related to bsc#1162514.
- 4.1.78

-------------------------------------------------------------------
Mon Apr 13 12:36:58 UTC 2020 - Knut Anderssen <kanderssen@suse.com>

Expand Down
2 changes: 1 addition & 1 deletion package/yast2.spec
Expand Up @@ -17,7 +17,7 @@


Name: yast2
Version: 4.1.77
Version: 4.1.78

Release: 0
Summary: YaST2 - Main Package
Expand Down

0 comments on commit e7e11c8

Please sign in to comment.