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-2826) Added Facter.flush #2140

Merged
merged 1 commit into from
Oct 20, 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
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 @@ -228,6 +228,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unit test should be separated according to the AAA pattern (Arrange, Act, Assert).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks!


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