Skip to content

Commit

Permalink
Move SystemdSocketClass module to real SystemdSocket class
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Aug 14, 2018
1 parent 8e81e54 commit 3f50bd0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 54 deletions.
2 changes: 1 addition & 1 deletion library/systemd/src/lib/yast2/systemd_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def restart

# Returns socket associated with service or nil if there is no such socket
#
# @return [Yast::SystemdSocketClass::Socket,nil]
# @return [Yast::SystemdSocket,nil]
# @see SystemdSocket.for_service
def socket
@socket ||= Yast::SystemdSocket.for_service(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
require "yast2/systemd_socket_finder"

module Yast
# Respresent missed socket
class SystemdSocketNotFound < StandardError
def initialize(socket_name)
super "Socket unit '#{socket_name}' not found"
end
end

###
# Systemd.socket unit control API
#
Expand Down Expand Up @@ -57,53 +64,41 @@ module Yast
# socket = Yast::SystemdSocket.find('iscsid', :can_start=>'CanStart', :triggers=>'Triggers')
# socket.properties.can_start # 'yes'
# socket.properties.triggers # 'iscsid.service'
#
##

class SystemdSocketNotFound < StandardError
def initialize(socket_name)
super "Socket unit '#{socket_name}' not found"
end
end

class SystemdSocketClass < Module
class SystemdSocket < SystemdUnit
UNIT_SUFFIX = ".socket".freeze

# @param propmap [SystemdUnit::PropMap]
def find(socket_name, propmap = {})
socket_name += UNIT_SUFFIX unless socket_name.end_with?(UNIT_SUFFIX)
socket = Socket.new(socket_name, propmap)
return nil if socket.properties.not_found?
socket
end
class << self
# @param propmap [SystemdUnit::PropMap]
def find(socket_name, propmap = {})
socket_name += UNIT_SUFFIX unless socket_name.end_with?(UNIT_SUFFIX)
socket = new(socket_name, propmap)
return nil if socket.properties.not_found?
socket
end

# @param propmap [SystemdUnit::PropMap]
def find!(socket_name, propmap = {})
find(socket_name, propmap) || raise(SystemdSocketNotFound, socket_name)
end
# @param propmap [SystemdUnit::PropMap]
def find!(socket_name, propmap = {})
find(socket_name, propmap) || raise(SystemdSocketNotFound, socket_name)
end

# @param propmap [SystemdUnit::PropMap]
def all(propmap = {})
sockets = Systemctl.socket_units.map do |socket_unit|
Socket.new(socket_unit, propmap)
# @param propmap [SystemdUnit::PropMap]
def all(propmap = {})
sockets = Systemctl.socket_units.map { |socket_unit| new(socket_unit, propmap) }
sockets.select { |s| s.properties.supported? }
end
sockets.select { |s| s.properties.supported? }
end

# Returns the socket for a given service
#
# @param service_name [String] Service name (without the `.service` extension)
# @return [Yast::SystemdSocketClass::Socket,nil]
def for_service(service_name)
@socket_finder ||= Yast2::SystemdSocketFinder.new
@socket_finder.for_service(service_name)
# Returns the socket for a given service
#
# @param service_name [String] Service name (without the `.service` extension)
# @return [Yast::SystemdSocketClass::Socket,nil]
def for_service(service_name)
@socket_finder ||= Yast2::SystemdSocketFinder.new
@socket_finder.for_service(service_name)
end
end

class Socket < SystemdUnit
def listening?
properties.sub_state == "listening"
end
def listening?
properties.sub_state == "listening"
end
end
SystemdSocket = SystemdSocketClass.new
end
4 changes: 2 additions & 2 deletions library/systemd/test/systemd_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ module Yast
describe "#socket" do
subject(:service) { SystemdService.find(service_name) }
let(:service_name) { "sshd" }
let(:socket) { instance_double(SystemdSocketClass::Socket) }
let(:socket) { instance_double(SystemdSocket) }

it "returns the socket for the service" do
expect(SystemdSocket).to receive(:for_service).with(service_name)
Expand Down Expand Up @@ -224,7 +224,7 @@ module Yast
end

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

it "returns true" do
expect(service.socket?).to eq(true)
Expand Down
18 changes: 10 additions & 8 deletions library/systemd/test/systemd_socket_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Yast
import "SystemdSocket"

describe SystemdSocket do
subject(:systemd_socket) { SystemdSocketClass.new }
subject(:systemd_socket) { described_class }

include SystemdSocketStubs

Expand All @@ -16,7 +16,7 @@ module Yast

describe ".find" do
it "returns the unit object specified in parameter" do
socket = SystemdSocket.find "iscsid"
socket = systemd_socket.find "iscsid"
expect(socket).to be_a(SystemdUnit)
expect(socket.unit_type).to eq("socket")
expect(socket.unit_name).to eq("iscsid")
Expand All @@ -25,29 +25,29 @@ module Yast

describe ".find!" do
it "returns the unit object specified in parameter" do
socket = SystemdSocket.find "iscsid"
socket = systemd_socket.find "iscsid"
expect(socket).to be_a(SystemdUnit)
expect(socket.unit_type).to eq("socket")
expect(socket.unit_name).to eq("iscsid")
end

it "raises SystemdSocketNotFound error if unit does not exist" do
stub_sockets(socket: "unknown")
expect { SystemdSocket.find!("unknown") }.to raise_error(SystemdSocketNotFound)
expect { systemd_socket.find!("unknown") }.to raise_error(SystemdSocketNotFound)
end
end

describe ".all" do
it "returns all supported sockets found" do
sockets = SystemdSocket.all
sockets = systemd_socket.all
expect(sockets).to be_a(Array)
expect(sockets).not_to be_empty
sockets.each { |s| expect(s.unit_type).to eq("socket") }
end

describe ".for_service" do
let(:finder) { instance_double(Yast2::SystemdSocketFinder, for_service: socket) }
let(:socket) { instance_double(SystemdSocketClass::Socket) }
let(:socket) { instance_double(SystemdSocket) }

before do
allow(Yast2::SystemdSocketFinder).to receive(:new).and_return(finder)
Expand All @@ -69,13 +69,15 @@ module Yast

describe "#listening?" do
it "returns true if the socket is accepting connections" do
socket = SystemdSocket.find "iscsid"
socket = systemd_socket.find "iscsid"

expect(socket.listening?).to eq(true)
end

it "returns false if the socket is dead" do
socket = SystemdSocket.find "iscsid"
socket = systemd_socket.find "iscsid"
socket.properties.sub_state = "dead"

expect(socket.listening?).to eq(false)
end
end
Expand Down
2 changes: 1 addition & 1 deletion library/systemd/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

require "yast2/systemd_unit"
require "yast2/systemd_service"
require "yast2/systemd_socket"

Yast.import "SystemdSocket"
Yast.import "SystemdTarget"

# Find a Term in given content
Expand Down
4 changes: 1 addition & 3 deletions library/systemd/test/yast2/system_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@
let(:service_found) { true }

let(:service_socket) do
instance_double(Yast::SystemdSocketClass::Socket,
enabled?: socket_enabled,
active?: socket_active)
instance_double(Yast::SystemdSocket, enabled?: socket_enabled, active?: socket_active)
end

let(:socket_enabled) { true }
Expand Down

0 comments on commit 3f50bd0

Please sign in to comment.