diff options
author | AbhishekKr <abhikumar163@gmail.com> | 2016-09-10 02:28:30 +0530 |
---|---|---|
committer | AbhishekKr <abhikumar163@gmail.com> | 2016-09-10 02:28:30 +0530 |
commit | 5c01c0f74941d4f788e254ae4d040c0e4a2a477d (patch) | |
tree | 8cf25328d577db77d191d88bdc85277629553f3e /spec/unit/provider | |
parent | c3c6cfc156d0a0bfe7c7dd4f273a2c2bdc4b0281 (diff) | |
parent | 3731bff0641d0cb12780bc52376e50d9a0a71644 (diff) | |
download | chef-5c01c0f74941d4f788e254ae4d040c0e4a2a477d.tar.gz |
Merge branch 'master' of https://github.com/chef/chef
Diffstat (limited to 'spec/unit/provider')
44 files changed, 705 insertions, 444 deletions
diff --git a/spec/unit/provider/cookbook_file_spec.rb b/spec/unit/provider/cookbook_file_spec.rb index b375784d00..f49cc7d3da 100644 --- a/spec/unit/provider/cookbook_file_spec.rb +++ b/spec/unit/provider/cookbook_file_spec.rb @@ -26,12 +26,12 @@ describe Chef::Provider::CookbookFile do let(:node) { double("Chef::Node") } let(:events) { double("Chef::Events").as_null_object } # mock all the methods let(:run_context) { double("Chef::RunContext", :node => node, :events => events) } - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates"))) - } - let(:resource_path) { + end + let(:resource_path) do canonicalize_path(File.expand_path(File.join(enclosing_directory, "seattle.txt"))) - } + end # Subject diff --git a/spec/unit/provider/cron/unix_spec.rb b/spec/unit/provider/cron/unix_spec.rb index 83e0f431bf..5e1fcb35ab 100644 --- a/spec/unit/provider/cron/unix_spec.rb +++ b/spec/unit/provider/cron/unix_spec.rb @@ -91,9 +91,9 @@ describe Chef::Provider::Cron::Unix do let (:exitstatus) { 2 } it "should raise an exception if another error occurs" do - expect { + expect do provider.send(:read_crontab) - }.to raise_error(Chef::Exceptions::Cron, "Error determining state of #{new_resource.name}, exit: 2") + end.to raise_error(Chef::Exceptions::Cron, "Error determining state of #{new_resource.name}, exit: 2") end it "logs the crontab output to debug" do @@ -130,9 +130,9 @@ describe Chef::Provider::Cron::Unix do context "when writing the crontab fails" do let(:exitstatus) { 1 } it "should raise an exception if the command returns non-zero" do - expect { + expect do provider.send(:write_crontab, "Foo") - }.to raise_error(Chef::Exceptions::Cron, /Error updating state of #{new_resource.name}, exit: 1/) + end.to raise_error(Chef::Exceptions::Cron, /Error updating state of #{new_resource.name}, exit: 1/) end end end diff --git a/spec/unit/provider/cron_spec.rb b/spec/unit/provider/cron_spec.rb index 010b1b09f3..64916ef454 100644 --- a/spec/unit/provider/cron_spec.rb +++ b/spec/unit/provider/cron_spec.rb @@ -199,9 +199,9 @@ CRONTAB # Chef Name: foo[bar] (baz) 21 */4 * * * some_prog 1234567 CRONTAB - expect { + expect do @provider.load_current_resource - }.not_to raise_error + end.not_to raise_error end end @@ -462,10 +462,10 @@ CRONTAB @new_resource.environment "TEST" => "LOL" expect(@provider).to receive(:write_crontab).with(<<-ENDCRON) # Chef Name: cronhole some stuff -MAILTO=foo@example.com -PATH=/usr/bin:/my/custom/path -SHELL=/bin/foosh -HOME=/home/foo +MAILTO="foo@example.com" +PATH="/usr/bin:/my/custom/path" +SHELL="/bin/foosh" +HOME="/home/foo" TEST=LOL 30 * * * * /bin/true ENDCRON @@ -524,10 +524,10 @@ TEST=LOL # Another comment # Chef Name: cronhole some stuff -MAILTO=foo@example.com -PATH=/usr/bin:/my/custom/path -SHELL=/bin/foosh -HOME=/home/foo +MAILTO="foo@example.com" +PATH="/usr/bin:/my/custom/path" +SHELL="/bin/foosh" +HOME="/home/foo" TEST=LOL 30 * * * * /bin/true ENDCRON @@ -585,10 +585,10 @@ TEST=LOL 0 2 * * * /some/other/command # Chef Name: cronhole some stuff -MAILTO=foo@example.com -PATH=/usr/bin:/my/custom/path -SHELL=/bin/foosh -HOME=/home/foo +MAILTO="foo@example.com" +PATH="/usr/bin:/my/custom/path" +SHELL="/bin/foosh" +HOME="/home/foo" TEST=LOL 30 * * * * /bin/true # Chef Name: something else @@ -679,10 +679,10 @@ HOME=/home/foo 0 2 * * * /some/other/command # Chef Name: cronhole some stuff -MAILTO=foo@example.com -PATH=/usr/bin:/my/custom/path -SHELL=/bin/foosh -HOME=/home/foo +MAILTO="foo@example.com" +PATH="/usr/bin:/my/custom/path" +SHELL="/bin/foosh" +HOME="/home/foo" 30 * * * * /bin/true # Chef Name: something else diff --git a/spec/unit/provider/deploy_spec.rb b/spec/unit/provider/deploy_spec.rb index 62d9123e7b..e69714280c 100644 --- a/spec/unit/provider/deploy_spec.rb +++ b/spec/unit/provider/deploy_spec.rb @@ -167,9 +167,9 @@ describe Chef::Provider::Deploy do allow(@provider).to receive(:deploy) { raise "Unexpected error" } allow(@provider).to receive(:previous_release_path).and_return("previous_release") expect(@provider).not_to receive(:rollback) - expect { + expect do @provider.run_action(:deploy) - }.to raise_exception(RuntimeError, "Unexpected error") + end.to raise_exception(RuntimeError, "Unexpected error") end it "rollbacks to previous release if error happens on deploy" do @@ -178,9 +178,9 @@ describe Chef::Provider::Deploy do allow(@provider).to receive(:deploy) { raise "Unexpected error" } allow(@provider).to receive(:previous_release_path).and_return("previous_release") expect(@provider).to receive(:rollback) - expect { + expect do @provider.run_action(:deploy) - }.to raise_exception(RuntimeError, "Unexpected error") + end.to raise_exception(RuntimeError, "Unexpected error") end describe "on systems without broken Dir.glob results" do @@ -232,17 +232,17 @@ describe Chef::Provider::Deploy do #FileUtils.should_receive(:rm_rf).with("/my/deploy/dir/releases/20040815162342") #@provider.run_action(:rollback) #@provider.release_path.should eql(NIL) -- no check needed since assertions will fail - expect { + expect do @provider.run_action(:rollback) - }.to raise_exception(RuntimeError, "There is no release to rollback to!") + end.to raise_exception(RuntimeError, "There is no release to rollback to!") end it "an exception is raised when there are no releases" do all_releases = [] allow(Dir).to receive(:glob).with("/my/deploy/dir/releases/*").and_return(all_releases) - expect { + expect do @provider.run_action(:rollback) - }.to raise_exception(RuntimeError, "There is no release to rollback to!") + end.to raise_exception(RuntimeError, "There is no release to rollback to!") end end end diff --git a/spec/unit/provider/dsc_resource_spec.rb b/spec/unit/provider/dsc_resource_spec.rb index 2a00c2ed7f..34eb9727f8 100644 --- a/spec/unit/provider/dsc_resource_spec.rb +++ b/spec/unit/provider/dsc_resource_spec.rb @@ -28,11 +28,11 @@ describe Chef::Provider::DscResource do end context "when Powershell does not support Invoke-DscResource" do - let (:node) { + let (:node) do node = Chef::Node.new node.automatic[:languages][:powershell][:version] = "4.0" node - } + end it "raises a ProviderNotFound exception" do expect(provider).not_to receive(:meta_configuration) expect { provider.run_action(:run) }.to raise_error( @@ -44,11 +44,11 @@ describe Chef::Provider::DscResource do context "when RefreshMode is not set to Disabled" do context "and the WMF 5 is a preview release" do - let (:node) { + let (:node) do node = Chef::Node.new node.automatic[:languages][:powershell][:version] = "5.0.10018.0" node - } + end it "raises an exception" do expect(provider).to receive(:dsc_refresh_mode_disabled?).and_return(false) expect { provider.run_action(:run) }.to raise_error( @@ -56,11 +56,11 @@ describe Chef::Provider::DscResource do end end context "and the WMF is 5 RTM or newer" do - let (:node) { + let (:node) do node = Chef::Node.new node.automatic[:languages][:powershell][:version] = "5.0.10586.0" node - } + end it "does not raises an exception" do expect(provider).to receive(:test_resource) expect(provider).to receive(:set_resource) @@ -72,11 +72,11 @@ describe Chef::Provider::DscResource do end context "when the LCM supports Invoke-DscResource" do - let (:node) { + let (:node) do node = Chef::Node.new node.automatic[:languages][:powershell][:version] = "5.0.10018.0" node - } + end let (:resource_result) { double("CmdletResult", return_value: { "InDesiredState" => true }, stream: "description") } let (:invoke_dsc_resource) { double("cmdlet", run!: resource_result) } let (:store) { double("ResourceStore", find: resource_records) } @@ -153,10 +153,11 @@ describe Chef::Provider::DscResource do end context "multiple resource are found" do - let (:resource_records) { [ + let (:resource_records) do + [ { "Module" => { "Name" => "ModuleName1" } }, { "Module" => { "Name" => "ModuleName2" } }, - ] } + ] end it "raises MultipleDscResourcesFound" do expect { provider.run_action(:run) }.to raise_error(Chef::Exceptions::MultipleDscResourcesFound) diff --git a/spec/unit/provider/dsc_script_spec.rb b/spec/unit/provider/dsc_script_spec.rb index 3877a37e61..5f091b8813 100644 --- a/spec/unit/provider/dsc_script_spec.rb +++ b/spec/unit/provider/dsc_script_spec.rb @@ -23,11 +23,11 @@ require "spec_helper" describe Chef::Provider::DscScript do context "when DSC is available" do - let (:node) { + let (:node) do node = Chef::Node.new node.automatic[:languages][:powershell][:version] = "4.0" node - } + end let (:events) { Chef::EventDispatch::Dispatcher.new } let (:run_context) { Chef::RunContext.new(node, {}, events) } let (:resource) { Chef::Resource::DscScript.new("script", run_context) } @@ -35,7 +35,7 @@ describe Chef::Provider::DscScript do Chef::Provider::DscScript.new(resource, run_context) end - describe '#load_current_resource' do + describe "#load_current_resource" do it "describes the resource as converged if there were 0 DSC resources" do allow(provider).to receive(:run_configuration).with(:test).and_return([]) provider.load_current_resource @@ -75,7 +75,7 @@ describe Chef::Provider::DscScript do end end - describe '#generate_configuration_document' do + describe "#generate_configuration_document" do # I think integration tests should cover these cases it "uses configuration_document_from_script_path when a dsc script file is given" do @@ -123,7 +123,7 @@ describe Chef::Provider::DscScript do end end - describe '#generate_description' do + describe "#generate_description" do it "removes the resource name from the beginning of any log line from the LCM" do dsc_resource_info = Chef::Util::DSC::ResourceInfo.new("resourcename", true, ["resourcename doing something", "lastline"]) provider.instance_variable_set("@dsc_resources_info", [dsc_resource_info]) @@ -156,16 +156,16 @@ describe Chef::Provider::DscScript do it "raises an exception for powershell version '#{version}'" do node.automatic[:languages][:powershell][:version] = version - expect { + expect do provider.run_action(:run) - }.to raise_error(Chef::Exceptions::ProviderNotFound) + end.to raise_error(Chef::Exceptions::ProviderNotFound) end end it "raises an exception if Powershell is not present" do - expect { + expect do provider.run_action(:run) - }.to raise_error(Chef::Exceptions::ProviderNotFound) + end.to raise_error(Chef::Exceptions::ProviderNotFound) end end diff --git a/spec/unit/provider/env/windows_spec.rb b/spec/unit/provider/env/windows_spec.rb index abe2344443..5ddc1d6f91 100644 --- a/spec/unit/provider/env/windows_spec.rb +++ b/spec/unit/provider/env/windows_spec.rb @@ -24,16 +24,16 @@ describe Chef::Provider::Env::Windows, :windows_only do let(:run_context) { Chef::RunContext.new(node, {}, events) } context "when environment variable is not PATH" do - let(:new_resource) { + let(:new_resource) do new_resource = Chef::Resource::Env.new("CHEF_WINDOWS_ENV_TEST") new_resource.value("foo") new_resource - } - let(:provider) { + end + let(:provider) do provider = Chef::Provider::Env::Windows.new(new_resource, run_context) allow(provider).to receive(:env_obj).and_return(double("null object").as_null_object) provider - } + end describe "action_create" do before do @@ -76,16 +76,16 @@ describe Chef::Provider::Env::Windows, :windows_only do describe "for PATH" do let(:system_root) { "%SystemRoot%" } let(:system_root_value) { 'D:\Windows' } - let(:new_resource) { + let(:new_resource) do new_resource = Chef::Resource::Env.new("PATH") new_resource.value(system_root) new_resource - } - let(:provider) { + end + let(:provider) do provider = Chef::Provider::Env::Windows.new(new_resource, run_context) allow(provider).to receive(:env_obj).and_return(double("null object").as_null_object) provider - } + end before do stub_const("ENV", { "PATH" => "" }) diff --git a/spec/unit/provider/file/content_spec.rb b/spec/unit/provider/file/content_spec.rb index a31c75baf4..f840d92dbb 100644 --- a/spec/unit/provider/file/content_spec.rb +++ b/spec/unit/provider/file/content_spec.rb @@ -28,12 +28,12 @@ describe Chef::Provider::File::Content do double("Chef::Provider::File::Resource (current)") end - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates"))) - } - let(:resource_path) { + end + let(:resource_path) do canonicalize_path(File.expand_path(File.join(enclosing_directory, "seattle.txt"))) - } + end let(:new_resource) do double("Chef::Provider::File::Resource (new)", :name => "seattle.txt", :path => resource_path) @@ -77,9 +77,9 @@ describe Chef::Provider::File::Content do end context "when creating a tempfiles in destdir fails" do - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path("/nonexisting/path") - } + end it "returns a tempfile in the tempdir when :file_deployment_uses_destdir is set to :auto" do Chef::Config[:file_staging_uses_destdir] = :auto diff --git a/spec/unit/provider/file_spec.rb b/spec/unit/provider/file_spec.rb index 11bef8a44d..d8d4ed2286 100644 --- a/spec/unit/provider/file_spec.rb +++ b/spec/unit/provider/file_spec.rb @@ -35,12 +35,12 @@ describe Chef::Provider::File do let(:node) { double("Chef::Node") } let(:events) { double("Chef::Events").as_null_object } # mock all the methods let(:run_context) { double("Chef::RunContext", :node => node, :events => events) } - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates"))) - } - let(:resource_path) { + end + let(:resource_path) do canonicalize_path(File.expand_path(File.join(enclosing_directory, "seattle.txt"))) - } + end # Subject diff --git a/spec/unit/provider/launchd_spec.rb b/spec/unit/provider/launchd_spec.rb index 4286405ef5..2893044833 100644 --- a/spec/unit/provider/launchd_spec.rb +++ b/spec/unit/provider/launchd_spec.rb @@ -51,7 +51,8 @@ describe Chef::Provider::Launchd do </plist> XML - let(:test_hash) do { + let(:test_hash) do + { "Label" => "call.mom.weekly", "Program" => "/Library/scripts/call_mom.sh", "StartCalendarInterval" => { diff --git a/spec/unit/provider/link_spec.rb b/spec/unit/provider/link_spec.rb index 6bb08551a2..9426cf41dc 100644 --- a/spec/unit/provider/link_spec.rb +++ b/spec/unit/provider/link_spec.rb @@ -249,4 +249,159 @@ describe Chef::Resource::Link, :not_supported_on_win2k3 do end end end + + describe "action_delete" do + before(:each) do + stat = double("stats", :ino => 5) + allow(stat).to receive(:uid).and_return(501) + allow(stat).to receive(:gid).and_return(501) + allow(stat).to receive(:mode).and_return(0755) + allow(provider.file_class).to receive(:stat).with( + "#{CHEF_SPEC_DATA}/fofile-link").and_return(stat) + + provider.load_current_resource + end + + shared_context "delete link to directories on Windows" do + before do + allow(::File).to receive(:directory?).with( + "#{CHEF_SPEC_DATA}/fofile-link").and_return(true) + end + + it "invokes Dir.delete method to delete the link" do + expect(::Dir).to receive(:delete).with(provider.new_resource.target_file) + expect(Chef::Log).to receive(:info).with("#{provider.new_resource} deleted") + provider.run_action(:delete) + end + end + + shared_context "delete link to directories on Linux" do + before do + allow(::File).to receive(:directory?).with( + "#{CHEF_SPEC_DATA}/fofile-link").and_return(true) + end + + it "invokes File.delete method to delete the link" do + expect(::File).to receive(:delete).with(provider.new_resource.target_file) + expect(Chef::Log).to receive(:info).with("#{provider.new_resource} deleted") + provider.run_action(:delete) + end + end + + shared_context "delete link to files" do + before do + allow(::File).to receive(:directory?).with( + "#{CHEF_SPEC_DATA}/fofile-link").and_return(false) + end + + it "invokes File.delete method to delete the link" do + expect(::File).to receive(:delete).with(provider.new_resource.target_file) + expect(Chef::Log).to receive(:info).with("#{provider.new_resource} deleted") + provider.run_action(:delete) + end + end + + shared_context "soft links prerequisites" do + before(:each) do + allow(provider.file_class).to receive(:symlink?).with( + "#{CHEF_SPEC_DATA}/fofile-link").and_return(true) + allow(provider.file_class).to receive(:readlink).with( + "#{CHEF_SPEC_DATA}/fofile-link").and_return("#{CHEF_SPEC_DATA}/fofile") + end + end + + shared_context "hard links prerequisites" do + let(:new_resource) do + result = Chef::Resource::Link.new("#{CHEF_SPEC_DATA}/fofile-link") + result.to "#{CHEF_SPEC_DATA}/fofile" + result.link_type :hard + result + end + + before(:each) do + stat = double("stats", :ino => 5) + allow(stat).to receive(:uid).and_return(502) + allow(stat).to receive(:gid).and_return(502) + allow(stat).to receive(:mode).and_return(0644) + + allow(provider.file_class).to receive(:symlink?).with( + "#{CHEF_SPEC_DATA}/fofile-link").and_return(false) + + allow(File).to receive(:exists?).with( + "#{CHEF_SPEC_DATA}/fofile-link").and_return(true) + allow(File).to receive(:exists?).with( + "#{CHEF_SPEC_DATA}/fofile").and_return(true) + + allow(provider.file_class).to receive(:stat).with( + "#{CHEF_SPEC_DATA}/fofile").and_return(stat) + end + end + + context "on Windows platform" do + let(:resource_link) do + Chef::Resource::Link.new(provider.new_resource.name) + end + + before(:each) do + allow(Chef::Resource::Link).to receive(:new).with( + provider.new_resource.name).and_return(resource_link) + allow(resource_link).to receive(:verify_links_supported!) + allow(Chef::Platform).to receive(:windows?).and_return(true) + end + + context "soft links" do + include_context "soft links prerequisites" + + context "to directories" do + include_context "delete link to directories on Windows" + end + + context "to files" do + include_context "delete link to files" + end + end + + context "hard links" do + include_context "hard links prerequisites" + + context "to directories" do + include_context "delete link to directories on Windows" + end + + context "to files" do + include_context "delete link to files" + end + end + end + + context "on Linux platform" do + before(:each) do + allow(Chef::Platform).to receive(:windows?).and_return(false) + end + + context "soft links" do + include_context "soft links prerequisites" + + context "to directories" do + include_context "delete link to directories on Linux" + end + + context "to files" do + include_context "delete link to files" + end + end + + context "hard links" do + include_context "hard links prerequisites" + + context "to directories" do + include_context "delete link to directories on Linux" + end + + context "to files" do + include_context "delete link to files" + end + end + end + end end diff --git a/spec/unit/provider/mount/mount_spec.rb b/spec/unit/provider/mount/mount_spec.rb index 42585d9e3e..0b956d9bb9 100644 --- a/spec/unit/provider/mount/mount_spec.rb +++ b/spec/unit/provider/mount/mount_spec.rb @@ -82,7 +82,7 @@ describe Chef::Provider::Mount::Mount do it "should raise an error if the mount device does not exist" do allow(::File).to receive(:exists?).with("/dev/sdz1").and_return false - expect { @provider.load_current_resource();@provider.mountable? }.to raise_error(Chef::Exceptions::Mount) + expect { @provider.load_current_resource(); @provider.mountable? }.to raise_error(Chef::Exceptions::Mount) end it "should not call mountable? with load_current_resource - CHEF-1565" do @@ -99,25 +99,25 @@ describe Chef::Provider::Mount::Mount do @new_resource.device "d21afe51-a0fe-4dc6-9152-ac733763ae0a" expect(@provider).to receive(:shell_out).with("/sbin/findfs UUID=d21afe51-a0fe-4dc6-9152-ac733763ae0a").and_return(status) expect(::File).to receive(:exists?).with("").and_return(false) - expect { @provider.load_current_resource();@provider.mountable? }.to raise_error(Chef::Exceptions::Mount) + expect { @provider.load_current_resource(); @provider.mountable? }.to raise_error(Chef::Exceptions::Mount) end it "should raise an error if the mount point does not exist" do allow(::File).to receive(:exists?).with("/tmp/foo").and_return false - expect { @provider.load_current_resource();@provider.mountable? }.to raise_error(Chef::Exceptions::Mount) + expect { @provider.load_current_resource(); @provider.mountable? }.to raise_error(Chef::Exceptions::Mount) end %w{tmpfs fuse cgroup}.each do |fstype| it "does not expect the device to exist for #{fstype}" do @new_resource.fstype(fstype) @new_resource.device("whatever") - expect { @provider.load_current_resource();@provider.mountable? }.not_to raise_error + expect { @provider.load_current_resource(); @provider.mountable? }.not_to raise_error end end it "does not expect the device to exist if it's none" do @new_resource.device("none") - expect { @provider.load_current_resource();@provider.mountable? }.not_to raise_error + expect { @provider.load_current_resource(); @provider.mountable? }.not_to raise_error end it "should set mounted true if the mount point is found in the mounts list" do diff --git a/spec/unit/provider/mount/solaris_spec.rb b/spec/unit/provider/mount/solaris_spec.rb index 51ace83470..264c8b9b36 100644 --- a/spec/unit/provider/mount/solaris_spec.rb +++ b/spec/unit/provider/mount/solaris_spec.rb @@ -41,7 +41,7 @@ describe Chef::Provider::Mount::Solaris, :unix_only do let(:options) { nil } - let(:new_resource) { + let(:new_resource) do new_resource = Chef::Resource::Mount.new(mountpoint) new_resource.device device new_resource.device_type device_type @@ -50,13 +50,13 @@ describe Chef::Provider::Mount::Solaris, :unix_only do new_resource.options options new_resource.supports :remount => false new_resource - } + end - let(:provider) { + let(:provider) do Chef::Provider::Mount::Solaris.new(new_resource, run_context) - } + end - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" #device device mount FS fsck mount mount #to mount to fsck point type pass at boot options @@ -74,21 +74,21 @@ describe Chef::Provider::Mount::Solaris, :unix_only do # ufs /dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes - EOF - } + end - let(:vfstab_file) { + let(:vfstab_file) do t = Tempfile.new("rspec-vfstab") t.write(vfstab_file_contents) t.close t - } + end - let(:mount_output) { + let(:mount_output) do <<-EOF.gsub /^\s*/, "" /dev/dsk/c0t0d0s0 on / type ufs read/write/setuid/intr/largefiles/xattr/onerror=panic/dev=2200000 on Tue Jul 31 22:34:46 2012 /dev/dsk/c0t2d0s7 on /mnt/foo type ufs read/write/setuid/intr/largefiles/xattr/onerror=panic/dev=2200007 on Tue Jul 31 22:34:46 2012 EOF - } + end before do stub_const("Chef::Provider::Mount::Solaris::VFSTAB", vfstab_file.path ) @@ -214,7 +214,7 @@ describe Chef::Provider::Mount::Solaris, :unix_only do describe "#load_current_resource" do context "when loading a normal UFS filesystem with noauto, don't mount at boot" do - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" #device device mount FS fsck mount mount #to mount to fsck point type pass at boot options @@ -232,7 +232,7 @@ describe Chef::Provider::Mount::Solaris, :unix_only do # ufs /dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 no - EOF - } + end before do provider.load_current_resource @@ -244,16 +244,16 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the device is an smbfs mount" do - let(:mount_output) { + let(:mount_output) do <<-EOF.gsub /^\s*/, "" //solarsystem/tmp on /mnt type smbfs read/write/setuid/devices/dev=5080000 on Tue Mar 29 11:40:18 2011 EOF - } - let(:vfstab_file_contents) { + end + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" //WORKGROUP;username:password@host/share - /mountpoint smbfs - no fileperms=0777,dirperms=0777 EOF - } + end let(:fsck_device) { "-" } @@ -263,17 +263,17 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the device is an NFS mount" do - let(:mount_output) { + let(:mount_output) do <<-EOF.gsub /^\s*/, "" cartman:/share2 on /cartman type nfs rsize=32768,wsize=32768,NFSv4,dev=4000004 on Tue Mar 29 11:40:18 2011 EOF - } + end - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" cartman:/share2 - /cartman nfs - yes rw,soft EOF - } + end let(:fsck_device) { "-" } @@ -334,17 +334,17 @@ describe Chef::Provider::Mount::Solaris, :unix_only do let(:target) { "/dev/mapper/target" } - let(:mount_output) { + let(:mount_output) do <<-EOF.gsub /^\s*/, "" #{target} on /mnt/foo type ufs read/write/setuid/intr/largefiles/xattr/onerror=panic/dev=2200007 on Tue Jul 31 22:34:46 2012 EOF - } + end - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" #{target} /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes - EOF - } + end before do expect(File).to receive(:symlink?).with(device).at_least(:once).and_return(true) @@ -371,17 +371,17 @@ describe Chef::Provider::Mount::Solaris, :unix_only do let(:absolute_target) { File.expand_path(target, File.dirname(device)) } - let(:mount_output) { + let(:mount_output) do <<-EOF.gsub /^\s*/, "" #{absolute_target} on /mnt/foo type ufs read/write/setuid/intr/largefiles/xattr/onerror=panic/dev=2200007 on Tue Jul 31 22:34:46 2012 EOF - } + end - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" #{absolute_target} /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes - EOF - } + end before do expect(File).to receive(:symlink?).with(device).at_least(:once).and_return(true) @@ -404,12 +404,12 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the matching mount point is last in the mounts list" do - let(:mount_output) { + let(:mount_output) do <<-EOF.gsub /^\s*/, "" /dev/dsk/c0t0d0s0 on /mnt/foo type ufs read/write/setuid/intr/largefiles/xattr/onerror=panic/dev=2200000 on Tue Jul 31 22:34:46 2012 /dev/dsk/c0t2d0s7 on /mnt/foo type ufs read/write/setuid/intr/largefiles/xattr/onerror=panic/dev=2200007 on Tue Jul 31 22:34:46 2012 EOF - } + end it "should set mounted true" do provider.load_current_resource() expect(provider.current_resource.mounted).to be_truthy @@ -417,12 +417,12 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the matching mount point is not last in the mounts list" do - let(:mount_output) { + let(:mount_output) do <<-EOF.gsub /^\s*/, "" /dev/dsk/c0t2d0s7 on /mnt/foo type ufs read/write/setuid/intr/largefiles/xattr/onerror=panic/dev=2200007 on Tue Jul 31 22:34:46 2012 /dev/dsk/c0t0d0s0 on /mnt/foo type ufs read/write/setuid/intr/largefiles/xattr/onerror=panic/dev=2200000 on Tue Jul 31 22:34:46 2012 EOF - } + end it "should set mounted false" do provider.load_current_resource() expect(provider.current_resource.mounted).to be_falsey @@ -430,11 +430,11 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the matching mount point is not in the mounts list (mountpoint wrong)" do - let(:mount_output) { + let(:mount_output) do <<-EOF.gsub /^\s*/, "" /dev/dsk/c0t2d0s7 on /mnt/foob type ufs read/write/setuid/intr/largefiles/xattr/onerror=panic/dev=2200007 on Tue Jul 31 22:34:46 2012 EOF - } + end it "should set mounted false" do provider.load_current_resource() expect(provider.current_resource.mounted).to be_falsey @@ -442,11 +442,11 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the matching mount point is not in the mounts list (raw device wrong)" do - let(:mount_output) { + let(:mount_output) do <<-EOF.gsub /^\s*/, "" /dev/dsk/c0t2d0s72 on /mnt/foo type ufs read/write/setuid/intr/largefiles/xattr/onerror=panic/dev=2200007 on Tue Jul 31 22:34:46 2012 EOF - } + end it "should set mounted false" do provider.load_current_resource() expect(provider.current_resource.mounted).to be_falsey @@ -454,12 +454,12 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the mount point is last in fstab" do - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" /dev/dsk/c0t2d0s72 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes - /dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes - EOF - } + end it "should set enabled to true" do provider.load_current_resource @@ -468,12 +468,12 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the mount point is not last in fstab and is a substring of another mount" do - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" /dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes - /dev/dsk/c0t2d0s72 /dev/rdsk/c0t2d0s7 /mnt/foo/bar ufs 2 yes - EOF - } + end it "should set enabled to true" do provider.load_current_resource @@ -482,12 +482,12 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the mount point is not last in fstab" do - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" /dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes - /dev/dsk/c0t2d0s72 /dev/rdsk/c0t2d0s72 /mnt/foo ufs 2 yes - EOF - } + end it "should set enabled to false" do provider.load_current_resource @@ -496,11 +496,11 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the mount point is not in fstab, but the mountpoint is a substring of one that is" do - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" /dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foob ufs 2 yes - EOF - } + end it "should set enabled to false" do provider.load_current_resource @@ -509,11 +509,11 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the mount point is not in fstab, but the device is a substring of one that is" do - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" /dev/dsk/c0t2d0s72 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes - EOF - } + end it "should set enabled to false" do provider.load_current_resource @@ -522,11 +522,11 @@ describe Chef::Provider::Mount::Solaris, :unix_only do end context "when the mountpoint line is commented out" do - let(:vfstab_file_contents) { + let(:vfstab_file_contents) do <<-EOF.gsub /^\s*/, "" #/dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes - EOF - } + end it "should set enabled to false" do provider.load_current_resource diff --git a/spec/unit/provider/osx_profile_spec.rb b/spec/unit/provider/osx_profile_spec.rb index e307bc2c22..a1dcf3ecd6 100644 --- a/spec/unit/provider/osx_profile_spec.rb +++ b/spec/unit/provider/osx_profile_spec.rb @@ -238,6 +238,12 @@ describe Chef::Provider::OsxProfile do ).to eql(new_resource.identifier) end + it "should work with spaces in the identifier" do + provider.action = :remove + provider.define_resource_requirements + expect { provider.process_resource_requirements }.not_to raise_error + end + it "should build the shellout remove command correctly" do new_resource.identifier "com.testprofile.screensaver" new_resource.action(:remove) diff --git a/spec/unit/provider/package/chocolatey_spec.rb b/spec/unit/provider/package/chocolatey_spec.rb index 8344c3d0ec..704ef1aef2 100644 --- a/spec/unit/provider/package/chocolatey_spec.rb +++ b/spec/unit/provider/package/chocolatey_spec.rb @@ -462,18 +462,18 @@ describe "behavior when Chocolatey is not installed" do Chef::Provider::Package::Chocolatey.new(new_resource, run_context) end - before { + before do # the shellout sometimes returns "", but test nil to be safe. allow(provider).to receive(:choco_install_path).and_return(nil) provider.instance_variable_set("@choco_install_path", nil) # we don't care what this returns, but we have to let it be called. allow(provider).to receive(:shell_out!).and_return(double(:stdout => "")) - } + end - let(:error_regex) { + let(:error_regex) do /Could not locate.*install.*cookbook.*PowerShell.*GetEnvironmentVariable/m - } + end context "#choco_exe" do it "triggers a MissingLibrary exception when Chocolatey is not installed" do diff --git a/spec/unit/provider/package/freebsd/port_spec.rb b/spec/unit/provider/package/freebsd/port_spec.rb index b593da4ff1..4ae8d960a2 100644 --- a/spec/unit/provider/package/freebsd/port_spec.rb +++ b/spec/unit/provider/package/freebsd/port_spec.rb @@ -74,8 +74,8 @@ describe Chef::Provider::Package::Freebsd::Port do it "should check 'pkg info' if make supports WITH_PKGNG if freebsd version is < 1000017" do pkg_enabled = OpenStruct.new(:stdout => "yes\n") - [1000016, 1000000, 901503, 902506, 802511].each do |__freebsd_version| - @node.automatic_attrs[:os_version] = __freebsd_version + [1000016, 1000000, 901503, 902506, 802511].each do |freebsd_version| + @node.automatic_attrs[:os_version] = freebsd_version expect(@new_resource).to receive(:shell_out!).with("make -V WITH_PKGNG", env: nil).and_return(pkg_enabled) expect(@provider).to receive(:shell_out!).with('pkg info "zsh"', env: nil, returns: [0, 70], timeout: 900).and_return(@pkg_info) expect(@provider.current_installed_version).to eq("3.1.7") @@ -83,8 +83,8 @@ describe Chef::Provider::Package::Freebsd::Port do end it "should check 'pkg info' if the freebsd version is greater than or equal to 1000017" do - __freebsd_version = 1000017 - @node.automatic_attrs[:os_version] = __freebsd_version + freebsd_version = 1000017 + @node.automatic_attrs[:os_version] = freebsd_version expect(@provider).to receive(:shell_out!).with('pkg info "zsh"', env: nil, returns: [0, 70], timeout: 900).and_return(@pkg_info) expect(@provider.current_installed_version).to eq("3.1.7") end diff --git a/spec/unit/provider/package/rubygems_spec.rb b/spec/unit/provider/package/rubygems_spec.rb index f87c261ec0..4934505583 100644 --- a/spec/unit/provider/package/rubygems_spec.rb +++ b/spec/unit/provider/package/rubygems_spec.rb @@ -16,7 +16,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -require "pp" module GemspecBackcompatCreator def gemspec(name, version) @@ -64,7 +63,7 @@ describe Chef::Provider::Package::Rubygems::CurrentGemEnvironment do begin @gem_env.with_gem_sources("http://gems.example.org") do sources_in_block = Gem.sources - raise RuntimeError, "sources should be reset even in case of an error" + raise "sources should be reset even in case of an error" end rescue RuntimeError end @@ -78,7 +77,7 @@ describe Chef::Provider::Package::Rubygems::CurrentGemEnvironment do begin @gem_env.with_gem_sources(nil) do sources_in_block = Gem.sources - raise RuntimeError, "sources should be reset even in case of an error" + raise "sources should be reset even in case of an error" end rescue RuntimeError end @@ -86,34 +85,55 @@ describe Chef::Provider::Package::Rubygems::CurrentGemEnvironment do expect(Gem.sources).to eq(normal_sources) end - it "finds a matching gem candidate version" do - dep = Gem::Dependency.new("rspec", ">= 0") - dep_installer = Gem::DependencyInstaller.new - allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) - latest = [[gemspec("rspec", Gem::Version.new("1.3.0")), "https://rubygems.org/"]] - expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(latest) - expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new("rspec", ">= 0"))).to eq(Gem::Version.new("1.3.0")) - end + context "new default rubygems behavior" do + before do + Chef::Config[:rubygems_cache_enabled] = false + end - it "finds a matching gem candidate version on rubygems 2.0.0+" do - dep = Gem::Dependency.new("rspec", ">= 0") - dep_installer = Gem::DependencyInstaller.new - allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) - best_gem = double("best gem match", :spec => gemspec("rspec", Gem::Version.new("1.3.0")), :source => "https://rubygems.org") - available_set = double("Gem::AvailableSet test double") - expect(available_set).to receive(:pick_best!) - expect(available_set).to receive(:set).and_return([best_gem]) - expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(available_set) - expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new("rspec", ">= 0"))).to eq(Gem::Version.new("1.3.0")) + it "finds a matching gem candidate version on rubygems 2.0.0+" do + dep = Gem::Dependency.new("rspec", ">= 0") + dep_installer = Gem::DependencyInstaller.new + allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) + expect(dep_installer).not_to receive(:find_gems_with_sources).with(dep).and_call_original + expect(@gem_env.candidate_version_from_remote(dep)).to be_kind_of(Gem::Version) + end + + it "gives the candidate version as nil if none is found" do + dep = Gem::Dependency.new("lksdjflksdjflsdkfj", ">= 0") + dep_installer = Gem::DependencyInstaller.new + allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) + expect(dep_installer).not_to receive(:find_gems_with_sources).with(dep).and_call_original + expect(@gem_env.candidate_version_from_remote(dep)).to be_nil + end + + it "finds a matching gem from a specific gemserver when explicit sources are given (to a server that doesn't respond to api requests)" do + dep = Gem::Dependency.new("rspec", ">= 0") + dep_installer = Gem::DependencyInstaller.new + allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) + expect(dep_installer).not_to receive(:find_gems_with_sources).with(dep).and_call_original + expect(@gem_env.candidate_version_from_remote(dep, "http://production.cf.rubygems.org")).to be_kind_of(Gem::Version) + end end - it "gives the candidate version as nil if none is found" do - dep = Gem::Dependency.new("rspec", ">= 0") - latest = [] - dep_installer = Gem::DependencyInstaller.new - allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) - expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(latest) - expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new("rspec", ">= 0"))).to be_nil + context "old rubygems caching behavior" do + before do + Chef::Config[:rubygems_cache_enabled] = true + end + + it "finds a matching gem candidate version on rubygems 2.0.0+" do + dep = Gem::Dependency.new("rspec", ">= 0") + expect(@gem_env.candidate_version_from_remote(dep)).to be_kind_of(Gem::Version) + end + + it "gives the candidate version as nil if none is found" do + dep = Gem::Dependency.new("lksdjflksdjflsdkfj", ">= 0") + expect(@gem_env.candidate_version_from_remote(dep)).to be_nil + end + + it "finds a matching gem from a specific gemserver when explicit sources are given" do + dep = Gem::Dependency.new("rspec", ">= 0") + expect(@gem_env.candidate_version_from_remote(dep, "http://production.cf.rubygems.org")).to be_kind_of(Gem::Version) + end end it "finds a matching candidate version from a .gem file when the path to the gem is supplied" do @@ -122,17 +142,6 @@ describe Chef::Provider::Package::Rubygems::CurrentGemEnvironment do expect(@gem_env.candidate_version_from_file(Gem::Dependency.new("chef-integration-test", ">= 0.2.0"), location)).to be_nil end - it "finds a matching gem from a specific gemserver when explicit sources are given" do - dep = Gem::Dependency.new("rspec", ">= 0") - latest = [[gemspec("rspec", Gem::Version.new("1.3.0")), "https://rubygems.org/"]] - - expect(@gem_env).to receive(:with_gem_sources).with("http://gems.example.com").and_yield - dep_installer = Gem::DependencyInstaller.new - allow(@gem_env).to receive(:dependency_installer).and_return(dep_installer) - expect(dep_installer).to receive(:find_gems_with_sources).with(dep).and_return(latest) - expect(@gem_env.candidate_version_from_remote(Gem::Dependency.new("rspec", ">=0"), "http://gems.example.com")).to eq(Gem::Version.new("1.3.0")) - end - it "installs a gem with a hash of options for the dependency installer" do dep_installer = Gem::DependencyInstaller.new expect(@gem_env).to receive(:dependency_installer).with(:install_dir => "/foo/bar").and_return(dep_installer) @@ -545,7 +554,6 @@ describe Chef::Provider::Package::Rubygems do expect(provider.candidate_version).to eq("0.1.0") end end - end describe "when installing a gem" do @@ -586,10 +594,9 @@ describe Chef::Provider::Package::Rubygems do context "when source is a path" do let(:source) { CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem" } - let(:domain) { { domain: :local } } it "installs the gem from file via the gems api" do - expect(provider.gem_env).to receive(:install).with(source, domain) + expect(provider.gem_env).to receive(:install).with(source) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -597,11 +604,10 @@ describe Chef::Provider::Package::Rubygems do context "when the gem name is a file path and source is nil" do let(:gem_name) { CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem" } - let(:domain) { { domain: :local } } it "installs the gem from file via the gems api" do expect(new_resource.source).to eq(gem_name) - expect(provider.gem_env).to receive(:install).with(gem_name, domain) + expect(provider.gem_env).to receive(:install).with(gem_name) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end diff --git a/spec/unit/provider/package/windows/exe_spec.rb b/spec/unit/provider/package/windows/exe_spec.rb index b514f80a35..f18cbf3dca 100644 --- a/spec/unit/provider/package/windows/exe_spec.rb +++ b/spec/unit/provider/package/windows/exe_spec.rb @@ -122,6 +122,15 @@ describe Chef::Provider::Package::Windows::Exe do end end + context "When timeout value is passed" do + it "removes installed package and quotes uninstall string" do + new_resource.timeout = 300 + allow(::File).to receive(:exist?).with("uninst_dir/uninst_file").and_return(true) + expect(provider).to receive(:shell_out!).with(/start \"\" \/wait \"uninst_dir\/uninst_file\" \/S \/NCRC & exit %%%%ERRORLEVEL%%%%/, :timeout => 300, :returns => [0]) + provider.remove_package + end + end + context "several packages installed with quoted uninstall strings" do let(:uninstall_hash) do [ diff --git a/spec/unit/provider/package/yum_spec.rb b/spec/unit/provider/package/yum_spec.rb index ab726134e9..e9aec933e2 100644 --- a/spec/unit/provider/package/yum_spec.rb +++ b/spec/unit/provider/package/yum_spec.rb @@ -312,7 +312,8 @@ describe Chef::Provider::Package::Yum do end context "when the package name isn't found" do - let(:yum_cache) { double( + let(:yum_cache) do + double( "Chef::Provider::Yum::YumCache", :reload_installed => true, :reset => true, @@ -322,7 +323,7 @@ describe Chef::Provider::Package::Yum do :version_available? => true, :disable_extra_repo_control => true ) - } + end before do allow(Chef::Provider::Package::Yum::YumCache).to receive(:instance).and_return(yum_cache) @@ -1063,21 +1064,21 @@ describe Chef::Provider::Package::Yum::RPMVersion do end it "should raise an error unless passed 1 or 3 args" do - expect { + expect do Chef::Provider::Package::Yum::RPMVersion.new() - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMVersion.new("1:1.6.5-9.36.el5") - }.not_to raise_error - expect { + end.not_to raise_error + expect do Chef::Provider::Package::Yum::RPMVersion.new("1:1.6.5-9.36.el5", "extra") - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMVersion.new("1", "1.6.5", "9.36.el5") - }.not_to raise_error - expect { + end.not_to raise_error + expect do Chef::Provider::Package::Yum::RPMVersion.new("1", "1.6.5", "9.36.el5", "extra") - }.to raise_error(ArgumentError) + end.to raise_error(ArgumentError) end # thanks version_class_spec.rb! @@ -1229,9 +1230,9 @@ describe Chef::Provider::Package::Yum::RPMPackage do it "should always have at least one provide, itself" do expect(@rpm.provides.size).to eq(1) - @rpm.provides[0].name == "testing" - @rpm.provides[0].version.evr == "1:1.6.5-9.36.el5" - @rpm.provides[0].flag == :== + expect(@rpm.provides[0].name).to eql("testing") + expect(@rpm.provides[0].version.evr).to eql("1:1.6.5-9.36.el5") + expect(@rpm.provides[0].flag).to eql(:==) end end @@ -1253,37 +1254,37 @@ describe Chef::Provider::Package::Yum::RPMPackage do it "should always have at least one provide, itself" do expect(@rpm.provides.size).to eq(1) - @rpm.provides[0].name == "testing" - @rpm.provides[0].version.evr == "1:1.6.5-9.36.el5" - @rpm.provides[0].flag == :== + expect(@rpm.provides[0].name).to eql("testing") + expect(@rpm.provides[0].version.evr).to eql("1:1.6.5-9.36.el5") + expect(@rpm.provides[0].flag).to eql(:==) end end it "should raise an error unless passed 4 or 6 args" do - expect { + expect do Chef::Provider::Package::Yum::RPMPackage.new() - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMPackage.new("testing") - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMPackage.new("testing", "1:1.6.5-9.36.el5") - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMPackage.new("testing", "1:1.6.5-9.36.el5", "x86_64") - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMPackage.new("testing", "1:1.6.5-9.36.el5", "x86_64", []) - }.not_to raise_error - expect { + end.not_to raise_error + expect do Chef::Provider::Package::Yum::RPMPackage.new("testing", "1", "1.6.5", "9.36.el5", "x86_64") - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMPackage.new("testing", "1", "1.6.5", "9.36.el5", "x86_64", []) - }.not_to raise_error - expect { + end.not_to raise_error + expect do Chef::Provider::Package::Yum::RPMPackage.new("testing", "1", "1.6.5", "9.36.el5", "x86_64", [], "extra") - }.to raise_error(ArgumentError) + end.to raise_error(ArgumentError) end describe "<=>" do @@ -1397,27 +1398,27 @@ describe Chef::Provider::Package::Yum::RPMDependency do end it "should raise an error unless passed 3 or 5 args" do - expect { + expect do Chef::Provider::Package::Yum::RPMDependency.new() - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMDependency.new("testing") - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMDependency.new("testing", "1:1.6.5-9.36.el5") - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMDependency.new("testing", "1:1.6.5-9.36.el5", :==) - }.not_to raise_error - expect { + end.not_to raise_error + expect do Chef::Provider::Package::Yum::RPMDependency.new("testing", "1:1.6.5-9.36.el5", :==, "extra") - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do Chef::Provider::Package::Yum::RPMDependency.new("testing", "1", "1.6.5", "9.36.el5", :==) - }.not_to raise_error - expect { + end.not_to raise_error + expect do Chef::Provider::Package::Yum::RPMDependency.new("testing", "1", "1.6.5", "9.36.el5", :==, "extra") - }.to raise_error(ArgumentError) + end.to raise_error(ArgumentError) end describe "parse" do @@ -1485,12 +1486,12 @@ describe Chef::Provider::Package::Yum::RPMDependency do it "should raise an error unless a RPMDependency is passed" do @rpmprovide = Chef::Provider::Package::Yum::RPMDependency.new("testing", "1:1.6.5-9.36.el5", :==) @rpmrequire = Chef::Provider::Package::Yum::RPMDependency.new("testing", "1:1.6.5-9.36.el5", :>=) - expect { + expect do @rpmprovide.satisfy?("hi") - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do @rpmprovide.satisfy?(@rpmrequire) - }.not_to raise_error + end.not_to raise_error end it "should validate dependency satisfaction logic for standard examples" do @@ -1712,12 +1713,12 @@ describe Chef::Provider::Package::Yum::RPMDb do it "should raise an error unless a RPMDependency is passed" do @rpmprovide = Chef::Provider::Package::Yum::RPMDependency.new("testing", "1:1.6.5-9.36.el5", :==) @rpmrequire = Chef::Provider::Package::Yum::RPMDependency.new("testing", "1:1.6.5-9.36.el5", :>=) - expect { + expect do @rpmdb.whatprovides("hi") - }.to raise_error(ArgumentError) - expect { + end.to raise_error(ArgumentError) + expect do @rpmdb.whatprovides(@rpmrequire) - }.not_to raise_error + end.not_to raise_error end it "should return an Array of packages statisfying a RPMDependency" do @@ -1748,13 +1749,13 @@ describe Chef::Provider::Package::Yum::YumCache do end end - let(:yum_exe) { + let(:yum_exe) do StringIO.new("#!/usr/bin/python\n\naldsjfa\ldsajflkdsjf\lajsdfj") - } + end - let(:bin_exe) { + let(:bin_exe) do StringIO.new(SecureRandom.random_bytes) - } + end before(:each) do @stdin = double("STDIN", :nil_object => true) diff --git a/spec/unit/provider/package_spec.rb b/spec/unit/provider/package_spec.rb index 2ef58db9f3..40b7516b5c 100644 --- a/spec/unit/provider/package_spec.rb +++ b/spec/unit/provider/package_spec.rb @@ -396,13 +396,13 @@ describe Chef::Provider::Package do describe "when installing the preseed file to the cache location" do let(:response_file_destination) { Dir.tmpdir + "/preseed--java--java-6.seed" } - let(:response_file_resource) { + let(:response_file_resource) do response_file_resource = Chef::Resource::CookbookFile.new(response_file_destination, run_context) response_file_resource.cookbook_name = "java" response_file_resource.backup(false) response_file_resource.source("java.response") response_file_resource - } + end before do expect(provider).to receive(:preseed_resource).with("java", "6").and_return(response_file_resource) diff --git a/spec/unit/provider/powershell_script_spec.rb b/spec/unit/provider/powershell_script_spec.rb index 96869ff31c..4fd3f3534d 100644 --- a/spec/unit/provider/powershell_script_spec.rb +++ b/spec/unit/provider/powershell_script_spec.rb @@ -20,7 +20,7 @@ require "spec_helper" describe Chef::Provider::PowershellScript, "action_run" do let(:powershell_version) { nil } - let(:node) { + let(:node) do node = Chef::Node.new node.default["kernel"] = Hash.new node.default["kernel"][:machine] = :x86_64.to_s @@ -28,14 +28,14 @@ describe Chef::Provider::PowershellScript, "action_run" do node.default[:languages] = { :powershell => { :version => powershell_version } } end node - } + end - let(:provider) { + let(:provider) do empty_events = Chef::EventDispatch::Dispatcher.new run_context = Chef::RunContext.new(node, {}, empty_events) new_resource = Chef::Resource::PowershellScript.new("run some powershell code", run_context) Chef::Provider::PowershellScript.new(new_resource, run_context) - } + end context "when setting interpreter flags" do context "on nano" do diff --git a/spec/unit/provider/remote_directory_spec.rb b/spec/unit/provider/remote_directory_spec.rb index b6fd4cfc8e..cb1b6e3cd8 100644 --- a/spec/unit/provider/remote_directory_spec.rb +++ b/spec/unit/provider/remote_directory_spec.rb @@ -121,7 +121,7 @@ describe Chef::Provider::RemoteDirectory do @node.automatic_attrs[:platform] = :just_testing @node.automatic_attrs[:platform_version] = :just_testing - @destination_dir = Dir.mktmpdir << "/remote_directory_test" + @destination_dir = make_canonical_temp_directory << "/remote_directory_test" @resource.path(@destination_dir) end diff --git a/spec/unit/provider/remote_file/ftp_spec.rb b/spec/unit/provider/remote_file/ftp_spec.rb index 9963c401d2..b2fbb7300c 100644 --- a/spec/unit/provider/remote_file/ftp_spec.rb +++ b/spec/unit/provider/remote_file/ftp_spec.rb @@ -19,12 +19,12 @@ require "spec_helper" describe Chef::Provider::RemoteFile::FTP do - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates"))) - } - let(:resource_path) { + end + let(:resource_path) do canonicalize_path(File.expand_path(File.join(enclosing_directory, "seattle.txt"))) - } + end let(:new_resource) do r = Chef::Resource::RemoteFile.new("remote file ftp backend test (new resource)") diff --git a/spec/unit/provider/remote_file/local_file_spec.rb b/spec/unit/provider/remote_file/local_file_spec.rb index 31f14fbe45..6f345cadd1 100644 --- a/spec/unit/provider/remote_file/local_file_spec.rb +++ b/spec/unit/provider/remote_file/local_file_spec.rb @@ -17,6 +17,8 @@ # require "spec_helper" +require "uri" +require "addressable/uri" describe Chef::Provider::RemoteFile::LocalFile do @@ -47,7 +49,7 @@ describe Chef::Provider::RemoteFile::LocalFile do end describe "when given local windows path with spaces" do - let(:uri) { URI.parse(URI.escape("file:///z:/windows/path/foo & bar.txt")) } + let(:uri) { URI.parse(Addressable::URI.encode("file:///z:/windows/path/foo & bar.txt")) } it "returns a valid windows local path" do expect(fetcher.source_path).to eq("z:/windows/path/foo & bar.txt") end @@ -61,7 +63,7 @@ describe Chef::Provider::RemoteFile::LocalFile do end describe "when given unc windows path with spaces" do - let(:uri) { URI.parse(URI.escape("file:////server/share/windows/path/foo & bar.txt")) } + let(:uri) { URI.parse(Addressable::URI.encode("file:////server/share/windows/path/foo & bar.txt")) } it "returns a valid windows unc path" do expect(fetcher.source_path).to eq("//server/share/windows/path/foo & bar.txt") end diff --git a/spec/unit/provider/remote_file/sftp_spec.rb b/spec/unit/provider/remote_file/sftp_spec.rb index 673ea015c0..7be507dc89 100644 --- a/spec/unit/provider/remote_file/sftp_spec.rb +++ b/spec/unit/provider/remote_file/sftp_spec.rb @@ -20,12 +20,12 @@ require "spec_helper" describe Chef::Provider::RemoteFile::SFTP do #built out dependencies - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates"))) - } - let(:resource_path) { + end + let(:resource_path) do canonicalize_path(File.expand_path(File.join(enclosing_directory, "seattle.txt"))) - } + end let(:new_resource) do r = Chef::Resource::RemoteFile.new("remote file sftp backend test (new resource)") diff --git a/spec/unit/provider/remote_file_spec.rb b/spec/unit/provider/remote_file_spec.rb index 6107f93c41..6ceb1d450d 100644 --- a/spec/unit/provider/remote_file_spec.rb +++ b/spec/unit/provider/remote_file_spec.rb @@ -37,12 +37,12 @@ describe Chef::Provider::RemoteFile do let(:node) { double("Chef::Node") } let(:events) { double("Chef::Events").as_null_object } # mock all the methods let(:run_context) { double("Chef::RunContext", :node => node, :events => events) } - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates"))) - } - let(:resource_path) { + end + let(:resource_path) do canonicalize_path(File.expand_path(File.join(enclosing_directory, "seattle.txt"))) - } + end subject(:provider) do provider = described_class.new(resource, run_context) diff --git a/spec/unit/provider/script_spec.rb b/spec/unit/provider/script_spec.rb index 4e8d8bdf59..7e34a8f083 100644 --- a/spec/unit/provider/script_spec.rb +++ b/spec/unit/provider/script_spec.rb @@ -25,12 +25,12 @@ describe Chef::Provider::Script, "action_run" do let(:run_context) { Chef::RunContext.new(node, {}, events) } - let(:new_resource) { + let(:new_resource) do new_resource = Chef::Resource::Script.new("run some perl code") new_resource.code "$| = 1; print 'i like beans'" new_resource.interpreter "perl" new_resource - } + end let(:provider) { Chef::Provider::Script.new(new_resource, run_context) } @@ -87,9 +87,9 @@ describe Chef::Provider::Script, "action_run" do end describe "when running the script" do - let (:default_opts) { + let (:default_opts) do { timeout: 3600, returns: 0, log_level: :info, log_tag: "script[run some perl code]" } - } + end before do allow(STDOUT).to receive(:tty?).and_return(false) diff --git a/spec/unit/provider/service/debian_service_spec.rb b/spec/unit/provider/service/debian_service_spec.rb index 2192671370..799ed991a3 100644 --- a/spec/unit/provider/service/debian_service_spec.rb +++ b/spec/unit/provider/service/debian_service_spec.rb @@ -39,9 +39,9 @@ describe Chef::Provider::Service::Debian do expect(File).to receive(:exists?).with("/usr/sbin/update-rc.d") .and_return(false) @provider.define_resource_requirements - expect { + expect do @provider.process_resource_requirements - }.to raise_error(Chef::Exceptions::Service) + end.to raise_error(Chef::Exceptions::Service) end context "when update-rc.d shows init linked to rc*.d/" do @@ -108,9 +108,9 @@ describe Chef::Provider::Service::Debian do it "raises an error" do @provider.define_resource_requirements - expect { + expect do @provider.process_resource_requirements - }.to raise_error(Chef::Exceptions::Service) + end.to raise_error(Chef::Exceptions::Service) end end diff --git a/spec/unit/provider/service/freebsd_service_spec.rb b/spec/unit/provider/service/freebsd_service_spec.rb index 68d4d63991..10eb3c1a14 100644 --- a/spec/unit/provider/service/freebsd_service_spec.rb +++ b/spec/unit/provider/service/freebsd_service_spec.rb @@ -257,10 +257,11 @@ PS_SAMPLE end context "when the enable variable partial matches (left) some other service and we are disabled" do - let(:lines) { [ + let(:lines) do + [ %Q{thing_#{new_resource.service_name}_enable="YES"}, %Q{#{new_resource.service_name}_enable="NO"}, - ] } + ] end it "sets enabled based on the exact match (false)" do provider.determine_enabled_status! expect(current_resource.enabled).to be false @@ -268,10 +269,11 @@ PS_SAMPLE end context "when the enable variable partial matches (right) some other service and we are disabled" do - let(:lines) { [ + let(:lines) do + [ %Q{#{new_resource.service_name}_thing_enable="YES"}, %Q{#{new_resource.service_name}_enable="NO"}, - ] } + ] end it "sets enabled based on the exact match (false)" do provider.determine_enabled_status! expect(current_resource.enabled).to be false @@ -279,10 +281,11 @@ PS_SAMPLE end context "when the enable variable partial matches (left) some other disabled service and we are enabled" do - let(:lines) { [ + let(:lines) do + [ %Q{thing_#{new_resource.service_name}_enable="NO"}, %Q{#{new_resource.service_name}_enable="YES"}, - ] } + ] end it "sets enabled based on the exact match (true)" do provider.determine_enabled_status! expect(current_resource.enabled).to be true @@ -290,10 +293,11 @@ PS_SAMPLE end context "when the enable variable partial matches (right) some other disabled service and we are enabled" do - let(:lines) { [ + let(:lines) do + [ %Q{#{new_resource.service_name}_thing_enable="NO"}, %Q{#{new_resource.service_name}_enable="YES"}, - ] } + ] end it "sets enabled based on the exact match (true)" do provider.determine_enabled_status! expect(current_resource.enabled).to be true diff --git a/spec/unit/provider/service/openbsd_service_spec.rb b/spec/unit/provider/service/openbsd_service_spec.rb index b11015a63a..872a3bc400 100644 --- a/spec/unit/provider/service/openbsd_service_spec.rb +++ b/spec/unit/provider/service/openbsd_service_spec.rb @@ -174,10 +174,11 @@ describe Chef::Provider::Service::Openbsd do end context "when the enable variable partial matches (left) some other service and we are disabled" do - let(:lines) { [ + let(:lines) do + [ %Q{thing_#{provider.builtin_service_enable_variable_name}="YES"}, %Q{#{provider.builtin_service_enable_variable_name}="NO"}, - ] } + ] end it "sets enabled based on the exact match (false)" do provider.determine_enabled_status! expect(current_resource.enabled).to be false @@ -185,10 +186,11 @@ describe Chef::Provider::Service::Openbsd do end context "when the enable variable partial matches (right) some other service and we are disabled" do - let(:lines) { [ + let(:lines) do + [ %Q{#{provider.builtin_service_enable_variable_name}_thing="YES"}, %Q{#{provider.builtin_service_enable_variable_name}}, - ] } + ] end it "sets enabled based on the exact match (false)" do provider.determine_enabled_status! expect(current_resource.enabled).to be false @@ -196,10 +198,11 @@ describe Chef::Provider::Service::Openbsd do end context "when the enable variable partial matches (left) some other disabled service and we are enabled" do - let(:lines) { [ + let(:lines) do + [ %Q{thing_#{provider.builtin_service_enable_variable_name}="NO"}, %Q{#{provider.builtin_service_enable_variable_name}="YES"}, - ] } + ] end it "sets enabled based on the exact match (true)" do provider.determine_enabled_status! expect(current_resource.enabled).to be true @@ -207,10 +210,11 @@ describe Chef::Provider::Service::Openbsd do end context "when the enable variable partial matches (right) some other disabled service and we are enabled" do - let(:lines) { [ + let(:lines) do + [ %Q{#{provider.builtin_service_enable_variable_name}_thing="NO"}, %Q{#{provider.builtin_service_enable_variable_name}="YES"}, - ] } + ] end it "sets enabled based on the exact match (true)" do provider.determine_enabled_status! expect(current_resource.enabled).to be true diff --git a/spec/unit/provider/service/systemd_service_spec.rb b/spec/unit/provider/service/systemd_service_spec.rb index e0a94127b7..4e25f499f6 100644 --- a/spec/unit/provider/service/systemd_service_spec.rb +++ b/spec/unit/provider/service/systemd_service_spec.rb @@ -21,7 +21,7 @@ require "spec_helper" describe Chef::Provider::Service::Systemd do - let(:node) { + let(:node) do node = Chef::Node.new node.default["etc"] = Hash.new node.default["etc"]["passwd"] = { @@ -30,7 +30,7 @@ describe Chef::Provider::Service::Systemd do }, } node - } + end let(:events) { Chef::EventDispatch::Dispatcher.new } @@ -196,14 +196,14 @@ describe Chef::Provider::Service::Systemd do context "when a user is specified" do it "should call '#{systemctl_path} --user start service_name' if no start command is specified" do current_resource.user("joe") - expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --user start #{service_name}", { "environment" => { "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/10000/bus" }, "user" => "joe" }).and_return(shell_out_success) + expect(provider).to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --user start #{service_name}", { :environment => { "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/10000/bus" }, :user => "joe" }).and_return(shell_out_success) provider.start_service end it "should not call '#{systemctl_path} --user start service_name' if it is already running" do current_resource.running(true) current_resource.user("joe") - expect(provider).not_to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --user start #{service_name}", { "environment" => { "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/10000/bus" }, "user" => "joe" }) + expect(provider).not_to receive(:shell_out_with_systems_locale!).with("#{systemctl_path} --user start #{service_name}", { :environment => { "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/10000/bus" }, :user => "joe" }) provider.start_service end end diff --git a/spec/unit/provider/service/upstart_service_spec.rb b/spec/unit/provider/service/upstart_service_spec.rb index 0692bdb64a..f768a46ccf 100644 --- a/spec/unit/provider/service/upstart_service_spec.rb +++ b/spec/unit/provider/service/upstart_service_spec.rb @@ -105,17 +105,21 @@ describe Chef::Provider::Service::Upstart do end describe "when the status command uses the new format" do - before do + it "should set running to true if the goal state is 'start'" do + @stdout = StringIO.new("rsyslog start/running") + allow(@provider).to receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) + @provider.load_current_resource + expect(@current_resource.running).to be_truthy end - it "should set running to true if the status command returns 0" do - @stdout = StringIO.new("rsyslog start/running") + it "should set running to true if the goal state is 'start' but current state is not 'running'" do + @stdout = StringIO.new("rsyslog start/starting") allow(@provider).to receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) @provider.load_current_resource expect(@current_resource.running).to be_truthy end - it "should set running to false if the status command returns anything except 0" do + it "should set running to false if the goal state is 'stop'" do @stdout = StringIO.new("rsyslog stop/waiting") allow(@provider).to receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) @provider.load_current_resource @@ -124,17 +128,21 @@ describe Chef::Provider::Service::Upstart do end describe "when the status command uses the new format with an instance" do - before do + it "should set running to true if the goal state is 'start'" do + @stdout = StringIO.new("rsyslog (test) start/running, process 100") + allow(@provider).to receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) + @provider.load_current_resource + expect(@current_resource.running).to be_truthy end - it "should set running to true if the status command returns 0" do - @stdout = StringIO.new("rsyslog (test) start/running, process 100") + it "should set running to true if the goal state is 'start' but current state is not 'running'" do + @stdout = StringIO.new("rsyslog (test) start/starting, process 100") allow(@provider).to receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) @provider.load_current_resource expect(@current_resource.running).to be_truthy end - it "should set running to false if the status command returns anything except 0" do + it "should set running to false if the goal state is 'stop'" do @stdout = StringIO.new("rsyslog (test) stop/waiting, process 100") allow(@provider).to receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) @provider.load_current_resource @@ -143,14 +151,21 @@ describe Chef::Provider::Service::Upstart do end describe "when the status command uses the old format" do - it "should set running to true if the status command returns 0" do + it "should set running to true if the goal state is 'start'" do @stdout = StringIO.new("rsyslog (start) running, process 32225") allow(@provider).to receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) @provider.load_current_resource expect(@current_resource.running).to be_truthy end - it "should set running to false if the status command returns anything except 0" do + it "should set running to true if the goal state is 'start' but current state is not 'running'" do + @stdout = StringIO.new("rsyslog (start) starting, process 32225") + allow(@provider).to receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) + @provider.load_current_resource + expect(@current_resource.running).to be_truthy + end + + it "should set running to false if the goal state is 'stop'" do @stdout = StringIO.new("rsyslog (stop) waiting") allow(@provider).to receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) @provider.load_current_resource @@ -214,8 +229,8 @@ describe Chef::Provider::Service::Upstart do end end - it "should track state when we fail to obtain service status via upstart_state" do - expect(@provider).to receive(:upstart_state).and_raise Chef::Exceptions::Exec + it "should track state when we fail to obtain service status via upstart_goal_state" do + expect(@provider).to receive(:upstart_goal_state).and_raise Chef::Exceptions::Exec @provider.load_current_resource expect(@provider.instance_variable_get("@command_success")).to eq(false) end diff --git a/spec/unit/provider/service/windows_spec.rb b/spec/unit/provider/service/windows_spec.rb index f944d8f6c6..d4c451511d 100644 --- a/spec/unit/provider/service/windows_spec.rb +++ b/spec/unit/provider/service/windows_spec.rb @@ -151,15 +151,15 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do let(:old_run_as_user) { new_resource.run_as_user } let(:old_run_as_password) { new_resource.run_as_password } - before { + before do new_resource.run_as_user(".\\wallace") new_resource.run_as_password("Wensleydale") - } + end - after { + after do new_resource.run_as_user(old_run_as_user) new_resource.run_as_password(old_run_as_password) - } + end it "calls #grant_service_logon if the :run_as_user and :run_as_password attributes are present" do expect(Win32::Service).to receive(:start) @@ -409,17 +409,17 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do shared_context "testing private methods" do - let(:private_methods) { + let(:private_methods) do described_class.private_instance_methods - } + end - before { + before do described_class.send(:public, *private_methods) - } + end - after { + after do described_class.send(:private, *private_methods) - } + end end describe "grant_service_logon" do diff --git a/spec/unit/provider/systemd_unit_spec.rb b/spec/unit/provider/systemd_unit_spec.rb index 42604c22eb..7f2907c982 100644 --- a/spec/unit/provider/systemd_unit_spec.rb +++ b/spec/unit/provider/systemd_unit_spec.rb @@ -58,8 +58,8 @@ describe Chef::Provider::SystemdUnit do let(:user_cmd_opts) do { - "user" => "joe", - "environment" => { + :user => "joe", + :environment => { "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/1000/bus", }, } diff --git a/spec/unit/provider/template/content_spec.rb b/spec/unit/provider/template/content_spec.rb index 0f936c8f11..8f30d8f868 100644 --- a/spec/unit/provider/template/content_spec.rb +++ b/spec/unit/provider/template/content_spec.rb @@ -20,13 +20,13 @@ require "spec_helper" describe Chef::Provider::Template::Content do - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path(Dir.mktmpdir) - } + end - let(:resource_path) { + let(:resource_path) do canonicalize_path(File.expand_path(File.join(enclosing_directory, "openldap_stuff.conf"))) - } + end let(:new_resource) do double("Chef::Resource::Template (new)", @@ -46,10 +46,10 @@ describe Chef::Provider::Template::Content do :helper_modules => []) end - let(:rendered_file_locations) { + let(:rendered_file_locations) do [Dir.tmpdir + "/openldap_stuff.conf", enclosing_directory + "/openldap_stuff.conf"] - } + end let(:run_context) do cookbook_repo = File.expand_path(File.join(CHEF_SPEC_DATA, "cookbooks")) @@ -101,9 +101,9 @@ describe Chef::Provider::Template::Content do end context "when creating a tempfile in destdir fails" do - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path("/nonexisting/path") - } + end it "returns a tempfile in the tempdir when :file_deployment_uses_destdir is set to :auto" do Chef::Config[:file_staging_uses_destdir] = :auto diff --git a/spec/unit/provider/template_spec.rb b/spec/unit/provider/template_spec.rb index 488039ad18..306fd6ea71 100644 --- a/spec/unit/provider/template_spec.rb +++ b/spec/unit/provider/template_spec.rb @@ -27,12 +27,12 @@ describe Chef::Provider::Template do let(:node) { double("Chef::Node") } let(:events) { double("Chef::Events").as_null_object } # mock all the methods let(:run_context) { double("Chef::RunContext", :node => node, :events => events) } - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates"))) - } - let(:resource_path) { + end + let(:resource_path) do canonicalize_path(File.expand_path(File.join(enclosing_directory, "seattle.txt"))) - } + end # Subject @@ -61,12 +61,12 @@ describe Chef::Provider::Template do let(:node) { double("Chef::Node") } let(:events) { double("Chef::Events").as_null_object } # mock all the methods let(:run_context) { double("Chef::RunContext", :node => node, :events => events) } - let(:enclosing_directory) { + let(:enclosing_directory) do canonicalize_path(File.expand_path(File.join(CHEF_SPEC_DATA, "templates"))) - } - let(:resource_path) { + end + let(:resource_path) do canonicalize_path(File.expand_path(File.join(enclosing_directory, "seattle.txt"))) - } + end # Subject diff --git a/spec/unit/provider/user/dscl_spec.rb b/spec/unit/provider/user/dscl_spec.rb index bf8b3169d7..dfaaa377d3 100644 --- a/spec/unit/provider/user/dscl_spec.rb +++ b/spec/unit/provider/user/dscl_spec.rb @@ -24,52 +24,52 @@ describe Chef::Provider::User::Dscl do before do allow(ChefConfig).to receive(:windows?) { false } end - let(:shellcmdresult) { + let(:shellcmdresult) do Struct.new(:stdout, :stderr, :exitstatus) - } - let(:node) { + end + let(:node) do node = Chef::Node.new allow(node).to receive(:[]).with(:platform_version).and_return(mac_version) allow(node).to receive(:[]).with(:platform).and_return("mac_os_x") node - } + end - let(:events) { + let(:events) do Chef::EventDispatch::Dispatcher.new - } + end - let(:run_context) { + let(:run_context) do Chef::RunContext.new(node, {}, events) - } + end - let(:new_resource) { - r = Chef::Resource::User.new("toor") + let(:new_resource) do + r = Chef::Resource::User::DsclUser.new("toor") r.password(password) r.salt(salt) r.iterations(iterations) r - } + end - let(:provider) { + let(:provider) do Chef::Provider::User::Dscl.new(new_resource, run_context) - } + end - let(:mac_version) { + let(:mac_version) do "10.9.1" - } + end let(:password) { nil } let(:salt) { nil } let(:iterations) { nil } - let(:salted_sha512_password) { + let(:salted_sha512_password) do "0f543f021c63255e64e121a3585601b8ecfedf6d2\ 705ddac69e682a33db5dbcdb9b56a2520bc8fff63a\ 2ba6b7984c0737ff0b7949455071581f7affcd536d\ 402b6cdb097" - } + end - let(:salted_sha512_pbkdf2_password) { + let(:salted_sha512_pbkdf2_password) do "c734b6e4787c3727bb35e29fdd92b97c\ 1de12df509577a045728255ec7c6c5f5\ c18efa05ed02b682ffa7ebc05119900e\ @@ -78,24 +78,24 @@ b1d4880833aa7a190afc13e2bf0936b8\ 9464a8c234f3919082400b4f939bb77b\ c5adbbac718b7eb99463a7b679571e0f\ 1c9fef2ef08d0b9e9c2bcf644eed2ffc" - } + end - let(:salted_sha512_pbkdf2_salt) { + let(:salted_sha512_pbkdf2_salt) do "2d942d8364a9ccf2b8e5cb7ed1ff58f78\ e29dbfee7f9db58859144d061fd0058" - } + end - let(:salted_sha512_pbkdf2_iterations) { + let(:salted_sha512_pbkdf2_iterations) do 25000 - } + end - let(:vagrant_sha_512) { + let(:vagrant_sha_512) do "6f75d7190441facc34291ebbea1fc756b242d4f\ e9bcff141bccb84f1979e27e539539aa31f9f7dcc92c0cea959\ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30" - } + end - let(:vagrant_sha_512_pbkdf2) { + let(:vagrant_sha_512_pbkdf2) do "12601a90db17cbf\ 8ba4808e6382fb0d3b9d8a6c1a190477bf680ab21afb\ 6065467136e55cc208a6f74156e3daf20fb13369ef4b\ @@ -103,15 +103,15 @@ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30" 47cca84341a7f93a27147343f89fb843fb46c0017d2\ 64afa4976baacf941b915bd1ec1ca24c30b3e759e02\ 403e02f59fe7ff5938a7636c" - } + end - let(:vagrant_sha_512_pbkdf2_salt) { + let(:vagrant_sha_512_pbkdf2_salt) do "ee954be472fdc60ddf89484781433993625f006af6ec810c08f49a7e413946a1" - } + end - let(:vagrant_sha_512_pbkdf2_iterations) { + let(:vagrant_sha_512_pbkdf2_iterations) do 34482 - } + end describe "when shelling out to dscl" do it "should run dscl with the supplied cmd /Path args" do @@ -214,9 +214,9 @@ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30" end describe "when modifying the home directory" do - let(:current_resource) { + let(:current_resource) do new_resource.dup - } + end before do new_resource.supports({ :manage_home => true }) @@ -307,9 +307,9 @@ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30" end describe "when on Mac 10.6" do - let(:mac_version) { + let(:mac_version) do "10.6.5" - } + end it "should raise an error" do expect { run_requirements }.to raise_error(Chef::Exceptions::User) @@ -317,9 +317,9 @@ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30" end describe "when on Mac 10.7" do - let(:mac_version) { + let(:mac_version) do "10.7.5" - } + end describe "when password is SALTED-SHA512" do let(:password) { salted_sha512_password } @@ -340,9 +340,9 @@ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30" [ "10.9", "10.10"].each do |version| describe "when on Mac #{version}" do - let(:mac_version) { + let(:mac_version) do "#{version}.2" - } + end describe "when password is SALTED-SHA512" do let(:password) { salted_sha512_password } @@ -413,9 +413,9 @@ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30" let(:password) { "something" } # Load password during load_current_resource describe "on 10.7" do - let(:mac_version) { + let(:mac_version) do "10.7.5" - } + end let(:user_plist_file) { "10.7" } @@ -478,9 +478,9 @@ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30" end describe "on 10.8" do - let(:mac_version) { + let(:mac_version) do "10.8.3" - } + end let(:user_plist_file) { "10.8" } @@ -504,9 +504,9 @@ e68d1f9821b26689312366") describe "on 10.7 upgraded to 10.8" do # In this scenario user password is still in 10.7 format - let(:mac_version) { + let(:mac_version) do "10.8.3" - } + end let(:user_plist_file) { "10.7-8" } @@ -542,9 +542,9 @@ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30") end describe "on 10.9" do - let(:mac_version) { + let(:mac_version) do "10.9.1" - } + end let(:user_plist_file) { "10.9" } @@ -646,9 +646,9 @@ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30") describe "prepare_password_shadow_info" do describe "when on Mac 10.7" do - let(:mac_version) { + let(:mac_version) do "10.7.1" - } + end describe "when the password is plain text" do let(:password) { "vagrant" } @@ -676,9 +676,9 @@ ea18e18b720e358e7fbe3cfbeaa561456f6ba008937a30") ["10.8", "10.9", "10.10"].each do |version| describe "when on Mac #{version}" do - let(:mac_version) { + let(:mac_version) do "#{version}.1" - } + end describe "when the password is plain text" do let(:password) { "vagrant" } diff --git a/spec/unit/provider/user/linux_spec.rb b/spec/unit/provider/user/linux_spec.rb new file mode 100644 index 0000000000..ac94592859 --- /dev/null +++ b/spec/unit/provider/user/linux_spec.rb @@ -0,0 +1,76 @@ +# +# Author:: Adam Jacob (<adam@chef.io>) +# Author:: Daniel DeLeo (<dan@chef.io>) +# Copyright:: Copyright 2008-2016, Chef Software 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" +require "chef/provider/user/useradd" + +describe Chef::Provider::User::Linux do + + subject(:provider) do + p = described_class.new(@new_resource, @run_context) + p.current_resource = @current_resource + p + end + + supported_useradd_options = { + "comment" => "-c", + "gid" => "-g", + "uid" => "-u", + "shell" => "-s", + "password" => "-p", + } + + include_examples "a useradd-based user provider", supported_useradd_options + + describe "manage_home behavior" do + before(:each) do + @new_resource = Chef::Resource::User::LinuxUser.new("adam", @run_context) + @current_resource = Chef::Resource::User::LinuxUser.new("adam", @run_context) + end + + it "sets supports manage_home to true" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false + expect( @new_resource.supports[:manage_home] ).to be true + end + + it "sets supports non-unique to true" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false + expect( @new_resource.supports[:non_unique] ).to be true + end + + it "defaults manage_home to true" do + expect( @new_resource.manage_home ).to be false + end + + it "by default manage_home is false and we use -M" do + expect( provider.useradd_options ).to eql(["-M"]) + end + + it "setting manage_home to false includes -M" do + @new_resource.manage_home false + expect( provider.useradd_options ).to eql(["-M"]) + end + + it "setting manage_home to true includes -m" do + @new_resource.manage_home true + expect( provider.useradd_options ).to eql(["-m"]) + end + end +end diff --git a/spec/unit/provider/user/pw_spec.rb b/spec/unit/provider/user/pw_spec.rb index 1e9fda9f7e..624bcfc67d 100644 --- a/spec/unit/provider/user/pw_spec.rb +++ b/spec/unit/provider/user/pw_spec.rb @@ -24,7 +24,7 @@ describe Chef::Provider::User::Pw do @events = Chef::EventDispatch::Dispatcher.new @run_context = Chef::RunContext.new(@node, {}, @events) - @new_resource = Chef::Resource::User.new("adam") + @new_resource = Chef::Resource::User::PwUser.new("adam") @new_resource.comment "Adam Jacob" @new_resource.uid 1000 @new_resource.gid 1000 @@ -34,7 +34,7 @@ describe Chef::Provider::User::Pw do @new_resource.supports :manage_home => true - @current_resource = Chef::Resource::User.new("adam") + @current_resource = Chef::Resource::User::PwUser.new("adam") @current_resource.comment "Adam Jacob" @current_resource.uid 1000 @current_resource.gid 1000 @@ -170,7 +170,6 @@ describe Chef::Provider::User::Pw do end it "logs an appropriate message" do - expect(Chef::Log).to receive(:debug).with("user[adam] no change needed to password") @provider.modify_password end end @@ -194,7 +193,6 @@ describe Chef::Provider::User::Pw do end it "logs an appropriate message" do - expect(Chef::Log).to receive(:debug).with("user[adam] no change needed to password") @provider.modify_password end end @@ -206,7 +204,6 @@ describe Chef::Provider::User::Pw do end it "should log an appropriate message" do - expect(Chef::Log).to receive(:debug).with("user[adam] updating password") @provider.modify_password end @@ -236,7 +233,7 @@ describe Chef::Provider::User::Pw do describe "when loading the current state" do before do - @provider.new_resource = Chef::Resource::User.new("adam") + @provider.new_resource = Chef::Resource::User::PwUser.new("adam") end it "should raise an error if the required binary /usr/sbin/pw doesn't exist" do diff --git a/spec/unit/provider/user/solaris_spec.rb b/spec/unit/provider/user/solaris_spec.rb index 07a39a1f0c..860c9e41dd 100644 --- a/spec/unit/provider/user/solaris_spec.rb +++ b/spec/unit/provider/user/solaris_spec.rb @@ -25,9 +25,9 @@ require "spec_helper" describe Chef::Provider::User::Solaris do - let(:shellcmdresult) { + let(:shellcmdresult) do Struct.new(:stdout, :stderr, :exitstatus) - } + end subject(:provider) do p = described_class.new(@new_resource, @run_context) @@ -44,8 +44,8 @@ describe Chef::Provider::User::Solaris do @events = Chef::EventDispatch::Dispatcher.new @run_context = Chef::RunContext.new(@node, {}, @events) - @new_resource = Chef::Resource::User.new("adam", @run_context) - @current_resource = Chef::Resource::User.new("adam", @run_context) + @new_resource = Chef::Resource::User::SolarisUser.new("adam", @run_context) + @current_resource = Chef::Resource::User::SolarisUser.new("adam", @run_context) @new_resource.password "hocus-pocus" @@ -81,7 +81,7 @@ describe Chef::Provider::User::Solaris do @events = Chef::EventDispatch::Dispatcher.new @run_context = Chef::RunContext.new(@node, {}, @events) - @new_resource = Chef::Resource::User.new("dave") + @new_resource = Chef::Resource::User::SolarisUser.new("dave") @current_resource = @new_resource.dup @provider = Chef::Provider::User::Solaris.new(@new_resource, @run_context) diff --git a/spec/unit/provider/user/useradd_spec.rb b/spec/unit/provider/user/useradd_spec.rb deleted file mode 100644 index 7c67449a86..0000000000 --- a/spec/unit/provider/user/useradd_spec.rb +++ /dev/null @@ -1,51 +0,0 @@ -# -# Author:: Adam Jacob (<adam@chef.io>) -# Author:: Daniel DeLeo (<dan@chef.io>) -# Copyright:: Copyright 2008-2016, Chef Software 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" -require "chef/provider/user/useradd" - -describe Chef::Provider::User::Useradd do - - subject(:provider) do - p = described_class.new(@new_resource, @run_context) - p.current_resource = @current_resource - p - end - - supported_useradd_options = { - "comment" => "-c", - "gid" => "-g", - "uid" => "-u", - "shell" => "-s", - "password" => "-p", - } - - include_examples "a useradd-based user provider", supported_useradd_options - - describe "manage_user" do - # CHEF-5247: Chef::Provider::User::Solaris subclasses Chef::Provider::User::Useradd, but does not use usermod to change passwords. - # Thus, a call to Solaris#manage_user calls Solaris#manage_password and Useradd#manage_user, but the latter should be a no-op. - it "should not run the command if universal_options is an empty array" do - allow(provider).to receive(:universal_options).and_return([]) - expect(provider).not_to receive(:shell_out!) - provider.manage_user - end - end -end diff --git a/spec/unit/provider/user/windows_spec.rb b/spec/unit/provider/user/windows_spec.rb index 4a62e6ec9d..324b5f503f 100644 --- a/spec/unit/provider/user/windows_spec.rb +++ b/spec/unit/provider/user/windows_spec.rb @@ -30,10 +30,10 @@ end describe Chef::Provider::User::Windows do before(:each) do @node = Chef::Node.new - @new_resource = Chef::Resource::User.new("monkey") + @new_resource = Chef::Resource::User::WindowsUser.new("monkey") @events = Chef::EventDispatch::Dispatcher.new @run_context = Chef::RunContext.new(@node, {}, @events) - @current_resource = Chef::Resource::User.new("monkey") + @current_resource = Chef::Resource::User::WindowsUser.new("monkey") @net_user = double("Chef::Util::Windows::NetUser") allow(Chef::Util::Windows::NetUser).to receive(:new).and_return(@net_user) @@ -89,7 +89,7 @@ describe Chef::Provider::User::Windows do describe "and the attributes do not match" do before do - @current_resource = Chef::Resource::User.new("adam") + @current_resource = Chef::Resource::User::WindowsUser.new("adam") @current_resource.comment "Adam Jacob-foo" @current_resource.uid 1111 @current_resource.gid 1111 diff --git a/spec/unit/provider/user_spec.rb b/spec/unit/provider/user_spec.rb index 1cdbe462f7..1a8ad6ad1b 100644 --- a/spec/unit/provider/user_spec.rb +++ b/spec/unit/provider/user_spec.rb @@ -190,7 +190,7 @@ describe Chef::Provider::User do end describe "compare_user" do - let(:mapping) { + let(:mapping) do { "username" => %w{adam Adam}, "comment" => ["Adam Jacob", "adam jacob"], @@ -200,7 +200,7 @@ describe Chef::Provider::User do "shell" => ["/usr/bin/zsh", "/bin/bash"], "password" => %w{abcd 12345}, } - } + end %w{uid gid comment home shell password}.each do |attribute| it "should return true if #{attribute} doesn't match" do diff --git a/spec/unit/provider/yum_repository_spec.rb b/spec/unit/provider/yum_repository_spec.rb new file mode 100644 index 0000000000..5b019f7d3e --- /dev/null +++ b/spec/unit/provider/yum_repository_spec.rb @@ -0,0 +1,35 @@ +# +# Author:: Thom May (<thom@chef.io>) +# Copyright:: Copyright (c) 2016 Chef Software, 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::YumRepository do + let(:new_resource) { Chef::Resource::YumRepository.new("multiverse") } + + let(:provider) do + node = Chef::Node.new + events = Chef::EventDispatch::Dispatcher.new + run_context = Chef::RunContext.new(node, {}, events) + Chef::Provider::YumRepository.new(new_resource, run_context) + end + + it "responds to load_current_resource" do + expect(provider).to respond_to(:load_current_resource) + end + +end |