Skip to content

Commit

Permalink
Merge pull request #2034 from IrimieBogdan/FACT-2747
Browse files Browse the repository at this point in the history
(FACT-2747) Add missing legacy facts on all platforms
  • Loading branch information
Bogdan Irimie authored Aug 28, 2020
2 parents b30bef4 + 86a797e commit 0b91206
Show file tree
Hide file tree
Showing 25 changed files with 387 additions and 97 deletions.
19 changes: 17 additions & 2 deletions lib/facter/facts/aix/disks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ module Facts
module Aix
class Disks
FACT_NAME = 'disks'
ALIASES = %w[blockdevices blockdevice_.*_size'].freeze

def call_the_resolver
facts = []
disks = Facter::Resolvers::Aix::Disks.resolve(:disks)

disks = disks&.empty? ? nil : disks
return Facter::ResolvedFact.new(FACT_NAME, nil) if disks.nil? || disks.empty?

Facter::ResolvedFact.new(FACT_NAME, disks)
blockdevices = disks.keys.join(',')
facts.push(Facter::ResolvedFact.new(FACT_NAME, disks))
facts.push(Facter::ResolvedFact.new('blockdevices', blockdevices, :legacy))
add_legacy_facts(disks, facts)

facts
end

private

def add_legacy_facts(disks, facts)
disks.each do |disk_name, disk_info|
facts.push(Facter::ResolvedFact.new("blockdevice_#{disk_name}_size", disk_info[:size_bytes].to_s, :legacy))
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/aix/sshfp_algorithm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def call_the_resolver
result = Facter::Resolvers::SshResolver.resolve(:ssh)
result.each do |ssh|
facts << Facter::ResolvedFact.new("sshfp_#{ssh.name.to_sym}",
"#{ssh.fingerprint.sha1} \n #{ssh.fingerprint.sha256}", :legacy)
"#{ssh.fingerprint.sha1}\n#{ssh.fingerprint.sha256}", :legacy)
end
facts
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/freebsd/sshfp_algorithm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def call_the_resolver
result = Facter::Resolvers::SshResolver.resolve(:ssh)
result.each do |ssh|
facts << Facter::ResolvedFact.new("sshfp_#{ssh.name.to_sym}",
"#{ssh.fingerprint.sha1} \n #{ssh.fingerprint.sha256}", :legacy)
"#{ssh.fingerprint.sha1}\n#{ssh.fingerprint.sha256}", :legacy)
end
facts
end
Expand Down
21 changes: 20 additions & 1 deletion lib/facter/facts/linux/disks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,30 @@ module Facts
module Linux
class Disks
FACT_NAME = 'disks'
ALIASES = %w[blockdevices blockdevice_.*_model blockdevice_.*_size blockdevice_.*_vendor'].freeze

def call_the_resolver
facts = []
disks = Facter::Resolvers::Linux::Disk.resolve(:disks)

Facter::ResolvedFact.new(FACT_NAME, disks)
return Facter::ResolvedFact.new(FACT_NAME, nil) if disks.nil? || disks.empty?

blockdevices = disks.keys.join(',')
facts.push(Facter::ResolvedFact.new(FACT_NAME, disks))
facts.push(Facter::ResolvedFact.new('blockdevices', blockdevices, :legacy))
add_legacy_facts(disks, facts)

facts
end

private

def add_legacy_facts(disks, facts)
disks.each do |disk_name, disk_info|
facts.push(Facter::ResolvedFact.new("blockdevice_#{disk_name}_model", disk_info[:model], :legacy))
facts.push(Facter::ResolvedFact.new("blockdevice_#{disk_name}_size", disk_info[:size_bytes].to_s, :legacy))
facts.push(Facter::ResolvedFact.new("blockdevice_#{disk_name}_vendor", disk_info[:vendor], :legacy))
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/facter/facts/linux/processors/speed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Facts
module Linux
module Processors
class Speed
FACT_NAME = 'processors.speed'

def call_the_resolver
fact_value = Facter::Resolvers::Linux::Processors.resolve(:speed)
speed = Facter::FactsUtils::UnitConverter.hertz_to_human_readable(fact_value)
Facter::ResolvedFact.new(FACT_NAME, speed)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/facter/facts/linux/sshfp_algorithm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def call_the_resolver
result = Facter::Resolvers::SshResolver.resolve(:ssh)
result.each do |ssh|
facts << Facter::ResolvedFact.new("sshfp_#{ssh.name.to_sym}",
"#{ssh.fingerprint.sha1} \n #{ssh.fingerprint.sha256}", :legacy)
"#{ssh.fingerprint.sha1}\n#{ssh.fingerprint.sha256}", :legacy)
end
facts
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/macosx/sshfp_algorithm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def call_the_resolver
result = Facter::Resolvers::SshResolver.resolve(:ssh)
result.each do |ssh|
facts << Facter::ResolvedFact.new("sshfp_#{ssh.name.to_sym}",
"#{ssh.fingerprint.sha1} \n #{ssh.fingerprint.sha256}", :legacy)
"#{ssh.fingerprint.sha1}\n#{ssh.fingerprint.sha256}", :legacy)
end
facts
end
Expand Down
23 changes: 21 additions & 2 deletions lib/facter/facts/solaris/disks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,29 @@ module Facts
module Solaris
class Disks
FACT_NAME = 'disks'
ALIASES = %w[blockdevices blockdevice_.*_size blockdevice_.*_vendor'].freeze

def call_the_resolver
fact_value = Facter::Resolvers::Solaris::Disks.resolve(:disks)
Facter::ResolvedFact.new(FACT_NAME, fact_value)
facts = []
disks = Facter::Resolvers::Solaris::Disks.resolve(:disks)

return Facter::ResolvedFact.new(FACT_NAME, nil) if disks.nil? || disks.empty?

blockdevices = disks.keys.join(',')
facts.push(Facter::ResolvedFact.new(FACT_NAME, disks))
facts.push(Facter::ResolvedFact.new('blockdevices', blockdevices, :legacy))
add_legacy_facts(disks, facts)

facts
end

private

def add_legacy_facts(disks, facts)
disks.each do |disk_name, disk_info|
facts.push(Facter::ResolvedFact.new("blockdevice_#{disk_name}_size", disk_info[:size_bytes].to_s, :legacy))
facts.push(Facter::ResolvedFact.new("blockdevice_#{disk_name}_vendor", disk_info[:vendor], :legacy))
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/solaris/sshfp_algorithm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def call_the_resolver
result = Facter::Resolvers::SshResolver.resolve(:ssh)
result.each do |ssh|
facts << Facter::ResolvedFact.new("sshfp_#{ssh.name.to_sym}",
"#{ssh.fingerprint.sha1} \n #{ssh.fingerprint.sha256}", :legacy)
"#{ssh.fingerprint.sha1}\n#{ssh.fingerprint.sha256}", :legacy)
end
facts
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/solaris/zones.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def call_the_resolver
end

