forked from puppetlabs/facter
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(FACT-2934)Set last exit code after Execution.exec
- Loading branch information
1 parent
b018d98
commit 4cf1dbb
Showing
7 changed files
with
109 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
acceptance/tests/custom_facts/execution_execute_sets_status_env_variable.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
test_name "(FACT-2934) Facter::Core::Execution sets $?" do | ||
tag 'risk:high' | ||
|
||
agents.each do |agent| | ||
command = if agent['platform'] =~/windows/ | ||
'cmd /c exit 1' | ||
else | ||
`which false`.chomp | ||
end | ||
|
||
content = <<-EOM | ||
Facter.add(:foo) do | ||
setcode do | ||
Facter::Util::Resolution.exec('#{command}') | ||
"#{command} exited with code: %{status}" % {status: $?.exitstatus} | ||
end | ||
end | ||
EOM | ||
|
||
|
||
fact_dir = agent.tmpdir('facter') | ||
fact_file = File.join(fact_dir, 'test_facts.rb') | ||
create_remote_file(agent, fact_file, content) | ||
|
||
teardown do | ||
agent.rm_rf(fact_dir) | ||
end | ||
|
||
step "Facter: should resolve the custom fact" do | ||
on(agent, facter('--custom-dir', fact_dir, 'foo')) do |facter_output| | ||
assert_match(/exited with code: 1/, facter_output.stdout.chomp) | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
# | ||
# Because Open3 uses Process.detach the env $? is not set so | ||
# this class reimplements Open3.popen3 with Process.wait instead. | ||
# [FACT-2934] When calling Facter::Core::Execution, $? and $CHILD_STATUS | ||
# ruby env variables should be set. | ||
# | ||
|
||
module Facter | ||
module Core | ||
module Execution | ||
class Popen3 | ||
def self.popen_rune(cmd, opts, child_io, parent_io) # :nodoc: | ||
pid = spawn(*cmd, opts) | ||
child_io.each(&:close) | ||
result = [*parent_io, pid] | ||
if defined? yield | ||
begin | ||
return yield(*result) | ||
ensure | ||
parent_io.each(&:close) | ||
Process.wait(pid) | ||
end | ||
end | ||
result | ||
end | ||
|
||
def self.popen3e(*cmd, &block) | ||
opts = if cmd.last.is_a? Hash | ||
cmd.pop.dup | ||
else | ||
{} | ||
end | ||
in_r, in_w = IO.pipe | ||
opts[:in] = in_r | ||
in_w.sync = true | ||
out_r, out_w = IO.pipe | ||
opts[:out] = out_w | ||
err_r, err_w = IO.pipe | ||
opts[:err] = err_w | ||
popen_rune(cmd, opts, [in_r, out_w, err_w], [in_w, out_r, err_r], &block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters