Skip to content

Commit 8423373

Browse files
authored
Merge pull request #2121 from IrimieBogdan/FACT-2817
(FACT-2817) Only invalidate session cache on clear and reset.
2 parents 3b9d8c4 + bb6357e commit 8423373

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
test_name 'facter should not update it`s session cache in same session' do
2+
tag 'risk:high'
3+
4+
fact_content = <<-EOM
5+
require 'facter'
6+
7+
seconds_before = Facter.value('system_uptime.seconds')
8+
sleep(3)
9+
seconds_after = Facter.value('system_uptime.seconds')
10+
11+
puts seconds_before == seconds_after
12+
EOM
13+
14+
agents.each do |agent|
15+
fact_dir = agent.tmpdir('test_scripts')
16+
script_path = File.join(fact_dir, 'session_test.rb')
17+
create_remote_file(agent, script_path, fact_content)
18+
19+
teardown do
20+
agent.rm_rf(script_path)
21+
end
22+
23+
on(agent, "#{ruby_command(agent)} #{script_path}") do |ruby_result|
24+
assert_equal('true', ruby_result.stdout.chomp, 'Expect the session cache is not reset in same session')
25+
end
26+
end
27+
end

custom_facts/my_custom_fact.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Facter.add(:my_custom_fact) do
44
has_weight(10_000)
55
setcode do
6-
# 'my_custom_fact'
76
Facter.value('os')
87
end
98
end

lib/facter.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def clear
8383
Options[:custom_dir] = []
8484
LegacyFacter.collection.invalidate_custom_facts
8585
LegacyFacter.collection.reload_custom_facts
86+
SessionCache.invalidate_all_caches
8687
end
8788

8889
def core_value(user_query)
@@ -170,7 +171,6 @@ def debugging(debug_bool)
170171
def each
171172
log_blocked_facts
172173
resolved_facts = Facter::FactManager.instance.resolve_facts
173-
SessionCache.invalidate_all_caches
174174

175175
resolved_facts.each do |fact|
176176
yield(fact.name, fact.value)
@@ -206,6 +206,7 @@ def reset
206206
LegacyFacter.reset
207207
Options[:custom_dir] = []
208208
Options[:external_dir] = []
209+
SessionCache.invalidate_all_caches
209210
nil
210211
end
211212

@@ -270,7 +271,6 @@ def to_hash
270271
log_blocked_facts
271272

272273
resolved_facts = Facter::FactManager.instance.resolve_facts
273-
Facter::SessionCache.invalidate_all_caches
274274
Facter::FactCollection.new.build_fact_collection!(resolved_facts)
275275
end
276276

@@ -308,7 +308,6 @@ def value(user_query)
308308
def values(options, user_queries)
309309
init_cli_options(options, user_queries)
310310
resolved_facts = Facter::FactManager.instance.resolve_facts(user_queries)
311-
Facter::SessionCache.invalidate_all_caches
312311

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

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

0 commit comments

Comments
 (0)