From 530c9282fa2da6463bd98f10a1f028ae46797d84 Mon Sep 17 00:00:00 2001 From: Kozlov Alexander Date: Fri, 16 Oct 2020 03:05:53 +0300 Subject: [PATCH] (FACT-2844) Added disk_type field to disk fact 'disk' fact was extended with :type field, which indicates disk type: 'ssd' or 'hdd' depending on the value of queue/rotational file in sysfs --- lib/facter/resolvers/disk_resolver.rb | 14 ++++++-- spec/facter/resolvers/disk_resolver_spec.rb | 37 ++++++++++++++++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/facter/resolvers/disk_resolver.rb b/lib/facter/resolvers/disk_resolver.rb index a982acf391..d67480264a 100644 --- a/lib/facter/resolvers/disk_resolver.rb +++ b/lib/facter/resolvers/disk_resolver.rb @@ -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 @@ -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 diff --git a/spec/facter/resolvers/disk_resolver_spec.rb b/spec/facter/resolvers/disk_resolver_spec.rb index 1abe24c83e..c8984a6685 100644 --- a/spec/facter/resolvers/disk_resolver_spec.rb +++ b/spec/facter/resolvers/disk_resolver_spec.rb @@ -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']) @@ -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 @@ -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) @@ -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