Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Adding a fact to show you swap file sizes #47

Merged
merged 1 commit into from
Apr 22, 2016
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
30 changes: 30 additions & 0 deletions lib/facter/swap_file_size.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
if File.exists?('/proc/swaps')
swap_file_hash = {}

swap_file_output = Facter::Util::Resolution.exec('cat /proc/swaps')

# Sample Output
# Filename Type Size Used Priority
# /mnt/swap.1 file 1019900 0 -1
# /tmp/swapfile.fallocate file 1019900 0 -2
swap_file_output_array = swap_file_output.split("\n")

# Remove the header line
swap_file_output_array.shift

swap_file_output_array.each do |line|

swap_file_line_array = line.gsub(/\s+/m, ' ').strip.split(" ")

swap_file_hash[swap_file_line_array[0]] = swap_file_line_array[2]

end

Facter.add('swap_file_sizes') do
confine :kernel => 'Linux'
setcode do
swap_file_hash
end
end

end
33 changes: 33 additions & 0 deletions spec/unit/facter/swapfiles_fact_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "spec_helper"

describe Facter::Util::Fact do
before {
Facter.clear
}

describe 'swap_files' do
context 'returns swap_files when present' do
before do
Facter.fact(:kernel).stubs(:value).returns("Linux")
File.stubs(:exists?)
File.expects(:exists?).with('/proc/swaps').returns(true)
Facter::Util::Resolution.stubs(:exec)
end
it do
proc_swap_output = <<-EOS
Filename Type Size Used Priority
/mnt/swap.1 file 1019900 0 -1
/tmp/swapfile.fallocate file 1019900 0 -2
EOS
Facter::Util::Resolution.expects(:exec).with('cat /proc/swaps').returns(proc_swap_output)
expect(Facter.value(:swap_file_sizes)).to eq(
{
"/mnt/swap.1"=>"1019900",
"/tmp/swapfile.fallocate"=>"1019900"
}
)
end
end

end
end