Skip to content

Commit 8e78b5f

Browse files
Merge pull request #301 from rtib/GH-298
[GH-298] fix lvm_vg_* facts
2 parents 0f60b28 + 7d8052b commit 8e78b5f

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

lib/facter/lvm_support.rb

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,32 @@
1313

1414
# lvm_vgs: [0-9]+
1515
# Number of VGs
16+
vg_list = []
1617
Facter.add('lvm_vgs') do
1718
confine lvm_support: true
1819

19-
setcode do
20-
vgs = Facter::Core::Execution.execute('vgs -o name --noheadings 2>/dev/null', timeout: 30)
20+
vgs = Facter::Core::Execution.execute('vgs -o name --noheadings 2>/dev/null', timeout: 30)
2121

22-
if vgs.nil?
23-
0
24-
else
25-
vg_list = vgs.split
22+
if vgs.nil?
23+
setcode { 0 }
24+
else
25+
vg_list = vgs.split
26+
setcode { vg_list.length }
27+
end
28+
end
2629

27-
# lvm_vg_[0-9]+
28-
# VG name by index
29-
vg_list.each_with_index do |vg, i|
30-
Facter.add("lvm_vg_#{i}") { setcode { vg } }
31-
Facter.add("lvm_vg_#{vg}_pvs") do
32-
setcode do
33-
pvs = Facter::Core::Execution.execute("vgs -o pv_name #{vg} 2>/dev/null", timeout: 30)
34-
res = nil
35-
res = pvs.split("\n").grep(%r{^\s+/}).collect(&:strip).sort.join(',') unless pvs.nil?
36-
res
37-
end
38-
end
30+
# lvm_vg_[0-9]+
31+
# VG name by index
32+
vg_list.each_with_index do |vg, i|
33+
Facter.add("lvm_vg_#{i}") { setcode { vg } }
34+
Facter.add("lvm_vg_#{vg}_pvs") do
35+
setcode do
36+
pvs = Facter::Core::Execution.execute("vgs -o pv_name #{vg} 2>/dev/null", timeout: 30)
37+
res = nil
38+
unless pvs.nil?
39+
res = pvs.split("\n").select { |l| l =~ %r{^\s+/} }.map(&:strip).sort.join(',')
3940
end
40-
vg_list.length
41+
res
4142
end
4243
end
4344
end
@@ -48,20 +49,17 @@
4849
Facter.add('lvm_pvs') do
4950
confine lvm_support: true
5051

51-
setcode do
52-
pvs = Facter::Core::Execution.execute('pvs -o name --noheadings 2>/dev/null', timeout: 30)
53-
54-
if pvs.nil?
55-
0
56-
else
57-
pv_list = pvs.split
58-
59-
# lvm_pv_[0-9]+
60-
# PV name by index
61-
pv_list.each_with_index do |pv, i|
62-
Facter.add("lvm_pv_#{i}") { setcode { pv } }
63-
end
64-
pv_list.length
65-
end
52+
pvs = Facter::Core::Execution.execute('pvs -o name --noheadings 2>/dev/null', timeout: 30)
53+
if pvs.nil?
54+
setcode { 0 }
55+
else
56+
pv_list = pvs.split
57+
setcode { pv_list.length }
6658
end
6759
end
60+
61+
# lvm_pv_[0-9]+
62+
# PV name by index
63+
pv_list.each_with_index do |pv, i|
64+
Facter.add("lvm_pv_#{i}") { setcode { pv } }
65+
end

spec/unit/facter/lvm_support_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
context 'when there are vgs' do
6565
it 'lists vgs' do
6666
Facter::Core::Execution.stubs(:execute) # All other calls
67-
Facter::Core::Execution.expects(:execute).at_least(1).with('vgs -o name --noheadings 2>/dev/null', timeout: 30).returns("vg0\nvg1")
67+
Facter::Core::Execution.expects(:execute).at_least(1).with('vgs -o name --noheadings 2>/dev/null', timeout: 30).returns(" vg0\n vg1")
6868
Facter::Core::Execution.expects(:execute).at_least(1).with('vgs -o pv_name vg0 2>/dev/null', timeout: 30).returns(" PV\n /dev/pv3\n /dev/pv2")
6969
Facter::Core::Execution.expects(:execute).at_least(1).with('vgs -o pv_name vg1 2>/dev/null', timeout: 30).returns(" PV\n /dev/pv0")
7070
Facter.fact(:lvm_support).expects(:value).at_least(1).returns(true)
@@ -86,7 +86,6 @@
8686

8787
context 'when there is no lvm support' do
8888
it 'does not exist' do
89-
Facter.fact(:lvm_support).expects(:value).at_least(1).returns(nil)
9089
Facter.value(:lvm_pvs).should be_nil
9190
end
9291
end
@@ -104,7 +103,7 @@
104103
context 'when there are pvs' do
105104
it 'lists pvs' do
106105
Facter::Core::Execution.stubs('execute') # All other calls
107-
Facter::Core::Execution.expects('execute').at_least(1).with('pvs -o name --noheadings 2>/dev/null', timeout: 30).returns("pv0\npv1")
106+
Facter::Core::Execution.expects('execute').at_least(1).with('pvs -o name --noheadings 2>/dev/null', timeout: 30).returns(" pv0\n pv1")
108107
Facter.fact(:lvm_support).expects(:value).at_least(1).returns(true)
109108
Facter.value(:lvm_pvs).should == 2
110109
Facter.value(:lvm_pv_0).should == 'pv0'

0 commit comments

Comments
 (0)