diff options
author | Pete Higgins <pete@peterhiggins.org> | 2020-06-02 16:26:58 -0700 |
---|---|---|
committer | Pete Higgins <pete@peterhiggins.org> | 2020-06-03 11:46:53 -0700 |
commit | f9e8f76bea0752b79cb97a5509dae8cce3eaf9c3 (patch) | |
tree | 341e7fe1e6582dddcbf84552a7471229bd240c5b /spec/unit/provider | |
parent | f7cf0e629006cdcf37a540d9724bc8e57304d0fb (diff) | |
download | chef-f9e8f76bea0752b79cb97a5509dae8cce3eaf9c3.tar.gz |
Fix script provider tests.
Signed-off-by: Pete Higgins <pete@peterhiggins.org>
Diffstat (limited to 'spec/unit/provider')
-rw-r--r-- | spec/unit/provider/script_spec.rb | 130 |
1 files changed, 20 insertions, 110 deletions
diff --git a/spec/unit/provider/script_spec.rb b/spec/unit/provider/script_spec.rb index 68d6bdb697..3bde5d7bb5 100644 --- a/spec/unit/provider/script_spec.rb +++ b/spec/unit/provider/script_spec.rb @@ -34,124 +34,34 @@ describe Chef::Provider::Script, "action_run" do let(:provider) { Chef::Provider::Script.new(new_resource, run_context) } - let(:tempfile) { Tempfile.open("rspec-provider-script") } - - before(:each) do - allow(provider).to receive(:shell_out!).and_return(true) - allow(provider).to receive(:script_file).and_return(tempfile) - end - - context "#script_file" do - it "creates a temporary file to store the script" do - allow(provider).to receive(:script_file).and_call_original - expect(provider.script_file).to be_an_instance_of(Tempfile) + describe "#command" do + it "is only the intepreter in quotes by default" do + expect(provider.command.strip).to eq(%Q{"perl"}) end - end - context "#unlink_script_file" do - it "unlinks the tempfile" do - tempfile_path = tempfile.path - provider.unlink_script_file - expect(File.exist?(tempfile_path)).to be false + it "is the interpreter in quotes with the flags when flags are used" do + new_resource.flags "-f" + expect(provider.command).to eq(%Q{"perl" -f}) end end - context "when configuring the script file's security" do - context "when not running on Windows" do - before do - allow(ChefUtils).to receive(:windows?).and_return(false) - end - context "#set_owner_and_group" do - it "sets the owner and group for the script file" do - new_resource.user "toor" - new_resource.group "wheel" - expect(FileUtils).to receive(:chown).with("toor", "wheel", tempfile.path) - provider.set_owner_and_group - end - end - end - - context "when running on Windows" do - before do - allow(ChefUtils).to receive(:windows?).and_return(true) - expect(new_resource.user).to eq(nil) - stub_const("Chef::ReservedNames::Win32::API::Security::GENERIC_READ", 1) - stub_const("Chef::ReservedNames::Win32::API::Security::GENERIC_EXECUTE", 4) - stub_const("Chef::ReservedNames::Win32::Security", Class.new) - stub_const("Chef::ReservedNames::Win32::Security::SecurableObject", Class.new) - stub_const("Chef::ReservedNames::Win32::Security::SID", Class.new) - stub_const("Chef::ReservedNames::Win32::Security::ACE", Class.new) - stub_const("Chef::ReservedNames::Win32::Security::ACL", Class.new) - end - - context "when an alternate user is not specified" do - it "does not attempt to set the script file's security descriptor" do - expect(provider).to receive(:grant_alternate_user_read_access) - expect(Chef::ReservedNames::Win32::Security::SecurableObject).not_to receive(:new) - provider.set_owner_and_group - end - end - - context "when an alternate user is specified" do - let(:security_descriptor) { instance_double("Chef::ReservedNames::Win32::Security::SecurityDescriptor", dacl: []) } - let(:securable_object) { instance_double("Chef::ReservedNames::Win32::Security::SecurableObject", :security_descriptor => security_descriptor, :dacl= => nil) } - it "sets the script file's security descriptor" do - new_resource.user("toor") - expect(Chef::ReservedNames::Win32::Security::SecurableObject).to receive(:new).and_return(securable_object) - expect(Chef::ReservedNames::Win32::Security::SID).to receive(:from_account).and_return(nil) - expect(Chef::ReservedNames::Win32::Security::ACE).to receive(:access_allowed).and_return(nil) - expect(Chef::ReservedNames::Win32::Security::ACL).to receive(:create).and_return(nil) - expect(securable_object).to receive(:dacl=) - provider.set_owner_and_group - end - end - end - end - - context "with the script file set to the correct owner and group" do + describe "#action_run" do before do - allow(provider).to receive(:set_owner_and_group) + allow(provider).to receive(:stream_to_stdout?).and_return(false) end - describe "when writing the script to the file" do - it "should put the contents of the script in the temp file" do - allow(provider).to receive(:unlink_script_file) # stub to avoid remove - provider.action_run - expect(IO.read(tempfile.path)).to eq("$| = 1; print 'i like beans'\n") - provider.unlink_script_file - end - - it "closes before executing the script and unlinks it when finished" do - tempfile_path = tempfile.path - provider.action_run - expect(tempfile).to be_closed - expect(File.exist?(tempfile_path)).to be false - end - end - - describe "when running the script" do - let (:default_opts) do - { timeout: 3600, returns: 0, default_env: false, log_level: :info, log_tag: "script[run some perl code]" } - end - - before do - allow(STDOUT).to receive(:tty?).and_return(false) - end - - it 'should set the command to "interpreter" "tempfile"' do - expect(provider.command).to eq(%Q{"perl" "#{tempfile.path}"}) - end - - it "should call shell_out! with the command" do - expect(provider).to receive(:shell_out!).with(provider.command, default_opts).and_return(true) - provider.action_run - end - - it "should set the command to 'interpreter flags tempfile'" do - new_resource.flags "-f" - expect(provider.command).to eq(%Q{"perl" -f "#{tempfile.path}"}) - end + it "should call shell_out! with the command and correct options" do + opts = { + timeout: 3600, + returns: 0, + default_env: false, + log_level: :info, + log_tag: "script[run some perl code]", + input: "$| = 1; print 'i like beans'", + } + + expect(provider).to receive(:shell_out!).with(provider.command, opts).and_return(true) + provider.action_run end end - end |