summaryrefslogtreecommitdiff
path: root/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb
blob: 3c8d5dfa29153fa927b95344b667ae67f3ee5300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

BAD_RECIPE=<<-E
#
# Cookbook Name:: syntax-err
# Recipe:: default
#
# Copyright 2012, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#


file "/tmp/explode-me" do
  mode 0655
  owner "root"
  this_is_not_a_valid_method
end
E

describe Chef::Formatters::ErrorInspectors::CompileErrorInspector do

  let(:node_name) { "test-node.example.com" }

  let(:description) { Chef::Formatters::ErrorDescription.new("Error Evaluating File:") }

  let(:exception) do
    e = NoMethodError.new("undefined method `this_is_not_a_valid_method' for Chef::Resource::File")
    e.set_backtrace(trace)
    e
  end

  # Change to $stdout to print error messages for manual inspection
  let(:stdout) { StringIO.new }

  let(:outputter) { Chef::Formatters::IndentableOutputStream.new(StringIO.new, STDERR) }

  subject(:inspector) { described_class.new(path_to_failed_file, exception) }

  describe "finding the code responsible for the error" do

    context "when the stacktrace includes cookbook files" do

      let(:trace) do
        [
          "/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'"
        ]
      end

      let(:expected_filtered_trace) do
        [
          "/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'",
        ]
      end

      let(:path_to_failed_file) { "/home/someuser/dev-laptop/cookbooks/syntax-err/recipes/default.rb" }

      before 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.
        allow(Chef::Config).to receive(:cookbook_path).and_return([ "/home/someuser/dev-laptop/cookbooks" ])
      end

      describe "when scrubbing backtraces" do
        it "shows backtrace lines from cookbook files" do
          expect(inspector.filtered_bt).to eq(expected_filtered_trace)
        end
      end

      describe "when explaining an error in the compile phase" do
        before do
          recipe_lines = BAD_RECIPE.split("\n").map {|l| l << "\n" }
          expect(IO).to receive(:readlines).with(path_to_failed_file).and_return(recipe_lines)
          inspector.add_explanation(description)
        end

        it "reports the error was not located within cookbooks" do
          expect(inspector.found_error_in_cookbooks?).to be(true)
        end

        it "finds the line number of the error from the stacktrace" do
          expect(inspector.culprit_line).to eq(14)
        end

        it "prints a pretty message" do
          description.display(outputter)
        end
      end
    end

    context "when the error is a RuntimeError about frozen object" do
      let(:exception) do
        e = RuntimeError.new("can't modify frozen Array")
        e.set_backtrace(trace)
        e
      end

      let(:path_to_failed_file) { "/tmp/kitchen/cache/cookbooks/foo/recipes/default.rb" }

      let(:trace) do
        [
          "/tmp/kitchen/cache/cookbooks/foo/recipes/default.rb:2:in `block in from_file'",
          "/tmp/kitchen/cache/cookbooks/foo/recipes/default.rb:1:in `from_file'"
        ]
      end

      describe "when explaining a runtime error in the compile phase" do
        it "correctly detects RuntimeError for frozen objects" do
          expect(inspector.exception_message_modifying_frozen?).to be(true)
        end

        # could also test for description.section to be called, but would have
        # to adjust every other test to begin using a test double for description
      end
    end

    context "when the error does not contain any lines from cookbooks" do

      let(:trace) do
        [
          "/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:144:in `rescue in block in load_libraries'",
          "/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:138:in `block in load_libraries'",
          "/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:230:in `call'",
          "/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:230:in `block (2 levels) in foreach_cookbook_load_segment'",
          "/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:229:in `each'",
          "/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:229:in `block in foreach_cookbook_load_segment'",
          "/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:227:in `each'",
          "/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:227:in `foreach_cookbook_load_segment'",
          "/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:137:in `load_libraries'"
        ]
      end

      let(:exception) do
        e = Chef::Exceptions::RecipeNotFound.new("recipe nope:nope not found")
        e.set_backtrace(trace)
        e
      end

      let(:path_to_failed_file) { nil }

      it "gives a full, non-filtered trace" do
        expect(inspector.filtered_bt).to eq(trace)
      end

      it "does not error when displaying the error" do
        expect { description.display(outputter) }.to_not raise_error
      end

      it "reports the error was not located within cookbooks" do
        expect(inspector.found_error_in_cookbooks?).to be(false)
      end

    end
  end

  describe "when explaining an error on windows" do

    let(:trace_with_upcase_drive) do
      [
        "C:/opscode/chef/var/cache/cookbooks/foo/recipes/default.rb:14 in `from_file'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:144:in `rescue in block in load_libraries'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:138:in `block in load_libraries'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:230:in `call'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:230:in `block (2 levels) in foreach_cookbook_load_segment'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:229:in `each'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:229:in `block in foreach_cookbook_load_segment'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:227:in `each'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:227:in `foreach_cookbook_load_segment'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:137:in `load_libraries'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/run_context.rb:62:in `load'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/client.rb:198:in `setup_run_context'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/client.rb:418:in `do_run'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/client.rb:176:in `run'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/application/client.rb:283:in `block in run_application'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/application/client.rb:270:in `loop'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/application/client.rb:270:in `run_application'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/lib/chef/application.rb:70:in `run'",
        "C:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-10.14.0/bin/chef-client:26:in `<top (required)>'",
        "C:/opscode/chef/bin/chef-client:19:in `load'",
        "C:/opscode/chef/bin/chef-client:19:in `<main>'"
      ]
    end

    let(:trace) { trace_with_upcase_drive }

    let(:path_to_failed_file) { "/var/cache/cookbooks/foo/recipes/default.rb" }

    before do
      allow(Chef::Config).to receive(:cookbook_path).and_return([ "C:/opscode/chef/var/cache/cookbooks" ])
      recipe_lines = BAD_RECIPE.split("\n").map {|l| l << "\n" }
      expect(IO).to receive(:readlines).at_least(1).times.with(full_path_to_failed_file).and_return(recipe_lines)
      inspector.add_explanation(description)
    end

    context "when the drive letter in the path is uppercase" do

      let(:full_path_to_failed_file) { "C:/opscode/chef#{path_to_failed_file}" }

      it "reports the error was not located within cookbooks" do
        expect(inspector.found_error_in_cookbooks?).to be(true)
      end

      it "finds the culprit recipe name" do
        expect(inspector.culprit_file).to eq("C:/opscode/chef/var/cache/cookbooks/foo/recipes/default.rb")
      end

      it "finds the line number of the error from the stack trace" do
        expect(inspector.culprit_line).to eq(14)
      end

      it "prints a pretty message" do
        description.display(outputter)
      end
    end

    context "when the drive letter in the path is lowercase" do

      let(:trace) do
        trace_with_upcase_drive.map { |line| line.gsub(/^C:/, "c:") }
      end

      let(:full_path_to_failed_file) { "c:/opscode/chef#{path_to_failed_file}" }

      it "reports the error was not located within cookbooks" do
        expect(inspector.found_error_in_cookbooks?).to be(true)
      end

      it "finds the culprit recipe name from the stacktrace" do
        expect(inspector.culprit_file).to eq("c:/opscode/chef/var/cache/cookbooks/foo/recipes/default.rb")
      end

      it "finds the line number of the error from the stack trace" do
        expect(inspector.culprit_line).to eq(14)
      end

      it "prints a pretty message" do
        description.display(outputter)
      end
    end

  end

end