diff options
Diffstat (limited to 'spec/unit')
-rwxr-xr-x | spec/unit/facter.rb | 27 | ||||
-rw-r--r-- | spec/unit/util/macaddress.rb | 75 | ||||
-rw-r--r-- | spec/unit/util/virtual.rb | 21 | ||||
-rw-r--r-- | spec/unit/virtual.rb | 20 |
4 files changed, 136 insertions, 7 deletions
diff --git a/spec/unit/facter.rb b/spec/unit/facter.rb index 9f7b4cf..20f9ed1 100755 --- a/spec/unit/facter.rb +++ b/spec/unit/facter.rb @@ -135,6 +135,14 @@ describe Facter do Facter.should respond_to(:debugging?) end + it "should have a method to query timing mode" do + Facter.should respond_to(:timing?) + end + + it "should have a method to show timing information" do + Facter.should respond_to(:show_time) + end + it "should have a method to warn" do Facter.should respond_to(:warn) end @@ -204,6 +212,25 @@ describe Facter do end end + describe "when setting timing mode" do + it "should have timing enabled using 1" do + Facter.timing(1) + Facter.should be_timing + end + it "should have timing enabled using true" do + Facter.timing(true) + Facter.should be_timing + end + it "should have timing disabled using 0" do + Facter.timing(0) + Facter.should_not be_timing + end + it "should have timing disabled using false" do + Facter.timing(false) + Facter.should_not be_timing + end + end + describe "when registering directories to search" do after { Facter.instance_variable_set("@search_path", []) } diff --git a/spec/unit/util/macaddress.rb b/spec/unit/util/macaddress.rb new file mode 100644 index 0000000..8349b36 --- /dev/null +++ b/spec/unit/util/macaddress.rb @@ -0,0 +1,75 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'facter/util/macaddress' + +context "Darwin" do + test_cases = [ + # version, iface, real macaddress, fallback macaddress + ["9.8.0", 'en0', "00:17:f2:06:e4:2e", "00:17:f2:06:e4:2e"], + ["10.3.0", 'en0', "00:17:f2:06:e3:c2", "00:17:f2:06:e3:c2"], + ["10.6.4", 'en1', "58:b0:35:7f:25:b3", "58:b0:35:fa:08:b1"] + ] + + test_cases.each do |version, default_iface, macaddress, fallback_macaddress| + netstat_file = File.join(SPECDIR, "fixtures", "netstat", "darwin_#{version.tr('.', '_')}") + ifconfig_file_no_iface = File.join(SPECDIR, "fixtures", "ifconfig", "darwin_#{version.tr('.', '_')}") + ifconfig_file = "#{ifconfig_file_no_iface}_#{default_iface}" + + context "version #{version}" do + + describe Facter::Util::Macaddress::Darwin do + + describe ".default_interface" do + context "when netstat has a default interface" do + + before do + Facter::Util::Macaddress::Darwin.stubs(:netstat_command).returns("cat #{netstat_file}") + end + + it "should return the default interface name" do + Facter::Util::Macaddress::Darwin.default_interface.should == default_iface + end + end + + end + + describe ".macaddress" do + context "when netstat has a default interface" do + before do + Facter.stubs(:warn) + Facter::Util::Macaddress::Darwin.stubs(:default_interface).returns('') + Facter::Util::Macaddress::Darwin.stubs(:ifconfig_command).returns("cat #{ifconfig_file}") + end + + it "should return the macaddress of the default interface" do + Facter::Util::Macaddress::Darwin.macaddress.should == macaddress + end + + end + + context "when netstat does not have a default interface" do + before do + Facter::Util::Macaddress::Darwin.stubs(:default_interface).returns("") + Facter::Util::Macaddress::Darwin.stubs(:ifconfig_command).returns("cat #{ifconfig_file_no_iface}") + end + + it "should warn about the lack of default" do + Facter.expects(:warn).with("Could not find a default route. Using first non-loopback interface") + Facter::Util::Macaddress::Darwin.stubs(:default_interface).returns('') + Facter::Util::Macaddress::Darwin.macaddress + end + + it "should return the macaddress of the first non-loopback interface" do + Facter::Util::Macaddress::Darwin.macaddress.should == fallback_macaddress + end + + end + end + + end + + end + end +end diff --git a/spec/unit/util/virtual.rb b/spec/unit/util/virtual.rb index 57ef440..72186f7 100644 --- a/spec/unit/util/virtual.rb +++ b/spec/unit/util/virtual.rb @@ -57,6 +57,26 @@ describe Facter::Util::Virtual do Facter::Util::Virtual.should_not be_vserver end + fixture_path = File.join(SPECDIR, 'fixtures', 'virtual', 'proc_self_status') + + test_cases = [ + [File.join(fixture_path, 'vserver_2_1', 'guest'), true, 'vserver 2.1 guest'], + [File.join(fixture_path, 'vserver_2_1', 'host'), true, 'vserver 2.1 host'], + [File.join(fixture_path, 'vserver_2_3', 'guest'), true, 'vserver 2.3 guest'], + [File.join(fixture_path, 'vserver_2_3', 'host'), true, 'vserver 2.3 host'] + ] + + test_cases.each do |status_file, expected, description| + context "with /proc/self/status from #{description}" do + it "should detect vserver as #{expected.inspect}" do + status = File.read(status_file) + FileTest.stubs(:exists?).with("/proc/self/status").returns(true) + File.stubs(:read).with("/proc/self/status").returns(status) + Facter::Util::Virtual.vserver?.should == expected + end + end + end + it "should identify vserver_host when /proc/virtual exists" do Facter::Util::Virtual.expects(:vserver?).returns(true) FileTest.stubs(:exists?).with("/proc/virtual").returns(true) @@ -101,6 +121,7 @@ describe Facter::Util::Virtual do end it "should detect kvm on FreeBSD" do + FileTest.stubs(:exists?).with("/proc/cpuinfo").returns(false) Facter.fact(:kernel).stubs(:value).returns("FreeBSD") Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n hw.model").returns("QEMU Virtual CPU version 0.12.4") Facter::Util::Virtual.should be_kvm diff --git a/spec/unit/virtual.rb b/spec/unit/virtual.rb index ee673be..a7767c9 100644 --- a/spec/unit/virtual.rb +++ b/spec/unit/virtual.rb @@ -40,43 +40,49 @@ describe "is_virtual fact" do it "should be virtual when running on xen" do Facter.fact(:kernel).stubs(:value).returns("Linux") Facter.fact(:virtual).stubs(:value).returns("xenu") - Facter.fact(:is_virtual).value.should == true + Facter.fact(:is_virtual).value.should == "true" end it "should be false when running on xen0" do Facter.fact(:kernel).stubs(:value).returns("Linux") Facter.fact(:virtual).stubs(:value).returns("xen0") - Facter.fact(:is_virtual).value.should == false + Facter.fact(:is_virtual).value.should == "false" end it "should be true when running on vmware" do Facter.fact(:kernel).stubs(:value).returns("Linux") Facter.fact(:virtual).stubs(:value).returns("vmware") - Facter.fact(:is_virtual).value.should == true + Facter.fact(:is_virtual).value.should == "true" end it "should be true when running on openvz" do Facter.fact(:kernel).stubs(:value).returns("Linux") Facter.fact(:virtual).stubs(:value).returns("openvzve") - Facter.fact(:is_virtual).value.should == true + Facter.fact(:is_virtual).value.should == "true" end it "should be true when running on kvm" do Facter.fact(:kernel).stubs(:value).returns("Linux") Facter.fact(:virtual).stubs(:value).returns("kvm") - Facter.fact(:is_virtual).value.should == true + Facter.fact(:is_virtual).value.should == "true" end it "should be true when running in jail" do Facter.fact(:kernel).stubs(:value).returns("FreeBSD") Facter.fact(:virtual).stubs(:value).returns("jail") - Facter.fact(:is_virtual).value.should == true + Facter.fact(:is_virtual).value.should == "true" + end + + it "should be true when running in zone" do + Facter.fact(:kernel).stubs(:value).returns("SunOS") + Facter.fact(:virtual).stubs(:value).returns("zone") + Facter.fact(:is_virtual).value.should == "true" end it "should be true when running on hp-vm" do Facter.fact(:kernel).stubs(:value).returns("HP-UX") Facter.fact(:virtual).stubs(:value).returns("hpvm") - Facter.fact(:is_virtual).value.should == true + Facter.fact(:is_virtual).value.should == "true" end end |