Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FACT-2817) Only invalidate session cache on clear and reset. #2121

Merged
merged 2 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions acceptance/tests/session_cached_is_not_refershed_in_session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
test_name 'facter should not update it`s session cache in same session' do
tag 'risk:high'

fact_content = <<-EOM
require 'facter'

seconds_before = Facter.value('system_uptime.seconds')
sleep(3)
seconds_after = Facter.value('system_uptime.seconds')

puts seconds_before == seconds_after
EOM

agents.each do |agent|
fact_dir = agent.tmpdir('test_scripts')
script_path = File.join(fact_dir, 'session_test.rb')
create_remote_file(agent, script_path, fact_content)

teardown do
agent.rm_rf(script_path)
end

on(agent, "#{ruby_command(agent)} #{script_path}") do |ruby_result|
assert_equal('true', ruby_result.stdout.chomp, 'Expect the session cache is not reset in same session')
end
end
end
1 change: 0 additions & 1 deletion custom_facts/my_custom_fact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Facter.add(:my_custom_fact) do
has_weight(10_000)
setcode do
# 'my_custom_fact'
Facter.value('os')
end
end
7 changes: 2 additions & 5 deletions lib/facter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def clear
Options[:custom_dir] = []
LegacyFacter.collection.invalidate_custom_facts
LegacyFacter.collection.reload_custom_facts
SessionCache.invalidate_all_caches
end

def core_value(user_query)
Expand Down Expand Up @@ -170,7 +171,6 @@ def debugging(debug_bool)
def each
log_blocked_facts
resolved_facts = Facter::FactManager.instance.resolve_facts
SessionCache.invalidate_all_caches

resolved_facts.each do |fact|
yield(fact.name, fact.value)
Expand Down Expand Up @@ -206,6 +206,7 @@ def reset
LegacyFacter.reset
Options[:custom_dir] = []
Options[:external_dir] = []
SessionCache.invalidate_all_caches
nil
end

Expand Down Expand Up @@ -270,7 +271,6 @@ def to_hash
log_blocked_facts

resolved_facts = Facter::FactManager.instance.resolve_facts
Facter::SessionCache.invalidate_all_caches
Facter::FactCollection.new.build_fact_collection!(resolved_facts)
end

Expand Down Expand Up @@ -308,7 +308,6 @@ def value(user_query)
def values(options, user_queries)
init_cli_options(options, user_queries)
resolved_facts = Facter::FactManager.instance.resolve_facts(user_queries)
Facter::SessionCache.invalidate_all_caches

if user_queries.count.zero?
Facter::FactCollection.new.build_fact_collection!(resolved_facts)
Expand Down Expand Up @@ -336,7 +335,6 @@ def to_user_output(cli_options, *args)
logger.info("executed with command line: #{ARGV.drop(1).join(' ')}")
log_blocked_facts
resolved_facts = Facter::FactManager.instance.resolve_facts(args)
SessionCache.invalidate_all_caches
fact_formatter = Facter::FormatterFactory.build(Facter::Options.get)

status = error_check(resolved_facts)
Expand Down Expand Up @@ -426,7 +424,6 @@ def add_fact_to_searched_facts(user_query, value)
def resolve_fact(user_query)
user_query = user_query.to_s
resolved_facts = Facter::FactManager.instance.resolve_facts([user_query])
SessionCache.invalidate_all_caches
# we must make a distinction between custom facts that return nil and nil facts
# Nil facts should not be packaged as ResolvedFacts! (add_fact_to_searched_facts packages facts)
resolved_facts = resolved_facts.reject { |fact| fact.type == :nil }
Expand Down