diff options
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/cookbook/cookbook_version_loader_spec.rb | 9 | ||||
-rw-r--r-- | spec/unit/knife/ssh_spec.rb | 70 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 25 | ||||
-rw-r--r-- | spec/unit/provider/directory_spec.rb | 10 | ||||
-rw-r--r-- | spec/unit/provider/dsc_resource_spec.rb | 175 | ||||
-rw-r--r-- | spec/unit/provider/launchd_spec.rb | 4 | ||||
-rw-r--r-- | spec/unit/provider/package/aix_spec.rb | 6 | ||||
-rw-r--r-- | spec/unit/provider/package/cab_spec.rb | 64 | ||||
-rw-r--r-- | spec/unit/provider/package/chocolatey_spec.rb | 42 | ||||
-rw-r--r-- | spec/unit/provider/remote_file/sftp_spec.rb | 6 | ||||
-rw-r--r-- | spec/unit/provider/route_spec.rb | 8 | ||||
-rw-r--r-- | spec/unit/resource/chocolatey_package_spec.rb | 12 | ||||
-rw-r--r-- | spec/unit/resource/dnf_package_spec.rb | 99 | ||||
-rw-r--r-- | spec/unit/resource/dsc_resource_spec.rb | 6 | ||||
-rw-r--r-- | spec/unit/resource/yum_package_spec.rb | 20 | ||||
-rw-r--r-- | spec/unit/win32/error_spec.rb | 67 | ||||
-rw-r--r-- | spec/unit/win32/security_spec.rb | 66 |
17 files changed, 616 insertions, 73 deletions
diff --git a/spec/unit/cookbook/cookbook_version_loader_spec.rb b/spec/unit/cookbook/cookbook_version_loader_spec.rb index 87d0f1e032..786e17f35b 100644 --- a/spec/unit/cookbook/cookbook_version_loader_spec.rb +++ b/spec/unit/cookbook/cookbook_version_loader_spec.rb @@ -104,6 +104,15 @@ describe Chef::Cookbook::CookbookVersionLoader do end + context "when a cookbook's metadata.rb does not parse but the compiled metadata.json is present" do + let(:cookbook_path) { File.join(CHEF_SPEC_DATA, "prefer_metadata_json") } + + it "reads the cookbook" do + expect(loaded_cookbook.metadata.name.to_s).to eq("prefer_metadata_json") + expect(loaded_cookbook.metadata.version.to_s).to eq("1.2.3") + end + end + context "when the given path is not actually a cookbook" do let(:cookbook_path) { File.join(CHEF_SPEC_DATA, "cookbooks/NOTHING_HERE_FOLKS") } diff --git a/spec/unit/knife/ssh_spec.rb b/spec/unit/knife/ssh_spec.rb index 44a133d858..6141a8a6df 100644 --- a/spec/unit/knife/ssh_spec.rb +++ b/spec/unit/knife/ssh_spec.rb @@ -21,54 +21,53 @@ require "net/ssh" require "net/ssh/multi" describe Chef::Knife::Ssh do - before(:each) do - Chef::Config[:client_key] = CHEF_SPEC_DATA + "/ssl/private_key.pem" - end + let(:query_result) { double("chef search results") } before do + Chef::Config[:client_key] = CHEF_SPEC_DATA + "/ssl/private_key.pem" @knife = Chef::Knife::Ssh.new @knife.merge_configs - @node_foo = Chef::Node.new - @node_foo.automatic_attrs[:fqdn] = "foo.example.org" - @node_foo.automatic_attrs[:ipaddress] = "10.0.0.1" + @node_foo = {} + @node_foo["fqdn"] = "foo.example.org" + @node_foo["ipaddress"] = "10.0.0.1" + @node_foo["cloud"] = {} + + @node_bar = {} + @node_bar["fqdn"] = "bar.example.org" + @node_bar["ipaddress"] = "10.0.0.2" + @node_bar["cloud"] = {} - @node_bar = Chef::Node.new - @node_bar.automatic_attrs[:fqdn] = "bar.example.org" - @node_bar.automatic_attrs[:ipaddress] = "10.0.0.2" end describe "#configure_session" do context "manual is set to false (default)" do before do @knife.config[:manual] = false - @query = Chef::Search::Query.new - end - - def configure_query(node_array) - allow(@query).to receive(:search).and_return([node_array]) - allow(Chef::Search::Query).to receive(:new).and_return(@query) + allow(query_result).to receive(:search).with(any_args).and_yield(@node_foo).and_yield(@node_bar) + allow(Chef::Search::Query).to receive(:new).and_return(query_result) end def self.should_return_specified_attributes it "returns an array of the attributes specified on the command line OR config file, if only one is set" do + @node_bar["config"] = "10.0.0.2" + @node_foo["config"] = "10.0.0.1" @knife.config[:attribute] = "ipaddress" Chef::Config[:knife][:ssh_attribute] = "ipaddress" # this value will be in the config file - configure_query([@node_foo, @node_bar]) expect(@knife).to receive(:session_from_list).with([["10.0.0.1", nil], ["10.0.0.2", nil]]) @knife.configure_session end it "returns an array of the attributes specified on the command line even when a config value is set" do + @node_bar["config"] = "10.0.0.2" + @node_foo["config"] = "10.0.0.1" Chef::Config[:knife][:ssh_attribute] = "config_file" # this value will be in the config file @knife.config[:attribute] = "ipaddress" # this is the value of the command line via #configure_attribute - configure_query([@node_foo, @node_bar]) expect(@knife).to receive(:session_from_list).with([["10.0.0.1", nil], ["10.0.0.2", nil]]) @knife.configure_session end end it "searchs for and returns an array of fqdns" do - configure_query([@node_foo, @node_bar]) expect(@knife).to receive(:session_from_list).with([ ["foo.example.org", nil], ["bar.example.org", nil], @@ -80,11 +79,10 @@ describe Chef::Knife::Ssh do context "when cloud hostnames are available" do before do - @node_foo.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-1.compute-1.amazonaws.com" - @node_bar.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-2.compute-1.amazonaws.com" + @node_foo["cloud"]["public_hostname"] = "ec2-10-0-0-1.compute-1.amazonaws.com" + @node_bar["cloud"]["public_hostname"] = "ec2-10-0-0-2.compute-1.amazonaws.com" end it "returns an array of cloud public hostnames" do - configure_query([@node_foo, @node_bar]) expect(@knife).to receive(:session_from_list).with([ ["ec2-10-0-0-1.compute-1.amazonaws.com", nil], ["ec2-10-0-0-2.compute-1.amazonaws.com", nil], @@ -97,12 +95,11 @@ describe Chef::Knife::Ssh do context "when cloud hostnames are available but empty" do before do - @node_foo.automatic_attrs[:cloud][:public_hostname] = "" - @node_bar.automatic_attrs[:cloud][:public_hostname] = "" + @node_foo["cloud"]["public_hostname"] = "" + @node_bar["cloud"]["public_hostname"] = "" end it "returns an array of fqdns" do - configure_query([@node_foo, @node_bar]) expect(@knife).to receive(:session_from_list).with([ ["foo.example.org", nil], ["bar.example.org", nil], @@ -114,7 +111,7 @@ describe Chef::Knife::Ssh do end it "should raise an error if no host are found" do - configure_query([ ]) + allow(query_result).to receive(:search).with(any_args) expect(@knife.ui).to receive(:fatal) expect(@knife).to receive(:exit).with(10) @knife.configure_session @@ -122,10 +119,8 @@ describe Chef::Knife::Ssh do context "when there are some hosts found but they do not have an attribute to connect with" do before do - allow(@query).to receive(:search).and_return([[@node_foo, @node_bar]]) - @node_foo.automatic_attrs[:fqdn] = nil - @node_bar.automatic_attrs[:fqdn] = nil - allow(Chef::Search::Query).to receive(:new).and_return(@query) + @node_foo["fqdn"] = nil + @node_bar["fqdn"] = nil end it "should raise a specific error (CHEF-3402)" do @@ -158,31 +153,34 @@ describe Chef::Knife::Ssh do before do Chef::Config[:knife][:ssh_attribute] = nil @knife.config[:attribute] = nil - @node_foo.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-1.compute-1.amazonaws.com" - @node_bar.automatic_attrs[:cloud][:public_hostname] = "" + @node_foo["cloud"]["public_hostname"] = "ec2-10-0-0-1.compute-1.amazonaws.com" + @node_bar["cloud"]["public_hostname"] = "" end it "should return fqdn by default" do - expect(@knife.get_ssh_attribute(Chef::Node.new)).to eq("fqdn") + expect(@knife.get_ssh_attribute({ "fqdn" => "fqdn" })).to eq("fqdn") end it "should return cloud.public_hostname attribute if available" do - expect(@knife.get_ssh_attribute(@node_foo)).to eq("cloud.public_hostname") + expect(@knife.get_ssh_attribute(@node_foo)).to eq("ec2-10-0-0-1.compute-1.amazonaws.com") end it "should favor to attribute_from_cli over config file and cloud" do @knife.config[:attribute] = "command_line" Chef::Config[:knife][:ssh_attribute] = "config_file" + @node_foo["config"] = "command_line" + @node_foo["knife_config"] = "config_file" expect( @knife.get_ssh_attribute(@node_foo)).to eq("command_line") end it "should favor config file over cloud and default" do Chef::Config[:knife][:ssh_attribute] = "config_file" + @node_foo["knife_config"] = "config_file" expect( @knife.get_ssh_attribute(@node_foo)).to eq("config_file") end it "should return fqdn if cloud.hostname is empty" do - expect( @knife.get_ssh_attribute(@node_bar)).to eq("fqdn") + expect( @knife.get_ssh_attribute(@node_bar)).to eq("bar.example.org") end end @@ -190,7 +188,7 @@ describe Chef::Knife::Ssh do before :each do @knife.instance_variable_set(:@longest, 0) ssh_config = { :timeout => 50, :user => "locutus", :port => 23 } - allow(Net::SSH).to receive(:configuration_for).with("the.b.org").and_return(ssh_config) + allow(Net::SSH).to receive(:configuration_for).with("the.b.org", true).and_return(ssh_config) end it "uses the port from an ssh config file" do @@ -294,7 +292,7 @@ describe Chef::Knife::Ssh do describe "#run" do before do @query = Chef::Search::Query.new - expect(@query).to receive(:search).and_return([[@node_foo]]) + expect(@query).to receive(:search).and_yield(@node_foo) allow(Chef::Search::Query).to receive(:new).and_return(@query) allow(@knife).to receive(:ssh_command).and_return(exit_code) @knife.name_args = ["*:*", "false"] diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index 59b4b8a1c7..ac227c5479 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -337,14 +337,35 @@ describe Chef::Node do Chef::Config[:treat_deprecation_warnings_as_errors] = false expect(Chef).to receive(:deprecated).with(:attributes, /set is deprecated/) node.set[:snoopy][:is_a_puppy] = true - expect(node[:snoopy][:is_a_puppy]).to eq(true) + expect(node.normal[:snoopy][:is_a_puppy]).to eq(true) end it "set_unless is a deprecated alias for normal_unless" do Chef::Config[:treat_deprecation_warnings_as_errors] = false expect(Chef).to receive(:deprecated).with(:attributes, /set_unless is deprecated/) node.set_unless[:snoopy][:is_a_puppy] = false - expect(node[:snoopy][:is_a_puppy]).to eq(false) + expect(node.normal[:snoopy][:is_a_puppy]).to eq(false) + end + + it "normal_unless sets a value even if default or override attrs are set" do + node.default[:decontamination] = true + node.override[:decontamination] = false + node.normal_unless[:decontamination] = "foo" + expect(node.normal[:decontamination]).to eql("foo") + end + + it "default_unless sets a value even if normal or override attrs are set" do + node.normal[:decontamination] = true + node.override[:decontamination] = false + node.default_unless[:decontamination] = "foo" + expect(node.default[:decontamination]).to eql("foo") + end + + it "override_unless sets a value even if default or normal attrs are set" do + node.default[:decontamination] = true + node.normal[:decontamination] = false + node.override_unless[:decontamination] = "foo" + expect(node.override[:decontamination]).to eql("foo") end end diff --git a/spec/unit/provider/directory_spec.rb b/spec/unit/provider/directory_spec.rb index aebbaa6e81..4672db7d8d 100644 --- a/spec/unit/provider/directory_spec.rb +++ b/spec/unit/provider/directory_spec.rb @@ -148,6 +148,16 @@ describe Chef::Provider::Directory do directory.run_action(:create) expect(new_resource).not_to be_updated end + + context "in why run mode" do + before { Chef::Config[:why_run] = true } + after { Chef::Config[:why_run] = false } + + it "does not modify new_resource" do + expect(directory).not_to receive(:load_resource_attributes_from_file).with(new_resource) + directory.run_action(:create) + end + end end describe "when the directory does not exist" do diff --git a/spec/unit/provider/dsc_resource_spec.rb b/spec/unit/provider/dsc_resource_spec.rb index 34eb9727f8..96356e5d73 100644 --- a/spec/unit/provider/dsc_resource_spec.rb +++ b/spec/unit/provider/dsc_resource_spec.rb @@ -165,4 +165,179 @@ describe Chef::Provider::DscResource do end end end + + describe "define_resource_requirements" do + let (:node) do + set_node_object + end + + context "module usage is valid" do + before do + allow(provider).to receive(:module_usage_valid?).and_return(true) + allow(provider).to receive(:action_run) + end + + [:run].each do |action| + context "action #{action}" do + it "does not raise the exception" do + expect { provider.run_action(action) }.not_to raise_error + end + end + end + end + + context "module usage is invalid" do + before do + allow(provider).to receive(:module_usage_valid?).and_return(false) + allow(provider).to receive(:action_run) + end + + [:run].each do |action| + context "action #{action}" do + it "raises the exception" do + expect { provider.run_action(action) }.to raise_error( + Chef::Exceptions::DSCModuleNameMissing + ) + end + end + end + end + end + + describe "module_usage_valid?" do + let (:node) do + set_node_object + end + + context "module_name and module_version both are not provided" do + before do + provider.instance_variable_set(:@module_name, nil) + provider.instance_variable_set(:@module_version, nil) + end + + it "returns true" do + response = provider.send(:module_usage_valid?) + expect(response).to be == true + end + end + + context "module_name and module_version both are provided" do + before do + provider.instance_variable_set(:@module_name, "my_module") + provider.instance_variable_set(:@module_version, "1.3") + end + + it "returns true" do + response = provider.send(:module_usage_valid?) + expect(response).to be == true + end + end + + context "module_name is given but module_version is not given" do + before do + provider.instance_variable_set(:@module_name, "my_module") + provider.instance_variable_set(:@module_version, nil) + end + + it "returns true" do + response = provider.send(:module_usage_valid?) + expect(response).to be == true + end + end + + context "module_name is not given but module_version is given" do + before do + provider.instance_variable_set(:@module_name, nil) + provider.instance_variable_set(:@module_version, "1.4.0.1") + end + + it "returns false" do + response = provider.send(:module_usage_valid?) + expect(response).to be == false + end + end + end + + describe "module_info_object" do + let (:node) do + set_node_object + end + + context "module_version is not given" do + before do + provider.instance_variable_set(:@module_version, nil) + allow(provider).to receive(:module_name).and_return("my_module") + end + + it "returns only name of the module" do + response = provider.send(:module_info_object) + expect(response).to be == "my_module" + end + end + + context "module_version is given" do + before do + provider.instance_variable_set(:@module_version, "1.3.1") + allow(provider).to receive(:module_name).and_return("my_module") + end + + it "returns the module info object" do + response = provider.send(:module_info_object) + expect(response).to be == "@{ModuleName='my_module';ModuleVersion='1.3.1'}" + end + end + end + + describe "invoke_resource" do + let (:node) do + set_node_object + end + + let(:cmdlet) { double(:run! => nil) } + + before(:each) do + allow(provider).to receive(:translate_type).and_return("my_properties") + provider.instance_variable_set(:@new_resource, double( + :properties => "my_properties", :resource => "my_resource", :timeout => 123 + )) + end + + context "when module_version is not given" do + before do + allow(provider).to receive(:module_info_object).and_return("my_module") + end + + it "invokes Invoke-DscResource command with module name" do + expect(Chef::Util::Powershell::Cmdlet).to receive(:new).with( + node, + "Invoke-DscResource -Method my_method -Name my_resource -Property my_properties -Module my_module -Verbose", + "my_output_format" + ).and_return(cmdlet) + provider.send(:invoke_resource, "my_method", "my_output_format") + end + end + + context "when module_version is given" do + before do + allow(provider).to receive(:module_info_object).and_return( + "@{ModuleName='my_module';ModuleVersion='my_module_version'}" + ) + end + + it "invokes Invoke-DscResource command with module info object" do + expect(Chef::Util::Powershell::Cmdlet).to receive(:new).with( + node, + "Invoke-DscResource -Method my_method -Name my_resource -Property my_properties -Module @{ModuleName='my_module';ModuleVersion='my_module_version'} -Verbose", + "my_output_format" + ).and_return(cmdlet) + provider.send(:invoke_resource, "my_method", "my_output_format") + end + end + end +end + +def set_node_object + node = Chef::Node.new + node.automatic[:languages][:powershell][:version] = "5.0.10586.0" + node end diff --git a/spec/unit/provider/launchd_spec.rb b/spec/unit/provider/launchd_spec.rb index 3e45433c62..693801f99b 100644 --- a/spec/unit/provider/launchd_spec.rb +++ b/spec/unit/provider/launchd_spec.rb @@ -185,8 +185,8 @@ XML end describe "hash is passed" do - it "should produce the test_plist from the hash" do - new_resource.hash test_hash + it "should produce the test_plist content from the plist_hash property" do + new_resource.plist_hash test_hash expect(provider.content?).to be_truthy expect(provider.content).to eql(test_plist) end diff --git a/spec/unit/provider/package/aix_spec.rb b/spec/unit/provider/package/aix_spec.rb index 2b574bb7df..dba56b186d 100644 --- a/spec/unit/provider/package/aix_spec.rb +++ b/spec/unit/provider/package/aix_spec.rb @@ -28,7 +28,7 @@ describe Chef::Provider::Package::Aix do @new_resource.source("/tmp/samba.base") @provider = Chef::Provider::Package::Aix.new(@new_resource, @run_context) - allow(::File).to receive(:exists?).and_return(true) + allow(::File).to receive(:exists?).with(@new_resource.source).and_return(true) end describe "assessing the current package status" do @@ -57,7 +57,7 @@ describe Chef::Provider::Package::Aix do it "should raise an exception if a source is supplied but not found" do allow(@provider).to receive(:shell_out).and_return(@empty_status) - allow(::File).to receive(:exists?).and_return(false) + allow(::File).to receive(:exists?).with(@new_resource.source).and_return(false) @provider.load_current_resource @provider.define_resource_requirements expect { @provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Package) @@ -154,7 +154,7 @@ describe Chef::Provider::Package::Aix do @provider.install_package("samba.base", "3.3.12.0") end - it "should run when the package is a path to install" do + it "should run installp -aYF -d when the package is a path to install" do @new_resource = Chef::Resource::Package.new("/tmp/samba.base") @provider = Chef::Provider::Package::Aix.new(@new_resource, @run_context) expect(@new_resource.source).to eq("/tmp/samba.base") diff --git a/spec/unit/provider/package/cab_spec.rb b/spec/unit/provider/package/cab_spec.rb index 5e16795aa8..5c86f781f3 100644 --- a/spec/unit/provider/package/cab_spec.rb +++ b/spec/unit/provider/package/cab_spec.rb @@ -49,7 +49,7 @@ The operation completed successfully end before do - new_resource.source = "C:\\Temp\\Test6.1-KB2664825-v3-x64.cab" + new_resource.source = File.join("#{ENV['TEMP']}", "test6.1-kb2664825-v3-x64.cab") installed_package_list_obj = double(stdout: installed_package_list_stdout) allow(provider).to receive(:dism_command).with("/Get-Packages").and_return(installed_package_list_obj) package_version_obj = double(stdout: package_version_stdout) @@ -126,6 +126,63 @@ The operation completed successfully. end end + describe "#source_resource" do + before do + new_resource.source = "https://www.something.com/Test6.1-KB2664825-v3-x64.cab" + new_resource.cookbook_name = "cab_package" + end + + it "sets the desired parameters of downloades cab file" do + allow(provider).to receive(:default_download_cache_path).and_return("C:\\chef\\cache\\package") + source_resource = provider.source_resource + expect(source_resource.path).to be == "C:\\chef\\cache\\package" + expect(source_resource.name).to be == "windows_test_pkg" + expect(source_resource.source).to be == [new_resource.source] + expect(source_resource.cookbook_name).to be == "cab_package" + end + end + + describe "#default_download_cache_path" do + before do + new_resource.source = "https://www.something.com/Test6.1-KB2664825-v3-x64.cab" + end + + it "returns a clean cache path where the cab file is downloaded" do + allow(Chef::FileCache).to receive(:create_cache_path).and_return(ENV["TEMP"]) + path = provider.default_download_cache_path + if windows? + expect(path).to be == File.join("#{ENV['TEMP']}", "\\", "Test6.1-KB2664825-v3-x64.cab") + else + expect(path).to be == File.join("#{ENV['TEMP']}", "Test6.1-KB2664825-v3-x64.cab") + end + end + end + + describe "#cab_file_source" do + context "when local file path is set" do + it "returns local cab file source path" do + new_resource.source = File.join("#{ENV['TEMP']}", "test6.1-kb2664825-v3-x64.cab") + path = provider.cab_file_source + if windows? + expect(path).to be == File.join("#{ENV['TEMP'].downcase}", "\\", "test6.1-kb2664825-v3-x64.cab") + else + expect(path).to be == File.join("#{ENV['TEMP']}", "test6.1-kb2664825-v3-x64.cab") + end + end + end + context "when url is set" do + it "calls download_source_file method" do + new_resource.source = "https://www.something.com/test6.1-kb2664825-v3-x64.cab" + if windows? + expect(provider).to receive(:download_source_file).and_return(File.join("#{ENV['TEMP'].downcase}", "\\", "test6.1-kb2664825-v3-x64.cab")) + else + expect(provider).to receive(:download_source_file).and_return(File.join("#{ENV['TEMP']}", "test6.1-kb2664825-v3-x64.cab")) + end + provider.cab_file_source + end + end + end + describe "#initialize" do it "returns the correct class" do expect(provider).to be_kind_of(Chef::Provider::Package::Cab) @@ -192,12 +249,9 @@ The operation completed successfully. context "Invalid package source" do def package_version_stdout package_version_stdout = <<-EOF - Deployment Image Servicing and Management tool Version: 6.1.7600.16385 - Image Version: 6.1.7600.16385 - An error occurred trying to open - c:\\temp\\test6.1-KB2664825-v3-x64.cab Error: 0x80070003 Error: 3 The system cannot find the path specified. @@ -206,7 +260,7 @@ The DISM log file can be found at C:\\Windows\\Logs\\DISM\\dism.log. end before do - new_resource.source = "C:\\Temp\\Test6.1-KB2664825-v3-x64.cab" + new_resource.source = "#{ENV['TEMP']}/test6.1-kb2664825-v3-x64.cab" installed_package_list_obj = double(stdout: installed_package_list_stdout) allow(provider).to receive(:dism_command).with("/Get-Packages").and_return(installed_package_list_obj) end diff --git a/spec/unit/provider/package/chocolatey_spec.rb b/spec/unit/provider/package/chocolatey_spec.rb index 0429a4b202..68b121960c 100644 --- a/spec/unit/provider/package/chocolatey_spec.rb +++ b/spec/unit/provider/package/chocolatey_spec.rb @@ -46,7 +46,7 @@ ConEmu|15.10.25.0 allow(provider).to receive(:choco_install_path).and_return(choco_install_path) allow(provider).to receive(:choco_exe).and_return(choco_exe) local_list_obj = double(:stdout => local_list_stdout) - allow(provider).to receive(:shell_out!).with("#{choco_exe} list -l -r", { :timeout => timeout }).and_return(local_list_obj) + allow(provider).to receive(:shell_out!).with("#{choco_exe} list -l -r", { :returns => [0], :timeout => timeout }).and_return(local_list_obj) end def allow_remote_list(package_names, args = nil) @@ -59,7 +59,7 @@ Git|2.6.2 munin-node|1.6.1.20130823 EOF remote_list_obj = double(stdout: remote_list_stdout) - allow(provider).to receive(:shell_out!).with("#{choco_exe} list -r #{package_names.join ' '}#{args}", { timeout: timeout }).and_return(remote_list_obj) + allow(provider).to receive(:shell_out!).with("#{choco_exe} list -r #{package_names.join ' '}#{args}", { :returns => [0], timeout: timeout }).and_return(remote_list_obj) end describe "#initialize" do @@ -180,7 +180,7 @@ munin-node|1.6.1.20130823 it "should install a single package" do allow_remote_list(["git"]) provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y git", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y git", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -191,7 +191,7 @@ munin-node|1.6.1.20130823 allow_remote_list(["git"]) new_resource.timeout(timeout) provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y git", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y git", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -220,7 +220,7 @@ munin-node|1.6.1.20130823 new_resource.package_name("ConEmu") new_resource.version("15.10.25.1") provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y --version 15.10.25.1 conemu", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y --version 15.10.25.1 conemu", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -233,7 +233,7 @@ munin-node|1.6.1.20130823 new_resource.package_name(%w{chocolatey ConEmu}) new_resource.version([nil, "15.10.25.1"]) provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y --version 15.10.25.1 conemu", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y --version 15.10.25.1 conemu", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -243,7 +243,7 @@ munin-node|1.6.1.20130823 new_resource.package_name("conemu") new_resource.version("15.10.25.1") provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y --version 15.10.25.1 conemu", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y --version 15.10.25.1 conemu", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -253,8 +253,8 @@ munin-node|1.6.1.20130823 new_resource.package_name(%w{ConEmu git}) new_resource.version(["15.10.25.1", nil]) provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y --version 15.10.25.1 conemu", { :timeout => timeout }).and_return(double) - expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y git", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y --version 15.10.25.1 conemu", { :returns => [0], :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y git", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -263,7 +263,7 @@ munin-node|1.6.1.20130823 allow_remote_list(["git", "munin-node"]) new_resource.package_name(["git", "munin-node"]) provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y git munin-node", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y git munin-node", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -273,7 +273,7 @@ munin-node|1.6.1.20130823 allow_remote_list(["git"], " -source localpackages") new_resource.source("localpackages") provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y -source localpackages git", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y -source localpackages git", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -283,7 +283,7 @@ munin-node|1.6.1.20130823 allow_remote_list(["git"]) new_resource.options("-force") provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y -force git", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} install -y -force git", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -317,7 +317,7 @@ munin-node|1.6.1.20130823 it "should install a package that is not installed" do allow_remote_list(["git"]) provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y git", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y git", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:upgrade) expect(new_resource).to be_updated_by_last_action end @@ -326,7 +326,7 @@ munin-node|1.6.1.20130823 allow_remote_list(["ConEmu"]) new_resource.package_name("ConEmu") provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y conemu", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y conemu", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:upgrade) expect(new_resource).to be_updated_by_last_action end @@ -335,7 +335,7 @@ munin-node|1.6.1.20130823 allow_remote_list(["conemu"]) new_resource.package_name("conemu") provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y conemu", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y conemu", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:upgrade) expect(new_resource).to be_updated_by_last_action end @@ -344,7 +344,7 @@ munin-node|1.6.1.20130823 allow_remote_list(["chocolatey"]) new_resource.package_name("chocolatey") provider.load_current_resource - expect(provider).not_to receive(:shell_out!).with("#{choco_exe} upgrade -y chocolatey", { :timeout => timeout }) + expect(provider).not_to receive(:shell_out!).with("#{choco_exe} upgrade -y chocolatey", { :returns => [0], :timeout => timeout }) provider.run_action(:upgrade) expect(new_resource).not_to be_updated_by_last_action end @@ -353,7 +353,7 @@ munin-node|1.6.1.20130823 allow_remote_list(["git"]) new_resource.version("2.6.2") provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y --version 2.6.2 git", { :timeout => timeout }) + expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y --version 2.6.2 git", { :returns => [0], :timeout => timeout }) provider.run_action(:upgrade) expect(new_resource).to be_updated_by_last_action end @@ -361,7 +361,7 @@ munin-node|1.6.1.20130823 it "upgrading multiple packages uses a single command" do allow_remote_list(%w{conemu git}) new_resource.package_name(%w{conemu git}) - expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y conemu git", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y conemu git", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:upgrade) expect(new_resource).to be_updated_by_last_action end @@ -413,7 +413,7 @@ munin-node|1.6.1.20130823 allow_remote_list(["ConEmu"]) new_resource.package_name("ConEmu") provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} uninstall -y ConEmu", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} uninstall -y ConEmu", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:remove) expect(new_resource).to be_updated_by_last_action end @@ -422,7 +422,7 @@ munin-node|1.6.1.20130823 allow_remote_list(["conemu"]) new_resource.package_name("conemu") provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} uninstall -y conemu", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} uninstall -y conemu", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:remove) expect(new_resource).to be_updated_by_last_action end @@ -432,7 +432,7 @@ munin-node|1.6.1.20130823 allow_remote_list(%w{git conemu}) new_resource.package_name(%w{git conemu}) provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} uninstall -y conemu", { :timeout => timeout }).and_return(double) + expect(provider).to receive(:shell_out!).with("#{choco_exe} uninstall -y conemu", { :returns => [0], :timeout => timeout }).and_return(double) provider.run_action(:remove) expect(new_resource).to be_updated_by_last_action end diff --git a/spec/unit/provider/remote_file/sftp_spec.rb b/spec/unit/provider/remote_file/sftp_spec.rb index 7be507dc89..ddab1605f0 100644 --- a/spec/unit/provider/remote_file/sftp_spec.rb +++ b/spec/unit/provider/remote_file/sftp_spec.rb @@ -117,7 +117,7 @@ describe Chef::Provider::RemoteFile::SFTP do end context "and the URI specifies an alternate port" do - let(:uri) { URI.parse("ftp://conan:cthu1hu@opscode.com:8021/seattle.txt") } + let(:uri) { URI.parse("sftp://conan:cthu1hu@opscode.com:8021/seattle.txt") } it "should connect on an alternate port when one is provided" do expect(Net::SFTP).to receive(:start).with("opscode.com:8021", "conan", :password => "cthu1hu") @@ -127,10 +127,10 @@ describe Chef::Provider::RemoteFile::SFTP do end context "and the uri specifies a nested path" do - let(:uri) { URI.parse("ftp://conan:cthu1hu@opscode.com/the/whole/path/seattle.txt") } + let(:uri) { URI.parse("sftp://conan:cthu1hu@opscode.com/the/whole/path/seattle.txt") } it "should fetch the file from the correct path" do - expect(sftp).to receive(:download!).with("the/whole/path/seattle.txt", "/tmp/somedir/remote-file-sftp-backend-spec-test") + expect(sftp).to receive(:download!).with("/the/whole/path/seattle.txt", "/tmp/somedir/remote-file-sftp-backend-spec-test") fetcher.fetch end end diff --git a/spec/unit/provider/route_spec.rb b/spec/unit/provider/route_spec.rb index 7058d76590..03d1ad2477 100644 --- a/spec/unit/provider/route_spec.rb +++ b/spec/unit/provider/route_spec.rb @@ -230,13 +230,19 @@ describe Chef::Provider::Route do @run_context.resource_collection << Chef::Resource::Route.new("192.168.1.0/24 via 192.168.0.1") @run_context.resource_collection << Chef::Resource::Route.new("192.168.2.0/24 via 192.168.0.1") @run_context.resource_collection << Chef::Resource::Route.new("192.168.3.0/24 via 192.168.0.1") + @run_context.resource_collection << Chef::Resource::Route.new("Complex Route").tap do |r| + r.target "192.168.4.0" + r.gateway "192.168.0.1" + r.netmask "255.255.255.0" + end @provider.action = :add @provider.generate_config - expect(route_file.string.split("\n").size).to eq(3) + expect(route_file.string.split("\n").size).to eq(4) expect(route_file.string).to match(/^192\.168\.1\.0\/24 via 192\.168\.0\.1$/) expect(route_file.string).to match(/^192\.168\.2\.0\/24 via 192\.168\.0\.1$/) expect(route_file.string).to match(/^192\.168\.3\.0\/24 via 192\.168\.0\.1$/) + expect(route_file.string).to match(/^192\.168\.4\.0\/24 via 192\.168\.0\.1$/) end end end diff --git a/spec/unit/resource/chocolatey_package_spec.rb b/spec/unit/resource/chocolatey_package_spec.rb index e43803a65f..9b433045c1 100644 --- a/spec/unit/resource/chocolatey_package_spec.rb +++ b/spec/unit/resource/chocolatey_package_spec.rb @@ -64,4 +64,16 @@ describe Chef::Resource::ChocolateyPackage do resource.version(["1.2.3", "4.5.6"]) expect(resource.version).to eql(["1.2.3", "4.5.6"]) end + + it "the default returns should be 0" do + expect(resource.returns).to eql([0]) + end + + # Integer, Array + [ 0, [0, 48, 49] ].each do |val| + it "supports setting an alternate return value as a #{val.class}" do + resource.returns(val) + expect(resource.returns).to eql(val) + end + end end diff --git a/spec/unit/resource/dnf_package_spec.rb b/spec/unit/resource/dnf_package_spec.rb new file mode 100644 index 0000000000..0cc673d897 --- /dev/null +++ b/spec/unit/resource/dnf_package_spec.rb @@ -0,0 +1,99 @@ +# +# Copyright:: Copyright 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 "support/shared/unit/resource/static_provider_resolution" + +describe Chef::Resource::DnfPackage, "initialize" do + + static_provider_resolution( + resource: Chef::Resource::DnfPackage, + provider: Chef::Provider::Package::Dnf, + name: :dnf_package, + action: :install, + os: "linux", + platform_family: "rhel" + ) + +end + +describe Chef::Resource::DnfPackage, "arch" do + before(:each) do + @resource = Chef::Resource::DnfPackage.new("foo") + end + + it "should set the arch variable to whatever is passed in" do + @resource.arch("i386") + expect(@resource.arch).to eql(["i386"]) + end +end + +describe Chef::Resource::DnfPackage, "flush_cache" do + before(:each) do + @resource = Chef::Resource::DnfPackage.new("foo") + end + + it "should default the flush timing to false" do + flush_hash = { :before => false, :after => false } + expect(@resource.flush_cache).to eq(flush_hash) + end + + it "should allow you to set the flush timing with an array" do + flush_array = [ :before, :after ] + flush_hash = { :before => true, :after => true } + @resource.flush_cache(flush_array) + expect(@resource.flush_cache).to eq(flush_hash) + end + + it "should allow you to set the flush timing with a hash" do + flush_hash = { :before => true, :after => true } + @resource.flush_cache(flush_hash) + expect(@resource.flush_cache).to eq(flush_hash) + end + + it "should allow 'true' for flush_cache" do + @resource.flush_cache(true) + expect(@resource.flush_cache).to eq({ before: true, after: true }) + end + + it "should allow 'false' for flush_cache" do + @resource.flush_cache(false) + expect(@resource.flush_cache).to eq({ before: false, after: false }) + end + + it "should allow ':before' for flush_cache" do + @resource.flush_cache(:before) + expect(@resource.flush_cache).to eq({ before: true, after: false }) + end + + it "should allow ':after' for flush_cache" do + @resource.flush_cache(:after) + expect(@resource.flush_cache).to eq({ before: false, after: true }) + end +end + +describe Chef::Resource::DnfPackage, "allow_downgrade" do + before(:each) do + @resource = Chef::Resource::DnfPackage.new("foo") + end + + it "should allow you to specify whether allow_downgrade is true or false" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false + expect { @resource.allow_downgrade true }.not_to raise_error + expect { @resource.allow_downgrade false }.not_to raise_error + end +end diff --git a/spec/unit/resource/dsc_resource_spec.rb b/spec/unit/resource/dsc_resource_spec.rb index b610c262cc..b687811392 100644 --- a/spec/unit/resource/dsc_resource_spec.rb +++ b/spec/unit/resource/dsc_resource_spec.rb @@ -18,6 +18,7 @@ require "spec_helper" describe Chef::Resource::DscResource do let(:dsc_test_resource_name) { "DSCTest" } + let(:dsc_test_resource_module_version) { "2.7.2" } let(:dsc_test_property_name) { :DSCTestProperty } let(:dsc_test_property_value) { "DSCTestValue" } let(:dsc_test_reboot_action) { :reboot_now } @@ -53,6 +54,11 @@ describe Chef::Resource::DscResource do expect(dsc_test_resource.module_name).to eq(dsc_test_resource_name) end + it "allows the module_version attribute to be set" do + dsc_test_resource.module_version(dsc_test_resource_module_version) + expect(dsc_test_resource.module_version).to eq(dsc_test_resource_module_version) + end + it "allows the reboot_action attribute to be set" do dsc_test_resource.reboot_action(dsc_test_reboot_action) expect(dsc_test_resource.reboot_action).to eq(dsc_test_reboot_action) diff --git a/spec/unit/resource/yum_package_spec.rb b/spec/unit/resource/yum_package_spec.rb index dd0d3ae928..bc2d19d50e 100644 --- a/spec/unit/resource/yum_package_spec.rb +++ b/spec/unit/resource/yum_package_spec.rb @@ -65,6 +65,26 @@ describe Chef::Resource::YumPackage, "flush_cache" do @resource.flush_cache(flush_hash) expect(@resource.flush_cache).to eq(flush_hash) end + + it "should allow 'true' for flush_cache" do + @resource.flush_cache(true) + expect(@resource.flush_cache).to eq({ before: true, after: true }) + end + + it "should allow 'false' for flush_cache" do + @resource.flush_cache(false) + expect(@resource.flush_cache).to eq({ before: false, after: false }) + end + + it "should allow ':before' for flush_cache" do + @resource.flush_cache(:before) + expect(@resource.flush_cache).to eq({ before: true, after: false }) + end + + it "should allow ':after' for flush_cache" do + @resource.flush_cache(:after) + expect(@resource.flush_cache).to eq({ before: false, after: true }) + end end describe Chef::Resource::YumPackage, "allow_downgrade" do diff --git a/spec/unit/win32/error_spec.rb b/spec/unit/win32/error_spec.rb new file mode 100644 index 0000000000..e8b6d5af6a --- /dev/null +++ b/spec/unit/win32/error_spec.rb @@ -0,0 +1,67 @@ +# +# Author:: Aliasgar Batterywala (aliasgar.batterywala@msystechnologies.com) +# Copyright:: Copyright 2017, 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" +if Chef::Platform.windows? + require "chef/win32/error" + require "chef/win32/api/error" +end + +describe "Chef::Win32::Error", :windows_only do + describe "self.raise!" do + context "code is not passed to the raise! method" do + context "last error received is user_not_found" do + it "raises UserIDNotFound exception" do + expect(Chef::ReservedNames::Win32::Error).to receive(:get_last_error).and_return( + Chef::ReservedNames::Win32::API::Error::ERROR_USER_NOT_FOUND + ) + expect(Chef::ReservedNames::Win32::Error).to receive_message_chain(:format_message, :strip) + expect { Chef::ReservedNames::Win32::Error.raise! }.to raise_error Chef::Exceptions::UserIDNotFound + end + end + + context "last error received is not user_not_found" do + it "raises Win32APIError exception" do + expect(Chef::ReservedNames::Win32::Error).to receive(:get_last_error).and_return( + Chef::ReservedNames::Win32::API::Error::ERROR_BAD_USERNAME + ) + expect(Chef::ReservedNames::Win32::Error).to receive_message_chain(:format_message, :strip).and_return("Bad Username") + expect { Chef::ReservedNames::Win32::Error.raise! }.to raise_error Chef::Exceptions::Win32APIError + end + end + end + + context "code is passed to the raise! method" do + context "last error received is user_not_found" do + it "raises UserIDNotFound exception" do + expect(Chef::ReservedNames::Win32::Error).to_not receive(:get_last_error) + expect(Chef::ReservedNames::Win32::Error).to receive_message_chain(:format_message, :strip) + expect { Chef::ReservedNames::Win32::Error.raise! nil, Chef::ReservedNames::Win32::API::Error::ERROR_USER_NOT_FOUND }.to raise_error Chef::Exceptions::UserIDNotFound + end + end + + context "last error received is not user_not_found" do + it "raises Win32APIError exception" do + expect(Chef::ReservedNames::Win32::Error).to_not receive(:get_last_error) + expect(Chef::ReservedNames::Win32::Error).to receive_message_chain(:format_message, :strip).and_return("Bad Username") + expect { Chef::ReservedNames::Win32::Error.raise! nil, Chef::ReservedNames::Win32::API::Error::ERROR_BAD_USERNAME }.to raise_error Chef::Exceptions::Win32APIError + end + end + end + end +end diff --git a/spec/unit/win32/security_spec.rb b/spec/unit/win32/security_spec.rb new file mode 100644 index 0000000000..1c755061ce --- /dev/null +++ b/spec/unit/win32/security_spec.rb @@ -0,0 +1,66 @@ +# +# Author:: Aliasgar Batterywala (aliasgar.batterywala@msystechnologies.com) +# Copyright:: Copyright 2017, 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" +if Chef::Platform.windows? + require "chef/win32/error" + require "chef/win32/security" + require "chef/win32/api/error" +end + +describe "Chef::Win32::Security", :windows_only do + describe "self.get_named_security_info" do + context "when HR result is ERROR_SUCCESS" do + it "does not raise the exception" do + expect(Chef::ReservedNames::Win32::Security).to receive(:GetNamedSecurityInfoW).and_return( + Chef::ReservedNames::Win32::API::Error::ERROR_SUCCESS + ) + expect { Chef::ReservedNames::Win32::Security.get_named_security_info "/temp_path" }.to_not raise_error + end + end + + context "when HR result is not ERROR_SUCCESS and not ERROR_USER_NOT_FOUND" do + it "raises Win32APIError exception" do + expect(Chef::ReservedNames::Win32::Security).to receive(:GetNamedSecurityInfoW).and_return( + Chef::ReservedNames::Win32::API::Error::ERROR_INVALID_ACCESS + ) + expect { Chef::ReservedNames::Win32::Security.get_named_security_info "/temp_path" }.to raise_error Chef::Exceptions::Win32APIError + end + end + end + + describe "self.set_named_security_info" do + context "when HR result is ERROR_SUCCESS" do + it "does not raise the exception" do + expect(Chef::ReservedNames::Win32::Security).to receive(:SetNamedSecurityInfoW).and_return( + Chef::ReservedNames::Win32::API::Error::ERROR_SUCCESS + ) + expect { Chef::ReservedNames::Win32::Security.set_named_security_info "/temp_path", :SE_FILE_OBJECT, {} }.to_not raise_error + end + end + + context "when HR result is not ERROR_SUCCESS but it is ERROR_USER_NOT_FOUND" do + it "raises UserIDNotFound exception" do + expect(Chef::ReservedNames::Win32::Security).to receive(:SetNamedSecurityInfoW).and_return( + Chef::ReservedNames::Win32::API::Error::ERROR_USER_NOT_FOUND + ) + expect { Chef::ReservedNames::Win32::Security.set_named_security_info "/temp_path", :SE_FILE_OBJECT, {} }.to raise_error Chef::Exceptions::Chef::Exceptions::UserIDNotFound + end + end + end +end |