From 10a50ac0fedaa0c7da96393c1e9add9e0766fcaf Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Thu, 15 Nov 2018 17:18:47 -0800 Subject: Convert service resource to use properties Also expand the testing a bit while I'm here. Signed-off-by: Tim Smith --- lib/chef/resource/service.rb | 170 +++++-------------------------------- spec/unit/resource/service_spec.rb | 47 ++++++---- 2 files changed, 55 insertions(+), 162 deletions(-) diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index c5197d5f06..e99fd634f1 100644 --- a/lib/chef/resource/service.rb +++ b/lib/chef/resource/service.rb @@ -1,7 +1,7 @@ # # Author:: AJ Christensen () # Author:: Tyler Cloke () -# Copyright:: Copyright 2008-2017, Chef Software Inc. +# Copyright:: Copyright 2008-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,8 +25,6 @@ class Chef class Service < Chef::Resource identity_attr :service_name - state_attrs :enabled, :running, :masked - description "Use the service resource to manage a service." default_action :nothing @@ -37,135 +35,42 @@ class Chef property :supports, Hash, default: { restart: nil, reload: nil, status: nil }, coerce: proc { |x| x.is_a?(Array) ? x.each_with_object({}) { |i, m| m[i] = true } : x } - def initialize(name, run_context = nil) - super - @service_name = name - @enabled = nil - @running = nil - @masked = nil - @options = nil - @parameters = nil - @pattern = service_name - @start_command = nil - @stop_command = nil - @status_command = nil - @restart_command = nil - @reload_command = nil - @init_command = nil - @priority = nil - @timeout = nil - @run_levels = nil - @user = nil - end - - def service_name(arg = nil) - set_or_return( - :service_name, - arg, - kind_of: [ String ] - ) - end + property :service_name, String, name_property: true, identity: true # regex for match against ps -ef when !supports[:has_status] && status == nil - def pattern(arg = nil) - set_or_return( - :pattern, - arg, - kind_of: [ String ] - ) - end + property :pattern, String, default: lazy { service_name }, desired_state: false # command to call to start service - def start_command(arg = nil) - set_or_return( - :start_command, - arg, - kind_of: [ String, NilClass, FalseClass ] - ) - end + property :start_command, [ String, NilClass, FalseClass ], desired_state: false # command to call to stop service - def stop_command(arg = nil) - set_or_return( - :stop_command, - arg, - kind_of: [ String, NilClass, FalseClass ] - ) - end + property :stop_command, [ String, NilClass, FalseClass ], desired_state: false # command to call to get status of service - def status_command(arg = nil) - set_or_return( - :status_command, - arg, - kind_of: [ String, NilClass, FalseClass ] - ) - end + property :status_command, [ String, NilClass, FalseClass ], desired_state: false # command to call to restart service - def restart_command(arg = nil) - set_or_return( - :restart_command, - arg, - kind_of: [ String, NilClass, FalseClass ] - ) - end - - def reload_command(arg = nil) - set_or_return( - :reload_command, - arg, - kind_of: [ String, NilClass, FalseClass ] - ) - end + property :restart_command, [ String, NilClass, FalseClass ], desired_state: false + + property :reload_command, [ String, NilClass, FalseClass ], desired_state: false # The path to the init script associated with the service. On many # distributions this is '/etc/init.d/SERVICE_NAME' by default. In # non-standard configurations setting this value will save having to # specify overrides for the start_command, stop_command and # restart_command properties. - def init_command(arg = nil) - set_or_return( - :init_command, - arg, - kind_of: [ String ] - ) - end + property :init_command, String, desired_state: false # if the service is enabled or not - def enabled(arg = nil) - set_or_return( - :enabled, - arg, - kind_of: [ TrueClass, FalseClass ] - ) - end + property :enabled, [ TrueClass, FalseClass ], skip_docs: true # if the service is running or not - def running(arg = nil) - set_or_return( - :running, - arg, - kind_of: [ TrueClass, FalseClass ] - ) - end + property :running, [ TrueClass, FalseClass ], skip_docs: true # if the service is masked or not - def masked(arg = nil) - set_or_return( - :masked, - arg, - kind_of: [ TrueClass, FalseClass ] - ) - end - - def options(arg = nil) - set_or_return( - :options, - arg.respond_to?(:split) ? arg.shellsplit : arg, - kind_of: [ Array, String ] - ) - end + property :masked, [ TrueClass, FalseClass ], skip_docs: true + + property :options, [ Array, String ], coerce: proc { |x| x.respond_to?(:split) ? x.shellsplit : x } # Priority arguments can have two forms: # @@ -177,45 +82,16 @@ class Chef # runlevel 2, stopped in 3 with priority 55 and no symlinks or # similar for other runlevels # - def priority(arg = nil) - set_or_return( - :priority, - arg, - kind_of: [ Integer, String, Hash ] - ) - end + property :priority, [ Integer, String, Hash ] # timeout only applies to the windows service manager - def timeout(arg = nil) - set_or_return( - :timeout, - arg, - kind_of: Integer - ) - end - - def parameters(arg = nil) - set_or_return( - :parameters, - arg, - kind_of: [ Hash ] - ) - end - - def run_levels(arg = nil) - set_or_return( - :run_levels, - arg, - kind_of: [ Array ] ) - end - - def user(arg = nil) - set_or_return( - :user, - arg, - kind_of: [ String ] - ) - end + property :timeout, Integer, desired_state: false + + property :parameters, Hash + + property :run_levels, Array + + property :user, String end end end diff --git a/spec/unit/resource/service_spec.rb b/spec/unit/resource/service_spec.rb index 963c2b2d89..eb32ddf596 100644 --- a/spec/unit/resource/service_spec.rb +++ b/spec/unit/resource/service_spec.rb @@ -1,7 +1,7 @@ # # Author:: AJ Christensen () # Author:: Tyler Cloke () -# Copyright:: Copyright 2008-2017, Chef Software Inc. +# Copyright:: Copyright 2008-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,7 +26,7 @@ describe Chef::Resource::Service do expect(resource.provider).to eq(nil) end - it "sets the service_name to the first argument to new" do + it "sets the service_name property as the name_property" do expect(resource.service_name).to eql("chef") end @@ -45,16 +45,17 @@ describe Chef::Resource::Service do expect { resource.action :unmask }.not_to raise_error end - it "sets the pattern to be the service name by default" do - expect(resource.pattern).to eql("chef") + it "Uses the service_name property as the default for the pattern property" do + resource.service_name "something" + expect(resource.pattern).to eql("something") end - it "accepts a string for the service name" do + it "accepts a String for the service name property" do resource.service_name "something" expect(resource.service_name).to eql("something") end - it "accepts a string for the service pattern" do + it "accepts a String for the service pattern" do resource.pattern ".*" expect(resource.pattern).to eql(".*") end @@ -65,7 +66,7 @@ describe Chef::Resource::Service do end.to raise_error(ArgumentError) end - it "accepts a string for the service start command" do + it "accepts a String for the service_start command" do resource.start_command "/etc/init.d/chef start" expect(resource.start_command).to eql("/etc/init.d/chef start") end @@ -76,7 +77,7 @@ describe Chef::Resource::Service do end.to raise_error(ArgumentError) end - it "accepts a string for the service stop command" do + it "accepts a String for the service stop command" do resource.stop_command "/etc/init.d/chef stop" expect(resource.stop_command).to eql("/etc/init.d/chef stop") end @@ -87,7 +88,23 @@ describe Chef::Resource::Service do end.to raise_error(ArgumentError) end - it "accepts a string for the service status command" do + it "accepts a String for the user property" do + resource.user "fakey_fakerton" + expect(resource.user).to eql("fakey_fakerton") + end + + it "accepts an Array for the run_levels property" do + resource.run_levels ["foo"] + expect(resource.run_levels).to eql(["foo"]) + end + + it "accepts a Hash for the parameters property" do + param_hash = { something: nil } + resource.parameters param_hash + expect(resource.parameters).to eql(param_hash) + end + + it "accepts a String for the service status command" do resource.status_command "/etc/init.d/chef status" expect(resource.status_command).to eql("/etc/init.d/chef status") end @@ -98,7 +115,7 @@ describe Chef::Resource::Service do end.to raise_error(ArgumentError) end - it "accepts a string for the service restart command" do + it "accepts a String for the service restart command" do resource.restart_command "/etc/init.d/chef restart" expect(resource.restart_command).to eql("/etc/init.d/chef restart") end @@ -109,7 +126,7 @@ describe Chef::Resource::Service do end.to raise_error(ArgumentError) end - it "accepts a string for the service reload command" do + it "accepts a String for the service reload command" do resource.reload_command "/etc/init.d/chef reload" expect(resource.reload_command).to eql("/etc/init.d/chef reload") end @@ -120,7 +137,7 @@ describe Chef::Resource::Service do end.to raise_error(ArgumentError) end - it "accepts a string for the service init command" do + it "accepts a String for the service init command" do resource.init_command "/etc/init.d/chef" expect(resource.init_command).to eql("/etc/init.d/chef") end @@ -136,12 +153,12 @@ describe Chef::Resource::Service do expect(resource.options).to eql(["-r", "-s"]) end - it "accepts a string for options" do + it "accepts a String for options" do resource.options "-r" expect(resource.options).to eql(["-r"]) end - it "accepts a string with multiple flags for options" do + it "accepts a String with multiple flags for options" do resource.options "-r -s" expect(resource.options).to eql(["-r", "-s"]) end @@ -163,7 +180,7 @@ describe Chef::Resource::Service do expect(resource.send(attrib)).to eql(false) end - it "does not accept a string for #{attrib}" do + it "does not accept a String for #{attrib}" do expect { resource.send(attrib, "poop") }.to raise_error(ArgumentError) end -- cgit v1.2.1