diff options
author | danielsdeleo <dan@opscode.com> | 2012-08-22 11:37:15 -0700 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2012-08-22 12:01:35 -0700 |
commit | ca3b636dfe30f31f2596f06e793fd32044aadce7 (patch) | |
tree | a2301c220a21b35df153877e83b6581cd8c24aed | |
parent | 7b6c5def26b4200cb5f05de43fbcd7318cfd49fb (diff) | |
download | chef-ca3b636dfe30f31f2596f06e793fd32044aadce7.tar.gz |
[CHEF-3382] filter backtraces on cookbook_path
...instead of file_cache_path, which is only correct on chef-client (not
solo).
Also, regex escape the cookbook path(s) to avoid issues with regex
metacharacters in the path(s).
4 files changed, 46 insertions, 8 deletions
diff --git a/chef/lib/chef/formatters/error_inspectors/compile_error_inspector.rb b/chef/lib/chef/formatters/error_inspectors/compile_error_inspector.rb index ca359d6271..5d3e2ceb29 100644 --- a/chef/lib/chef/formatters/error_inspectors/compile_error_inspector.rb +++ b/chef/lib/chef/formatters/error_inspectors/compile_error_inspector.rb @@ -85,7 +85,8 @@ class Chef end def filtered_bt - r = exception.backtrace.select {|l| l =~ /^#{Chef::Config.file_cache_path}/ } + filters = Array(Chef::Config.cookbook_path).map {|p| /^#{Regexp.escape(p)}/ } + r = exception.backtrace.select {|line| filters.any? {|filter| line =~ filter }} return r.count > 0 ? r : exception.backtrace end diff --git a/chef/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb b/chef/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb index 44031db9ba..9de3fc50ef 100644 --- a/chef/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb +++ b/chef/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb @@ -84,7 +84,8 @@ class Chef end def filtered_bt - Array( exception.backtrace ).select {|l| l =~ /^#{Chef::Config.file_cache_path}/ } + filters = Array(Chef::Config.cookbook_path).map {|p| /^#{Regexp.escape(p)}/ } + exception.backtrace.select {|line| filters.any? {|filter| line =~ filter }} end private diff --git a/chef/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb b/chef/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb index c8e1024599..70d5068e76 100644 --- a/chef/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb +++ b/chef/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb @@ -40,20 +40,45 @@ describe Chef::Formatters::ErrorInspectors::CompileErrorInspector do before do @node_name = "test-node.example.com" @description = Chef::Formatters::ErrorDescription.new("Error Evaluating File:") + @exception = NoMethodError.new("undefined method `this_is_not_a_valid_method' for Chef::Resource::File") + @outputter = Chef::Formatters::Outputter.new(StringIO.new, STDERR) #@outputter = Chef::Formatters::Outputter.new(STDOUT, STDERR) + end + + describe "when scrubbing backtraces" do + it "shows backtrace lines from cookbook files" do + # Error inspector originally used file_cache_path which is incorrect on + # chef-solo. Using cookbook_path should do the right thing for client and + # solo. + Chef::Config.stub!(:cookbook_path).and_return([ "/home/someuser/dev-laptop/cookbooks" ]) + @trace = [ + "/home/someuser/dev-laptop/cookbooks/syntax-err/recipes/default.rb:14:in `from_file'", + "/home/someuser/dev-laptop/cookbooks/syntax-err/recipes/default.rb:11:in `from_file'", + "/home/someuser/.multiruby/gems/chef/lib/chef/client.rb:123:in `run'" + ] + @exception.set_backtrace(@trace) + @path = "/var/chef/cache/cookbooks/syntax-err/recipes/default.rb" + @inspector = described_class.new(@path, @exception) - Chef::Config.stub!(:file_cache_path).and_return("/var/chef/cache") + @expected_filtered_trace = [ + "/home/someuser/dev-laptop/cookbooks/syntax-err/recipes/default.rb:14:in `from_file'", + "/home/someuser/dev-laptop/cookbooks/syntax-err/recipes/default.rb:11:in `from_file'", + ] + @inspector.filtered_bt.should == @expected_filtered_trace + end end describe "when explaining an error in the compile phase" do before do - IO.should_receive(:readlines).with("/var/chef/cache/cookbooks/syntax-err/recipes/default.rb").and_return(BAD_RECIPE.split("\n")) + Chef::Config.stub!(:cookbook_path).and_return([ "/var/chef/cache/cookbooks" ]) + recipe_lines = BAD_RECIPE.split("\n").map {|l| l << "\n" } + IO.should_receive(:readlines).with("/var/chef/cache/cookbooks/syntax-err/recipes/default.rb").and_return(recipe_lines) @trace = [ "/var/chef/cache/cookbooks/syntax-err/recipes/default.rb:14:in `from_file'", - "/var/chef/cache/cookbooks/syntax-err/recipes/default.rb:11:in `from_file'" + "/var/chef/cache/cookbooks/syntax-err/recipes/default.rb:11:in `from_file'", + "/usr/local/lib/ruby/gems/chef/lib/chef/client.rb:123:in `run'" # should not display ] - @exception = NoMethodError.new("undefined method `this_is_not_a_valid_method' for Chef::Resource::File") @exception.set_backtrace(@trace) @path = "/var/chef/cache/cookbooks/syntax-err/recipes/default.rb" @inspector = described_class.new(@path, @exception) diff --git a/chef/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb b/chef/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb index fe39fe54a4..eb674adb76 100644 --- a/chef/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb +++ b/chef/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb @@ -36,7 +36,8 @@ describe Chef::Formatters::ErrorInspectors::ResourceFailureInspector do @description = Chef::Formatters::ErrorDescription.new("Error Converging Resource:") @outputter = Chef::Formatters::Outputter.new(StringIO.new, STDERR) #@outputter = Chef::Formatters::Outputter.new(STDOUT, STDERR) - Chef::Config.stub!(:file_cache_path).and_return("/var/chef/cache") + + Chef::Config.stub!(:cookbook_path).and_return([ "/var/chef/cache" ]) end describe "when explaining an error converging a resource" do @@ -54,7 +55,8 @@ describe Chef::Formatters::ErrorInspectors::ResourceFailureInspector do @trace = [ "/var/chef/cache/cookbooks/syntax-err/recipes/default.rb:14:in `from_file'", - "/var/chef/cache/cookbooks/syntax-err/recipes/default.rb:11:in `from_file'" + "/var/chef/cache/cookbooks/syntax-err/recipes/default.rb:11:in `from_file'", + "/usr/local/lib/ruby/gems/chef/lib/chef/client.rb:123:in `run'" # should not display ] @exception = Chef::Exceptions::Package.new("No such package 'non-existing-package'") @exception.set_backtrace(@trace) @@ -62,6 +64,15 @@ describe Chef::Formatters::ErrorInspectors::ResourceFailureInspector do @inspector.add_explanation(@description) end + it "filters chef core code from the backtrace" do + @expected_filtered_trace = [ + "/var/chef/cache/cookbooks/syntax-err/recipes/default.rb:14:in `from_file'", + "/var/chef/cache/cookbooks/syntax-err/recipes/default.rb:11:in `from_file'", + ] + + @inspector.filtered_bt.should == @expected_filtered_trace + end + it "prints a pretty message" do @description.display(@outputter) end |