resolved_facts << Facter::ResolvedFact.new('solaris_zones.zones', zones)
resolved_facts << Facter::ResolvedFact.new('zones', results.count, :legacy)
resolved_facts << Facter::ResolvedFact.new('zones', results.count.to_s, :legacy)

resolved_facts.flatten
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/windows/sshfp_algorithm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def call_the_resolver

result&.each do |ssh|
facts << Facter::ResolvedFact.new("sshfp_#{ssh.name.to_sym}",
"#{ssh.fingerprint.sha1} \n #{ssh.fingerprint.sha256}", :legacy)
"#{ssh.fingerprint.sha1}\n#{ssh.fingerprint.sha256}", :legacy)
end
facts
end
Expand Down
26 changes: 24 additions & 2 deletions lib/facter/resolvers/processors_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class Processors < BaseResolver
@log = Facter::Log.new(self)
@semaphore = Mutex.new
@fact_list ||= {}
MHZ_TO_HZ = 1_000_000
class << self
# :count
# :models
# :physical_count
# :speed

private

Expand Down Expand Up @@ -38,6 +40,7 @@ def read_processors(cpuinfo_output)
count_processors(tokens)
construct_models_list(tokens)
count_physical_processors(tokens)
build_speed(tokens)
end
end

Expand All @@ -46,15 +49,34 @@ def count_processors(tokens)
end

