diff options
author | Vivek Singh <vivek.singh@msystechnologies.com> | 2020-09-08 09:02:01 +0530 |
---|---|---|
committer | Vivek Singh <vivek.singh@msystechnologies.com> | 2020-09-08 09:02:01 +0530 |
commit | cffe6515dfaa445e30faccb3dd48670037c4d03b (patch) | |
tree | 1d409a5ef8bd6af1cc5f9af3c791683b71c954b6 /lib/chef/data_collector | |
parent | bef4d5046cfe2234c8224fc8a280f0efba5d93ec (diff) | |
download | chef-cffe6515dfaa445e30faccb3dd48670037c4d03b.tar.gz |
Data collector multiple fixes
- Fix invalid output_locations raise start_time error.
- Improve validate_output_locations!
Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
Diffstat (limited to 'lib/chef/data_collector')
-rw-r--r-- | lib/chef/data_collector/config_validation.rb | 17 | ||||
-rw-r--r-- | lib/chef/data_collector/run_end_message.rb | 4 | ||||
-rw-r--r-- | lib/chef/data_collector/run_start_message.rb | 2 |
3 files changed, 17 insertions, 6 deletions
diff --git a/lib/chef/data_collector/config_validation.rb b/lib/chef/data_collector/config_validation.rb index 1a7940b0ae..5f8e7d3fe5 100644 --- a/lib/chef/data_collector/config_validation.rb +++ b/lib/chef/data_collector/config_validation.rb @@ -46,14 +46,14 @@ class Chef return unless output_locations # but deliberately setting an empty output_location we consider to be an error (XXX: but should we?) - if output_locations.empty? + unless valid_hash_with_keys?(output_locations, :urls, :files) raise Chef::Exceptions::ConfigurationError, "Chef::Config[:data_collector][:output_locations] is empty. Please supply an hash of valid URLs and / or local file paths." end # loop through all the types and locations and validate each one-by-one output_locations.each do |type, locations| - locations.each do |location| + Array(locations).each do |location| validate_url!(location) if type == :urls validate_file!(location) if type == :files end @@ -105,7 +105,7 @@ class Chef # validate an output_location file def validate_file!(file) - open(file, "a") {} + File.open(File.expand_path(file), "a") {} rescue Errno::ENOENT raise Chef::Exceptions::ConfigurationError, "Chef::Config[:data_collector][:output_locations][:files] contains the location #{file}, which is a non existent file path." @@ -125,6 +125,17 @@ class Chef "Chef::Config[:data_collector][:output_locations][:urls] contains the url #{url} which is not valid." end + # Validate a non-empty hash that includes either of keys of both. + # + # @param hash [Hash] the hash contains data collector output_locations. + # @param keys [Array] the multiple keys as arguments array. + # @return [Boolean] true if the hash contains either of keys or both. + # + def valid_hash_with_keys?(hash, *keys) + return false if hash.empty? || !hash.is_a?(Hash) + + keys.any? { |k| hash.key?(k) } + end end end end diff --git a/lib/chef/data_collector/run_end_message.rb b/lib/chef/data_collector/run_end_message.rb index 6f9f90b323..1900effa26 100644 --- a/lib/chef/data_collector/run_end_message.rb +++ b/lib/chef/data_collector/run_end_message.rb @@ -60,8 +60,8 @@ class Chef "cookbooks" => ( node && node["cookbooks"] ) || {}, "policy_name" => node&.policy_name, "policy_group" => node&.policy_group, - "start_time" => run_status.start_time.utc.iso8601, - "end_time" => run_status.end_time.utc.iso8601, + "start_time" => run_status&.start_time&.utc&.iso8601, + "end_time" => run_status&.end_time&.utc&.iso8601, "source" => solo_run? ? "chef_solo" : "chef_client", "status" => status, "total_resource_count" => all_action_records(action_collection).count, diff --git a/lib/chef/data_collector/run_start_message.rb b/lib/chef/data_collector/run_start_message.rb index e5023b83ed..20ac867ef1 100644 --- a/lib/chef/data_collector/run_start_message.rb +++ b/lib/chef/data_collector/run_start_message.rb @@ -51,7 +51,7 @@ class Chef "organization_name" => organization, "run_id" => run_status&.run_id, "source" => solo_run? ? "chef_solo" : "chef_client", - "start_time" => run_status.start_time.utc.iso8601, + "start_time" => run_status&.start_time&.utc&.iso8601, } end end |