diff options
author | Jacob Helwig <jacob@puppetlabs.com> | 2010-11-29 00:49:10 -0800 |
---|---|---|
committer | Jacob Helwig <jacob@puppetlabs.com> | 2011-01-24 14:50:05 -0800 |
commit | cbbfe55a7f0508c09d3b58fa8b8b95654c78bc06 (patch) | |
tree | ad0c374b20b1c5b6311a744ea5d15eaae8a43a31 | |
parent | 4ce611ffc27b7697882e85ad9a686b33c1cf6852 (diff) | |
download | facter-cbbfe55a7f0508c09d3b58fa8b8b95654c78bc06.tar.gz facter-cbbfe55a7f0508c09d3b58fa8b8b95654c78bc06.tar.xz facter-cbbfe55a7f0508c09d3b58fa8b8b95654c78bc06.zip |
Refactor util/uptime.rb tests to reduce duplication using contexts
Reviewed-by: Paul Berry <paul@puppetlabs.com>
-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 |