Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport error info for Chef 12 #6173

Merged
merged 2 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/chef/formatters/error_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# limitations under the License.
#

require "chef/version"

class Chef
module Formatters
# == Formatters::ErrorDescription
Expand Down Expand Up @@ -45,7 +47,7 @@ def display(out)
display_section(heading, text, out)
end
end
display_section("Platform:", RUBY_PLATFORM, out)
display_section("System Info:", error_context_info, out)
end

def for_json
Expand All @@ -64,6 +66,21 @@ def display_section(heading, text, out)
out.puts "\n"
end

def error_context_info
context_info = { chef_version: Chef::VERSION }
if Chef.node
context_info[:platform] = Chef.node["platform"]
context_info[:platform_version] = Chef.node["platform_version"]
end
# A string like "ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]"
context_info[:ruby] = RUBY_DESCRIPTION
# The argv[0] value.
context_info[:program_name] = $PROGRAM_NAME
# This is kind of wonky but it's the only way to get the entry path script.
context_info[:executable] = File.realpath(caller.last[/^(.*):\d+:in /, 1])
context_info.map { |k, v| "#{k}=#{v}" }.join("\n")
end

end
end
end
59 changes: 52 additions & 7 deletions spec/unit/formatters/error_description_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,21 @@

describe "#display" do
before do
stub_const("RUBY_PLATFORM", "ruby-foo-9000")
stub_const("Chef::VERSION", "1.2.3")
stub_const("RUBY_DESCRIPTION", "ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]")
allow(subject).to receive(:caller) { Kernel.caller + ["/test/bin/chef-client:1:in `<main>'"] }
allow(File).to receive(:realpath).and_call_original
allow(File).to receive(:realpath).with("/test/bin/chef-client").and_return("/test/bin/chef-client")
end

around do |ex|
old_program_name = $PROGRAM_NAME
begin
$PROGRAM_NAME = "chef-client"
ex.run
ensure
$PROGRAM_NAME = old_program_name
end
end

context "when no sections have been added" do
Expand All @@ -60,9 +74,12 @@
test title
================================================================================

Platform:
---------
ruby-foo-9000
System Info:
------------
chef_version=1.2.3
ruby=ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
program_name=chef-client
executable=/test/bin/chef-client

END
end
Expand All @@ -84,9 +101,37 @@
------------
test text

Platform:
---------
ruby-foo-9000
System Info:
------------
chef_version=1.2.3
ruby=ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
program_name=chef-client
executable=/test/bin/chef-client

END
end

end

context "when node object is available" do
it "should output the expected sections" do
# This can't be in a before block because the spec-wide helper calls a
# reset on global values.
Chef.set_node({ "platform" => "openvms", "platform_version" => "8.4-2L1" })
subject.display(out)
expect(out.out.string).to eq <<-END
================================================================================
test title
================================================================================

System Info:
------------
chef_version=1.2.3
platform=openvms
platform_version=8.4-2L1
ruby=ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
program_name=chef-client
executable=/test/bin/chef-client

END
end
Expand Down