diff options
author | Tim Smith <tsmith@chef.io> | 2021-10-22 21:22:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-22 21:22:11 -0700 |
commit | ce3cff547004f035e9db090da4708c394230e586 (patch) | |
tree | d96c254c3511a2ecce485cb1b1454737102ff860 | |
parent | ce70c573c79d966ba94fe7992750fcf470068864 (diff) | |
parent | 4646a4b9d28c19cc750907825cc972f4722e6316 (diff) | |
download | chef-ce3cff547004f035e9db090da4708c394230e586.tar.gz |
Merge pull request #12206 from chef/lcg/fix-reporting-for-recipe-files
Fix reporting/data_collector for @recipe_files
-rw-r--r-- | lib/chef/data_collector/run_end_message.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource_reporter.rb | 2 | ||||
-rw-r--r-- | spec/unit/data_collector_spec.rb | 25 | ||||
-rw-r--r-- | spec/unit/resource_spec.rb | 5 |
5 files changed, 32 insertions, 4 deletions
diff --git a/lib/chef/data_collector/run_end_message.rb b/lib/chef/data_collector/run_end_message.rb index 91cf21e643..48b1d55aff 100644 --- a/lib/chef/data_collector/run_end_message.rb +++ b/lib/chef/data_collector/run_end_message.rb @@ -128,7 +128,7 @@ class Chef if new_resource.cookbook_name hash["cookbook_name"] = new_resource.cookbook_name - hash["cookbook_version"] = new_resource.cookbook_version.version + hash["cookbook_version"] = new_resource.cookbook_version&.version hash["recipe_name"] = new_resource.recipe_name end diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 517c30bc19..e7ac91594f 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -1508,7 +1508,7 @@ class Chef # @return Chef::CookbookVersion The cookbook in which this Resource was defined. # def cookbook_version - if cookbook_name + if cookbook_name && cookbook_name != "@recipe_files" run_context.cookbook_collection[cookbook_name] end end diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb index c6a7be133f..242a6e9f0f 100644 --- a/lib/chef/resource_reporter.rb +++ b/lib/chef/resource_reporter.rb @@ -41,7 +41,7 @@ class Chef as_hash["result"] = action_record.action.to_s if new_resource.cookbook_name as_hash["cookbook_name"] = new_resource.cookbook_name - as_hash["cookbook_version"] = new_resource.cookbook_version.version + as_hash["cookbook_version"] = new_resource.cookbook_version&.version end as_hash diff --git a/spec/unit/data_collector_spec.rb b/spec/unit/data_collector_spec.rb index 24f8807d2e..eb698d6661 100644 --- a/spec/unit/data_collector_spec.rb +++ b/spec/unit/data_collector_spec.rb @@ -164,7 +164,7 @@ describe Chef::DataCollector do "after" => after_resource&.state_for_resource_reporter || {}, "before" => before_resource&.state_for_resource_reporter || {}, "cookbook_name" => cookbook_name, - "cookbook_version" => cookbook_version.version, + "cookbook_version" => cookbook_version&.version, "delta" => resource_has_diff(new_resource, status) ? new_resource.diff : "", "duration" => duration, "id" => new_resource.identity, @@ -567,6 +567,29 @@ describe Chef::DataCollector do it_behaves_like "sends a converge message" end + context "when the run contains a file resource that is up-to-date from a @recipe_files, returns nil for the version" do + let(:total_resource_count) { 1 } + let(:updated_resource_count) { 0 } + let(:cookbook_name) { "@recipe_files" } + let(:resource_record) { [ resource_record_for(new_resource, current_resource, after_resource, :create, "up-to-date", "1234") ] } + let(:status) { "success" } + let(:cookbook_version) { nil } + + before do + allow(new_resource).to receive(:cookbook_version).and_call_original + events.resource_action_start(new_resource, :create) + events.resource_current_state_loaded(new_resource, :create, current_resource) + events.resource_up_to_date(new_resource, :create) + events.resource_after_state_loaded(new_resource, :create, after_resource) + new_resource.instance_variable_set(:@elapsed_time, 1.2345) + events.resource_completed(new_resource) + events.converge_complete + run_status.stop_clock + end + + it_behaves_like "sends a converge message" + end + context "when the run contains a file resource that is updated" do let(:total_resource_count) { 1 } let(:updated_resource_count) { 1 } diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index 5f662dea60..faf87076fd 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -348,6 +348,11 @@ describe Chef::Resource do it "should recognize dynamically defined resources" do expect(resource.defined_at).to eq("dynamically defined") end + + it "should return nil for the cookbook_version when the cookbook_name is @recipe_files" do + resource.cookbook_name = "@recipe_files" + expect(resource.cookbook_version).to be nil + end end describe "to_s" do |