def construct_models_list(tokens)
@fact_list[:models] << tokens.last.strip if tokens.first.strip == 'model name'
return unless tokens.first.strip == 'model name' || tokens.first.strip == 'cpu'

@fact_list[:models] << tokens.last.strip
end

def count_physical_processors(tokens)
@fact_list[:physical_processors] << tokens.last.strip.to_i if tokens.first.strip == 'physical id'
end

def physical_devices_count
Dir.entries('/sys/devices/system/cpu').count { |dir| dir =~ /cpu[0-9]+$/ }
Dir.entries('/sys/devices/system/cpu')
.select { |dir| dir =~ /cpu[0-9]+$/ }
.count { |dir| File.exist?("/sys/devices/system/cpu/#{dir}/topology/physical_package_id") }
end

def build_speed(tokens)
build_speed_for_power_pc(tokens) if tokens.first.strip == 'clock'
build_speed_for_x86(tokens) if tokens.first.strip == 'cpu MHz'
end

def build_speed_for_power_pc(tokens)
speed = tokens.last.strip.match(/^(\d+).*MHz/)[1]
@fact_list[:speed] = speed.to_i * MHZ_TO_HZ
end

def build_speed_for_x86(tokens)
speed = tokens.last.strip.match(/^(\d+).*/)[1]
@fact_list[:speed] = speed.to_i * MHZ_TO_HZ
end
end
end
Expand Down
25 changes: 18 additions & 7 deletions spec/facter/facts/aix/disks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@

