summaryrefslogtreecommitdiffstats
path: root/spec/unit/util
diff options
context:
space:
mode:
authorRein Henrichs <reinh@reinh.com>2010-08-02 17:28:45 -0700
committerRein Henrichs <reinh@reinh.com>2010-08-03 14:59:29 -0700
commita2bcacdc54fc9e9446bd5b084e70d60aaaeeebd2 (patch)
tree45cb11ad615785f496cadb2b057a0a2c743a75e0 /spec/unit/util
parent1bd2ca29d8fd7d11e75096ceeeb704fe887cad31 (diff)
downloadfacter-a2bcacdc54fc9e9446bd5b084e70d60aaaeeebd2.tar.gz
facter-a2bcacdc54fc9e9446bd5b084e70d60aaaeeebd2.tar.xz
facter-a2bcacdc54fc9e9446bd5b084e70d60aaaeeebd2.zip
[#2330] Uptime should not make redundant system calls
Rewrite of uptime facts and supporting utility methods. Works on unix, BSD, windows. No longer makes redundant system calls. Uses Facter::Util::Uptime utility methods: * Implemented uptime_seconds_unix using /proc/uptime or who -b on unix, sysctl on BSD. Added unit tests for the behaviors of get_uptime_seconds_unix: read from proc/uptime, read uptime from "sysctl -b kern.boottime", read uptime from "who -b", and return nil if nothing else works. * Implemented uptime_seconds_win using the Win32 API. Facts implemented: * uptime_{seconds,hours,days} Returns the respective integer value. * uptime Returns human readable uptime statistic that preserves original behavior. Examples: 3 days 1 day 5:08 hours 0:35 hours
Diffstat (limited to 'spec/unit/util')
-rw-r--r--spec/unit/util/uptime.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/spec/unit/util/uptime.rb b/spec/unit/util/uptime.rb
new file mode 100644
index 0000000..9ba6665
--- /dev/null
+++ b/spec/unit/util/uptime.rb
@@ -0,0 +1,53 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'facter/util/uptime'
+
+describe Facter::Util::Uptime do
+
+ describe ".get_uptime_seconds_unix" do
+ context "when /proc/uptime is available" do
+ before do
+ uptime_file = File.join(SPECDIR, "fixtures", "uptime", "ubuntu_proc_uptime")
+ Facter::Util::Uptime.stubs(:uptime_file).returns(uptime_file)
+ end
+
+ it "should return the uptime in seconds as an integer" do
+ Facter::Util::Uptime.get_uptime_seconds_unix.should == 5097686
+ end
+
+ end
+
+ it "should use sysctl kern.boottime when /proc/uptime not available" do
+ nonexistent_file = '/non/existent/file'
+ File.exists?(nonexistent_file).should == false
+ Facter::Util::Uptime.stubs(:uptime_file).returns(nonexistent_file)
+ sysctl_output_file = File.join(SPECDIR, 'fixtures', 'uptime', 'sysctl_kern_boottime') # Aug 01 14:13:47 -0700 2010
+ Facter::Util::Uptime.stubs(:uptime_sysctl_cmd).returns("cat #{sysctl_output_file}")
+ Time.stubs(:now).returns Time.parse("Aug 01 15:13:47 -0700 2010") # one hour later
+ Facter::Util::Uptime.get_uptime_seconds_unix.should == 60 * 60
+ end
+
+ it "should use who -b when neither /proc/uptime nor sysctl kern.boottime is available" do
+ nonexistent_file = '/non/existent/file'
+ File.exists?(nonexistent_file).should == false
+ Facter::Util::Uptime.stubs(:uptime_file).returns(nonexistent_file)
+ Facter::Util::Uptime.stubs(:uptime_sysctl_cmd).returns("cat #{nonexistent_file}")
+ who_b_output_file = File.join(SPECDIR, 'fixtures', 'uptime', 'who_b_boottime') # Aug 1 14:13
+ Facter::Util::Uptime.stubs(:uptime_who_cmd).returns("cat #{who_b_output_file}")
+ Time.stubs(:now).returns Time.parse("Aug 01 15:13") # one hour later
+ Facter::Util::Uptime.get_uptime_seconds_unix.should == 60 * 60
+ end
+
+ it "should return nil when none of /proc/uptime, sysctl kern.boottime, or who -b is available" do
+ nonexistent_file = '/non/existent/file'
+ File.exists?(nonexistent_file).should == false
+ Facter::Util::Uptime.stubs(:uptime_file).returns(nonexistent_file)
+ Facter::Util::Uptime.stubs(:uptime_sysctl_cmd).returns("cat #{nonexistent_file}")
+ Facter::Util::Uptime.stubs(:uptime_who_cmd).returns("cat #{nonexistent_file}")
+ Facter::Util::Uptime.get_uptime_seconds_unix.should == nil
+ end
+ end
+
+end