Skip to content

Commit

Permalink
Added disk_type field to disk fact
Browse files Browse the repository at this point in the history
'disk' fact was extended with :type field, which indicates disk type:
'ssd' or 'hdd' depending on the value of queue/rotational file in sysfs
  • Loading branch information
kozl authored and Aleksandr Kozlov committed Oct 20, 2020
1 parent 8674aaa commit e5b2a50
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
14 changes: 11 additions & 3 deletions lib/facter/resolvers/disk_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Disk < BaseResolver
@log = Facter::Log.new(self)
@fact_list ||= {}
DIR = '/sys/block'
FILE_PATHS = { model: 'device/model', size: 'size', vendor: 'device/vendor' }.freeze
FILE_PATHS = { model: 'device/model', size: 'size', vendor: 'device/vendor', type: 'queue/rotational' }.freeze
class << self
private

Expand All @@ -25,8 +25,16 @@ def read_facts(fact_name)
result = Util::FileHelper.safe_read(file_path).strip
next if result.empty?

# Linux always considers sectors to be 512 bytes long independently of the devices real block size.
value[key] = file =~ /size/ ? construct_size(value, result) : result
value[key] = case key
when :size
# Linux always considers sectors to be 512 bytes long
# independently of the devices real block size.
construct_size(value, result)
when :type
result == '0' ? 'ssd' : 'hdd'
else
result
end
end
end

Expand Down
37 changes: 32 additions & 5 deletions spec/facter/resolvers/disk_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
subject(:resolver) { Facter::Resolvers::Linux::Disk }

let(:disk_files) { %w[sr0/device sr0/size sda/device sda/size] }
let(:paths) { { model: '/device/model', size: '/size', vendor: '/device/vendor' } }
let(:paths) { { model: '/device/model', size: '/size', vendor: '/device/vendor', type: '/queue/rotational' } }
let(:disks) { %w[sr0 sda] }
let(:size) { '12' }
let(:model) { 'test' }
let(:rotational) { '1' }
let(:nonrotational) { '0' }

before do
allow(Dir).to receive(:entries).with('/sys/block').and_return(['.', '..', 'sr0', 'sda'])
Expand All @@ -20,8 +22,8 @@

context 'when device dir for blocks exists' do
let(:expected_output) do
{ 'sda' => { model: 'test', size: '6.00 KiB', size_bytes: 6144, vendor: 'test' },
'sr0' => { model: 'test', size: '6.00 KiB', size_bytes: 6144, vendor: 'test' } }
{ 'sda' => { model: 'test', size: '6.00 KiB', size_bytes: 6144, vendor: 'test', type: 'hdd' },
'sr0' => { model: 'test', size: '6.00 KiB', size_bytes: 6144, vendor: 'test', type: 'hdd' } }
end

before do
Expand All @@ -33,6 +35,9 @@
if value == '/size'
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with("/sys/block/#{disk}#{value}").and_return(size)
elsif value == 'queue/rotational'
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with("/sys/block/#{disk}#{value}").and_return(rotational)
else
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with("/sys/block/#{disk}#{value}").and_return(model)
Expand All @@ -47,10 +52,32 @@
end
end

context 'when disk are non-rotational' do
let(:expected_output) do
{ 'sda' => { model: 'test', size: '6.00 KiB', size_bytes: 6144, vendor: 'test', type: 'ssd' },
'sr0' => { model: 'test', size: '6.00 KiB', size_bytes: 6144, vendor: 'test', type: 'ssd' } }
end

before do
paths.each do |_key, value|
disks.each do |disk|
if value == '/queue/rotational'
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with("/sys/block/#{disk}#{value}").and_return(nonrotational)
end
end
end
end

it 'returns disks fact' do
expect(resolver.resolve(:disks)).to eql(expected_output)
end
end

context 'when size files are not readable' do
let(:expected_output) do
{ 'sda' => { model: 'test', vendor: 'test' },
'sr0' => { model: 'test', vendor: 'test' } }
{ 'sda' => { model: 'test', vendor: 'test', type: 'hdd' },
'sr0' => { model: 'test', vendor: 'test', type: 'hdd' } }
end

before do
Expand Down

0 comments on commit e5b2a50

Please sign in to comment.