Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add .find! methods to system unit modules #187

Merged
merged 3 commits into from

2 participants

@vmoravec
Collaborator

The reasoning behind this is:
If you can't handle any nil at the place of calling, use the finder with exclamation mark; SystemdSocketNotFound or SystemdServiceNotFound exception will be raised.

 socket = Yast::SystemdSocket.find!('IcanHasCheez')
# => Yast::SystemdSocketNotFound: Socket unit 'IcanHasCheez' not found
@vmoravec vmoravec referenced this pull request in yast/yast-iscsi-client
Merged

add service 'scsci', use systemdSocket #18

library/systemd/src/modules/systemd_service.rb
@@ -66,6 +77,12 @@ def find service_name, properties={}
service
end
+ def find! service_name, properties={}
+ service = find(service_name, properties)
+ return service if service
+ raise SystemdServiceNotFound, "Service unit '#{service_name}' not found"
@jreidinger Owner

well, you can use also one-liner with || or or

find(service_name, properties) or raise SystemdServiceNotFound, "Service unit '#{service_name}' not found"
@vmoravec Collaborator
vmoravec added a note

Agree, but its 107 characters, you would have to split it into 2 lines to make it fit into a common editor or github window.

@jreidinger Owner

ok, just nitpick. In fact I expect that you have own constructor for exception and pass only service_name.
then it will look like

find(service_name, properties) or raise(SystemdServiceNotFound, service_name)
@vmoravec Collaborator
vmoravec added a note

Good point, thanks, I will adapt the exception classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jreidinger
Owner

just small style nitpick, LGTM

@vmoravec vmoravec merged commit c09dbbf into yast:master
@vmoravec vmoravec deleted the vmoravec:add-exceptions-systemd branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 6, 2014
  1. @vmoravec
  2. @vmoravec

    Typo in test example

    vmoravec authored
Commits on Mar 7, 2014
  1. @vmoravec

    Improve error handling

    vmoravec authored
