Skip to content

Commit

Permalink
Fix socket detection
Browse files Browse the repository at this point in the history
* Some research is needed to find a better version, but the current
  implementation does not work when the socket is already stopped
  or YaST is running on 1st stage.
  • Loading branch information
imobachgs committed Aug 8, 2018
1 parent ce97b06 commit 04797bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
16 changes: 5 additions & 11 deletions library/systemd/src/modules/systemd_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,13 @@ def restart

# Returns socket associated with service or nil if there is no such socket
#
# @note The current implementation is too simplistic. At this point, checking the
# 'Triggers' property of each socket would be a better way. However, it won't work
# during installation as 'systemctl show' is not available.
#
# @return [Yast::SystemdSocketClass::Socket]
def socket
return @socket if @socket

# not triggered
socket_name = properties.triggered_by
return unless socket_name

# this may be a space separated list
socket_name = socket_name[/\S+\.socket/]
return unless socket_name # triggered by non-socket

@socket = Yast::SystemdSocket.find(socket_name)
@socket ||= Yast::SystemdSocket.find(name)
end

# Determines whether the service has an associated socket
Expand Down
26 changes: 15 additions & 11 deletions library/systemd/test/systemd_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,22 @@ module Yast

describe "#socket" do
subject(:service) { SystemdService.find(service_name) }
let(:service_name) { "sshd" }

before { stub_services(service: service_name) }
before do
allow(SystemdSocket).to receive(:find).with(service_name).and_return(socket)
end

context "when the service is triggered by a socket" do
let(:service_name) { "cups" }
context "when a socket named after the service exists" do
let(:socket) { instance_double(SystemdSocketClass::Socket) }

it "returns the socket" do
expect(service.socket).to be_a(Yast::SystemdSocketClass::Socket)
expect(service.socket.unit_name).to eq("iscsid")
expect(service.socket).to eq(socket)
end
end

context "when the service is not triggered by a socket" do
let(:service_name) { "sshd" }
context "when no socket named after the service exists" do
let(:socket) { nil }

it "returns nil" do
expect(service.socket).to be_nil
Expand All @@ -201,20 +203,22 @@ module Yast
end

describe "#socket?" do
subject(:service) { SystemdService.find(service_name) }
subject(:service) { SystemdService.find("sshd") }

before { stub_services(service: service_name) }
before do
allow(service).to receive(:socket).and_return(socket)
end

context "when there is an associated socket" do
let(:service_name) { "cups" }
let(:socket) { instance_double(SystemdSocketClass::Socket) }

it "returns true" do
expect(service.socket?).to eq(true)
end
end

context "when there is no associated socket" do
let(:service_name) { "sshd" }
let(:socket) { nil }

it "returns false" do
expect(service.socket?).to eq(false)
Expand Down

0 comments on commit 04797bc

Please sign in to comment.