diff options
author | Mike Dodge <mikedodge04@fb.com> | 2015-12-21 11:18:15 -0800 |
---|---|---|
committer | Mike Dodge <mikedodge04@fb.com> | 2016-02-15 12:48:30 -0800 |
commit | de6857c4445fb1fb92139f0f6e1d9e654e4f28c2 (patch) | |
tree | fdf35a071c1440819f9b8f2a1025966150ebf775 | |
parent | 66c4f6af84f33ac40ff2912a61ee9a3be2ca878c (diff) | |
download | chef-de6857c4445fb1fb92139f0f6e1d9e654e4f28c2.tar.gz |
added specs
-rw-r--r-- | lib/chef/provider/launchd.rb | 15 | ||||
-rw-r--r-- | lib/chef/resource/launchd.rb | 16 | ||||
-rw-r--r-- | spec/unit/provider/launchd_spec.rb | 186 | ||||
-rw-r--r-- | spec/unit/resource/launchd_spec.rb | 32 |
4 files changed, 232 insertions, 17 deletions
diff --git a/lib/chef/provider/launchd.rb b/lib/chef/provider/launchd.rb index a6f54e7627..38304355c7 100644 --- a/lib/chef/provider/launchd.rb +++ b/lib/chef/provider/launchd.rb @@ -104,17 +104,15 @@ class Chef res.group(@group) if @group res.mode(@mode) if @mode res.owner(@owner) if @owner - res.source(@source) if @source res end def cookbook_file_resource res = Chef::Resource::CookbookFile.new(@path, run_context) + res.cookbook_name = @cookbook if @cookbook res.name(@path) res.backup(@backup) if @backup - res.content(@content) if @content - res.cookbook_name = @cookbook if @cookbook res.group(@group) if @group res.mode(@mode) if @mode res.owner(@owner) if @owner @@ -124,21 +122,16 @@ class Chef end def define_resource_requirements - super requirements.assert( :create, :create_if_missing, :delete, :enable, :disable ) do |a| type = @new_resource.type.to_s - a.assertion { !['daemon', 'agent'].include?(type) } - a.failure_message( - Chef::Exceptions::UnsupportedAction, - "service_type must be 'daemon' or 'agent'" - ) + a.assertion { ['daemon', 'agent'].include?(type) } + error_msg = 'type must be daemon or agent' + a.failure_message Chef::Exceptions::ValidationFailed, error_msg end end - private - def content? if @new_resource.hash @new_resource.hash.to_plist diff --git a/lib/chef/resource/launchd.rb b/lib/chef/resource/launchd.rb index 44ceaf33f9..0ae1425d4f 100644 --- a/lib/chef/resource/launchd.rb +++ b/lib/chef/resource/launchd.rb @@ -49,7 +49,7 @@ class Chef def source(arg = nil) set_or_return( :source, arg, - :kind_of => [ String, Array ] + :kind_of => [String, Array] ) end @@ -77,38 +77,42 @@ class Chef def backup(arg=nil) set_or_return( :backup, arg, - :kind_of => [ Integer, FalseClass ] + :kind_of => [Integer, FalseClass] ) end def group(arg = nil) set_or_return( :group, arg, - :kind_of => [ String, Integer ] + :kind_of => [String, Integer] ) end def mode(arg = nil) set_or_return( :mode, arg, - :kind_of => [ String, Integer ] + :kind_of => [String, Integer] ) end def owner(arg = nil) set_or_return( :owner, arg, - :kind_of => [ String, Integer ] + :kind_of => [String, Integer] ) end def type(type = nil) - type = type ? type.downcase : nil + type = type ? type.downcase : 'daemon' if type == 'daemon' @path = "/Library/LaunchDaemons/#{name}.plist" elsif type == 'agent' @path = "/Library/LaunchAgents/#{name}.plist" + else + error_msg = 'type must be daemon or agent' + raise Chef::Exceptions::ValidationFailed, error_msg end + type end def abandon_process_group(arg = nil) diff --git a/spec/unit/provider/launchd_spec.rb b/spec/unit/provider/launchd_spec.rb new file mode 100644 index 0000000000..150ac4bdf1 --- /dev/null +++ b/spec/unit/provider/launchd_spec.rb @@ -0,0 +1,186 @@ +# +# Author:: Mike Dodge (<mikedodge04@gmail.com>) +# Copyright:: Copyright (c) 2015 Facebook, Inc. +# 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' + +describe Chef::Provider::Launchd do + + context "When launchd manages call.mom.weekly" do + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:provider) { Chef::Provider::Launchd.new(new_resource, run_context) } + + let(:label) {'call.mom.weekly'} + let(:new_resource) { Chef::Resource::Launchd.new(label)} + let!(:current_resource) { Chef::Resource::Launchd.new(label)} + let(:test_plist) { String.new <<-XML } +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> +\t<key>Label</key> +\t<string>call.mom.weekly</string> +\t<key>Program</key> +\t<string>/Library/scripts/call_mom.sh</string> +\t<key>StartCalendarInterval</key> +\t<dict> +\t\t<key>Hourly</key> +\t\t<integer>10</integer> +\t\t<key>Weekday</key> +\t\t<integer>7</integer> +\t</dict> +\t<key>TimeOut</key> +\t<integer>300</integer> +</dict> +</plist> +XML + + let(:test_hash) do { + 'Label' => 'call.mom.weekly', + 'Program' => '/Library/scripts/call_mom.sh', + 'StartCalendarInterval' => { + 'Hourly' => 10, + 'Weekday' => 7, + }, + 'TimeOut' => 300 + } end + + before(:each) do + provider.load_current_resource + end + + it 'resource name and label should be call.mom.weekly' do + expect(new_resource.name).to eql(label) + expect(new_resource.label).to eql(label) + end + + it 'path should be /Library/LaunchDaemons/call.mom.weekly.plist' do + expect(new_resource.path). + to eq('/Library/LaunchDaemons/call.mom.weekly.plist') + end + + def run_resource_setup_for_action(action) + new_resource.action(action) + provider.action = action + provider.load_current_resource + provider.define_resource_requirements + provider.process_resource_requirements + end + + describe 'with type is set to' do + describe 'agent' do + it 'path should be /Library/LaunchAgents/call.mom.weekly.plist' do + new_resource.type 'agent' + expect(new_resource.path). + to eq('/Library/LaunchAgents/call.mom.weekly.plist') + end + end + end + + describe 'with a :create action and' do + describe 'program is passed' do + it 'should produce the test_plist from properties' do + new_resource.program '/Library/scripts/call_mom.sh' + new_resource.time_out 300 + new_resource.start_calendar_interval 'Hourly' => 10, 'Weekday' => 7 + expect(provider.content?).to eql(test_plist) + end + end + + describe 'hash is passed' do + it 'should produce the test_plist from the hash' do + new_resource.hash test_hash + expect(provider.content?).to eql(test_plist) + end + end + end + + describe 'with an :enable action' do + describe 'and the file has been updated' do + before(:each) do + allow(provider).to receive( + :manage_plist).with(:create).and_return(true) + allow(provider).to receive( + :manage_service).with(:restart).and_return(true) + end + + it 'should call manage_service with a :restart action' do + expect(provider.manage_service(:restart)).to be_truthy + end + + it 'works with action enable' do + expect(run_resource_setup_for_action(:enable)).to be_truthy + provider.action_enable + end + end + + describe 'and the file has not been updated' do + before(:each) do + allow(provider).to receive( + :manage_plist).with(:create).and_return(nil) + allow(provider).to receive( + :manage_service).with(:enable).and_return(true) + end + + it 'should call manage_service with a :enable action' do + expect(provider.manage_service(:enable)).to be_truthy + end + + it 'works with action enable' do + expect(run_resource_setup_for_action(:enable)).to be_truthy + provider.action_enable + end + end + end + + describe 'with an :delete action' do + describe 'and the ld file is present' do + before(:each) do + allow(File).to receive(:exists?).and_return(true) + allow(provider).to receive( + :manage_service).with(:disable).and_return(true) + allow(provider).to receive( + :manage_plist).with(:delete).and_return(true) + end + + it 'should call manage_service with a :disable action' do + expect(provider.manage_service(:disable)).to be_truthy + end + + it 'works with action :delete' do + expect(run_resource_setup_for_action(:delete)).to be_truthy + provider.action_delete + end + end + + describe 'and the ld file is not present' do + before(:each) do + allow(File).to receive(:exists?).and_return(false) + allow(provider).to receive( + :manage_plist).with(:delete).and_return(true) + end + + it 'works with action :delete' do + expect(run_resource_setup_for_action(:delete)).to be_truthy + provider.action_delete + end + end + end + end +end diff --git a/spec/unit/resource/launchd_spec.rb b/spec/unit/resource/launchd_spec.rb new file mode 100644 index 0000000000..9076dbd84d --- /dev/null +++ b/spec/unit/resource/launchd_spec.rb @@ -0,0 +1,32 @@ +# + +require 'spec_helper' + +describe Chef::Resource::Launchd do + @launchd = Chef::Resource::Launchd.new('io.chef.chef-client') + let(:resource) { Chef::Resource::Launchd.new( + 'io.chef.chef-client', + run_context + )} + + it "should create a new Chef::Resource::Launchd" do + expect(resource).to be_a_kind_of(Chef::Resource) + expect(resource).to be_a_kind_of(Chef::Resource::Launchd) + end + + it "should have a resource name of Launchd" do + expect(resource.resource_name).to eql(:launchd) + end + + it "should have a default action of create" do + expect(resource.action).to eql([:create]) + end + + it "should accept enable, disable, create, and delete as actions" do + expect { resource.action :enable}.not_to raise_error + expect { resource.action :disable}.not_to raise_error + expect { resource.action :create}.not_to raise_error + expect { resource.action :delete}.not_to raise_error + end +end + |