Skip to content

Commit

Permalink
Merge pull request #785 from yast/add-systemd-service-build
Browse files Browse the repository at this point in the history
Add a SystemdService.build method
  • Loading branch information
imobachgs authored Aug 2, 2018
2 parents adb672c + 43d868a commit 3c8a84b
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 204 deletions.
64 changes: 41 additions & 23 deletions library/systemd/src/lib/yast2/system_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ def find!(name)
system_service
end

# Builds a service instance based on the given name
#
# @param name [String] Service name
# @return [SystemService] System service based on the given name
#
# @see Yast::SystemdServiceClass#build
def build(name)
new(Yast::SystemdService.build(name))
end

# Finds a set of services by their names
#
# @param names [Array<String>] service names to find
Expand Down Expand Up @@ -193,7 +203,8 @@ def substate
#
# @return [Symbol] :on_boot, :on_demand, :manual
def current_start_mode
@current_start_mode ||=
return @current_start_mode unless @current_start_mode.nil?
@current_start_mode =
if service.enabled?
:on_boot
elsif socket && socket.enabled?
Expand Down Expand Up @@ -251,11 +262,7 @@ def start_mode=(mode)
raise ArgumentError, "Invalid start mode: '#{mode}' for service '#{service.name}'"
end

if mode == current_start_mode
unregister_change(:start_mode)
else
register_change(:start_mode, mode)
end
register_change(:start_mode, mode)
end

# Whether the service supports :on_demand start mode
Expand All @@ -272,8 +279,8 @@ def support_start_on_demand?
#
# @return [Boolean] true if the service must be active; false otherwise
def active?
return new_value_for(:active) if changed?(:active)
currently_active?
new_value = new_value_for(:active)
new_value.nil? ? currently_active? : new_value
end

# Keywords to search for this service
Expand Down Expand Up @@ -385,7 +392,7 @@ def refresh!
#
# @return [Boolean]
def changed?(key = nil)
key ? changes.key?(key) : changes.any?
key ? changed_value?(key) : any_change?
end

private
Expand All @@ -406,16 +413,12 @@ def changed?(key = nil)
#
# @param value [Boolean] true to set this service as active
def active=(value)
if value == currently_active?
unregister_change(:active)
else
register_change(:active, value)
end
register_change(:active, value)
end

# Sets start mode to the underlying system
def save_start_mode
return unless changes[:start_mode]
return unless changed?(:start_mode)

result =
case changes[:start_mode]
Expand Down Expand Up @@ -540,13 +543,6 @@ def socket_active?
socket.active?
end

# Unregisters change for a given key
#
# @param key [Symbol] Change key
def unregister_change(key)
changes.delete(key)
end

# Registers change for a given key
#
# @param key [Symbol] Change key
Expand All @@ -565,8 +561,30 @@ def clear_changes
# @param key [Symbol] Change key
# @return [Object] New value
def new_value_for(key)
return nil unless changed?(key)
changes[key]
end

# Correspondence between changed values and methods to calculate their current value
CURRENT_VALUE_METHODS = {
active: :currently_active?,
start_mode: :current_start_mode
}.freeze

# Determines whether a value has been changed
#
# @param key [Symbol] Changed value
# @return [Boolean] true if it has changed; false otherwise.
def changed_value?(key)
new_value = new_value_for(key)
return false if new_value.nil?
new_value != send(CURRENT_VALUE_METHODS[key])
end

# Determines whether some value has been changed
#
# @return [Boolean] true if it has changed; false otherwise.
def any_change?
CURRENT_VALUE_METHODS.keys.any? { |k| changed_value?(k) }
end
end
end
18 changes: 15 additions & 3 deletions library/systemd/src/modules/systemd_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ class SystemdServiceClass < Module
# @param propmap [SystemdUnit::PropMap]
# @return [Service,nil] `nil` if not found
def find(service_name, propmap = {})
service_name += UNIT_SUFFIX unless service_name.end_with?(UNIT_SUFFIX)
propmap = SERVICE_PROPMAP.merge(propmap)
service = Service.new(service_name, propmap)
service = build(service_name, propmap)
return nil if service.properties.not_found?
service
end
Expand Down Expand Up @@ -138,6 +136,20 @@ def all(propmap = {})
end
end

# Instantiate a SystemdService object based on the given name
#
# Use with caution as the service might exist or not. If you need to react when
# the service does not exist, use SystemdServiceClass.find.
#
# @param service_name [String] "foo" or "foo.service"
# @param propmap [SystemdUnit::PropMap]
# @return [Service] System service with the given name
def build(service_name, propmap = {})
service_name += UNIT_SUFFIX unless service_name.end_with?(UNIT_SUFFIX)
propmap = SERVICE_PROPMAP.merge(propmap)
Service.new(service_name, propmap)
end

class Service < SystemdUnit
include Yast::Logger

Expand Down
33 changes: 30 additions & 3 deletions library/systemd/test/systemd_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,37 @@ module Yast
end
end

it "returns nil if the service unit does not exist" do
context "when the service does not exist" do
before do
properties = OpenStruct.new(
stdout: "", stderr: "Unit unknown.service could not be found.", exit: 1
)
allow_any_instance_of(Yast::SystemdUnit::Properties)
.to receive(:load_systemd_properties)
.and_return(properties)
end

it "returns nil" do
service = SystemdService.find("another")
expect(service).to be_nil
end
end
end

describe ".build" do
it "returns the service unit object specified in parameter" do
["sshd", "sshd.service"].each do |service_name|
service = SystemdService.build(service_name)
expect(service).to be_a(SystemdUnit)
expect(service.unit_type).to eq("service")
expect(service.unit_name).to eq("sshd")
end
end

it "returns a service instance even if the real service does not exist" do
stub_services(service: "unknown")
service = SystemdService.find("unknown")
expect(service).to be_nil
service = SystemdService.build("unknown")
expect(service.name).to eq("unknown")
end
end

Expand Down
Loading

0 comments on commit 3c8a84b

Please sign in to comment.