This page is out of date. Refresh to see the latest.
View
21 library/systemd/src/modules/systemd_service.rb
@@ -11,11 +11,20 @@ module Yast
# Yast.import 'SystemdService'
#
# ## Get a service unit by its name
+ # ## If the service unit can't be found, you'll get nil
+ #
+ # service = Yast::SystemdService.find('sshd') # service unit object
#
- # service = Yast::SystemdService.find('sshd')
# # or using the full unit id 'sshd.service'
+ #
# service = Yast::SystemdService.find('sshd.service')
#
+ # ## If you can't handle any nil at the place of calling,
+ # ## use the finder with exclamation mark;
+ # ## SystemdServiceNotFound exception will be raised
+ #
+ # service = Yast::SystemdService.find!('IcanHasMoar') # SystemdServiceNotFound: Service unit 'IcanHasMoar' not found
+ #
# ## Get basic unit properties
#
# service.unit_name # 'sshd'
@@ -56,6 +65,12 @@ module Yast
#
##
+ class SystemdServiceNotFound < StandardError
+ def initialize service_name
+ super "Service unit '#{service_name}' not found"
+ end
+ end
+
class SystemdServiceClass < Module
UNIT_SUFFIX = ".service"
@@ -66,6 +81,10 @@ def find service_name, properties={}
service
end
+ def find! service_name, properties={}
+ find(service_name, properties) || raise(SystemdServiceNotFound, service_name)
+ end
+
def all properties={}
services = Systemctl.service_units.map do |service_unit|
Service.new(service_unit, properties)
View
19 library/systemd/src/modules/systemd_socket.rb
@@ -11,8 +11,15 @@ module Yast
# Yast.import 'SystemdSocket'
#
# ## Get a socket unit by its name
+ # ## If the socket unit can't be found, you'll get a nil object
#
- # socket = Yast::SystemdSocket.find('iscsid')
+ # socket = Yast::SystemdSocket.find('iscsid') # socket unit object
+ #
+ # ## If you can't handle any nil at the place of calling,
+ # ## use the finder with exclamation mark;
+ # ## SystemdSocketNotFound exception will be raised
+ #
+ # socket = Yast::SystemdSocket.find!('IcanHasCheez') # SystemdSocketNotFound: Socket unit 'IcanHasCheez' not found
#
# ## Get basic unit properties
#
@@ -52,6 +59,12 @@ module Yast
#
##
+ class SystemdSocketNotFound < StandardError
+ def initialize socket_name
+ super "Socket unit '#{socket_name}' not found"
+ end
+ end
+
class SystemdSocketClass < Module
UNIT_SUFFIX = ".socket"
@@ -62,6 +75,10 @@ def find socket_name, properties={}
socket
end
+ def find! socket_name, properties={}
+ find(socket_name, properties) || raise(SystemdSocketNotFound, socket_name)
+ end
+
def all properties={}
sockets = Systemctl.socket_units.map do |socket_unit|
Socket.new(socket_unit, properties)
View
105 library/systemd/test/data/unknown_socket_properties
@@ -0,0 +1,105 @@
+Id=unknown.socket
+Names=unknown.socket
+Description=unknown.socket
+LoadState=not-found
+ActiveState=inactive
+SubState=dead
+InactiveExitTimestampMonotonic=0
+ActiveEnterTimestampMonotonic=0
+ActiveExitTimestampMonotonic=0
+InactiveEnterTimestampMonotonic=0
+CanStart=yes
+CanStop=yes
+CanReload=no
+CanIsolate=no
+StopWhenUnneeded=no
+RefuseManualStart=no
+RefuseManualStop=no
+AllowIsolate=no
+DefaultDependencies=yes
+OnFailureIsolate=no
+IgnoreOnIsolate=no
+IgnoreOnSnapshot=no
+NeedDaemonReload=no
+JobTimeoutUSec=0
+ConditionTimestampMonotonic=0
+ConditionResult=no
+LoadError=org.freedesktop.DBus.Error.FileNotFound "No such file or directory"
+Transient=no
+BindIPv6Only=default
+Backlog=128
+TimeoutUSec=1min 30s
+ControlPID=0
+DirectoryMode=0755
+SocketMode=0666
+Accept=no
+KeepAlive=no
+Priority=-1
+ReceiveBuffer=0
+SendBuffer=0
+IPTOS=-1
+IPTTL=-1
+PipeSize=0
+FreeBind=no
+Transparent=no
+Broadcast=no
+PassCredentials=no
+PassSecurity=no
+Mark=-1
+MaxConnections=64
+NConnections=0
+NAccepted=0
+Result=success
+ReusePort=no
+UMask=0022
+LimitCPU=18446744073709551615
+LimitFSIZE=18446744073709551615
+LimitDATA=18446744073709551615
+LimitSTACK=18446744073709551615
+LimitCORE=18446744073709551615
+LimitRSS=18446744073709551615
+LimitNOFILE=65536
+LimitAS=18446744073709551615
+LimitNPROC=7770
+LimitMEMLOCK=65536
+LimitLOCKS=18446744073709551615
+LimitSIGPENDING=7770
+LimitMSGQUEUE=819200
+LimitNICE=0
+LimitRTPRIO=0
+LimitRTTIME=18446744073709551615
+OOMScoreAdjust=0
+Nice=0
+IOScheduling=0
+CPUSchedulingPolicy=0
+CPUSchedulingPriority=0
+TimerSlackNSec=50000
+CPUSchedulingResetOnFork=no
+NonBlocking=no
+StandardInput=null
+StandardOutput=journal
+StandardError=inherit
+TTYReset=no
+TTYVHangup=no
+TTYVTDisallocate=no
+SyslogPriority=30
+SyslogLevelPrefix=yes
+SecureBits=0
+CapabilityBoundingSet=18446744073709551615
+MountFlags=0
+PrivateTmp=no
+PrivateNetwork=no
+SameProcessGroup=no
+IgnoreSIGPIPE=yes
+NoNewPrivileges=no
+KillMode=control-group
+KillSignal=15
+SendSIGKILL=yes
+SendSIGHUP=no
+CPUAccounting=no
+CPUShares=1024
+BlockIOAccounting=no
+BlockIOWeight=1000
+MemoryAccounting=no
+MemoryLimit=18446744073709551615
+DevicePolicy=auto
View
14 library/systemd/test/systemd_service_test.rb
@@ -29,6 +29,20 @@ module Yast
end
end
+ describe ".find!" do
+ it "returns the service unit object specified in parameter" do
+ service = SystemdService.find('sshd')
+ expect(service).to be_a(SystemdUnit)
+ expect(service.unit_type).to eq("service")
+ expect(service.unit_name).to eq("sshd")
+ end
+
+ it "raises SystemdServiceNotFound error if unit does not exist" do
+ stub_services(:service=>'unknown')
+ expect { SystemdService.find!('unknown') }.to raise_error(SystemdServiceNotFound)
+ end
+ end
+
describe ".all" do
it "returns all supported services found" do
services = SystemdService.all
View
14 library/systemd/test/systemd_socket_test.rb
@@ -21,6 +21,20 @@ module Yast
end
end
+ describe ".find!" do
+ it "returns the unit object specified in parameter" do
+ socket = SystemdSocket.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)
+ end
+ end
+
describe ".all" do
it "returns all supported sockets found" do
sockets = SystemdSocket.all
View
17 library/systemd/test/test_helper.rb
@@ -96,25 +96,22 @@ module SystemdSocketStubs
include SystemctlStubs
include SystemdUnitStubs
- def socket_properties
- @properties ||= OpenStruct.new(
- :stdout => File.read(File.join(__dir__, 'data', 'iscsid_socket_properties')),
- :stderr => '',
+ def load_socket_properties socket_name
+ OpenStruct.new(
+ :stdout => File.read(File.join(__dir__, "data", "#{socket_name}_socket_properties")),
+ :stderr => "",
:exit => 0
)
end
- def stub_sockets
+ def stub_sockets socket: 'iscsid'
stub_unit_command
stub_systemctl(:socket)
- stub_socket_properties
- end
-
- def stub_socket_properties
+ properties = load_socket_properties(socket)
Yast::SystemdUnit::Properties
.any_instance
.stub(:load_systemd_properties)
- .and_return(socket_properties)
+ .and_return(properties)
end
end
View
7 package/yast2.changes
@@ -1,4 +1,11 @@
-------------------------------------------------------------------
+Thu Mar 6 15:21:44 UTC 2014 - vmoravec@suse.com
+
+- Allow raising exceptions for not found systemd units;
+ updates the expectations for bnc#853300
+- 3.1.23
+
+-------------------------------------------------------------------
Thu Mar 6 07:48:29 UTC 2014 - vmoravec@suse.com
- Add systemd service support; needed by fate#314946
View
2  package/yast2.spec
@@ -17,7 +17,7 @@
Name: yast2
-Version: 3.1.22
+Version: 3.1.23
Release: 0
URL: https://github.com/yast/yast-yast2
Something went wrong with that request. Please try again.