diff options
-rw-r--r-- | spec/unit/util/uptime.rb | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/spec/unit/util/uptime.rb b/spec/unit/util/uptime.rb index 72c97ed..796b3c2 100644 --- a/spec/unit/util/uptime.rb +++ b/spec/unit/util/uptime.rb @@ -19,35 +19,42 @@ describe Facter::Util::Uptime do 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 + context "when /proc/uptime is not available" do + before :each do + @nonexistent_file = '/non/existent/file' + File.exists?(@nonexistent_file).should == false + Facter::Util::Uptime.stubs(:uptime_file).returns(@nonexistent_file) + 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 use 'sysctl kern.boottime'" do + 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 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 + context "nor is 'sysctl kern.boottime'" do + before :each do + Facter::Util::Uptime.stubs(:uptime_sysctl_cmd).returns("cat \"#{@nonexistent_file}\"") + end + + it "should use 'who -b'" do + 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 + + context "nor is 'who -b'" do + before :each do + Facter::Util::Uptime.stubs(:uptime_who_cmd).returns("cat \"#{@nonexistent_file}\"") + end + + it "should return nil" do + Facter::Util::Uptime.get_uptime_seconds_unix.should == nil + end + end + end end end - end |