summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjamesc <james@opscode.com>2013-07-09 17:54:25 -0700
committerdanielsdeleo <dan@opscode.com>2013-07-10 16:24:31 -0700
commit1c3783bc793115858c4b401aeb9ee3cd16be27d3 (patch)
tree1619ef14764f391340920560dc693fbcfe4ce1d2
parent532ed5d95c244a81d5b6eda67ea3e3ba3ada1f57 (diff)
downloadchef-1c3783bc793115858c4b401aeb9ee3cd16be27d3.tar.gz
OC-8641 - new_resource.cookbook_version is nil for some resources
Since the existing tests mocked out Resource#cookbook_version it missed the case where if Resource#cookbook_name is nil then cookbook_version would also be nil. Added guard logic around setting the cookbook_name, cookbook_version in resource_reporter.rb to protect against the case where cookbook_name is nil.
-rw-r--r--lib/chef/resource_reporter.rb10
-rw-r--r--spec/unit/resource_reporter_spec.rb51
2 files changed, 59 insertions, 2 deletions
diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb
index 44e8a068db..33d5d2da6a 100644
--- a/lib/chef/resource_reporter.rb
+++ b/lib/chef/resource_reporter.rb
@@ -64,8 +64,14 @@ class Chef
else
#as_hash["result"] = "failed"
end
- as_hash["cookbook_name"] = new_resource.cookbook_name
- as_hash["cookbook_version"] = new_resource.cookbook_version.version
+ if new_resource.cookbook_name
+ as_hash["cookbook_name"] = new_resource.cookbook_name
+ as_hash["cookbook_version"] = new_resource.cookbook_version.version
+ else
+ as_hash["cookbook_name"] = ""
+ as_hash["cookbook_version"] = "0.0.0"
+ end
+
as_hash
end
diff --git a/spec/unit/resource_reporter_spec.rb b/spec/unit/resource_reporter_spec.rb
index c4a1de9f13..ef513fdfcd 100644
--- a/spec/unit/resource_reporter_spec.rb
+++ b/spec/unit/resource_reporter_spec.rb
@@ -413,6 +413,57 @@ describe Chef::ResourceReporter do
end
+ context "when new_resource does not have a cookbook_name" do
+ before do
+ @bad_resource = Chef::Resource::File.new("/tmp/a-file.txt")
+ @bad_resource.cookbook_name = nil
+
+ @resource_reporter.resource_action_start(@bad_resource, :create)
+ @resource_reporter.resource_current_state_loaded(@bad_resource, :create, @current_resource)
+ @resource_reporter.resource_updated(@bad_resource, :create)
+ @resource_reporter.resource_completed(@bad_resource)
+ @run_status.stop_clock
+ @report = @resource_reporter.prepare_run_data
+ @first_update_report = @report["resources"].first
+ end
+
+ it "includes an updated resource's initial state" do
+ @first_update_report["before"].should == @current_resource.state
+ end
+
+ it "includes an updated resource's final state" do
+ @first_update_report["after"].should == @new_resource.state
+ end
+
+ it "includes the resource's name" do
+ @first_update_report["name"].should == @new_resource.name
+ end
+
+ it "includes the resource's id attribute" do
+ @first_update_report["id"].should == @new_resource.identity
+ end
+
+ it "includes the elapsed time for the resource to converge" do
+ # TODO: API takes integer number of milliseconds as a string. This
+ # should be an int.
+ @first_update_report.should have_key("duration")
+ @first_update_report["duration"].to_i.should be_within(100).of(0)
+ end
+
+ it "includes the action executed by the resource" do
+ # TODO: rename as "action"
+ @first_update_report["result"].should == "create"
+ end
+
+ it "includes a default (blank) cookbook name" do
+ @first_update_report["cookbook_name"].should == ""
+ end
+
+ it "includes a default (0.0.0) cookbook version" do
+ @first_update_report["cookbook_version"].should == "0.0.0"
+ end
+ end
+
end
describe "when updating resource history on the server" do