Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
(FACT-2086) Use core facts when weight is 0 for custom facts (#118)
Browse files Browse the repository at this point in the history
* (FACT-2086) Fix Facter interface for add method.
* (FACT-2086) Add test for empty resolved fact list in hocon formatter.
* (FACT-2086) Take into account weight when deciding between custom facts and core facts.
* (FACT-2086) Add tests for fact and collection.
  • Loading branch information
Bogdan Irimie authored and sebastian-miclea committed Oct 29, 2019
1 parent 40b4ee9 commit b1f8017
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Expand Up @@ -70,6 +70,7 @@ Metrics/ClassLength:
Exclude:
- 'lib/custom_facts/util/fact.rb'
- 'lib/resolvers/windows/networking_resolver.rb'
- 'lib/custom_facts/util/collection.rb'

Style/DoubleNegation:
Exclude:
Expand Down
4 changes: 2 additions & 2 deletions custom_facts/my_custom_fact.rb
@@ -1,9 +1,9 @@
# frozen_string_literal: true

LegacyFacter.add(:my_custom_fact) do
Facter.add(:my_custom_fact) do
has_weight(10_000)
setcode do
# 'my_custom_fact'
LegacyFacter.value('os')
Facter.value('os')
end
end
9 changes: 8 additions & 1 deletion lib/custom_facts/util/collection.rb
Expand Up @@ -137,9 +137,16 @@ def to_hash

def value(name)
fact = fact(name)

return Facter.core_value(name) if fact.nil?

fact&.value
value = fact&.value
weight = fact&.used_resolution_weight || 0

core_value = Facter.core_value(name)
return core_value if weight <= 0 && core_value

value
end

private
Expand Down
3 changes: 3 additions & 0 deletions lib/custom_facts/util/fact.rb
Expand Up @@ -17,6 +17,8 @@ class Fact
# @deprecated
attr_accessor :ldapname

attr_accessor :used_resolution_weight

# Creates a new fact, with no resolution mechanisms. See {Facter.add}
# for the public API for creating facts.
# @param name [String] the fact name
Expand Down Expand Up @@ -158,6 +160,7 @@ def sort_by_weight(resolutions)
def find_first_real_value(resolutions)
resolutions.each do |resolve|
value = resolve.value
@used_resolution_weight = resolve.weight
return value unless value.nil?
end
nil
Expand Down
2 changes: 2 additions & 0 deletions lib/custom_facts/util/resolution.rb
Expand Up @@ -16,6 +16,8 @@ class Resolution
attr_accessor :code
attr_writer :value

attr_reader :weight

extend LegacyFacter::Core::Execution

class << self
Expand Down
3 changes: 2 additions & 1 deletion lib/facter-ng.rb
Expand Up @@ -26,11 +26,12 @@ def self.value(user_query)
fact_collection.dig(*user_query.split('.'))
end

def add(name, options = {}, &block)
def self.add(name, options = {}, &block)
LegacyFacter.add(name, options, &block)
end

def self.core_value(user_query)
user_query = user_query.to_s
resolved_facts = Facter::FactManager.instance.resolve_core({}, [user_query])
fact_collection = FactCollection.new.build_fact_collection!(resolved_facts)
fact_collection.dig(*user_query.split('.'))
Expand Down
26 changes: 26 additions & 0 deletions spec/custom_facts/util/collection_spec.rb
Expand Up @@ -132,8 +132,34 @@
it 'should treat strings and symbols equivalently' do
expect(collection.value(:yayness)).to eq 'result'
end

describe 'when the fact is a core fact' do
it 'should call the core_value method' do
expect(Facter).to receive(:core_value).with('core_fact')
collection.value('core_fact')
end
end

describe 'when the weight of the resolution is 0' do
it 'should return core facts value is it exists' do
expect(Facter).to receive(:core_value).with('yayness').and_return('core_result')
expect(collection.value('yayness')).to eq('core_result')
end
end

describe 'when the weight of the resolution is greater than 0' do
it 'shoudl return the custom fact value' do
expect(collection.value('yayness')).to eq('result')
end
end
end

# describe 'when retriving a core fact' do
# before do
# # @fact = collection.add('YayNess', value: 'result')
# end
# end

it "should return the fact's value when the array index method is used" do
collection.add('myfact', value: 'foo')

Expand Down
20 changes: 20 additions & 0 deletions spec/custom_facts/util/fact_spec.rb
Expand Up @@ -149,6 +149,26 @@ def suitable?

expect(fact.value).to eq '1'
end

it 'sets weight of the resolution that gave the value' do
fact.add do
has_weight 1
setcode { '1' }
end

fact.add do
has_weight 2
setcode { nil }
end

fact.add do
has_weight 0
setcode { '0' }
end

expect(fact.value).to eq '1'
expect(fact.used_resolution_weight).to eq 1
end
end

describe '#flush' do
Expand Down
6 changes: 6 additions & 0 deletions spec/framework/formatters/hocon_fact_formatter_spec.rb
Expand Up @@ -40,4 +40,10 @@

expect(formatted_output).to eq(expected_output)
end

it 'formats to hocon for empty resolved fact array' do
formatted_output = Facter::HoconFactFormatter.new.format([])

expect(formatted_output).to eq(nil)
end
end

0 comments on commit b1f8017

Please sign in to comment.