summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrajaktaPurohit <prajakta@opscode.com>2014-02-04 11:30:00 -0800
committerPrajaktaPurohit <prajakta@opscode.com>2014-02-04 11:30:00 -0800
commit10d1e224cdd8a4f06b65af62d7113224e69291ab (patch)
tree11ade3b5e410109ac9d09b3785d5fca5e358a164
parent3743ff393832422dd5c36d9c093f11646889df01 (diff)
parent17c83dc0376945bb67625e9f69e50fbe5036b8d3 (diff)
downloadchef-10d1e224cdd8a4f06b65af62d7113224e69291ab.tar.gz
Merge pull request #1240 from opscode/praj/resource_name_and_id_to_str
The resource_name and resource_id while being sent to reporting are always strings.
-rw-r--r--lib/chef/resource_reporter.rb4
-rw-r--r--spec/unit/resource_reporter_spec.rb55
2 files changed, 57 insertions, 2 deletions
diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb
index d29949086e..04f4ee26de 100644
--- a/lib/chef/resource_reporter.rb
+++ b/lib/chef/resource_reporter.rb
@@ -50,8 +50,8 @@ class Chef
def for_json
as_hash = {}
as_hash["type"] = new_resource.class.dsl_name
- as_hash["name"] = new_resource.name
- as_hash["id"] = new_resource.identity
+ as_hash["name"] = new_resource.name.to_s
+ as_hash["id"] = new_resource.identity.to_s
as_hash["after"] = state(new_resource)
as_hash["before"] = current_resource ? state(current_resource) : {}
as_hash["duration"] = (elapsed_time * 1000).to_i.to_s
diff --git a/spec/unit/resource_reporter_spec.rb b/spec/unit/resource_reporter_spec.rb
index c6f41156a4..52fd44e692 100644
--- a/spec/unit/resource_reporter_spec.rb
+++ b/spec/unit/resource_reporter_spec.rb
@@ -262,6 +262,61 @@ describe Chef::ResourceReporter do
@resource_reporter.run_started(@run_status)
end
+ context "when the new_resource does not have a string for name and identity" do
+ context "the new_resource name and id are nil" do
+ before do
+ @bad_resource = Chef::Resource::File.new("/tmp/nameless_file.txt")
+ @bad_resource.stub(:name).and_return(nil)
+ @bad_resource.stub(:identity).and_return(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 "resource_name in prepared_run_data is a string" do
+ @first_update_report["name"].class.should == String
+ end
+
+ it "resource_id in prepared_run_data is a string" do
+ @first_update_report["id"].class.should == String
+ end
+ end
+
+ context "the new_resource name and id are hashes" do
+ before do
+ @bad_resource = Chef::Resource::File.new("/tmp/filename_as_hash.txt")
+ @bad_resource.stub(:name).and_return({:foo=>:bar})
+ @bad_resource.stub(:identity).and_return({:foo=>:bar})
+ @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
+ # Ruby 1.8.7 flattens out hash to string using join instead of inspect, resulting in
+ # irb(main):001:0> {:foo => :bar}.to_s
+ # => "foobar"
+ # instead of the expected
+ # irb(main):001:0> {:foo => :bar}.to_s
+ # => "{:foo=>:bar}"
+ # Hence checking for the class instead of the actual value.
+ it "resource_name in prepared_run_data is a string" do
+ @first_update_report["name"].class.should == String
+ end
+
+ it "resource_id in prepared_run_data is a string" do
+ @first_update_report["id"].class.should == String
+ end
+ end
+ end
+
+
context "for a successful client run" do
before do
# TODO: add inputs to generate expected output.