Skip to content

Commit

Permalink
(FACT-2826) Added Facter.flush (#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-miclea authored Oct 20, 2020
1 parent 6f1c5a1 commit 8fdf1b5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
52 changes: 52 additions & 0 deletions acceptance/tests/facter_flush.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
test_name 'facter should flush fact values' do
tag 'risk:high'

fact_content1 = <<-EOM
require 'facter'
Facter.add(:fact1) do
'this should be flushed'
end
Facter.flush
puts "Fact1: \#\{Facter.value(:fact1)\}"
EOM

fact_content2 = <<-EOM
require 'facter'
Facter.add(:fact2) do
on_flush do
puts 'before flush'
end
end
Facter.flush
EOM

agents.each do |agent|
fact_dir = agent.tmpdir('test_scripts')
script_path1 = File.join(fact_dir, 'flush_test1.rb')
script_path2 = File.join(fact_dir, 'flush_test2.rb')
create_remote_file(agent, script_path1, fact_content1)
create_remote_file(agent, script_path2, fact_content2)

teardown do
agent.rm_rf(script_path1)
agent.rm_rf(script_path2)
end

step 'fact value has been flushed' do
on(agent, "#{ruby_command(agent)} #{script_path1}") do |ruby_result|
assert_equal('Fact1: ', ruby_result.stdout.chomp)
end
end

step 'prints on_flush block gets called' do
on(agent, "#{ruby_command(agent)} #{script_path2}") do |ruby_result|
assert_equal('before flush', ruby_result.stdout.chomp)
end
end
end
end
12 changes: 12 additions & 0 deletions lib/facter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ def reset
nil
end

# Flushes cached values for all facts. This does not cause code to be
# reloaded; it only clears the cached results.
#
# @return [void]
#
# @api public
def flush
LegacyFacter.flush
SessionCache.invalidate_all_caches
nil
end

# Loads all facts
#
# @return [nil]
Expand Down
18 changes: 18 additions & 0 deletions spec/facter/facter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,24 @@ def mock_collection(method, os_name = nil, error = nil)
end
end

describe '#flush' do
it 'sends call to LegacyFacter' do
allow(LegacyFacter).to receive(:flush)

Facter.flush

expect(LegacyFacter).to have_received(:flush).once
end

it 'invalidates core cache' do
allow(Facter::SessionCache).to receive(:invalidate_all_caches)

Facter.flush

expect(Facter::SessionCache).to have_received(:invalidate_all_caches)
end
end

describe '#search' do
it 'sends call to Facter::Options' do
allow(Facter::Options).to receive(:[]=)
Expand Down

0 comments on commit 8fdf1b5

Please sign in to comment.