let(:disk) do
{
'disks' => {
'hdisk0' => {
'size' => '20.00 GiB',
'size_bytes' => 21_474_836_480
}
'hdisk0' => {
size: '20.00 GiB',
size_bytes: 21_474_836_480
}
}
end

let(:expecte_response) do
{
'hdisk0' => {
'size' => '20.00 GiB',
'size_bytes' => 21_474_836_480
}
}
end
Expand All @@ -26,8 +33,12 @@

it 'returns resolved fact with name disk and value' do
expect(fact.call_the_resolver)
.to be_an_instance_of(Facter::ResolvedFact)
.and have_attributes(name: 'disks', value: disk)
.to be_an_instance_of(Array)
.and contain_exactly(
an_object_having_attributes(name: 'disks', value: expecte_response),
an_object_having_attributes(name: 'blockdevices', value: 'hdisk0'),
an_object_having_attributes(name: 'blockdevice_hdisk0_size', value: '21474836480', type: :legacy)
)
end

context 'when resolver returns empty hash' do
Expand Down
4 changes: 2 additions & 2 deletions spec/facter/facts/aix/sshfp_algorithm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
[Facter::Ssh.new(Facter::FingerPrint.new('sha11', 'sha2561'), 'ecdsa', 'test', 'ecdsa'),
Facter::Ssh.new(Facter::FingerPrint.new('sha12', 'sha2562'), 'rsa', 'test', 'rsa')]
end
let(:legacy_fact1) { { name: 'ecdsa', value: "sha11 \n sha2561" } }
let(:legacy_fact2) { { name: 'rsa', value: "sha12 \n sha2562" } }
let(:legacy_fact1) { { name: 'ecdsa', value: "sha11\nsha2561" } }
let(:legacy_fact2) { { name: 'rsa', value: "sha12\nsha2562" } }

before do
allow(Facter::Resolvers::SshResolver).to \
Expand Down
4 changes: 2 additions & 2 deletions spec/facter/facts/freebsd/sshfp_algorithm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
[Facter::Ssh.new(Facter::FingerPrint.new('sha11', 'sha2561'), 'ecdsa', 'test', 'ecdsa'),
Facter::Ssh.new(Facter::FingerPrint.new('sha12', 'sha2562'), 'rsa', 'test', 'rsa')]
end
let(:legacy_fact1) { { name: 'ecdsa', value: "sha11 \n sha2561" } }
let(:legacy_fact2) { { name: 'rsa', value: "sha12 \n sha2562" } }
let(:legacy_fact1) { { name: 'ecdsa', value: "sha11\nsha2561" } }
let(:legacy_fact2) { { name: 'rsa', value: "sha12\nsha2562" } }

before do
allow(Facter::Resolvers::SshResolver).to \
Expand Down
53 changes: 44 additions & 9 deletions spec/facter/facts/linux/disks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@

let(:disk) do
{
'disks' => {
'sda' => {
'model' => 'Virtual disk',
'size' => '20.00 GiB',
'size_bytes' => 21_474_836_480,
'vendor' => 'VMware'
}
'sda' => {
model: 'Virtual disk',
size: '20.00 GiB',
size_bytes: 21_474_836_480,
vendor: 'VMware'
}
}
end

let(:expecte_response) do
{
'sda' => {
'model' => 'Virtual disk',
'size' => '20.00 GiB',
'size_bytes' => 21_474_836_480,
'vendor' => 'VMware'
}
}
end
Expand All @@ -28,8 +37,34 @@

it 'returns resolved fact with name disk and value' do
expect(fact.call_the_resolver)
.to be_an_instance_of(Facter::ResolvedFact)
.and have_attributes(name: 'disks', value: disk)
.to be_an_instance_of(Array)
.and contain_exactly(
an_object_having_attributes(name: 'disks', value: expecte_response),
an_object_having_attributes(name: 'blockdevices', value: 'sda'),
an_object_having_attributes(name: 'blockdevice_sda_model', value: 'Virtual disk', type: :legacy),
an_object_having_attributes(name: 'blockdevice_sda_size', value: '21474836480', type: :legacy),
an_object_having_attributes(name: 'blockdevice_sda_vendor', value: 'VMware', type: :legacy)
)
end

context 'when resolver returns empty hash' do
let(:disk) { {} }

it 'returns nil fact' do
expect(fact.call_the_resolver)
.to be_an_instance_of(Facter::ResolvedFact)
.and have_attributes(name: 'disks', value: nil)
end
end

context 'when resolver returns nil' do
let(:disk) { nil }

it 'returns nil fact' do
expect(fact.call_the_resolver)
.to be_an_instance_of(Facter::ResolvedFact)
.and have_attributes(name: 'disks', value: nil)
end
end
end
end
25 changes: 25 additions & 0 deletions spec/facter/facts/linux/processors/speed_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

describe Facts::Linux::Processors::Speed do
describe '#call_the_resolver' do
subject(:fact) { Facts::Linux::Processors::Speed.new }

let(:speed) { 1_800_000_000 }
let(:converted_speed) { '1.80 GHz' }

before do
allow(Facter::Resolvers::Linux::Processors).to \
receive(:resolve).with(:speed).and_return(speed)
end

it 'calls Facter::Resolvers::Linux::Processors' do
fact.call_the_resolver
expect(Facter::Resolvers::Linux::Processors).to have_received(:resolve).with(:speed)
end

it 'returns a resolved fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'processors.speed', value: converted_speed)
end
end
end
4 changes: 2 additions & 2 deletions spec/facter/facts/linux/sshfp_algorithm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
[Facter::Ssh.new(Facter::FingerPrint.new('sha11', 'sha2561'), 'ecdsa', 'test', 'ecdsa'),
Facter::Ssh.new(Facter::FingerPrint.new('sha12', 'sha2562'), 'rsa', 'test', 'rsa')]
end
let(:legacy_fact1) { { name: 'ecdsa', value: "sha11 \n sha2561" } }
let(:legacy_fact2) { { name: 'rsa', value: "sha12 \n sha2562" } }
let(:legacy_fact1) { { name: 'ecdsa', value: "sha11\nsha2561" } }
let(:legacy_fact2) { { name: 'rsa', value: "sha12\nsha2562" } }

before do
allow(Facter::Resolvers::SshResolver).to \
Expand Down
Loading

0 comments on commit 0b91206

Please sign in to comment.