summaryrefslogtreecommitdiffstats
path: root/spec
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
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')
-rw-r--r--spec/fixtures/uptime/sysctl_kern_boottimebin0 -> 16 bytes
-rw-r--r--spec/fixtures/uptime/ubuntu_proc_uptime1
-rw-r--r--spec/fixtures/uptime/who_b_boottime1
-rw-r--r--spec/spec_helper.rb2
-rw-r--r--spec/unit/uptime.rb112
-rw-r--r--spec/unit/util/uptime.rb53
6 files changed, 169 insertions, 0 deletions
diff --git a/spec/fixtures/uptime/sysctl_kern_boottime b/spec/fixtures/uptime/sysctl_kern_boottime
new file mode 100644
index 0000000..0c54fe4
--- /dev/null
+++ b/spec/fixtures/uptime/sysctl_kern_boottime
Binary files differ
diff --git a/spec/fixtures/uptime/ubuntu_proc_uptime b/spec/fixtures/uptime/ubuntu_proc_uptime
new file mode 100644
index 0000000..7e8ed74
--- /dev/null
+++ b/spec/fixtures/uptime/ubuntu_proc_uptime
@@ -0,0 +1 @@
+5097686.63 40756306.43
diff --git a/spec/fixtures/uptime/who_b_boottime b/spec/fixtures/uptime/who_b_boottime
new file mode 100644
index 0000000..9b29dcd
--- /dev/null
+++ b/spec/fixtures/uptime/who_b_boottime
@@ -0,0 +1 @@
+reboot ~ Aug 1 14:13
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 34992d7..c8bd547 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,5 +1,7 @@
dir = File.expand_path(File.dirname(__FILE__))
+SPECDIR = dir
+
$LOAD_PATH.unshift("#{dir}/")
$LOAD_PATH.unshift("#{dir}/../lib")
diff --git a/spec/unit/uptime.rb b/spec/unit/uptime.rb
new file mode 100644
index 0000000..19a55fe
--- /dev/null
+++ b/spec/unit/uptime.rb
@@ -0,0 +1,112 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../spec_helper'
+
+require 'facter'
+require 'facter/util/uptime'
+
+describe "uptime facts:" do
+ before { Facter.clear }
+ after { Facter.clear }
+
+ context "when uptime information is available" do
+ describe "uptime" do
+ test_cases = [
+ [60 * 60 * 24 * 3, '3 days'],
+ [60 * 60 * 24 * 3 + 25, '3 days'],
+ [60 * 60 * 24 * 1, '1 day'],
+ [60 * 60 * 24 * 1 + 25, '1 day'],
+ [60 * (60 * 3 + 45), '3:45 hours'],
+ [60 * (60 * 3 + 4), '3:04 hours'],
+ [60 * 60, '1:00 hours'],
+ [60 * 35, '0:35 hours']
+ ]
+
+ test_cases.each do |seconds, expected|
+ it "should return #{expected.inspect} for #{seconds} seconds" do
+ Facter::Util::Uptime.stubs(:get_uptime_seconds_unix).returns(seconds)
+ Facter::Util::Uptime.stubs(:get_uptime_seconds_win).returns(seconds)
+
+ Facter.fact(:uptime).value.should == expected
+ end
+ end
+ end
+
+ end
+
+ context "when uptime information is available" do
+ before do
+ Facter::Util::Uptime.stubs(:get_uptime_seconds_unix).returns(60 * 60 * 24 + 23)
+ Facter::Util::Uptime.stubs(:get_uptime_seconds_win).returns(60 * 60 * 24 + 23)
+ end
+
+ describe "uptime_seconds" do
+ it "should return the uptime in seconds" do
+ Facter.fact(:uptime_seconds).value.should == 60 * 60 * 24 + 23
+ end
+ end
+
+ describe "uptime_hours" do
+ it "should return the uptime in hours" do
+ Facter.fact(:uptime_hours).value.should == 24
+ end
+ end
+
+ describe "uptime_days" do
+ it "should return the uptime in days" do
+ Facter.fact(:uptime_days).value.should == 1
+ end
+ end
+ end
+
+ context "when uptime information is not available" do
+ before do
+ Facter::Util::Uptime.stubs(:get_uptime_seconds_unix).returns(nil)
+ Facter::Util::Uptime.stubs(:get_uptime_seconds_win).returns(nil)
+ $stderr, @old = StringIO.new, $stderr
+ end
+
+ after do
+ $stderr = @old
+ end
+
+ describe "uptime" do
+ it "should return 'unknown'" do
+ Facter.fact(:uptime).value.should == "unknown"
+ end
+ end
+
+ describe "uptime_seconds" do
+ it "should return nil" do
+ Facter.fact(:uptime_seconds).value.should == nil
+ end
+
+ it "should not print a warn message to stderr" do
+ Facter.fact(:uptime_seconds).value
+ $stderr.string.should == ""
+ end
+ end
+
+ describe "uptime_hours" do
+ it "should return nil" do
+ Facter.fact(:uptime_hours).value.should == nil
+ end
+
+ it "should not print a warn message to stderr" do
+ Facter.fact(:uptime_hours).value
+ $stderr.string.should == ""
+ end
+ end
+
+ describe "uptime_days" do
+ it "should return nil" do
+ Facter.fact(:uptime_days).value.should == nil
+ end
+
+ it "should not print a warn message to stderr" do
+ Facter.fact(:uptime_days).value
+ $stderr.string.should == ""
+ end
+ end
+ end
+end
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