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

Added disk_type field to disk fact #2145

Merged
merged 1 commit into from
Oct 21, 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
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