Skip to content

Commit

Permalink
Merge pull request #2312 from oanatmaria/FACT-2962
Browse files Browse the repository at this point in the history
(FACT-2962) Add all supported time units for ttls
  • Loading branch information
ciprianbadescu committed Mar 10, 2021
2 parents 57700d3 + 7386c51 commit d45d064
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
25 changes: 19 additions & 6 deletions lib/facter/framework/config/fact_groups.rb
Expand Up @@ -7,10 +7,15 @@ class FactGroups
attr_accessor :groups_ttls
attr_reader :groups, :block_list, :facts_ttls

STRING_TO_SECONDS = { 'seconds' => 1, 'second' => 1,
'minutes' => 60, 'minute' => 60,
'hours' => 3600, 'hour' => 3600,
'days' => 3600 * 24, 'day' => 3600 * 24 }.freeze
STRING_TO_SECONDS = { 'ns' => 1.fdiv(1_000_000_000), 'nanos' => 1.fdiv(1_000_000_000),
'nanoseconds' => 1.fdiv(1_000_000_000),
'us' => 1.fdiv(1_000_000), 'micros' => 1.fdiv(1_000_000), 'microseconds' => 1.fdiv(1_000_000),
'' => 1.fdiv(1000), 'ms' => 1.fdiv(1000), 'milis' => 1.fdiv(1000),
'milliseconds' => 1.fdiv(1000),
's' => 1, 'seconds' => 1,
'm' => 60, 'minutes' => 60,
'h' => 3600, 'hours' => 3600,
'd' => 3600 * 24, 'days' => 3600 * 24 }.freeze

def initialize
@groups = Facter::Config::FACT_GROUPS.dup
Expand Down Expand Up @@ -106,14 +111,22 @@ def load_groups

def ttls_to_seconds(ttls)
duration, unit = ttls.split(' ', 2)
unit = '' if duration && !unit
unit = append_s(unit)
seconds = STRING_TO_SECONDS[unit]
if seconds
duration.to_i * seconds
(duration.to_i * seconds).to_i
else
log = Log.new(self)
log.error("Could not parse time unit #{unit} (try second(s), minute(s), hour(s) or day(s))")
log.error("Could not parse time unit #{unit} (try #{STRING_TO_SECONDS.keys.reject(&:empty?).join(', ')})")
nil
end
end

def append_s(unit)
return unit + 's' if unit.length > 2 && unit[-1] != 's'

unit
end
end
end
20 changes: 18 additions & 2 deletions spec/framework/config/fact_groups_spec.rb
Expand Up @@ -114,6 +114,22 @@
expect(fg.get_group_ttls('memory')).to be_nil
end

context 'when ttls has short names for units' do
let(:ttls) { ['operating system' => '10000000000000 ns', 'memory' => '10000', 'hostname' => '30 h'] }

it 'returns os ttl in seconds' do
expect(fg.get_group_ttls('operating system')).to eq(10_000)
end

it 'returns memory ttl in seconds' do
expect(fg.get_group_ttls('memory')).to eq(10)
end

it 'returns hostname ttl in seconds' do
expect(fg.get_group_ttls('hostname')).to eq(108_000)
end
end

context 'when ttls has hour instead of hour' do
let(:ttls) { ['operating system' => '1 hour', 'memory' => '1 day', 'hostname' => '30 invalid_unit'] }
let(:logger) { instance_spy(Facter::Log) }
Expand All @@ -124,8 +140,8 @@

it 'logs an error message' do
fg.get_group_ttls('hostname')
expect(logger).to have_received(:error).with('Could not parse time unit invalid_unit'\
' (try second(s), minute(s), hour(s) or day(s))').twice
expect(logger).to have_received(:error).with('Could not parse time unit invalid_units '\
"(try #{Facter::FactGroups::STRING_TO_SECONDS.keys.reject(&:empty?).join(', ')})").twice
end

it 'returns os ttl in seconds' do
Expand Down

0 comments on commit d45d064

Please sign in to comment.