summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2017-04-25 13:00:31 -0700
committerGitHub <noreply@github.com>2017-04-25 13:00:31 -0700
commit45409087be147ae0cd43d6f8531ea256020e5ad4 (patch)
tree8a30fa6eed708a904365d155db3f413ad16849b9
parentd531bcc83c390ecbe323b6cf3d26ea01ed60495e (diff)
parent7c969e1e80c2b86eb0583aebb4098e6a693ffba0 (diff)
downloadohai-45409087be147ae0cd43d6f8531ea256020e5ad4.tar.gz
Merge pull request #991 from akitada/linux_net_cmds_path
Inject sane paths into shell_out
-rw-r--r--lib/ohai/mixin/command.rb5
-rw-r--r--spec/unit/mixin/command_spec.rb17
2 files changed, 18 insertions, 4 deletions
diff --git a/lib/ohai/mixin/command.rb b/lib/ohai/mixin/command.rb
index 10c93baf..01a00edc 100644
--- a/lib/ohai/mixin/command.rb
+++ b/lib/ohai/mixin/command.rb
@@ -27,8 +27,13 @@ module Ohai
# DISCLAIMER: Logging only works in the context of a plugin!!
# accept a command and any of the mixlib-shellout options
def shell_out(cmd, **options)
+ options = options.dup
# unless specified by the caller timeout after 30 seconds
options[:timeout] ||= 30
+ unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
+ options[:env] = options.key?(:env) ? options[:env].dup : {}
+ options[:env]["PATH"] ||= ((ENV["PATH"] || "").split(":") + %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin}).join(":")
+ end
so = Mixlib::ShellOut.new(cmd, options)
begin
so.run_command
diff --git a/spec/unit/mixin/command_spec.rb b/spec/unit/mixin/command_spec.rb
index 7b2385d0..4fc656b1 100644
--- a/spec/unit/mixin/command_spec.rb
+++ b/spec/unit/mixin/command_spec.rb
@@ -26,15 +26,24 @@ describe Ohai::Mixin::Command, "shell_out" do
let(:plugin_name) { :OSSparkleDream }
+ let(:options) { windows? ? { timeout: 30 } : { timeout: 30, env: { "PATH" => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" } } }
+
before(:each) do
allow(Ohai::Mixin::Command).to receive(:name).and_return(plugin_name)
+ @original_env = ENV.to_hash
+ ENV.clear
+ end
+
+ after(:each) do
+ ENV.clear
+ ENV.update(@original_env)
end
describe "when the command runs" do
it "logs the command and exitstatus" do
expect(Mixlib::ShellOut).
to receive(:new).
- with(cmd, { timeout: 30 }).
+ with(cmd, options).
and_return(shell_out)
expect(shell_out).
@@ -55,7 +64,7 @@ describe Ohai::Mixin::Command, "shell_out" do
it "logs the command and error message" do
expect(Mixlib::ShellOut).
to receive(:new).
- with(cmd, { timeout: 30 }).
+ with(cmd, options).
and_return(shell_out)
expect(shell_out).
@@ -76,7 +85,7 @@ describe Ohai::Mixin::Command, "shell_out" do
it "logs the command an timeout error message" do
expect(Mixlib::ShellOut).
to receive(:new).
- with(cmd, { timeout: 30 }).
+ with(cmd, options).
and_return(shell_out)
expect(shell_out).
@@ -94,7 +103,7 @@ describe Ohai::Mixin::Command, "shell_out" do
end
describe "when a timeout option is provided" do
- let(:options) { { timeout: 10 } }
+ let(:options) { windows? ? { timeout: 10 } : { timeout: 10, env: { "PATH" => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" } } }
it "runs the command with the provided timeout" do
expect(Mixlib::ShellOut).