#! /usr/bin/env ruby require 'spec_helper' describe "Memory facts" do before(:each) do Facter.collection.internal_loader.load(:memory) end after(:each) do Facter.clear end describe "when returning scaled sizes" do [ "memorysize", "memoryfree", "swapsize", "swapfree" ].each do |fact| describe "when #{fact}_mb does not exist" do before(:each) do Facter.fact(fact + "_mb").stubs(:value).returns(nil) end it "#{fact} should not exist either" do Facter.fact(fact).value.should be_nil end end { "200.00" => "200.00 MB", "1536.00" => "1.50 GB", "1572864.00" => "1.50 TB", }.each do |mbval, scval| it "should scale #{fact} when given #{mbval} MB" do Facter.fact(fact + "_mb").stubs(:value).returns(mbval) Facter.fact(fact).value.should == scval end end end after(:each) do Facter.clear end end describe "on Darwin" do before(:each) do Facter.clear Facter.fact(:kernel).stubs(:value).returns("Darwin") Facter::Util::Resolution.stubs(:exec).with('sysctl -n hw.memsize').returns('8589934592') sample_vm_stat = </dev/null').returns(swapusage) svmon = </dev/null').returns sample_prtconf vmstat_lines = </dev/null').returns sample_swap_line Facter.collection.internal_loader.load(:memory) end it "should return the current memory size in MB" do Facter.fact(:memorysize_mb).value.should == "2048.00" end it "should return the current memory free in MB" do Facter.fact(:memoryfree_mb).value.should == "465.06" end it "should return the current swap free in MB" do Facter.fact(:swapfree_mb).value.should == "1023.99" end it "should return the current swap size in MB" do Facter.fact(:swapsize_mb).value.should == "1023.99" end end describe "when multiple swaps exist" do before(:each) do sample_swap_line = </dev/null').returns sample_swap_line Facter.collection.internal_loader.load(:memory) end it "should return the current memory size in MB" do Facter.fact(:memorysize_mb).value.should == "2048.00" end it "should return the current memory free in MB" do Facter.fact(:memoryfree_mb).value.should == "465.06" end it "should return the current swap free in MB" do Facter.fact(:swapfree_mb).value.should == "2047.98" end it "should return the current swap size in MB" do Facter.fact(:swapsize_mb).value.should == "2047.98" end end describe "when no swap exists" do before(:each) do Facter::Util::Resolution.stubs(:exec).with('/usr/sbin/swap -l 2>/dev/null').returns "" Facter.collection.internal_loader.load(:memory) end it "should return the current memory size in MB" do Facter.fact(:memorysize_mb).value.should == "2048.00" end it "should return the current memory free in MB" do Facter.fact(:memoryfree_mb).value.should == "465.06" end it "should return 0 for the swap free in MB" do Facter.fact(:swapfree_mb).value.should == "0.00" end it "should return 0 for the swap size in MB" do Facter.fact(:swapsize_mb).value.should == "0.00" end end end describe "on DragonFly BSD" do before :each do Facter.clear Facter.fact(:kernel).stubs(:value).returns("dragonfly") swapusage = "total: 148342k bytes allocated = 0k used, 148342k available" Facter::Util::Resolution.stubs(:exec).with('/sbin/sysctl -n hw.pagesize').returns("4096") Facter::Util::Resolution.stubs(:exec).with('/sbin/sysctl -n vm.swap_size').returns("128461") Facter::Util::Resolution.stubs(:exec).with('/sbin/sysctl -n vm.swap_anon_use').returns("2635") Facter::Util::Resolution.stubs(:exec).with('/sbin/sysctl -n vm.swap_cache_use').returns("0") vmstat = <