Permalink
Browse files

(#14827) Fix broken swap units on Mac

Prior to this commit, the swap values for Mac OS X were based solely on
the text returned from 'sysctl swapusage', which was something like
'6144.00M'. This resulting string was passed directly on to Facter
without scaling or transformation.  This commit makes the swap facts on
OS X consistent with the swap facts on every other OS in that the facts
will be dynamically scaled and in the same format: '6.14 GB'.
  • Loading branch information...
1 parent 8f67b9f commit 611005baca5f22cafe6c115b59796561ff7c25fd @jeffweiss jeffweiss committed Jun 5, 2012
Showing with 54 additions and 60 deletions.
  1. +12 −32 lib/facter/memory.rb
  2. +42 −28 spec/unit/memory_spec.rb
View
44 lib/facter/memory.rb
@@ -39,36 +39,6 @@
end
end
-Facter.add("SwapSize") do
- confine :kernel => :Darwin
- setcode do
- swap = Facter::Util::Resolution.exec('sysctl vm.swapusage')
- swaptotal = 0
- if swap =~ /total = (\S+)/ then swaptotal = $1; end
- swaptotal
- end
-end
-
-Facter.add("SwapFree") do
- confine :kernel => :Darwin
- setcode do
- swap = Facter::Util::Resolution.exec('sysctl vm.swapusage')
- swapfree = 0
- if swap =~ /free = (\S+)/ then swapfree = $1; end
- swapfree
- end
-end
-
-Facter.add("SwapEncrypted") do
- confine :kernel => :Darwin
- setcode do
- swap = Facter::Util::Resolution.exec('sysctl vm.swapusage')
- encrypted = false
- if swap =~ /\(encrypted\)/ then encrypted = true; end
- encrypted
- end
-end
-
if Facter.value(:kernel) == "AIX" and Facter.value(:id) == "root"
swap = Facter::Util::Resolution.exec('swap -l')
swapfree, swaptotal = 0, 0
@@ -176,7 +146,7 @@
unless swap.empty?
# Parse the line:
# vm.swapusage: total = 128.00M used = 0.37M free = 127.63M (encrypted)
- if swap =~ /total\s=\s(\S+)\s+used\s=\s(\S+)\s+free\s=\s(\S+)\s/
+ if swap =~ /total\s=\s(\S+)M\s+used\s=\s(\S+)M\s+free\s=\s(\S+)M\s/
swaptotal += $1.to_i
swapfree += $3.to_i
end
@@ -200,11 +170,21 @@
Facter.add("memorysize") do
confine :kernel => :Darwin
- memtotal = Facter::Util::Resolution.exec("sysctl hw.memsize | cut -d':' -f2")
+ memtotal = Facter::Util::Resolution.exec("sysctl -n hw.memsize")
setcode do
Facter::Memory.scale_number(memtotal.to_f,"")
end
end
+
+ Facter.add("SwapEncrypted") do
+ confine :kernel => :Darwin
+ setcode do
+ swap = Facter::Util::Resolution.exec('sysctl vm.swapusage')
+ encrypted = false
+ if swap =~ /\(encrypted\)/ then encrypted = true; end
+ encrypted
+ end
+ end
end
if Facter.value(:kernel) == "SunOS"
View
70 spec/unit/memory_spec.rb
@@ -3,44 +3,58 @@
require 'spec_helper'
describe "Memory facts" do
- before do
- # We need these facts loaded, but they belong to a file with a
- # different name, so load the file explicitly.
- Facter.collection.loader.load(:memory)
- end
-
after do
Facter.clear
end
- it "should return the current swap size" do
-
- Facter.fact(:kernel).stubs(:value).returns("Darwin")
- Facter::Util::Resolution.stubs(:exec).with('sysctl vm.swapusage').returns("vm.swapusage: total = 64.00M used = 0.00M free = 64.00M (encrypted)")
- swapusage = "vm.swapusage: total = 64.00M used = 0.00M free = 64.00M (encrypted)"
+ describe "on Darwin" do
+ before(:each) do
+ Facter.fact(:kernel).stubs(:value).returns("Darwin")
+ Facter::Util::Resolution.stubs(:exec).with('sysctl -n hw.memsize').returns('8589934592')
+ sample_vm_stat = <<VMSTAT
+Mach Virtual Memory Statistics: (page size of 4096 bytes)
+Pages free: 28430.
+Pages active: 1152576.
+Pages inactive: 489054.
+Pages speculative: 7076.
+Pages wired down: 418217.
+"Translation faults": 1340091228.
+Pages copy-on-write: 16851357.
+Pages zero filled: 665168768.
+Pages reactivated: 3082708.
+Pageins: 13862917.
+Pageouts: 1384383.
+Object cache: 14 hits of 2619925 lookups (0% hit rate)
+VMSTAT
+ Facter::Util::Resolution.stubs(:exec).with('vm_stat').returns(sample_vm_stat)
+ Facter::Util::Resolution.stubs(:exec).with('sysctl vm.swapusage').returns("vm.swapusage: total = 64.00M used = 1.00M free = 63.00M (encrypted)")
- if swapusage =~ /total = (\S+).*/
- Facter.fact(:swapfree).value.should == $1
+ Facter.collection.loader.load(:memory)
end
- end
-
- it "should return the current swap free" do
- Facter.fact(:kernel).stubs(:value).returns("Darwin")
- Facter::Util::Resolution.stubs(:exec).with('sysctl vm.swapusage').returns("vm.swapusage: total = 64.00M used = 0.00M free = 64.00M (encrypted)")
- swapusage = "vm.swapusage: total = 64.00M used = 0.00M free = 64.00M (encrypted)"
- if swapusage =~ /free = (\S+).*/
- Facter.fact(:swapfree).value.should == $1
+ it "should return the current swap size" do
+ Facter.fact(:swapsize).value.should == "64.00 MB"
end
- end
- it "should return whether swap is encrypted" do
- Facter.fact(:kernel).stubs(:value).returns("Darwin")
- Facter::Util::Resolution.stubs(:exec).with('sysctl vm.swapusage').returns("vm.swapusage: total = 64.00M used = 0.00M free = 64.00M (encrypted)")
- swapusage = "vm.swapusage: total = 64.00M used = 0.00M free = 64.00M (encrypted)"
+ it "should return the current swap free" do
+ Facter.fact(:swapfree).value.should == "63.00 MB"
+ end
- swapusage =~ /\(encrypted\)/
- Facter.fact(:swapencrypted).value.should == true
+ it "should return whether swap is encrypted" do
+ Facter.fact(:swapencrypted).value.should == true
+ end
+
+ it "should return the memorysize" do
+ Facter.fact(:memorysize).value.should == "8.00 GB"
+ end
+
+ it "should return the memoryfree" do
+ Facter.fact(:memoryfree).value.should == "138.70 MB"
+ end
+
+ after(:each) do
+ Facter.clear
+ end
end
describe "on OpenBSD" do

0 comments on commit 611005b

Please sign in to comment.