Skip to content

Commit

Permalink
Add a fact exposing the sizes of physical disks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic Cope committed Jan 31, 2015
1 parent e3de06e commit 6354852
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/facter/megaraid_physical_drives_size.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Facter.add(:megaraid_physical_drives_size) do
confine :kernel => 'Linux'

setcode do
megacli = Facter.value(:megacli)
megaraid_adapters = Facter.value(:megaraid_adapters)

if megacli.nil? ||
megaraid_adapters.nil? || (megaraid_adapters == 0)
next nil
end

# XXX there is no support for handling more than one adapter
sizes = []
list = Facter::Util::Resolution.exec("#{megacli} -PDList -aALL -NoLog")
next if list.nil?
list.each_line do |line|
if line =~ /Raw Size: ([\.\d]+ [A-Z]?B)/
sizes.push($1)
end
end

sizes.join(',')
end
end
78 changes: 78 additions & 0 deletions spec/unit/facts/megaraid_physical_drives_size_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'spec_helper'

describe 'megaraid_physical_drives_size', :type => :fact do
before(:each) { Facter.clear }

describe 'on linux' do
context 'megacli not in path' do
it do
Facter.fact(:kernel).stubs(:value).returns('Linux')
Facter.fact(:megacli).stubs(:value).returns(nil)

Facter.fact(:megaraid_physical_drives_size).value.should be_nil
end
end

context 'megacli is broken' do
it do
Facter.fact(:kernel).stubs(:value).returns('Linux')
Facter.fact(:megacli).stubs(:value).returns('/usr/bin/MegaCli')
Facter.fact(:megaraid_adapters).stubs(:value).returns(nil)

Facter.fact(:megaraid_physical_drives_size).value.should be_nil
end

it do
Facter.fact(:kernel).stubs(:value).returns('Linux')
Facter.fact(:megacli).stubs(:value).returns('/usr/bin/MegaCli')
Facter.fact(:megaraid_adapters).stubs(:value).returns('1')
Facter::Util::Resolution.stubs(:exec).with('/usr/bin/MegaCli -PDList -aALL -NoLog').
returns(nil)

Facter.fact(:megaraid_physical_drives_size).value.should be_nil
end
end

context 'no adapters' do
it do
Facter.fact(:kernel).stubs(:value).returns('Linux')
Facter.fact(:megacli).stubs(:value).returns('/usr/bin/MegaCli')
Facter.fact(:megaraid_adapters).stubs(:value).returns('0')

Facter.fact(:megaraid_physical_drives_size).value.should be_nil
end
end

context '1 adapter' do
it do
Facter.fact(:kernel).stubs(:value).returns('Linux')
Facter.fact(:megacli).stubs(:value).returns('/usr/bin/MegaCli')
Facter.fact(:megaraid_adapters).stubs(:value).returns('1')
Facter::Util::Resolution.stubs(:exec).with('/usr/bin/MegaCli -PDList -aALL -NoLog').
returns(File.read(fixtures('megacli', 'pdlistaall')))

sizes = '3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,' +
'3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,' +
'3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,' +
'3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,' +
'3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,' +
'3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,' +
'3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,186.310 GB,' +
'186.310 GB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,' +
'3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,' +
'3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,' +
'3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB,3.638 TB'
Facter.fact(:megaraid_physical_drives_size).value.should == sizes
end
end
end # on linux

context 'not on linux' do
it do
Facter.fact(:kernel).stubs(:value).returns('Solaris')

Facter.fact(:megaraid_physical_drives_size).value.should be_nil
end
end # not on linux
end

0 comments on commit 6354852

Please sign in to comment.