diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2015-01-25 10:47:46 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2015-01-25 10:47:46 -0800 |
commit | cfcb6eb1ee82f7fa48944c103f2ee152c7f2c991 (patch) | |
tree | 9b10a58af4d08a51bed0bef260ba73a9cfe8d652 | |
parent | 27b0b69b3cd2d2ec84a7ff37ac46453ae942f938 (diff) | |
parent | ce87b2fba3ff5208892c3b537b0b586128764eb1 (diff) | |
download | chef-cfcb6eb1ee82f7fa48944c103f2ee152c7f2c991.tar.gz |
Merge pull request #2802 from chef/lcg/2212
Lcg/2212
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | lib/chef/platform/provider_mapping.rb | 3 | ||||
-rw-r--r-- | lib/chef/platform/provider_priority_map.rb | 1 | ||||
-rw-r--r-- | lib/chef/provider/service/openbsd.rb | 216 | ||||
-rw-r--r-- | lib/chef/providers.rb | 1 | ||||
-rw-r--r-- | spec/unit/provider/service/openbsd_service_spec.rb | 543 |
6 files changed, 766 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 63c04ff17d..11a5d11919 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,8 +24,10 @@ * [Pull 2684](https://github.com/chef/chef/pull/2684) Remove ole_initialize/uninitialize which cause problems with Ruby >= 2 * [**BinaryBabel**](https://github.com/binarybabel) Make knife cookbook site share prefer gnutar when packaging -* [**Dave Eddy**](https://github.com/bahamas10) +* [**Dave Eddy**](https://github.com/bahamas10) Support arrays for not_if and only_if +* [**Scott Bonds**](https://github.com/bonds) + Add service provider for OpenBSD ### Chef Contributions * ruby 1.9.3 support is dropped diff --git a/lib/chef/platform/provider_mapping.rb b/lib/chef/platform/provider_mapping.rb index 271e72f761..3c7ecf038c 100644 --- a/lib/chef/platform/provider_mapping.rb +++ b/lib/chef/platform/provider_mapping.rb @@ -366,7 +366,8 @@ class Chef :openbsd => { :default => { :group => Chef::Provider::Group::Usermod, - :package => Chef::Provider::Package::Openbsd + :package => Chef::Provider::Package::Openbsd, + :service => Chef::Provider::Service::Openbsd } }, :hpux => { diff --git a/lib/chef/platform/provider_priority_map.rb b/lib/chef/platform/provider_priority_map.rb index aa69761012..2517f46dfd 100644 --- a/lib/chef/platform/provider_priority_map.rb +++ b/lib/chef/platform/provider_priority_map.rb @@ -54,6 +54,7 @@ class Chef # priority :service, Chef::Provider::Service::Freebsd, os: [ "freebsd", "netbsd" ] + priority :service, Chef::Provider::Service::Openbsd, os: [ "openbsd" ] # # Solaris-en diff --git a/lib/chef/provider/service/openbsd.rb b/lib/chef/provider/service/openbsd.rb new file mode 100644 index 0000000000..d509ee10ff --- /dev/null +++ b/lib/chef/provider/service/openbsd.rb @@ -0,0 +1,216 @@ +# +# Author:: Scott Bonds (<scott@ggr.com>) +# Copyright:: Copyright (c) 2014 Scott Bonds +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/mixin/command' +require 'chef/mixin/shell_out' +require 'chef/provider/service/init' +require 'chef/resource/service' + +class Chef + class Provider + class Service + class Openbsd < Chef::Provider::Service::Init + + provides :service, os: [ "openbsd" ] + + include Chef::Mixin::ShellOut + + attr_reader :init_command, :rc_conf, :rc_conf_local, :enabled_state_found + + RC_CONF_PATH = '/etc/rc.conf' + RC_CONF_LOCAL_PATH = '/etc/rc.conf.local' + + def initialize(new_resource, run_context) + super + @rc_conf = ::File.read(RC_CONF_PATH) rescue '' + @rc_conf_local = ::File.read(RC_CONF_LOCAL_PATH) rescue '' + @init_command = ::File.exist?(rcd_script_path) ? rcd_script_path : nil + new_resource.supports[:status] = true + new_resource.status_command("#{default_init_command} check") + end + + def load_current_resource + @current_resource = Chef::Resource::Service.new(new_resource.name) + current_resource.service_name(new_resource.service_name) + + Chef::Log.debug("#{current_resource} found at #{init_command}") + + determine_current_status! + determine_enabled_status! + current_resource + end + + def define_resource_requirements + shared_resource_requirements + + requirements.assert(:start, :enable, :reload, :restart) do |a| + a.assertion { init_command } + a.failure_message Chef::Exceptions::Service, "#{new_resource}: unable to locate the rc.d script" + end + + requirements.assert(:all_actions) do |a| + a.assertion { enabled_state_found } + # for consistency with original behavior, this will not fail in non-whyrun mode; + # rather it will silently set enabled state=>false + a.whyrun "Unable to determine enabled/disabled state, assuming this will be correct for an actual run. Assuming disabled." + end + + requirements.assert(:start, :enable, :reload, :restart) do |a| + a.assertion { init_command && builtin_service_enable_variable_name != nil } + a.failure_message Chef::Exceptions::Service, "Could not find the service name in #{init_command} and rcvar" + # No recovery in whyrun mode - the init file is present but not correct. + end + end + + def enable_service + if !is_enabled? + if is_builtin? + if is_enabled_by_default? + update_rcl rc_conf_local.sub(/^#{Regexp.escape(builtin_service_enable_variable_name)}=.*/, '') + else + # add line with blank string, which means enable + update_rcl rc_conf_local + "\n" + "#{builtin_service_enable_variable_name}=\"\"\n" + end + else + # add to pkg_scripts, most recent addition goes last + old_services_list = rc_conf_local.match(/^pkg_scripts="(.*)"/) + old_services_list = old_services_list ? old_services_list[1].split(' ') : [] + new_services_list = old_services_list + [new_resource.service_name] + if rc_conf_local.match(/^pkg_scripts="(.*)"/) + new_rcl = rc_conf_local.sub(/^pkg_scripts="(.*)"/, "pkg_scripts=\"#{new_services_list.join(' ')}\"") + else + new_rcl = rc_conf_local + "\n" + "pkg_scripts=\"#{new_services_list.join(' ')}\"\n" + end + update_rcl new_rcl + end + end + end + + def disable_service + if is_enabled? + if is_builtin? + if is_enabled_by_default? + # add line to disable + update_rcl rc_conf_local + "\n" + "#{builtin_service_enable_variable_name}=\"NO\"\n" + else + # remove line to disable + update_rcl rc_conf_local.sub(/^#{Regexp.escape(builtin_service_enable_variable_name)}=.*/, '') + end + else + # remove from pkg_scripts + old_list = rc_conf_local.match(/^pkg_scripts="(.*)"/) + old_list = old_list ? old_list[1].split(' ') : [] + new_list = old_list - [new_resource.service_name] + update_rcl rc_conf_local.sub(/^pkg_scripts="(.*)"/, pkg_scripts="#{new_list.join(' ')}") + end + end + end + + private + + def rcd_script_found? + !init_command.nil? + end + + def rcd_script_path + "/etc/rc.d/#{new_resource.service_name}" + end + + def update_rcl(value) + FileUtils.touch RC_CONF_LOCAL_PATH if !::File.exists? RC_CONF_LOCAL_PATH + ::File.write(RC_CONF_LOCAL_PATH, value) + @rc_conf_local = value + end + + # The variable name used in /etc/rc.conf.local for enabling this service + def builtin_service_enable_variable_name + @bsevn ||= begin + result = nil + if rcd_script_found? + ::File.open(init_command) do |rcscript| + if m = rcscript.read.match(/^# \$OpenBSD: (\w+)[(.rc),]?/) + result = m[1] + "_flags" + end + end + end + # Fallback allows us to keep running in whyrun mode when + # the script does not exist. + result || new_resource.service_name + end + end + + def is_builtin? + result = false + var_name = builtin_service_enable_variable_name + if var_name + if rc_conf.match(/^#{Regexp.escape(var_name)}=(.*)/) + result = true + end + end + result + end + + def is_enabled_by_default? + result = false + var_name = builtin_service_enable_variable_name + if var_name + if m = rc_conf.match(/^#{Regexp.escape(var_name)}=(.*)/) + if !(m[1] =~ /"?[Nn][Oo]"?/) + result = true + end + end + end + result + end + + def determine_enabled_status! + result = false # Default to disabled if the service doesn't currently exist at all + @enabled_state_found = false + if is_builtin? + var_name = builtin_service_enable_variable_name + if var_name + if m = rc_conf_local.match(/^#{Regexp.escape(var_name)}=(.*)/) + @enabled_state_found = true + if !(m[1] =~ /"?[Nn][Oo]"?/) # e.g. looking for httpd_flags=NO + result = true + end + end + end + if !@enabled_state_found + result = is_enabled_by_default? + end + else + var_name = @new_resource.service_name + if var_name + if m = rc_conf_local.match(/^pkg_scripts="(.*)"/) + @enabled_state_found = true + if m[1].include?(var_name) # e.g. looking for 'gdm' in pkg_scripts="gdm unbound" + result = true + end + end + end + end + + current_resource.enabled result + end + alias :is_enabled? :determine_enabled_status! + + end + end + end +end diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb index 38c0e6fc9a..796a0f8fa6 100644 --- a/lib/chef/providers.rb +++ b/lib/chef/providers.rb @@ -81,6 +81,7 @@ require 'chef/provider/service/gentoo' require 'chef/provider/service/init' require 'chef/provider/service/invokercd' require 'chef/provider/service/debian' +require 'chef/provider/service/openbsd' require 'chef/provider/service/redhat' require 'chef/provider/service/insserv' require 'chef/provider/service/simple' diff --git a/spec/unit/provider/service/openbsd_service_spec.rb b/spec/unit/provider/service/openbsd_service_spec.rb new file mode 100644 index 0000000000..1b5206470e --- /dev/null +++ b/spec/unit/provider/service/openbsd_service_spec.rb @@ -0,0 +1,543 @@ +# +# Author:: Bryan McLellan (btm@loftninjas.org) +# Author:: Scott Bonds (scott@ggr.com) +# Copyright:: Copyright (c) 2009 Bryan McLellan +# Copyright:: Copyright (c) 2014 Scott Bonds +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +class Chef::Provider::Service::Openbsd + public :builtin_service_enable_variable_name + public :determine_enabled_status! + public :determine_current_status! + public :is_enabled? + attr_accessor :rc_conf, :rc_conf_local +end + +describe Chef::Provider::Service::Openbsd do + let(:node) do + node = Chef::Node.new + node.automatic_attrs[:command] = {:ps => "ps -ax"} + node + end + + let(:new_resource) do + new_resource = Chef::Resource::Service.new("sndiod") + new_resource.pattern("sndiod") + new_resource.supports({:status => false}) + new_resource + end + + let(:current_resource) do + current_resource = Chef::Resource::Service.new("sndiod") + current_resource + end + + let(:provider) do + events = Chef::EventDispatch::Dispatcher.new + run_context = Chef::RunContext.new(node, {}, events) + allow(::File).to receive(:read).with('/etc/rc.conf').and_return('') + allow(::File).to receive(:read).with('/etc/rc.conf.local').and_return('') + provider = Chef::Provider::Service::Openbsd.new(new_resource,run_context) + provider.action = :start + provider + end + + before do + allow(Chef::Resource::Service).to receive(:new).and_return(current_resource) + end + + def stub_etc_rcd_script + allow(::File).to receive(:exist?).and_return(false) + expect(::File).to receive(:exist?).with("/etc/rc.d/#{new_resource.service_name}").and_return(true) + end + + def run_load_current_resource + stub_etc_rcd_script + provider.load_current_resource + end + + describe Chef::Provider::Service::Openbsd, "initialize" do + it "should find /etc/rc.d init scripts" do + stub_etc_rcd_script + expect(provider.init_command).to eql "/etc/rc.d/sndiod" + end + + it "should set init_command to nil if it can't find anything" do + expect(::File).to receive(:exist?).with('/etc/rc.d/sndiod').and_return(false) + expect(provider.init_command).to be nil + end + end + + describe Chef::Provider::Service::Openbsd, "determine_current_status!" do + before do + stub_etc_rcd_script + provider.current_resource = current_resource + current_resource.service_name(new_resource.service_name) + end + + context "when a status command has been specified" do + let(:status) { double(:stdout => "", :exitstatus => 0) } + + before do + new_resource.status_command("/bin/chefhasmonkeypants status") + end + + it "should run the services status command if one has been specified" do + expect(provider).to receive(:shell_out).with("/bin/chefhasmonkeypants status").and_return(status) + provider.determine_current_status! + end + end + + context "when the service supports status" do + let(:status) { double(:stdout => "", :exitstatus => 0) } + + before do + new_resource.supports({:status => true}) + end + + it "should run '/etc/rc.d/service_name status'" do + expect(provider).to receive(:shell_out).with("/etc/rc.d/#{new_resource.service_name} check").and_return(status) + provider.determine_current_status! + end + + it "should set running to true if the status command returns 0" do + expect(provider).to receive(:shell_out).with("/etc/rc.d/#{new_resource.service_name} check").and_return(status) + provider.determine_current_status! + expect(current_resource.running).to be true + end + + it "should set running to false if the status command returns anything except 0" do + expect(provider).to receive(:shell_out).with("/etc/rc.d/#{new_resource.service_name} check").and_raise(Mixlib::ShellOut::ShellCommandFailed) + provider.determine_current_status! + expect(current_resource.running).to be false + end + end + end + + describe Chef::Provider::Service::Openbsd, "determine_enabled_status!" do + before do + stub_etc_rcd_script + provider.current_resource = current_resource + current_resource.service_name(new_resource.service_name) + + allow(provider).to receive(:service_enable_variable_name).and_return("#{new_resource.service_name}_enable") + end + + context "when the service is builtin" do + before do + expect(::File).to receive(:open).with("/etc/rc.d/#{new_resource.service_name}") + provider.rc_conf = "#{provider.builtin_service_enable_variable_name}=NO" + provider.rc_conf_local = lines.join("\n") + end + + %w{YES Yes yes yEs YeS}.each do |setting| + context "when the enable variable is set to #{setting}" do + let(:lines) { [ %Q{#{provider.builtin_service_enable_variable_name}="#{setting}"} ] } + it "sets enabled to true" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be true + end + end + end + + %w{No NO no nO None NONE none nOnE}.each do |setting| + context "when the enable variable is set to #{setting}" do + let(:lines) { [ %Q{#{provider.builtin_service_enable_variable_name}="#{setting}"} ] } + it "sets enabled to false" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end + end + end + + context "when the enable variable is garbage" do + let(:lines) { [ %Q{#{provider.builtin_service_enable_variable_name}_enable="alskdjflasdkjflakdfj"} ] } + it "sets enabled to false" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end + end + + context "when the enable variable partial matches (left) some other service and we are disabled" do + let(:lines) { [ + %Q{thing_#{provider.builtin_service_enable_variable_name}="YES"}, + %Q{#{provider.builtin_service_enable_variable_name}="NO"}, + ] } + it "sets enabled based on the exact match (false)" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end + end + + context "when the enable variable partial matches (right) some other service and we are disabled" do + let(:lines) { [ + %Q{#{provider.builtin_service_enable_variable_name}_thing="YES"}, + %Q{#{provider.builtin_service_enable_variable_name}}, + ] } + it "sets enabled based on the exact match (false)" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end + end + + context "when the enable variable partial matches (left) some other disabled service and we are enabled" do + let(:lines) { [ + %Q{thing_#{provider.builtin_service_enable_variable_name}="NO"}, + %Q{#{provider.builtin_service_enable_variable_name}="YES"}, + ] } + it "sets enabled based on the exact match (true)" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be true + end + end + + context "when the enable variable partial matches (right) some other disabled service and we are enabled" do + let(:lines) { [ + %Q{#{provider.builtin_service_enable_variable_name}_thing="NO"}, + %Q{#{provider.builtin_service_enable_variable_name}="YES"}, + ] } + it "sets enabled based on the exact match (true)" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be true + end + end + + context "when the enable variable only partial matches (left) some other enabled service" do + let(:lines) { [ %Q{thing_#{provider.builtin_service_enable_variable_name}_enable="YES"} ] } + it "sets enabled to false" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end + end + + context "when the enable variable only partial matches (right) some other enabled service" do + let(:lines) { [ %Q{#{provider.builtin_service_enable_variable_name}_thing_enable="YES"} ] } + it "sets enabled to false" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end + end + + context "when nothing matches" do + let(:lines) { [] } + it "sets enabled to true" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end + end + end + end + + describe Chef::Provider::Service::Openbsd, "load_current_resource" do + before(:each) do + stub_etc_rcd_script + expect(provider).to receive(:determine_current_status!) + current_resource.running(false) + allow(provider).to receive(:service_enable_variable_name).and_return "#{new_resource.service_name}_enable" + expect(::File).to receive(:open).with("/etc/rc.d/#{new_resource.service_name}") + end + + it "should create a current resource with the name of the new resource" do + expect(Chef::Resource::Service).to receive(:new).and_return(current_resource) + provider.load_current_resource + end + + it "should set the current resources service name to the new resources service name" do + provider.load_current_resource + expect(current_resource.service_name).to eq(new_resource.service_name) + end + + it "should return the current resource" do + expect(provider.load_current_resource).to eql(current_resource) + end + + end + + context "when testing actions" do + before(:each) do + stub_etc_rcd_script + expect(provider).to receive(:determine_current_status!) + current_resource.running(false) + expect(provider).to receive(:determine_enabled_status!) + current_resource.enabled(false) + provider.load_current_resource + end + + describe Chef::Provider::Service::Openbsd, "start_service" do + it "should call the start command if one is specified" do + new_resource.start_command("/etc/rc.d/chef startyousillysally") + expect(provider).to receive(:shell_out_with_systems_locale!).with("/etc/rc.d/chef startyousillysally") + provider.start_service() + end + + it "should call '/usr/local/etc/rc.d/service_name start' if no start command is specified" do + expect(provider).to receive(:shell_out_with_systems_locale!).with("/etc/rc.d/#{new_resource.service_name} start") + provider.start_service() + end + end + + describe Chef::Provider::Service::Openbsd, "stop_service" do + it "should call the stop command if one is specified" do + new_resource.stop_command("/etc/init.d/chef itoldyoutostop") + expect(provider).to receive(:shell_out_with_systems_locale!).with("/etc/init.d/chef itoldyoutostop") + provider.stop_service() + end + + it "should call '/usr/local/etc/rc.d/service_name stop' if no stop command is specified" do + expect(provider).to receive(:shell_out_with_systems_locale!).with("/etc/rc.d/#{new_resource.service_name} stop") + provider.stop_service() + end + end + + describe Chef::Provider::Service::Openbsd, "restart_service" do + it "should call 'restart' on the service_name if the resource supports it" do + new_resource.supports({:restart => true}) + expect(provider).to receive(:shell_out_with_systems_locale!).with("/etc/rc.d/#{new_resource.service_name} restart") + provider.restart_service() + end + + it "should call the restart_command if one has been specified" do + new_resource.restart_command("/etc/init.d/chef restartinafire") + expect(provider).to receive(:shell_out_with_systems_locale!).with("/etc/init.d/chef restartinafire") + provider.restart_service() + end + + it "otherwise it should call stop and start" do + expect(provider).to receive(:stop_service) + expect(provider).to receive(:start_service) + provider.restart_service() + end + end + end + + describe Chef::Provider::Service::Openbsd, "define_resource_requirements" do + before do + provider.current_resource = current_resource + end + + context "when the init script is not found" do + before do + provider.init_command = nil + allow(provider).to receive(:builtin_service_enable_variable_name).and_return("#{new_resource.service_name}_enable") + end + + [ "start", "reload", "restart", "enable" ].each do |action| + it "should raise an exception when the action is #{action}" do + provider.define_resource_requirements + provider.action = action + expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service) + end + end + + [ "stop", "disable" ].each do |action| + it "should not raise an error when the action is #{action}" do + provider.define_resource_requirements + provider.action = action + expect { provider.process_resource_requirements }.not_to raise_error + end + end + end + + context "when the init script is found, but the service_enable_variable_name is nil" do + before do + allow(provider).to receive(:builtin_service_enable_variable_name).and_return(nil) + end + + [ "start", "reload", "restart", "enable" ].each do |action| + it "should raise an exception when the action is #{action}" do + provider.action = action + provider.define_resource_requirements + expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service) + end + end + + [ "stop", "disable" ].each do |action| + it "should not raise an error when the action is #{action}" do + provider.action = action + provider.define_resource_requirements + expect { provider.process_resource_requirements }.not_to raise_error + end + end + end + end + + describe Chef::Provider::Service::Openbsd, "enable_service" do + before do + provider.current_resource = current_resource + allow(FileUtils).to receive(:touch).with('/etc/rc.conf.local') + end + context "is builtin and disabled by default" do + before do + provider.rc_conf = "#{provider.builtin_service_enable_variable_name}=NO" + end + context "is enabled" do + before do + provider.rc_conf_local = "#{provider.builtin_service_enable_variable_name}=\"\"" + end + it "should not change rc.conf.local since it is already enabled" do + expect(::File).not_to receive(:write) + provider.enable_service + end + end + context "is disabled" do + before do + provider.rc_conf_local = '' + end + it "should enable the service by adding a line to rc.conf.local" do + expect(::File).to receive(:write).with('/etc/rc.conf.local', include("#{provider.builtin_service_enable_variable_name}=\"\"")) + expect(provider.is_enabled?).to be false + provider.enable_service + expect(provider.is_enabled?).to be true + end + end + end + context "is builtin and enabled by default" do + before do + provider.rc_conf = "#{provider.builtin_service_enable_variable_name}=\"\"" + end + context "is enabled" do + before do + provider.rc_conf_local = '' + end + it "should not change rc.conf.local since it is already enabled" do + expect(::File).not_to receive(:write) + provider.enable_service + end + end + context "is disabled" do + before do + provider.rc_conf_local = "#{provider.builtin_service_enable_variable_name}=NO" + end + it "should enable the service by removing a line from rc.conf.local" do + expect(::File).to receive(:write).with('/etc/rc.conf.local', /^(?!#{provider.builtin_service_enable_variable_name})$/) + expect(provider.is_enabled?).to be false + provider.enable_service + expect(provider.is_enabled?).to be true + end + end + end + context "is not builtin" do + before do + provider.rc_conf = '' + end + context "is enabled" do + before do + provider.rc_conf_local = "pkg_scripts=\"#{new_resource.service_name}\"\n" + end + it "should not change rc.conf.local since it is already enabled" do + expect(::File).not_to receive(:write) + provider.enable_service + end + end + context "is disabled" do + before do + provider.rc_conf_local = '' + end + it "should enable the service by adding it to the pkg_scripts list" do + expect(::File).to receive(:write).with('/etc/rc.conf.local', "\npkg_scripts=\"#{new_resource.service_name}\"\n") + expect(provider.is_enabled?).to be false + provider.enable_service + expect(provider.is_enabled?).to be true + end + end + end + end + + describe Chef::Provider::Service::Openbsd, "disable_service" do + before do + provider.current_resource = current_resource + allow(FileUtils).to receive(:touch).with('/etc/rc.conf.local') + end + context "is builtin and disabled by default" do + before do + provider.rc_conf = "#{provider.builtin_service_enable_variable_name}=NO" + end + context "is enabled" do + before do + provider.rc_conf_local = "#{provider.builtin_service_enable_variable_name}=\"\"" + end + it "should disable the service by removing its line from rc.conf.local" do + expect(::File).to receive(:write).with('/etc/rc.conf.local', /^(?!#{provider.builtin_service_enable_variable_name})$/) + expect(provider.is_enabled?).to be true + provider.disable_service + expect(provider.is_enabled?).to be false + end + end + context "is disabled" do + before do + provider.rc_conf_local = '' + end + it "should not change rc.conf.local since it is already disabled" do + expect(::File).not_to receive(:write) + provider.disable_service + end + end + end + context "is builtin and enabled by default" do + before do + provider.rc_conf = "#{provider.builtin_service_enable_variable_name}=\"\"" + end + context "is enabled" do + before do + provider.rc_conf_local = '' + end + it "should disable the service by adding a line to rc.conf.local" do + expect(::File).to receive(:write).with('/etc/rc.conf.local', include("#{provider.builtin_service_enable_variable_name}=\"NO\"")) + expect(provider.is_enabled?).to be true + provider.disable_service + expect(provider.is_enabled?).to be false + end + end + context "is disabled" do + before do + provider.rc_conf_local = "#{provider.builtin_service_enable_variable_name}=NO" + end + it "should not change rc.conf.local since it is already disabled" do + expect(::File).not_to receive(:write) + provider.disable_service + end + end + end + context "is not builtin" do + before do + provider.rc_conf = '' + end + context "is enabled" do + before do + provider.rc_conf_local = "pkg_scripts=\"#{new_resource.service_name}\"\n" + end + it "should disable the service by removing it from the pkg_scripts list" do + expect(::File).to receive(:write).with('/etc/rc.conf.local', /^(?!#{new_resource.service_name})$/) + expect(provider.is_enabled?).to be true + provider.disable_service + expect(provider.is_enabled?).to be false + end + end + context "is disabled" do + before do + provider.rc_conf_local = '' + end + it "should not change rc.conf.local since it is already disabled" do + expect(::File).not_to receive(:write) + provider.disable_service + end + end + end + end + +end |