diff options
Diffstat (limited to 'spec/unit/provider/execute_spec.rb')
-rw-r--r-- | spec/unit/provider/execute_spec.rb | 63 |
1 files changed, 35 insertions, 28 deletions
diff --git a/spec/unit/provider/execute_spec.rb b/spec/unit/provider/execute_spec.rb index 4b0afcb928..58cb9f33a8 100644 --- a/spec/unit/provider/execute_spec.rb +++ b/spec/unit/provider/execute_spec.rb @@ -1,6 +1,6 @@ # # Author:: Prajakta Purohit (<prajakta@chef.io>) -# Copyright:: Copyright 2008-2016, Chef Software Inc. +# Copyright:: Copyright 2008-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -76,16 +76,34 @@ describe Chef::Provider::Execute do describe "#action_run" do it "runs shell_out with the default options" do - expect(provider).to receive(:shell_out!).with(new_resource.name, opts) + expect(provider).to receive(:shell_out_with_systems_locale!).with(new_resource.name, opts) expect(provider).to receive(:converge_by).with("execute foo_resource").and_call_original expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) expect(new_resource).to be_updated end + # this next test is tightly coupled to the implementation of the underlying shell_out mixin that we're using + # but the point is to ensure that we are not picking up the PATH mangling and locale-variable mangling that the internal + # shell_out API uses. we are asserting that we emulate `ls -la` when the user does `execute "ls -la"`, and to + # do that we get dirty and start mocking the implementation of the shell_out mixin itself. while arguments like + # "timeout", "returns", "log_level" and "log_tag" appear here, we MUST NOT have an "environment" or "env" argument + # that we are passing to Mixlib::ShellOut by default -- ever. you might have to add some other argument here from + # time to time, but you MUST NOT change the environment. + it "does not use shell_out in such a way as to insert extra environment variables" do + mock = instance_double(Mixlib::ShellOut) + expect(Mixlib::ShellOut).to receive(:new).with("foo_resource", { timeout: 3600, returns: 0, log_level: :info, log_tag: "execute[foo_resource]" }).and_return(mock) + expect(mock).to receive(:live_stream=).with(nil) + allow(mock).to receive(:live_stream) + expect(mock).to receive(:run_command) + expect(mock).to receive(:error!) + provider.run_action(:run) + expect(new_resource).to be_updated + end + it "if you pass a command attribute, it runs the command" do new_resource.command "/usr/argelbargle/bin/oogachacka 12345" - expect(provider).to receive(:shell_out!).with(new_resource.command, opts) + expect(provider).to receive(:shell_out_with_systems_locale!).with(new_resource.command, opts) expect(provider).to receive(:converge_by).with("execute #{new_resource.command}").and_call_original expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) @@ -96,7 +114,7 @@ describe Chef::Provider::Execute do new_resource.sensitive true # Since the resource is sensitive, it should not have :live_stream set opts.delete(:live_stream) - expect(provider).to receive(:shell_out!).with(new_resource.name, opts) + expect(provider).to receive(:shell_out_with_systems_locale!).with(new_resource.name, opts) expect(provider).to receive(:converge_by).with("execute sensitive resource").and_call_original expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) @@ -106,7 +124,7 @@ describe Chef::Provider::Execute do it "should do nothing if the sentinel file exists" do new_resource.creates "/foo_resource" expect(FileTest).to receive(:exist?).with(new_resource.creates).and_return(true) - expect(provider).not_to receive(:shell_out!) + expect(provider).not_to receive(:shell_out_with_systems_locale!) expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) expect(new_resource).not_to be_updated @@ -118,19 +136,9 @@ describe Chef::Provider::Execute do new_resource.creates "foo_resource" end - it "should warn in Chef-12", chef: "< 13" do - expect(Chef::Log).to receive(:warn).with(/relative path/) - expect(FileTest).to receive(:exist?).with(new_resource.creates).and_return(true) - expect(provider).not_to receive(:shell_out!) - provider.run_action(:run) - expect(new_resource).not_to be_updated - end - - it "should raise if user specified relative path without cwd for Chef-13", chef: ">= 13" do - expect(Chef::Log).to receive(:warn).with(/relative path/) - expect(FileTest).to receive(:exist?).with(new_resource.creates).and_return(true) - expect(provider).not_to receive(:shell_out!) - expect { provider.run_action(:run) }.to raise_error # @todo: add a real error for Chef-13 + it "should raise if user specified relative path without cwd for Chef-13" do + expect(provider).not_to receive(:shell_out_with_systems_locale!) + expect { provider.run_action(:run) }.to raise_error(Chef::Exceptions::Execute) end end @@ -140,7 +148,7 @@ describe Chef::Provider::Execute do expect(FileTest).not_to receive(:exist?).with(new_resource.creates) expect(FileTest).to receive(:exist?).with(File.join("/tmp", new_resource.creates)).and_return(true) expect(Chef::Log).not_to receive(:warn) - expect(provider).not_to receive(:shell_out!) + expect(provider).not_to receive(:shell_out_with_systems_locale!) provider.run_action(:run) expect(new_resource).not_to be_updated @@ -149,7 +157,7 @@ describe Chef::Provider::Execute do it "should not include stdout/stderr in failure exception for sensitive resource" do opts.delete(:live_stream) new_resource.sensitive true - expect(provider).to receive(:shell_out!).and_raise(Mixlib::ShellOut::ShellCommandFailed) + expect(provider).to receive(:shell_out_with_systems_locale!).and_raise(Mixlib::ShellOut::ShellCommandFailed) expect do provider.run_action(:run) end.to raise_error(Mixlib::ShellOut::ShellCommandFailed, /suppressed for sensitive resource/) @@ -158,7 +166,7 @@ describe Chef::Provider::Execute do describe "streaming output" do it "should not set the live_stream if sensitive is on" do new_resource.sensitive true - expect(provider).to receive(:shell_out!).with(new_resource.name, opts) + expect(provider).to receive(:shell_out_with_systems_locale!).with(new_resource.name, opts) expect(provider).to receive(:converge_by).with("execute sensitive resource").and_call_original expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) @@ -175,7 +183,7 @@ describe Chef::Provider::Execute do it "should set the live_stream if the log level is info or above" do nopts = opts nopts[:live_stream] = @live_stream - expect(provider).to receive(:shell_out!).with(new_resource.name, nopts) + expect(provider).to receive(:shell_out_with_systems_locale!).with(new_resource.name, nopts) expect(provider).to receive(:converge_by).with("execute foo_resource").and_call_original expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) @@ -187,7 +195,7 @@ describe Chef::Provider::Execute do new_resource.live_stream true nopts = opts nopts[:live_stream] = @live_stream - expect(provider).to receive(:shell_out!).with(new_resource.name, nopts) + expect(provider).to receive(:shell_out_with_systems_locale!).with(new_resource.name, nopts) expect(provider).to receive(:converge_by).with("execute foo_resource").and_call_original expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) @@ -196,7 +204,7 @@ describe Chef::Provider::Execute do it "should not set the live_stream if the resource is sensitive" do new_resource.sensitive true - expect(provider).to receive(:shell_out!).with(new_resource.name, opts) + expect(provider).to receive(:shell_out_with_systems_locale!).with(new_resource.name, opts) expect(provider).to receive(:converge_by).with("execute sensitive resource").and_call_original expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) @@ -209,7 +217,7 @@ describe Chef::Provider::Execute do nopts = opts nopts[:live_stream] = STDOUT allow(STDOUT).to receive(:tty?).and_return(true) - expect(provider).to receive(:shell_out!).with(new_resource.name, nopts) + expect(provider).to receive(:shell_out_with_systems_locale!).with(new_resource.name, nopts) expect(provider).to receive(:converge_by).with("execute foo_resource").and_call_original expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) @@ -219,7 +227,7 @@ describe Chef::Provider::Execute do it "should not set the live_stream to STDOUT if we are a TTY, not daemonized, but sensitive" do new_resource.sensitive true allow(STDOUT).to receive(:tty?).and_return(true) - expect(provider).to receive(:shell_out!).with(new_resource.name, opts) + expect(provider).to receive(:shell_out_with_systems_locale!).with(new_resource.name, opts) expect(provider).to receive(:converge_by).with("execute sensitive resource").and_call_original expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) @@ -229,7 +237,7 @@ describe Chef::Provider::Execute do it "should not set the live_stream to STDOUT if we are a TTY, but daemonized" do Chef::Config[:daemon] = true allow(STDOUT).to receive(:tty?).and_return(true) - expect(provider).to receive(:shell_out!).with(new_resource.name, opts) + expect(provider).to receive(:shell_out_with_systems_locale!).with(new_resource.name, opts) expect(provider).to receive(:converge_by).with("execute foo_resource").and_call_original expect(Chef::Log).not_to receive(:warn) provider.run_action(:run) @@ -238,6 +246,5 @@ describe Chef::Provider::Execute do end end - end end |