From fa3f0f404c4c373368151163f6b3412a6f4b411f Mon Sep 17 00:00:00 2001 From: Florin Dragos Date: Tue, 15 Sep 2020 12:17:25 +0300 Subject: [PATCH] (FACT-2786) Fix fact caching if fact is defined in multiple groups --- .../tests/custom_facts/cached_custom_fact.rb | 2 +- .../weighted_cached_custom_facts.rb | 2 +- .../ttls_cached_fact_in_multiple_groups.rb | 57 +++++++++++++++++ .../ttls_cached_individual_fact_name.rb | 63 +++++++++++++++++++ .../custom_facts/util/directory_loader.rb | 2 +- lib/facter/framework/config/fact_groups.rb | 38 ++++++++++- lib/facter/framework/core/cache_manager.rb | 48 ++++++++------ spec/facter/cache_manager_spec.rb | 34 ++++++---- 8 files changed, 210 insertions(+), 36 deletions(-) create mode 100644 acceptance/tests/options/config_file/ttls_cached_fact_in_multiple_groups.rb create mode 100644 acceptance/tests/options/config_file/ttls_cached_individual_fact_name.rb diff --git a/acceptance/tests/custom_facts/cached_custom_fact.rb b/acceptance/tests/custom_facts/cached_custom_fact.rb index 0f7a452548..bf7a083859 100644 --- a/acceptance/tests/custom_facts/cached_custom_fact.rb +++ b/acceptance/tests/custom_facts/cached_custom_fact.rb @@ -73,7 +73,7 @@ step 'should read from the cached file for a custom fact that has been cached' do on(agent, facter("#{custom_fact_name} --debug", environment: env)) do |facter_result| - assert_match(/Loading cached custom facts from file ".+"|loading cached values for cached-custom-facts facts/, facter_result.stderr, + assert_match(/Loading cached custom facts from file ".+"|loading cached values for random_custom_fact facts/, facter_result.stderr, 'Expected debug message to state that cached custom facts are read from file') end end diff --git a/acceptance/tests/custom_facts/weighted_cached_custom_facts.rb b/acceptance/tests/custom_facts/weighted_cached_custom_facts.rb index dcd9a363ea..3cc04ee30d 100644 --- a/acceptance/tests/custom_facts/weighted_cached_custom_facts.rb +++ b/acceptance/tests/custom_facts/weighted_cached_custom_facts.rb @@ -85,7 +85,7 @@ step 'should read from the cached file for a custom fact that has been cached' do on(agent, facter("#{duplicate_custom_fact_name} --debug", environment: env)) do |facter_result| - assert_match(/Loading cached custom facts from file ".+"|loading cached values for cached-custom-facts facts/, facter_result.stderr, + assert_match(/Loading cached custom facts from file ".+"|loading cached values for random_custom_fact facts/, facter_result.stderr, 'Expected debug message to state that cached custom facts are read from file') end end diff --git a/acceptance/tests/options/config_file/ttls_cached_fact_in_multiple_groups.rb b/acceptance/tests/options/config_file/ttls_cached_fact_in_multiple_groups.rb new file mode 100644 index 0000000000..aeca1824f7 --- /dev/null +++ b/acceptance/tests/options/config_file/ttls_cached_fact_in_multiple_groups.rb @@ -0,0 +1,57 @@ +# This test verifies that individual facts can be cached +test_name "ttls config with fact in multiple groups should not cache fact twice" do + tag 'risk:high' + + require 'facter/acceptance/user_fact_utils' + extend Facter::Acceptance::UserFactUtils + + # This fact must be resolvable on ALL platforms + # Do NOT use the 'kernel' fact as it is used to configure the tests + cached_fact_name = 'os.name' + first_fact_group = 'first' + second_fact_group = 'second' + + config = <