From a4fe4598b681b75f5dc66e8193cefbf069cac728 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Wed, 26 Jan 2011 22:48:55 -0800 Subject: Refactor #6044 -- port testing to rspec2 We have moved to rspec2 for puppet, and facter should follow suit. --- Rakefile | 14 ++++++++------ spec/Rakefile | 18 ------------------ spec/spec_helper.rb | 4 ++-- spec/unit/operatingsystemrelease.rb | 2 +- spec/unit/uptime.rb | 6 +++--- spec/unit/util/macaddress.rb | 10 +++++----- spec/unit/util/uptime.rb | 10 +++++----- spec/unit/util/virtual.rb | 2 +- spec/unit/util/xendomains.rb | 2 +- 9 files changed, 26 insertions(+), 42 deletions(-) delete mode 100644 spec/Rakefile mode change 100644 => 100755 spec/unit/operatingsystemrelease.rb mode change 100644 => 100755 spec/unit/uptime.rb mode change 100644 => 100755 spec/unit/util/macaddress.rb mode change 100644 => 100755 spec/unit/util/uptime.rb mode change 100644 => 100755 spec/unit/util/xendomains.rb diff --git a/Rakefile b/Rakefile index ba939be..9b79613 100644 --- a/Rakefile +++ b/Rakefile @@ -4,8 +4,8 @@ $: << File.expand_path('lib') $LOAD_PATH << File.join(File.dirname(__FILE__), 'tasks') require 'rubygems' -require 'spec' -require 'spec/rake/spectask' +require 'rspec' +require 'rspec/core/rake_task' begin require 'rcov' rescue LoadError @@ -62,12 +62,14 @@ task :default do sh %{rake -T} end -Spec::Rake::SpecTask.new(:spec) do |t| - t.spec_files = FileList['spec/**/*.rb'] +RSpec::Core::RakeTask.new do |t| + t.pattern ='spec/{unit,integration}/**/*.rb' + t.fail_on_error = false end -Spec::Rake::SpecTask.new('spec:rcov') do |t| - t.spec_files = FileList['spec/**/*.rb'] +RSpec::Core::RakeTask.new('spec:rcov') do |t| + t.pattern ='spec/{unit,integration}/**/*.rb' + t.fail_on_error = false if defined?(Rcov) t.rcov = true t.rcov_opts = ['--exclude', 'spec/*,test/*,results/*,/usr/lib/*,/usr/local/lib/*,gems/*'] diff --git a/spec/Rakefile b/spec/Rakefile deleted file mode 100644 index e2996f6..0000000 --- a/spec/Rakefile +++ /dev/null @@ -1,18 +0,0 @@ -require File.join(File.dirname(__FILE__), "spec_helper.rb") -require 'rake' -require 'spec/rake/spectask' - -basedir = File.dirname(__FILE__) -puppetlibdir = File.join(basedir, "../lib") -puppettestlibdir = File.join(basedir, "../test/lib") -speclibdir = File.join(basedir, "lib") - -libs = [puppetlibdir, puppettestlibdir, speclibdir] -desc "Run all specs" -Spec::Rake::SpecTask.new('all') do |t| - t.spec_files = FileList['integration/**/*.rb', 'unit/**/*.rb'] - t.libs = libs - t.spec_opts = ['--options', 'spec.opts'] -end - -task :default => [:all] diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c8bd547..d9db445 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,12 +7,12 @@ $LOAD_PATH.unshift("#{dir}/../lib") require 'rubygems' require 'mocha' -require 'spec' +require 'rspec' require 'facter' # load any monkey-patches Dir["#{dir}/monkey_patches/*.rb"].map { |file| require file } -Spec::Runner.configure do |config| +RSpec.configure do |config| config.mock_with :mocha end diff --git a/spec/unit/operatingsystemrelease.rb b/spec/unit/operatingsystemrelease.rb old mode 100644 new mode 100755 index 31d4ae8..5c821c3 --- a/spec/unit/operatingsystemrelease.rb +++ b/spec/unit/operatingsystemrelease.rb @@ -26,7 +26,7 @@ describe "Operating System Release fact" do } test_cases.each do |system, file| - context "with operatingsystem reported as #{system.inspect}" do + describe "with operatingsystem reported as #{system.inspect}" do it "should read the #{file.inspect} file" do Facter.fact(:operatingsystem).stubs(:value).returns(system) diff --git a/spec/unit/uptime.rb b/spec/unit/uptime.rb old mode 100644 new mode 100755 index 19a55fe..fc592e3 --- a/spec/unit/uptime.rb +++ b/spec/unit/uptime.rb @@ -9,7 +9,7 @@ describe "uptime facts:" do before { Facter.clear } after { Facter.clear } - context "when uptime information is available" do + describe "when uptime information is available" do describe "uptime" do test_cases = [ [60 * 60 * 24 * 3, '3 days'], @@ -34,7 +34,7 @@ describe "uptime facts:" do end - context "when uptime information is available" do + describe "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) @@ -59,7 +59,7 @@ describe "uptime facts:" do end end - context "when uptime information is not available" do + describe "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) diff --git a/spec/unit/util/macaddress.rb b/spec/unit/util/macaddress.rb old mode 100644 new mode 100755 index 1ccca18..09794ec --- a/spec/unit/util/macaddress.rb +++ b/spec/unit/util/macaddress.rb @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'facter/util/macaddress' -context "Darwin" do +describe "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"], @@ -17,12 +17,12 @@ context "Darwin" do 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 "version #{version}" do describe Facter::Util::Macaddress::Darwin do describe ".default_interface" do - context "when netstat has a default interface" do + describe "when netstat has a default interface" do before do Facter::Util::Macaddress::Darwin.stubs(:netstat_command).returns("cat \"#{netstat_file}\"") @@ -36,7 +36,7 @@ context "Darwin" do end describe ".macaddress" do - context "when netstat has a default interface" do + describe "when netstat has a default interface" do before do Facter.stubs(:warn) Facter::Util::Macaddress::Darwin.stubs(:default_interface).returns('') @@ -49,7 +49,7 @@ context "Darwin" do end - context "when netstat does not have a default interface" do + describe "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}\"") diff --git a/spec/unit/util/uptime.rb b/spec/unit/util/uptime.rb old mode 100644 new mode 100755 index b7e3089..c104856 --- a/spec/unit/util/uptime.rb +++ b/spec/unit/util/uptime.rb @@ -7,7 +7,7 @@ require 'facter/util/uptime' describe Facter::Util::Uptime do describe ".get_uptime_seconds_unix" do - context "when /proc/uptime is available" do + describe "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}\"") @@ -19,7 +19,7 @@ describe Facter::Util::Uptime do end - context "when /proc/uptime is not available" do + describe "when /proc/uptime is not available" do before :each do @nonexistent_file = '/non/existent/file' File.exists?(@nonexistent_file).should == false @@ -33,7 +33,7 @@ describe Facter::Util::Uptime do Facter::Util::Uptime.get_uptime_seconds_unix.should == 60 * 60 end - context "nor is 'sysctl kern.boottime'" do + describe "nor is 'sysctl kern.boottime'" do before :each do Facter::Util::Uptime.stubs(:uptime_sysctl_cmd).returns("cat \"#{@nonexistent_file}\"") end @@ -45,7 +45,7 @@ describe Facter::Util::Uptime do Facter::Util::Uptime.get_uptime_seconds_unix.should == 60 * 60 end - context "nor is 'kstat -p unix:::boot_time'" do + describe "nor is 'kstat -p unix:::boot_time'" do before :each do Facter::Util::Uptime.stubs(:uptime_kstat_cmd).returns("cat \"#{@nonexistent_file}\"") end @@ -57,7 +57,7 @@ describe Facter::Util::Uptime do Facter::Util::Uptime.get_uptime_seconds_unix.should == 60 * 60 end - context "nor is 'who -b'" do + describe "nor is 'who -b'" do before :each do Facter::Util::Uptime.stubs(:uptime_who_cmd).returns("cat \"#{@nonexistent_file}\"") end diff --git a/spec/unit/util/virtual.rb b/spec/unit/util/virtual.rb index 72186f7..66dc98c 100644 --- a/spec/unit/util/virtual.rb +++ b/spec/unit/util/virtual.rb @@ -67,7 +67,7 @@ describe Facter::Util::Virtual do ] test_cases.each do |status_file, expected, description| - context "with /proc/self/status from #{description}" do + describe "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) diff --git a/spec/unit/util/xendomains.rb b/spec/unit/util/xendomains.rb old mode 100644 new mode 100755 index a0fa345..bd9c5d5 --- a/spec/unit/util/xendomains.rb +++ b/spec/unit/util/xendomains.rb @@ -13,7 +13,7 @@ describe Facter::Util::Xendomains do Facter::Util::Xendomains.get_domains.should == %{web01,mailserver} end - context "when xm list isn't executable" do + describe "when xm list isn't executable" do it "should be nil" do Facter::Util::Resolution.stubs(:exec).with('/usr/sbin/xm list').returns(nil) Facter::Util::Xendomains.get_domains.should == nil -- cgit From b39f8923e9772c2e8c99ca9351114d63d2858cc9 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Thu, 27 Jan 2011 17:14:23 -0800 Subject: Refactor #6044 -- require spec_helper with a consistent path. Because we pull in spec_helper in individual tests, we need to use a consistent path or Ruby will evaluate it multiple times. Make the path consistent by expanding it before require. --- spec/integration/facter.rb | 2 +- spec/unit/facter.rb | 2 +- spec/unit/interfaces.rb | 2 +- spec/unit/operatingsystem.rb | 2 +- spec/unit/operatingsystemrelease.rb | 2 +- spec/unit/selinux.rb | 2 +- spec/unit/uptime.rb | 2 +- spec/unit/util/collection.rb | 2 +- spec/unit/util/confine.rb | 2 +- spec/unit/util/fact.rb | 2 +- spec/unit/util/ip.rb | 2 +- spec/unit/util/loader.rb | 2 +- spec/unit/util/macaddress.rb | 2 +- spec/unit/util/macosx.rb | 2 +- spec/unit/util/manufacturer.rb | 2 +- spec/unit/util/resolution.rb | 2 +- spec/unit/util/uptime.rb | 2 +- spec/unit/util/virtual.rb | 2 +- spec/unit/util/vlans.rb | 2 +- spec/unit/util/xendomains.rb | 2 +- spec/unit/virtual.rb | 2 +- 21 files changed, 21 insertions(+), 21 deletions(-) mode change 100644 => 100755 spec/unit/interfaces.rb mode change 100644 => 100755 spec/unit/operatingsystem.rb mode change 100644 => 100755 spec/unit/selinux.rb mode change 100644 => 100755 spec/unit/util/vlans.rb diff --git a/spec/integration/facter.rb b/spec/integration/facter.rb index 79a1f0f..8351de1 100755 --- a/spec/integration/facter.rb +++ b/spec/integration/facter.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe Facter do before do diff --git a/spec/unit/facter.rb b/spec/unit/facter.rb index 20f9ed1..e63bc76 100755 --- a/spec/unit/facter.rb +++ b/spec/unit/facter.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe Facter do diff --git a/spec/unit/interfaces.rb b/spec/unit/interfaces.rb old mode 100644 new mode 100755 index 49d5d1f..8b295d6 --- a/spec/unit/interfaces.rb +++ b/spec/unit/interfaces.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'facter' diff --git a/spec/unit/operatingsystem.rb b/spec/unit/operatingsystem.rb old mode 100644 new mode 100755 index de86230..be83916 --- a/spec/unit/operatingsystem.rb +++ b/spec/unit/operatingsystem.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'facter' diff --git a/spec/unit/operatingsystemrelease.rb b/spec/unit/operatingsystemrelease.rb index 5c821c3..1cfb4ac 100755 --- a/spec/unit/operatingsystemrelease.rb +++ b/spec/unit/operatingsystemrelease.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'facter' diff --git a/spec/unit/selinux.rb b/spec/unit/selinux.rb old mode 100644 new mode 100755 index 8afa463..43fd5bf --- a/spec/unit/selinux.rb +++ b/spec/unit/selinux.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'facter' diff --git a/spec/unit/uptime.rb b/spec/unit/uptime.rb index fc592e3..bd695fa 100755 --- a/spec/unit/uptime.rb +++ b/spec/unit/uptime.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'facter' require 'facter/util/uptime' diff --git a/spec/unit/util/collection.rb b/spec/unit/util/collection.rb index 7baef96..86b602f 100755 --- a/spec/unit/util/collection.rb +++ b/spec/unit/util/collection.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/collection' diff --git a/spec/unit/util/confine.rb b/spec/unit/util/confine.rb index 757ca26..147c70d 100755 --- a/spec/unit/util/confine.rb +++ b/spec/unit/util/confine.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/confine' require 'facter/util/values' diff --git a/spec/unit/util/fact.rb b/spec/unit/util/fact.rb index 1652032..db08670 100755 --- a/spec/unit/util/fact.rb +++ b/spec/unit/util/fact.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/fact' diff --git a/spec/unit/util/ip.rb b/spec/unit/util/ip.rb index a9aae76..d87b4b9 100755 --- a/spec/unit/util/ip.rb +++ b/spec/unit/util/ip.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/ip' diff --git a/spec/unit/util/loader.rb b/spec/unit/util/loader.rb index 0a28020..0bb823e 100755 --- a/spec/unit/util/loader.rb +++ b/spec/unit/util/loader.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/loader' diff --git a/spec/unit/util/macaddress.rb b/spec/unit/util/macaddress.rb index 09794ec..98215c4 100755 --- a/spec/unit/util/macaddress.rb +++ b/spec/unit/util/macaddress.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/macaddress' diff --git a/spec/unit/util/macosx.rb b/spec/unit/util/macosx.rb index 283fe75..44ba460 100755 --- a/spec/unit/util/macosx.rb +++ b/spec/unit/util/macosx.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/macosx' diff --git a/spec/unit/util/manufacturer.rb b/spec/unit/util/manufacturer.rb index 291a6ff..07473db 100644 --- a/spec/unit/util/manufacturer.rb +++ b/spec/unit/util/manufacturer.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/manufacturer' diff --git a/spec/unit/util/resolution.rb b/spec/unit/util/resolution.rb index 396f800..581d0e1 100755 --- a/spec/unit/util/resolution.rb +++ b/spec/unit/util/resolution.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/resolution' diff --git a/spec/unit/util/uptime.rb b/spec/unit/util/uptime.rb index c104856..8d3980c 100755 --- a/spec/unit/util/uptime.rb +++ b/spec/unit/util/uptime.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/uptime' diff --git a/spec/unit/util/virtual.rb b/spec/unit/util/virtual.rb index 66dc98c..12ba0ac 100644 --- a/spec/unit/util/virtual.rb +++ b/spec/unit/util/virtual.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/virtual' diff --git a/spec/unit/util/vlans.rb b/spec/unit/util/vlans.rb old mode 100644 new mode 100755 index e06a2af..0331234 --- a/spec/unit/util/vlans.rb +++ b/spec/unit/util/vlans.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/vlans' diff --git a/spec/unit/util/xendomains.rb b/spec/unit/util/xendomains.rb index bd9c5d5..dc7e178 100755 --- a/spec/unit/util/xendomains.rb +++ b/spec/unit/util/xendomains.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require 'facter/util/xendomains' diff --git a/spec/unit/virtual.rb b/spec/unit/virtual.rb index 9e8e358..a152b40 100644 --- a/spec/unit/virtual.rb +++ b/spec/unit/virtual.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/../spec_helper' +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'facter' require 'facter/util/virtual' -- cgit From 7a8be1677314ce3db6ae6590ae7d32a10605c8d3 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Thu, 27 Jan 2011 17:17:52 -0800 Subject: Refactor #6044 -- use _spec.rb as the pattern for spec tests. Rename all the spec tests to follow the rspec convention of *_spec.rb rather than unadorned *.rb; this also makes it easier to work with them consistently without using the Rakefile support that customizes that. --- Rakefile | 4 +- spec/integration/facter.rb | 27 ---- spec/integration/facter_spec.rb | 27 ++++ spec/unit/facter.rb | 250 ---------------------------- spec/unit/facter_spec.rb | 250 ++++++++++++++++++++++++++++ spec/unit/interfaces.rb | 19 --- spec/unit/interfaces_spec.rb | 19 +++ spec/unit/operatingsystem.rb | 39 ----- spec/unit/operatingsystem_spec.rb | 39 +++++ spec/unit/operatingsystemrelease.rb | 39 ----- spec/unit/operatingsystemrelease_spec.rb | 39 +++++ spec/unit/selinux.rb | 48 ------ spec/unit/selinux_spec.rb | 48 ++++++ spec/unit/uptime.rb | 112 ------------- spec/unit/uptime_spec.rb | 112 +++++++++++++ spec/unit/util/collection.rb | 255 ----------------------------- spec/unit/util/collection_spec.rb | 255 +++++++++++++++++++++++++++++ spec/unit/util/confine.rb | 140 ---------------- spec/unit/util/confine_spec.rb | 140 ++++++++++++++++ spec/unit/util/fact.rb | 129 --------------- spec/unit/util/fact_spec.rb | 129 +++++++++++++++ spec/unit/util/ip.rb | 202 ----------------------- spec/unit/util/ip_spec.rb | 202 +++++++++++++++++++++++ spec/unit/util/loader.rb | 229 -------------------------- spec/unit/util/loader_spec.rb | 229 ++++++++++++++++++++++++++ spec/unit/util/macaddress.rb | 75 --------- spec/unit/util/macaddress_spec.rb | 75 +++++++++ spec/unit/util/macosx.rb | 81 ---------- spec/unit/util/macosx_spec.rb | 81 ++++++++++ spec/unit/util/manufacturer.rb | 132 --------------- spec/unit/util/manufacturer_spec.rb | 132 +++++++++++++++ spec/unit/util/resolution.rb | 269 ------------------------------- spec/unit/util/resolution_spec.rb | 269 +++++++++++++++++++++++++++++++ spec/unit/util/uptime.rb | 73 --------- spec/unit/util/uptime_spec.rb | 73 +++++++++ spec/unit/util/virtual.rb | 151 ----------------- spec/unit/util/virtual_spec.rb | 151 +++++++++++++++++ spec/unit/util/vlans.rb | 14 -- spec/unit/util/vlans_spec.rb | 14 ++ spec/unit/util/xendomains.rb | 23 --- spec/unit/util/xendomains_spec.rb | 23 +++ spec/unit/virtual.rb | 185 --------------------- spec/unit/virtual_spec.rb | 185 +++++++++++++++++++++ 43 files changed, 2494 insertions(+), 2494 deletions(-) delete mode 100755 spec/integration/facter.rb create mode 100755 spec/integration/facter_spec.rb delete mode 100755 spec/unit/facter.rb create mode 100755 spec/unit/facter_spec.rb delete mode 100755 spec/unit/interfaces.rb create mode 100755 spec/unit/interfaces_spec.rb delete mode 100755 spec/unit/operatingsystem.rb create mode 100755 spec/unit/operatingsystem_spec.rb delete mode 100755 spec/unit/operatingsystemrelease.rb create mode 100755 spec/unit/operatingsystemrelease_spec.rb delete mode 100755 spec/unit/selinux.rb create mode 100755 spec/unit/selinux_spec.rb delete mode 100755 spec/unit/uptime.rb create mode 100755 spec/unit/uptime_spec.rb delete mode 100755 spec/unit/util/collection.rb create mode 100755 spec/unit/util/collection_spec.rb delete mode 100755 spec/unit/util/confine.rb create mode 100755 spec/unit/util/confine_spec.rb delete mode 100755 spec/unit/util/fact.rb create mode 100755 spec/unit/util/fact_spec.rb delete mode 100755 spec/unit/util/ip.rb create mode 100755 spec/unit/util/ip_spec.rb delete mode 100755 spec/unit/util/loader.rb create mode 100755 spec/unit/util/loader_spec.rb delete mode 100755 spec/unit/util/macaddress.rb create mode 100755 spec/unit/util/macaddress_spec.rb delete mode 100755 spec/unit/util/macosx.rb create mode 100755 spec/unit/util/macosx_spec.rb delete mode 100644 spec/unit/util/manufacturer.rb create mode 100644 spec/unit/util/manufacturer_spec.rb delete mode 100755 spec/unit/util/resolution.rb create mode 100755 spec/unit/util/resolution_spec.rb delete mode 100755 spec/unit/util/uptime.rb create mode 100755 spec/unit/util/uptime_spec.rb delete mode 100644 spec/unit/util/virtual.rb create mode 100644 spec/unit/util/virtual_spec.rb delete mode 100755 spec/unit/util/vlans.rb create mode 100755 spec/unit/util/vlans_spec.rb delete mode 100755 spec/unit/util/xendomains.rb create mode 100755 spec/unit/util/xendomains_spec.rb delete mode 100644 spec/unit/virtual.rb create mode 100644 spec/unit/virtual_spec.rb diff --git a/Rakefile b/Rakefile index 9b79613..9d7d906 100644 --- a/Rakefile +++ b/Rakefile @@ -63,12 +63,12 @@ task :default do end RSpec::Core::RakeTask.new do |t| - t.pattern ='spec/{unit,integration}/**/*.rb' + t.pattern ='spec/{unit,integration}/**/*_spec.rb' t.fail_on_error = false end RSpec::Core::RakeTask.new('spec:rcov') do |t| - t.pattern ='spec/{unit,integration}/**/*.rb' + t.pattern ='spec/{unit,integration}/**/*_spec.rb' t.fail_on_error = false if defined?(Rcov) t.rcov = true diff --git a/spec/integration/facter.rb b/spec/integration/facter.rb deleted file mode 100755 index 8351de1..0000000 --- a/spec/integration/facter.rb +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe Facter do - before do - Facter.reset - end - - after do - Facter.reset - end - - it "should create a new collection if one does not exist" do - Facter.reset - coll = mock('coll') - Facter::Util::Collection.stubs(:new).returns coll - Facter.collection.should equal(coll) - Facter.reset - end - - it "should remove the collection when reset" do - old = Facter.collection - Facter.reset - Facter.collection.should_not equal(old) - end -end diff --git a/spec/integration/facter_spec.rb b/spec/integration/facter_spec.rb new file mode 100755 index 0000000..8351de1 --- /dev/null +++ b/spec/integration/facter_spec.rb @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe Facter do + before do + Facter.reset + end + + after do + Facter.reset + end + + it "should create a new collection if one does not exist" do + Facter.reset + coll = mock('coll') + Facter::Util::Collection.stubs(:new).returns coll + Facter.collection.should equal(coll) + Facter.reset + end + + it "should remove the collection when reset" do + old = Facter.collection + Facter.reset + Facter.collection.should_not equal(old) + end +end diff --git a/spec/unit/facter.rb b/spec/unit/facter.rb deleted file mode 100755 index e63bc76..0000000 --- a/spec/unit/facter.rb +++ /dev/null @@ -1,250 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe Facter do - - it "should have a version" do - Facter.version.should =~ /^[0-9]+(\.[0-9]+)*$/ - end - - it "should have a method for returning its collection" do - Facter.should respond_to(:collection) - end - - it "should cache the collection" do - Facter.collection.should equal(Facter.collection) - end - - it "should delegate the :flush method to the collection" do - Facter.collection.expects(:flush) - Facter.flush - end - - it "should delegate the :fact method to the collection" do - Facter.collection.expects(:fact) - Facter.fact - end - - it "should delegate the :list method to the collection" do - Facter.collection.expects(:list) - Facter.list - end - - it "should load all facts when listing" do - Facter.collection.expects(:load_all) - Facter.collection.stubs(:list) - Facter.list - end - - it "should delegate the :to_hash method to the collection" do - Facter.collection.expects(:to_hash) - Facter.to_hash - end - - it "should load all facts when calling :to_hash" do - Facter.collection.expects(:load_all) - Facter.collection.stubs(:to_hash) - Facter.to_hash - end - - it "should delegate the :value method to the collection" do - Facter.collection.expects(:value) - Facter.value - end - - it "should delegate the :each method to the collection" do - Facter.collection.expects(:each) - Facter.each - end - - it "should load all facts when calling :each" do - Facter.collection.expects(:load_all) - Facter.collection.stubs(:each) - Facter.each - end - - it "should yield to the block when using :each" do - Facter.collection.stubs(:load_all) - Facter.collection.stubs(:each).yields "foo" - result = [] - Facter.each { |f| result << f } - result.should == %w{foo} - end - - describe "when provided code as a string" do - it "should execute the code in the shell" do - Facter.add("shell_testing") do - setcode "echo yup" - end - - Facter["shell_testing"].value.should == "yup" - end - end - - describe "when asked for a fact as an undefined Facter class method" do - describe "and the collection is already initialized" do - it "should return the fact's value" do - Facter.collection - Facter.ipaddress.should == Facter['ipaddress'].value - end - end - - describe "and the collection has been just reset" do - it "should return the fact's value" do - Facter.reset - Facter.ipaddress.should == Facter['ipaddress'].value - end - end - end - - describe "when passed code as a block" do - it "should execute the provided block" do - Facter.add("block_testing") { setcode { "foo" } } - - Facter["block_testing"].value.should == "foo" - end - end - - describe Facter[:hostname] do - it "should have its ldapname set to 'cn'" do - Facter[:hostname].ldapname.should == "cn" - end - end - - describe Facter[:ipaddress] do - it "should have its ldapname set to 'iphostnumber'" do - Facter[:ipaddress].ldapname.should == "iphostnumber" - end - end - - # #33 Make sure we only get one mac address - it "should only return one mac address" do - Facter.value(:macaddress).should_not be_include(" ") - end - - it "should have a method for registering directories to search" do - Facter.should respond_to(:search) - end - - it "should have a method for returning the registered search directories" do - Facter.should respond_to(:search_path) - end - - it "should have a method to query debugging mode" 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 - - describe "when warning" do - it "should warn if debugging is enabled" do - Facter.debugging(true) - Kernel.stubs(:warn) - Kernel.expects(:warn).with('foo') - Facter.warn('foo') - end - - it "should not warn if debugging is enabled but nil is passed" do - Facter.debugging(true) - Kernel.stubs(:warn) - Kernel.expects(:warn).never - Facter.warn(nil) - end - - it "should not warn if debugging is enabled but an empyt string is passed" do - Facter.debugging(true) - Kernel.stubs(:warn) - Kernel.expects(:warn).never - Facter.warn('') - end - - it "should not warn if debugging is disabled" do - Facter.debugging(false) - Kernel.stubs(:warn) - Kernel.expects(:warn).never - Facter.warn('foo') - end - - it "should warn for any given element for an array if debugging is enabled" do - Facter.debugging(true) - Kernel.stubs(:warn) - Kernel.expects(:warn).with('foo') - Kernel.expects(:warn).with('bar') - Facter.warn( ['foo','bar']) - end - end - - describe "when setting debugging mode" do - it "should have debugging enabled using 1" do - Facter.debugging(1) - Facter.should be_debugging - end - it "should have debugging enabled using true" do - Facter.debugging(true) - Facter.should be_debugging - end - it "should have debugging enabled using any string except off" do - Facter.debugging('aaaaa') - Facter.should be_debugging - end - it "should have debugging disabled using 0" do - Facter.debugging(0) - Facter.should_not be_debugging - end - it "should have debugging disabled using false" do - Facter.debugging(false) - Facter.should_not be_debugging - end - it "should have debugging disabled using the string 'off'" do - Facter.debugging('off') - Facter.should_not be_debugging - 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", []) } - - it "should allow registration of a directory" do - Facter.search "/my/dir" - end - - it "should allow registration of multiple directories" do - Facter.search "/my/dir", "/other/dir" - end - - it "should return all registered directories when asked" do - Facter.search "/my/dir", "/other/dir" - Facter.search_path.should == %w{/my/dir /other/dir} - end - end -end diff --git a/spec/unit/facter_spec.rb b/spec/unit/facter_spec.rb new file mode 100755 index 0000000..e63bc76 --- /dev/null +++ b/spec/unit/facter_spec.rb @@ -0,0 +1,250 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe Facter do + + it "should have a version" do + Facter.version.should =~ /^[0-9]+(\.[0-9]+)*$/ + end + + it "should have a method for returning its collection" do + Facter.should respond_to(:collection) + end + + it "should cache the collection" do + Facter.collection.should equal(Facter.collection) + end + + it "should delegate the :flush method to the collection" do + Facter.collection.expects(:flush) + Facter.flush + end + + it "should delegate the :fact method to the collection" do + Facter.collection.expects(:fact) + Facter.fact + end + + it "should delegate the :list method to the collection" do + Facter.collection.expects(:list) + Facter.list + end + + it "should load all facts when listing" do + Facter.collection.expects(:load_all) + Facter.collection.stubs(:list) + Facter.list + end + + it "should delegate the :to_hash method to the collection" do + Facter.collection.expects(:to_hash) + Facter.to_hash + end + + it "should load all facts when calling :to_hash" do + Facter.collection.expects(:load_all) + Facter.collection.stubs(:to_hash) + Facter.to_hash + end + + it "should delegate the :value method to the collection" do + Facter.collection.expects(:value) + Facter.value + end + + it "should delegate the :each method to the collection" do + Facter.collection.expects(:each) + Facter.each + end + + it "should load all facts when calling :each" do + Facter.collection.expects(:load_all) + Facter.collection.stubs(:each) + Facter.each + end + + it "should yield to the block when using :each" do + Facter.collection.stubs(:load_all) + Facter.collection.stubs(:each).yields "foo" + result = [] + Facter.each { |f| result << f } + result.should == %w{foo} + end + + describe "when provided code as a string" do + it "should execute the code in the shell" do + Facter.add("shell_testing") do + setcode "echo yup" + end + + Facter["shell_testing"].value.should == "yup" + end + end + + describe "when asked for a fact as an undefined Facter class method" do + describe "and the collection is already initialized" do + it "should return the fact's value" do + Facter.collection + Facter.ipaddress.should == Facter['ipaddress'].value + end + end + + describe "and the collection has been just reset" do + it "should return the fact's value" do + Facter.reset + Facter.ipaddress.should == Facter['ipaddress'].value + end + end + end + + describe "when passed code as a block" do + it "should execute the provided block" do + Facter.add("block_testing") { setcode { "foo" } } + + Facter["block_testing"].value.should == "foo" + end + end + + describe Facter[:hostname] do + it "should have its ldapname set to 'cn'" do + Facter[:hostname].ldapname.should == "cn" + end + end + + describe Facter[:ipaddress] do + it "should have its ldapname set to 'iphostnumber'" do + Facter[:ipaddress].ldapname.should == "iphostnumber" + end + end + + # #33 Make sure we only get one mac address + it "should only return one mac address" do + Facter.value(:macaddress).should_not be_include(" ") + end + + it "should have a method for registering directories to search" do + Facter.should respond_to(:search) + end + + it "should have a method for returning the registered search directories" do + Facter.should respond_to(:search_path) + end + + it "should have a method to query debugging mode" 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 + + describe "when warning" do + it "should warn if debugging is enabled" do + Facter.debugging(true) + Kernel.stubs(:warn) + Kernel.expects(:warn).with('foo') + Facter.warn('foo') + end + + it "should not warn if debugging is enabled but nil is passed" do + Facter.debugging(true) + Kernel.stubs(:warn) + Kernel.expects(:warn).never + Facter.warn(nil) + end + + it "should not warn if debugging is enabled but an empyt string is passed" do + Facter.debugging(true) + Kernel.stubs(:warn) + Kernel.expects(:warn).never + Facter.warn('') + end + + it "should not warn if debugging is disabled" do + Facter.debugging(false) + Kernel.stubs(:warn) + Kernel.expects(:warn).never + Facter.warn('foo') + end + + it "should warn for any given element for an array if debugging is enabled" do + Facter.debugging(true) + Kernel.stubs(:warn) + Kernel.expects(:warn).with('foo') + Kernel.expects(:warn).with('bar') + Facter.warn( ['foo','bar']) + end + end + + describe "when setting debugging mode" do + it "should have debugging enabled using 1" do + Facter.debugging(1) + Facter.should be_debugging + end + it "should have debugging enabled using true" do + Facter.debugging(true) + Facter.should be_debugging + end + it "should have debugging enabled using any string except off" do + Facter.debugging('aaaaa') + Facter.should be_debugging + end + it "should have debugging disabled using 0" do + Facter.debugging(0) + Facter.should_not be_debugging + end + it "should have debugging disabled using false" do + Facter.debugging(false) + Facter.should_not be_debugging + end + it "should have debugging disabled using the string 'off'" do + Facter.debugging('off') + Facter.should_not be_debugging + 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", []) } + + it "should allow registration of a directory" do + Facter.search "/my/dir" + end + + it "should allow registration of multiple directories" do + Facter.search "/my/dir", "/other/dir" + end + + it "should return all registered directories when asked" do + Facter.search "/my/dir", "/other/dir" + Facter.search_path.should == %w{/my/dir /other/dir} + end + end +end diff --git a/spec/unit/interfaces.rb b/spec/unit/interfaces.rb deleted file mode 100755 index 8b295d6..0000000 --- a/spec/unit/interfaces.rb +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -require 'facter' - -describe "Per Interface IP facts" do - before do - Facter.loadfacts - end - - it "should replace the ':' in an interface list with '_'" do - # So we look supported - Facter.fact(:kernel).stubs(:value).returns("SunOS") - - Facter::Util::IP.expects(:get_interfaces).returns %w{eth0:1 eth1:2} - Facter.fact(:interfaces).value.should == %{eth0_1,eth1_2} - end -end diff --git a/spec/unit/interfaces_spec.rb b/spec/unit/interfaces_spec.rb new file mode 100755 index 0000000..8b295d6 --- /dev/null +++ b/spec/unit/interfaces_spec.rb @@ -0,0 +1,19 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +require 'facter' + +describe "Per Interface IP facts" do + before do + Facter.loadfacts + end + + it "should replace the ':' in an interface list with '_'" do + # So we look supported + Facter.fact(:kernel).stubs(:value).returns("SunOS") + + Facter::Util::IP.expects(:get_interfaces).returns %w{eth0:1 eth1:2} + Facter.fact(:interfaces).value.should == %{eth0_1,eth1_2} + end +end diff --git a/spec/unit/operatingsystem.rb b/spec/unit/operatingsystem.rb deleted file mode 100755 index be83916..0000000 --- a/spec/unit/operatingsystem.rb +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -require 'facter' - -describe "Operating System fact" do - - before do - Facter.clear - end - - after do - Facter.clear - end - - it "should default to the kernel name" do - Facter.fact(:kernel).stubs(:value).returns("Nutmeg") - - Facter.fact(:operatingsystem).value.should == "Nutmeg" - end - - it "should be Solaris for SunOS" do - Facter.fact(:kernel).stubs(:value).returns("SunOS") - - Facter.fact(:operatingsystem).value.should == "Solaris" - end - - it "should identify Oracle VM as OVS" do - - Facter.fact(:kernel).stubs(:value).returns("Linux") - FileTest.stubs(:exists?).returns false - - FileTest.expects(:exists?).with("/etc/ovs-release").returns true - FileTest.expects(:exists?).with("/etc/enterprise-release").returns true - - Facter.fact(:operatingsystem).value.should == "OVS" - end -end diff --git a/spec/unit/operatingsystem_spec.rb b/spec/unit/operatingsystem_spec.rb new file mode 100755 index 0000000..be83916 --- /dev/null +++ b/spec/unit/operatingsystem_spec.rb @@ -0,0 +1,39 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +require 'facter' + +describe "Operating System fact" do + + before do + Facter.clear + end + + after do + Facter.clear + end + + it "should default to the kernel name" do + Facter.fact(:kernel).stubs(:value).returns("Nutmeg") + + Facter.fact(:operatingsystem).value.should == "Nutmeg" + end + + it "should be Solaris for SunOS" do + Facter.fact(:kernel).stubs(:value).returns("SunOS") + + Facter.fact(:operatingsystem).value.should == "Solaris" + end + + it "should identify Oracle VM as OVS" do + + Facter.fact(:kernel).stubs(:value).returns("Linux") + FileTest.stubs(:exists?).returns false + + FileTest.expects(:exists?).with("/etc/ovs-release").returns true + FileTest.expects(:exists?).with("/etc/enterprise-release").returns true + + Facter.fact(:operatingsystem).value.should == "OVS" + end +end diff --git a/spec/unit/operatingsystemrelease.rb b/spec/unit/operatingsystemrelease.rb deleted file mode 100755 index 1cfb4ac..0000000 --- a/spec/unit/operatingsystemrelease.rb +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -require 'facter' - -describe "Operating System Release fact" do - - before do - Facter.clear - end - - after do - Facter.clear - end - - test_cases = { - "CentOS" => "/etc/redhat-release", - "RedHat" => "/etc/redhat-release", - "Fedora" => "/etc/fedora-release", - "MeeGo" => "/etc/meego-release", - "OEL" => "/etc/enterprise-release", - "oel" => "/etc/enterprise-release", - "OVS" => "/etc/ovs-release", - "ovs" => "/etc/ovs-release" - } - - test_cases.each do |system, file| - describe "with operatingsystem reported as #{system.inspect}" do - it "should read the #{file.inspect} file" do - Facter.fact(:operatingsystem).stubs(:value).returns(system) - - File.expects(:open).with(file, "r").at_least(1) - - Facter.fact(:operatingsystemrelease).value - end - end - end -end diff --git a/spec/unit/operatingsystemrelease_spec.rb b/spec/unit/operatingsystemrelease_spec.rb new file mode 100755 index 0000000..1cfb4ac --- /dev/null +++ b/spec/unit/operatingsystemrelease_spec.rb @@ -0,0 +1,39 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +require 'facter' + +describe "Operating System Release fact" do + + before do + Facter.clear + end + + after do + Facter.clear + end + + test_cases = { + "CentOS" => "/etc/redhat-release", + "RedHat" => "/etc/redhat-release", + "Fedora" => "/etc/fedora-release", + "MeeGo" => "/etc/meego-release", + "OEL" => "/etc/enterprise-release", + "oel" => "/etc/enterprise-release", + "OVS" => "/etc/ovs-release", + "ovs" => "/etc/ovs-release" + } + + test_cases.each do |system, file| + describe "with operatingsystem reported as #{system.inspect}" do + it "should read the #{file.inspect} file" do + Facter.fact(:operatingsystem).stubs(:value).returns(system) + + File.expects(:open).with(file, "r").at_least(1) + + Facter.fact(:operatingsystemrelease).value + end + end + end +end diff --git a/spec/unit/selinux.rb b/spec/unit/selinux.rb deleted file mode 100755 index 43fd5bf..0000000 --- a/spec/unit/selinux.rb +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -require 'facter' - -describe "SELinux facts" do - - - after do - Facter.clear - end - - it "should return true if SELinux enabled" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - - FileTest.stubs(:exists?).returns false - File.stubs(:read).with("/proc/self/attr/current").returns("notkernel") - - FileTest.expects(:exists?).with("/selinux/enforce").returns true - FileTest.expects(:exists?).with("/proc/self/attr/current").returns true - File.expects(:read).with("/proc/self/attr/current").returns("kernel") - - Facter.fact(:selinux).value.should == "true" - end - - it "should return true if SELinux policy enabled" do - Facter.fact(:selinux).stubs(:value).returns("true") - - FileTest.stubs(:exists?).returns false - File.stubs(:read).with("/selinux/enforce").returns("0") - - FileTest.expects(:exists?).with("/selinux/enforce").returns true - File.expects(:read).with("/selinux/enforce").returns("1") - - Facter.fact(:selinux_enforced).value.should == "true" - end - - it "should return an SELinux policy version" do - Facter.fact(:selinux).stubs(:value).returns("true") - - File.stubs(:read).with("/selinux/policyvers").returns("") - - File.expects(:read).with("/selinux/policyvers").returns("1") - - Facter.fact(:selinux_policyversion).value.should == "1" - end -end diff --git a/spec/unit/selinux_spec.rb b/spec/unit/selinux_spec.rb new file mode 100755 index 0000000..43fd5bf --- /dev/null +++ b/spec/unit/selinux_spec.rb @@ -0,0 +1,48 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +require 'facter' + +describe "SELinux facts" do + + + after do + Facter.clear + end + + it "should return true if SELinux enabled" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + + FileTest.stubs(:exists?).returns false + File.stubs(:read).with("/proc/self/attr/current").returns("notkernel") + + FileTest.expects(:exists?).with("/selinux/enforce").returns true + FileTest.expects(:exists?).with("/proc/self/attr/current").returns true + File.expects(:read).with("/proc/self/attr/current").returns("kernel") + + Facter.fact(:selinux).value.should == "true" + end + + it "should return true if SELinux policy enabled" do + Facter.fact(:selinux).stubs(:value).returns("true") + + FileTest.stubs(:exists?).returns false + File.stubs(:read).with("/selinux/enforce").returns("0") + + FileTest.expects(:exists?).with("/selinux/enforce").returns true + File.expects(:read).with("/selinux/enforce").returns("1") + + Facter.fact(:selinux_enforced).value.should == "true" + end + + it "should return an SELinux policy version" do + Facter.fact(:selinux).stubs(:value).returns("true") + + File.stubs(:read).with("/selinux/policyvers").returns("") + + File.expects(:read).with("/selinux/policyvers").returns("1") + + Facter.fact(:selinux_policyversion).value.should == "1" + end +end diff --git a/spec/unit/uptime.rb b/spec/unit/uptime.rb deleted file mode 100755 index bd695fa..0000000 --- a/spec/unit/uptime.rb +++ /dev/null @@ -1,112 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -require 'facter' -require 'facter/util/uptime' - -describe "uptime facts:" do - before { Facter.clear } - after { Facter.clear } - - describe "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 - - describe "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 - - describe "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/uptime_spec.rb b/spec/unit/uptime_spec.rb new file mode 100755 index 0000000..bd695fa --- /dev/null +++ b/spec/unit/uptime_spec.rb @@ -0,0 +1,112 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +require 'facter' +require 'facter/util/uptime' + +describe "uptime facts:" do + before { Facter.clear } + after { Facter.clear } + + describe "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 + + describe "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 + + describe "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/collection.rb b/spec/unit/util/collection.rb deleted file mode 100755 index 86b602f..0000000 --- a/spec/unit/util/collection.rb +++ /dev/null @@ -1,255 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/collection' - -describe Facter::Util::Collection do - it "should have a method for adding facts" do - Facter::Util::Collection.new.should respond_to(:add) - end - - it "should have a method for returning a loader" do - Facter::Util::Collection.new.should respond_to(:loader) - end - - it "should use an instance of the Loader class as its loader" do - Facter::Util::Collection.new.loader.should be_instance_of(Facter::Util::Loader) - end - - it "should cache its loader" do - coll = Facter::Util::Collection.new - coll.loader.should equal(coll.loader) - end - - it "should have a method for loading all facts" do - Facter::Util::Collection.new.should respond_to(:load_all) - end - - it "should delegate its load_all method to its loader" do - coll = Facter::Util::Collection.new - coll.loader.expects(:load_all) - coll.load_all - end - - describe "when adding facts" do - before do - @coll = Facter::Util::Collection.new - end - - it "should create a new fact if no fact with the same name already exists" do - fact = mock 'fact' - Facter::Util::Fact.expects(:new).with { |name, *args| name == :myname }.returns fact - - @coll.add(:myname) - end - - it "should accept options" do - @coll.add(:myname, :ldapname => "whatever") { } - end - - it "should set any appropriate options on the fact instances" do - # Use a real fact instance, because we're using respond_to? - fact = Facter::Util::Fact.new(:myname) - fact.expects(:ldapname=).with("testing") - Facter::Util::Fact.expects(:new).with(:myname).returns fact - - @coll.add(:myname, :ldapname => "testing") - end - - it "should set appropriate options on the resolution instance" do - fact = Facter::Util::Fact.new(:myname) - Facter::Util::Fact.expects(:new).with(:myname).returns fact - - resolve = Facter::Util::Resolution.new(:myname) {} - fact.expects(:add).returns resolve - - @coll.add(:myname, :timeout => "myval") {} - end - - it "should not pass fact-specific options to resolutions" do - fact = Facter::Util::Fact.new(:myname) - Facter::Util::Fact.expects(:new).with(:myname).returns fact - - resolve = Facter::Util::Resolution.new(:myname) {} - fact.expects(:add).returns resolve - - fact.expects(:ldapname=).with("foo") - resolve.expects(:timeout=).with("myval") - - @coll.add(:myname, :timeout => "myval", :ldapname => "foo") {} - end - - it "should fail if invalid options are provided" do - lambda { @coll.add(:myname, :foo => :bar) }.should raise_error(ArgumentError) - end - - describe "and a block is provided" do - it "should use the block to add a resolution to the fact" do - fact = mock 'fact' - Facter::Util::Fact.expects(:new).returns fact - - fact.expects(:add) - - @coll.add(:myname) {} - end - end - end - - it "should have a method for retrieving facts by name" do - Facter::Util::Collection.new.should respond_to(:fact) - end - - describe "when retrieving facts" do - before do - @coll = Facter::Util::Collection.new - - @fact = @coll.add("YayNess") - end - - it "should return the fact instance specified by the name" do - @coll.fact("YayNess").should equal(@fact) - end - - it "should be case-insensitive" do - @coll.fact("yayness").should equal(@fact) - end - - it "should treat strings and symbols equivalently" do - @coll.fact(:yayness).should equal(@fact) - end - - it "should use its loader to try to load the fact if no fact can be found" do - @coll.loader.expects(:load).with(:testing) - @coll.fact("testing") - end - - it "should return nil if it cannot find or load the fact" do - @coll.loader.expects(:load).with(:testing) - @coll.fact("testing").should be_nil - end - end - - it "should have a method for returning a fact's value" do - Facter::Util::Collection.new.should respond_to(:value) - end - - describe "when returning a fact's value" do - before do - @coll = Facter::Util::Collection.new - @fact = @coll.add("YayNess") - - @fact.stubs(:value).returns "result" - end - - it "should use the 'fact' method to retrieve the fact" do - @coll.expects(:fact).with(:yayness).returns @fact - @coll.value(:yayness) - end - - it "should return the result of calling :value on the fact" do - @fact.expects(:value).returns "result" - - @coll.value("YayNess").should == "result" - end - - it "should be case-insensitive" do - @coll.value("yayness").should_not be_nil - end - - it "should treat strings and symbols equivalently" do - @coll.value(:yayness).should_not be_nil - end - end - - it "should return the fact's value when the array index method is used" do - @coll = Facter::Util::Collection.new - @coll.expects(:value).with("myfact").returns "foo" - @coll["myfact"].should == "foo" - end - - it "should have a method for flushing all facts" do - @coll = Facter::Util::Collection.new - @fact = @coll.add("YayNess") - - @fact.expects(:flush) - - @coll.flush - end - - it "should have a method that returns all fact names" do - @coll = Facter::Util::Collection.new - @coll.add(:one) - @coll.add(:two) - - @coll.list.sort { |a,b| a.to_s <=> b.to_s }.should == [:one, :two] - end - - it "should have a method for returning a hash of fact values" do - Facter::Util::Collection.new.should respond_to(:to_hash) - end - - describe "when returning a hash of values" do - before do - @coll = Facter::Util::Collection.new - @fact = @coll.add(:one) - @fact.stubs(:value).returns "me" - end - - it "should return a hash of fact names and values with the fact names as strings" do - @coll.to_hash.should == {"one" => "me"} - end - - it "should not include facts that did not return a value" do - f = @coll.add(:two) - f.stubs(:value).returns nil - @coll.to_hash.should_not be_include(:two) - end - end - - it "should have a method for iterating over all facts" do - Facter::Util::Collection.new.should respond_to(:each) - end - - it "should include Enumerable" do - Facter::Util::Collection.ancestors.should be_include(Enumerable) - end - - describe "when iterating over facts" do - before do - @coll = Facter::Util::Collection.new - @one = @coll.add(:one) - @two = @coll.add(:two) - end - - it "should yield each fact name and the fact value" do - @one.stubs(:value).returns "ONE" - @two.stubs(:value).returns "TWO" - facts = {} - @coll.each do |fact, value| - facts[fact] = value - end - facts.should == {"one" => "ONE", "two" => "TWO"} - end - - it "should convert the fact name to a string" do - @one.stubs(:value).returns "ONE" - @two.stubs(:value).returns "TWO" - facts = {} - @coll.each do |fact, value| - fact.should be_instance_of(String) - end - end - - it "should only yield facts that have values" do - @one.stubs(:value).returns "ONE" - @two.stubs(:value).returns nil - facts = {} - @coll.each do |fact, value| - facts[fact] = value - end - - facts.should_not be_include("two") - end - end -end diff --git a/spec/unit/util/collection_spec.rb b/spec/unit/util/collection_spec.rb new file mode 100755 index 0000000..86b602f --- /dev/null +++ b/spec/unit/util/collection_spec.rb @@ -0,0 +1,255 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/collection' + +describe Facter::Util::Collection do + it "should have a method for adding facts" do + Facter::Util::Collection.new.should respond_to(:add) + end + + it "should have a method for returning a loader" do + Facter::Util::Collection.new.should respond_to(:loader) + end + + it "should use an instance of the Loader class as its loader" do + Facter::Util::Collection.new.loader.should be_instance_of(Facter::Util::Loader) + end + + it "should cache its loader" do + coll = Facter::Util::Collection.new + coll.loader.should equal(coll.loader) + end + + it "should have a method for loading all facts" do + Facter::Util::Collection.new.should respond_to(:load_all) + end + + it "should delegate its load_all method to its loader" do + coll = Facter::Util::Collection.new + coll.loader.expects(:load_all) + coll.load_all + end + + describe "when adding facts" do + before do + @coll = Facter::Util::Collection.new + end + + it "should create a new fact if no fact with the same name already exists" do + fact = mock 'fact' + Facter::Util::Fact.expects(:new).with { |name, *args| name == :myname }.returns fact + + @coll.add(:myname) + end + + it "should accept options" do + @coll.add(:myname, :ldapname => "whatever") { } + end + + it "should set any appropriate options on the fact instances" do + # Use a real fact instance, because we're using respond_to? + fact = Facter::Util::Fact.new(:myname) + fact.expects(:ldapname=).with("testing") + Facter::Util::Fact.expects(:new).with(:myname).returns fact + + @coll.add(:myname, :ldapname => "testing") + end + + it "should set appropriate options on the resolution instance" do + fact = Facter::Util::Fact.new(:myname) + Facter::Util::Fact.expects(:new).with(:myname).returns fact + + resolve = Facter::Util::Resolution.new(:myname) {} + fact.expects(:add).returns resolve + + @coll.add(:myname, :timeout => "myval") {} + end + + it "should not pass fact-specific options to resolutions" do + fact = Facter::Util::Fact.new(:myname) + Facter::Util::Fact.expects(:new).with(:myname).returns fact + + resolve = Facter::Util::Resolution.new(:myname) {} + fact.expects(:add).returns resolve + + fact.expects(:ldapname=).with("foo") + resolve.expects(:timeout=).with("myval") + + @coll.add(:myname, :timeout => "myval", :ldapname => "foo") {} + end + + it "should fail if invalid options are provided" do + lambda { @coll.add(:myname, :foo => :bar) }.should raise_error(ArgumentError) + end + + describe "and a block is provided" do + it "should use the block to add a resolution to the fact" do + fact = mock 'fact' + Facter::Util::Fact.expects(:new).returns fact + + fact.expects(:add) + + @coll.add(:myname) {} + end + end + end + + it "should have a method for retrieving facts by name" do + Facter::Util::Collection.new.should respond_to(:fact) + end + + describe "when retrieving facts" do + before do + @coll = Facter::Util::Collection.new + + @fact = @coll.add("YayNess") + end + + it "should return the fact instance specified by the name" do + @coll.fact("YayNess").should equal(@fact) + end + + it "should be case-insensitive" do + @coll.fact("yayness").should equal(@fact) + end + + it "should treat strings and symbols equivalently" do + @coll.fact(:yayness).should equal(@fact) + end + + it "should use its loader to try to load the fact if no fact can be found" do + @coll.loader.expects(:load).with(:testing) + @coll.fact("testing") + end + + it "should return nil if it cannot find or load the fact" do + @coll.loader.expects(:load).with(:testing) + @coll.fact("testing").should be_nil + end + end + + it "should have a method for returning a fact's value" do + Facter::Util::Collection.new.should respond_to(:value) + end + + describe "when returning a fact's value" do + before do + @coll = Facter::Util::Collection.new + @fact = @coll.add("YayNess") + + @fact.stubs(:value).returns "result" + end + + it "should use the 'fact' method to retrieve the fact" do + @coll.expects(:fact).with(:yayness).returns @fact + @coll.value(:yayness) + end + + it "should return the result of calling :value on the fact" do + @fact.expects(:value).returns "result" + + @coll.value("YayNess").should == "result" + end + + it "should be case-insensitive" do + @coll.value("yayness").should_not be_nil + end + + it "should treat strings and symbols equivalently" do + @coll.value(:yayness).should_not be_nil + end + end + + it "should return the fact's value when the array index method is used" do + @coll = Facter::Util::Collection.new + @coll.expects(:value).with("myfact").returns "foo" + @coll["myfact"].should == "foo" + end + + it "should have a method for flushing all facts" do + @coll = Facter::Util::Collection.new + @fact = @coll.add("YayNess") + + @fact.expects(:flush) + + @coll.flush + end + + it "should have a method that returns all fact names" do + @coll = Facter::Util::Collection.new + @coll.add(:one) + @coll.add(:two) + + @coll.list.sort { |a,b| a.to_s <=> b.to_s }.should == [:one, :two] + end + + it "should have a method for returning a hash of fact values" do + Facter::Util::Collection.new.should respond_to(:to_hash) + end + + describe "when returning a hash of values" do + before do + @coll = Facter::Util::Collection.new + @fact = @coll.add(:one) + @fact.stubs(:value).returns "me" + end + + it "should return a hash of fact names and values with the fact names as strings" do + @coll.to_hash.should == {"one" => "me"} + end + + it "should not include facts that did not return a value" do + f = @coll.add(:two) + f.stubs(:value).returns nil + @coll.to_hash.should_not be_include(:two) + end + end + + it "should have a method for iterating over all facts" do + Facter::Util::Collection.new.should respond_to(:each) + end + + it "should include Enumerable" do + Facter::Util::Collection.ancestors.should be_include(Enumerable) + end + + describe "when iterating over facts" do + before do + @coll = Facter::Util::Collection.new + @one = @coll.add(:one) + @two = @coll.add(:two) + end + + it "should yield each fact name and the fact value" do + @one.stubs(:value).returns "ONE" + @two.stubs(:value).returns "TWO" + facts = {} + @coll.each do |fact, value| + facts[fact] = value + end + facts.should == {"one" => "ONE", "two" => "TWO"} + end + + it "should convert the fact name to a string" do + @one.stubs(:value).returns "ONE" + @two.stubs(:value).returns "TWO" + facts = {} + @coll.each do |fact, value| + fact.should be_instance_of(String) + end + end + + it "should only yield facts that have values" do + @one.stubs(:value).returns "ONE" + @two.stubs(:value).returns nil + facts = {} + @coll.each do |fact, value| + facts[fact] = value + end + + facts.should_not be_include("two") + end + end +end diff --git a/spec/unit/util/confine.rb b/spec/unit/util/confine.rb deleted file mode 100755 index 147c70d..0000000 --- a/spec/unit/util/confine.rb +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/confine' -require 'facter/util/values' - -include Facter::Util::Values - -describe Facter::Util::Confine do - it "should require a fact name" do - Facter::Util::Confine.new("yay", true).fact.should == "yay" - end - - it "should accept a value specified individually" do - Facter::Util::Confine.new("yay", "test").values.should == ["test"] - end - - it "should accept multiple values specified at once" do - Facter::Util::Confine.new("yay", "test", "other").values.should == ["test", "other"] - end - - it "should fail if no fact name is provided" do - lambda { Facter::Util::Confine.new(nil, :test) }.should raise_error(ArgumentError) - end - - it "should fail if no values were provided" do - lambda { Facter::Util::Confine.new("yay") }.should raise_error(ArgumentError) - end - - it "should have a method for testing whether it matches" do - Facter::Util::Confine.new("yay", :test).should respond_to(:true?) - end - - describe "when evaluating" do - before do - @confine = Facter::Util::Confine.new("yay", "one", "two", "Four", :xy, true, 1, [3,4]) - @fact = mock 'fact' - Facter.stubs(:[]).returns @fact - end - - it "should return false if the fact does not exist" do - Facter.expects(:[]).with("yay").returns nil - - @confine.true?.should be_false - end - - it "should use the returned fact to get the value" do - Facter.expects(:[]).with("yay").returns @fact - - @fact.expects(:value).returns nil - - @confine.true? - end - - it "should return false if the fact has no value" do - @fact.stubs(:value).returns nil - - @confine.true?.should be_false - end - - it "should return true if any of the provided values matches the fact's value" do - @fact.stubs(:value).returns "two" - - @confine.true?.should be_true - end - - it "should return true if any of the provided symbol values matches the fact's value" do - @fact.stubs(:value).returns :xy - - @confine.true?.should be_true - end - - it "should return true if any of the provided integer values matches the fact's value" do - @fact.stubs(:value).returns 1 - - @confine.true?.should be_true - end - - it "should return true if any of the provided boolan values matches the fact's value" do - @fact.stubs(:value).returns true - - @confine.true?.should be_true - end - - it "should return true if any of the provided array values matches the fact's value" do - @fact.stubs(:value).returns [3,4] - - @confine.true?.should be_true - end - - it "should return true if any of the provided symbol values matches the fact's string value" do - @fact.stubs(:value).returns :one - - @confine.true?.should be_true - end - - it "should return true if any of the provided string values matches case-insensitive the fact's value" do - @fact.stubs(:value).returns "four" - - @confine.true?.should be_true - end - - it "should return true if any of the provided symbol values matches case-insensitive the fact's string value" do - @fact.stubs(:value).returns :four - - @confine.true?.should be_true - end - - it "should return true if any of the provided symbol values matches the fact's string value" do - @fact.stubs(:value).returns :Xy - - @confine.true?.should be_true - end - - it "should return false if none of the provided values matches the fact's value" do - @fact.stubs(:value).returns "three" - - @confine.true?.should be_false - end - - it "should return false if none of the provided integer values matches the fact's value" do - @fact.stubs(:value).returns 2 - - @confine.true?.should be_false - end - - it "should return false if none of the provided boolan values matches the fact's value" do - @fact.stubs(:value).returns false - - @confine.true?.should be_false - end - - it "should return false if none of the provided array values matches the fact's value" do - @fact.stubs(:value).returns [1,2] - - @confine.true?.should be_false - end - end -end diff --git a/spec/unit/util/confine_spec.rb b/spec/unit/util/confine_spec.rb new file mode 100755 index 0000000..147c70d --- /dev/null +++ b/spec/unit/util/confine_spec.rb @@ -0,0 +1,140 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/confine' +require 'facter/util/values' + +include Facter::Util::Values + +describe Facter::Util::Confine do + it "should require a fact name" do + Facter::Util::Confine.new("yay", true).fact.should == "yay" + end + + it "should accept a value specified individually" do + Facter::Util::Confine.new("yay", "test").values.should == ["test"] + end + + it "should accept multiple values specified at once" do + Facter::Util::Confine.new("yay", "test", "other").values.should == ["test", "other"] + end + + it "should fail if no fact name is provided" do + lambda { Facter::Util::Confine.new(nil, :test) }.should raise_error(ArgumentError) + end + + it "should fail if no values were provided" do + lambda { Facter::Util::Confine.new("yay") }.should raise_error(ArgumentError) + end + + it "should have a method for testing whether it matches" do + Facter::Util::Confine.new("yay", :test).should respond_to(:true?) + end + + describe "when evaluating" do + before do + @confine = Facter::Util::Confine.new("yay", "one", "two", "Four", :xy, true, 1, [3,4]) + @fact = mock 'fact' + Facter.stubs(:[]).returns @fact + end + + it "should return false if the fact does not exist" do + Facter.expects(:[]).with("yay").returns nil + + @confine.true?.should be_false + end + + it "should use the returned fact to get the value" do + Facter.expects(:[]).with("yay").returns @fact + + @fact.expects(:value).returns nil + + @confine.true? + end + + it "should return false if the fact has no value" do + @fact.stubs(:value).returns nil + + @confine.true?.should be_false + end + + it "should return true if any of the provided values matches the fact's value" do + @fact.stubs(:value).returns "two" + + @confine.true?.should be_true + end + + it "should return true if any of the provided symbol values matches the fact's value" do + @fact.stubs(:value).returns :xy + + @confine.true?.should be_true + end + + it "should return true if any of the provided integer values matches the fact's value" do + @fact.stubs(:value).returns 1 + + @confine.true?.should be_true + end + + it "should return true if any of the provided boolan values matches the fact's value" do + @fact.stubs(:value).returns true + + @confine.true?.should be_true + end + + it "should return true if any of the provided array values matches the fact's value" do + @fact.stubs(:value).returns [3,4] + + @confine.true?.should be_true + end + + it "should return true if any of the provided symbol values matches the fact's string value" do + @fact.stubs(:value).returns :one + + @confine.true?.should be_true + end + + it "should return true if any of the provided string values matches case-insensitive the fact's value" do + @fact.stubs(:value).returns "four" + + @confine.true?.should be_true + end + + it "should return true if any of the provided symbol values matches case-insensitive the fact's string value" do + @fact.stubs(:value).returns :four + + @confine.true?.should be_true + end + + it "should return true if any of the provided symbol values matches the fact's string value" do + @fact.stubs(:value).returns :Xy + + @confine.true?.should be_true + end + + it "should return false if none of the provided values matches the fact's value" do + @fact.stubs(:value).returns "three" + + @confine.true?.should be_false + end + + it "should return false if none of the provided integer values matches the fact's value" do + @fact.stubs(:value).returns 2 + + @confine.true?.should be_false + end + + it "should return false if none of the provided boolan values matches the fact's value" do + @fact.stubs(:value).returns false + + @confine.true?.should be_false + end + + it "should return false if none of the provided array values matches the fact's value" do + @fact.stubs(:value).returns [1,2] + + @confine.true?.should be_false + end + end +end diff --git a/spec/unit/util/fact.rb b/spec/unit/util/fact.rb deleted file mode 100755 index db08670..0000000 --- a/spec/unit/util/fact.rb +++ /dev/null @@ -1,129 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/fact' - -describe Facter::Util::Fact do - it "should require a name" do - lambda { Facter::Util::Fact.new }.should raise_error(ArgumentError) - end - - it "should always downcase the name and convert it to a symbol" do - Facter::Util::Fact.new("YayNess").name.should == :yayness - end - - it "should default to its name converted to a string as its ldapname" do - Facter::Util::Fact.new("YayNess").ldapname.should == "yayness" - end - - it "should allow specifying the ldap name at initialization" do - Facter::Util::Fact.new("YayNess", :ldapname => "fooness").ldapname.should == "fooness" - end - - it "should fail if an unknown option is provided" do - lambda { Facter::Util::Fact.new('yay', :foo => :bar) }.should raise_error(ArgumentError) - end - - it "should have a method for adding resolution mechanisms" do - Facter::Util::Fact.new("yay").should respond_to(:add) - end - - describe "when adding resolution mechanisms" do - before do - @fact = Facter::Util::Fact.new("yay") - - @resolution = mock 'resolution' - @resolution.stub_everything - - end - - it "should fail if no block is given" do - lambda { @fact.add }.should raise_error(ArgumentError) - end - - it "should create a new resolution instance" do - Facter::Util::Resolution.expects(:new).returns @resolution - - @fact.add { } - end - - it "should instance_eval the passed block on the new resolution" do - @resolution.expects(:instance_eval) - - Facter::Util::Resolution.stubs(:new).returns @resolution - - @fact.add { } - end - - it "should re-sort the resolutions by length, so the most restricted resolutions are first" do - r1 = stub 'r1', :length => 1 - r2 = stub 'r2', :length => 2 - r3 = stub 'r3', :length => 0 - Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3) - @fact.add { } - @fact.add { } - @fact.add { } - - @fact.instance_variable_get("@resolves").should == [r2, r1, r3] - end - end - - it "should be able to return a value" do - Facter::Util::Fact.new("yay").should respond_to(:value) - end - - describe "when returning a value" do - before do - @fact = Facter::Util::Fact.new("yay") - end - - it "should return nil if there are no resolutions" do - Facter::Util::Fact.new("yay").value.should be_nil - end - - it "should return the first value returned by a resolution" do - r1 = stub 'r1', :length => 2, :value => nil, :suitable? => true - r2 = stub 'r2', :length => 1, :value => "yay", :suitable? => true - r3 = stub 'r3', :length => 0, :value => "foo", :suitable? => true - Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3) - @fact.add { } - @fact.add { } - @fact.add { } - - @fact.value.should == "yay" - end - - it "should short-cut returning the value once one is found" do - r1 = stub 'r1', :length => 2, :value => "foo", :suitable? => true - r2 = stub 'r2', :length => 1, :suitable? => true # would fail if 'value' were asked for - Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2) - @fact.add { } - @fact.add { } - - @fact.value - end - - it "should skip unsuitable resolutions" do - r1 = stub 'r1', :length => 2, :suitable? => false # would fail if 'value' were asked for' - r2 = stub 'r2', :length => 1, :value => "yay", :suitable? => true - Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2) - @fact.add { } - @fact.add { } - - @fact.value.should == "yay" - end - - it "should return nil if the value is the empty string" do - r1 = stub 'r1', :suitable? => true, :value => "" - Facter::Util::Resolution.expects(:new).returns r1 - @fact.add { } - - @fact.value.should be_nil - end - end - - it "should have a method for flushing the cached fact" do - Facter::Util::Fact.new(:foo).should respond_to(:flush) - end -end diff --git a/spec/unit/util/fact_spec.rb b/spec/unit/util/fact_spec.rb new file mode 100755 index 0000000..db08670 --- /dev/null +++ b/spec/unit/util/fact_spec.rb @@ -0,0 +1,129 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/fact' + +describe Facter::Util::Fact do + it "should require a name" do + lambda { Facter::Util::Fact.new }.should raise_error(ArgumentError) + end + + it "should always downcase the name and convert it to a symbol" do + Facter::Util::Fact.new("YayNess").name.should == :yayness + end + + it "should default to its name converted to a string as its ldapname" do + Facter::Util::Fact.new("YayNess").ldapname.should == "yayness" + end + + it "should allow specifying the ldap name at initialization" do + Facter::Util::Fact.new("YayNess", :ldapname => "fooness").ldapname.should == "fooness" + end + + it "should fail if an unknown option is provided" do + lambda { Facter::Util::Fact.new('yay', :foo => :bar) }.should raise_error(ArgumentError) + end + + it "should have a method for adding resolution mechanisms" do + Facter::Util::Fact.new("yay").should respond_to(:add) + end + + describe "when adding resolution mechanisms" do + before do + @fact = Facter::Util::Fact.new("yay") + + @resolution = mock 'resolution' + @resolution.stub_everything + + end + + it "should fail if no block is given" do + lambda { @fact.add }.should raise_error(ArgumentError) + end + + it "should create a new resolution instance" do + Facter::Util::Resolution.expects(:new).returns @resolution + + @fact.add { } + end + + it "should instance_eval the passed block on the new resolution" do + @resolution.expects(:instance_eval) + + Facter::Util::Resolution.stubs(:new).returns @resolution + + @fact.add { } + end + + it "should re-sort the resolutions by length, so the most restricted resolutions are first" do + r1 = stub 'r1', :length => 1 + r2 = stub 'r2', :length => 2 + r3 = stub 'r3', :length => 0 + Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3) + @fact.add { } + @fact.add { } + @fact.add { } + + @fact.instance_variable_get("@resolves").should == [r2, r1, r3] + end + end + + it "should be able to return a value" do + Facter::Util::Fact.new("yay").should respond_to(:value) + end + + describe "when returning a value" do + before do + @fact = Facter::Util::Fact.new("yay") + end + + it "should return nil if there are no resolutions" do + Facter::Util::Fact.new("yay").value.should be_nil + end + + it "should return the first value returned by a resolution" do + r1 = stub 'r1', :length => 2, :value => nil, :suitable? => true + r2 = stub 'r2', :length => 1, :value => "yay", :suitable? => true + r3 = stub 'r3', :length => 0, :value => "foo", :suitable? => true + Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3) + @fact.add { } + @fact.add { } + @fact.add { } + + @fact.value.should == "yay" + end + + it "should short-cut returning the value once one is found" do + r1 = stub 'r1', :length => 2, :value => "foo", :suitable? => true + r2 = stub 'r2', :length => 1, :suitable? => true # would fail if 'value' were asked for + Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2) + @fact.add { } + @fact.add { } + + @fact.value + end + + it "should skip unsuitable resolutions" do + r1 = stub 'r1', :length => 2, :suitable? => false # would fail if 'value' were asked for' + r2 = stub 'r2', :length => 1, :value => "yay", :suitable? => true + Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2) + @fact.add { } + @fact.add { } + + @fact.value.should == "yay" + end + + it "should return nil if the value is the empty string" do + r1 = stub 'r1', :suitable? => true, :value => "" + Facter::Util::Resolution.expects(:new).returns r1 + @fact.add { } + + @fact.value.should be_nil + end + end + + it "should have a method for flushing the cached fact" do + Facter::Util::Fact.new(:foo).should respond_to(:flush) + end +end diff --git a/spec/unit/util/ip.rb b/spec/unit/util/ip.rb deleted file mode 100755 index d87b4b9..0000000 --- a/spec/unit/util/ip.rb +++ /dev/null @@ -1,202 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/ip' - -describe Facter::Util::IP do - [:freebsd, :linux, :netbsd, :openbsd, :sunos, :darwin, :"hp-ux"].each do |platform| - it "should be supported on #{platform}" do - Facter::Util::IP.supported_platforms.should be_include(platform) - end - end - - it "should return a list of interfaces" do - Facter::Util::IP.should respond_to(:get_interfaces) - end - - it "should return an empty list of interfaces on an unknown kernel" do - Facter.stubs(:value).returns("UnknownKernel") - Facter::Util::IP.get_interfaces().should == [] - end - - it "should return a list with a single interface on Linux with a single interface" do - sample_output_file = File.dirname(__FILE__) + '/../data/linux_ifconfig_all_with_single_interface' - linux_ifconfig = File.new(sample_output_file).read() - Facter::Util::IP.stubs(:get_all_interface_output).returns(linux_ifconfig) - Facter::Util::IP.get_interfaces().should == ["eth0"] - end - - it "should return a list two interfaces on Darwin with two interfaces" do - sample_output_file = File.dirname(__FILE__) + '/../data/darwin_ifconfig_all_with_multiple_interfaces' - darwin_ifconfig = File.new(sample_output_file).read() - Facter::Util::IP.stubs(:get_all_interface_output).returns(darwin_ifconfig) - Facter::Util::IP.get_interfaces().should == ["lo0", "en0"] - end - - it "should return a list two interfaces on Solaris with two interfaces multiply reporting" do - sample_output_file = File.dirname(__FILE__) + '/../data/solaris_ifconfig_all_with_multiple_interfaces' - solaris_ifconfig = File.new(sample_output_file).read() - Facter::Util::IP.stubs(:get_all_interface_output).returns(solaris_ifconfig) - Facter::Util::IP.get_interfaces().should == ["lo0", "e1000g0"] - end - - it "should return a list three interfaces on HP-UX with three interfaces multiply reporting" do - sample_output_file = File.dirname(__FILE__) + '/../data/hpux_netstat_all_interfaces' - hpux_netstat = File.new(sample_output_file).read() - Facter::Util::IP.stubs(:get_all_interface_output).returns(hpux_netstat) - Facter::Util::IP.get_interfaces().should == ["lan1", "lan0", "lo0"] - end - - it "should return a value for a specific interface" do - Facter::Util::IP.should respond_to(:get_interface_value) - end - - it "should not return interface information for unsupported platforms" do - Facter.stubs(:value).with(:kernel).returns("bleah") - Facter::Util::IP.get_interface_value("e1000g0", "netmask").should == [] - end - - it "should return ipaddress information for Solaris" do - sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface" - solaris_ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("SunOS") - - Facter::Util::IP.get_interface_value("e1000g0", "ipaddress").should == "172.16.15.138" - end - - it "should return netmask information for Solaris" do - sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface" - solaris_ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("SunOS") - - Facter::Util::IP.get_interface_value("e1000g0", "netmask").should == "255.255.255.0" - end - - it "should return calculated network information for Solaris" do - sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface" - solaris_ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.stubs(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("SunOS") - - Facter::Util::IP.get_network_value("e1000g0").should == "172.16.15.0" - end - - it "should return ipaddress information for HP-UX" do - sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" - hpux_ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("HP-UX") - - Facter::Util::IP.get_interface_value("lan0", "ipaddress").should == "168.24.80.71" - end - - it "should return macaddress information for HP-UX" do - sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" - hpux_ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("HP-UX") - - Facter::Util::IP.get_interface_value("lan0", "macaddress").should == "00:13:21:BD:9C:B7" - end - - it "should return netmask information for HP-UX" do - sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" - hpux_ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("HP-UX") - - Facter::Util::IP.get_interface_value("lan0", "netmask").should == "255.255.255.0" - end - - it "should return calculated network information for HP-UX" do - sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" - hpux_ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.stubs(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("HP-UX") - - Facter::Util::IP.get_network_value("lan0").should == "168.24.80.0" - end - - it "should return interface information for FreeBSD supported via an alias" do - sample_output_file = File.dirname(__FILE__) + "/../data/6.0-STABLE_FreeBSD_ifconfig" - ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_single_interface_output).with("fxp0").returns(ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("FreeBSD") - - Facter::Util::IP.get_interface_value("fxp0", "macaddress").should == "00:0e:0c:68:67:7c" - end - - it "should return macaddress information for OS X" do - sample_output_file = File.dirname(__FILE__) + "/../data/Mac_OS_X_10.5.5_ifconfig" - ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_single_interface_output).with("en1").returns(ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("Darwin") - - Facter::Util::IP.get_interface_value("en1", "macaddress").should == "00:1b:63:ae:02:66" - end - - it "should return all interfaces correctly on OS X" do - sample_output_file = File.dirname(__FILE__) + "/../data/Mac_OS_X_10.5.5_ifconfig" - ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_all_interface_output).returns(ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("Darwin") - - Facter::Util::IP.get_interfaces().should == ["lo0", "gif0", "stf0", "en0", "fw0", "en1", "vmnet8", "vmnet1"] - end - - it "should return a human readable netmask on Solaris" do - sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface" - solaris_ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("SunOS") - - Facter::Util::IP.get_interface_value("e1000g0", "netmask").should == "255.255.255.0" - end - - it "should return a human readable netmask on HP-UX" do - sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" - hpux_ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("HP-UX") - - Facter::Util::IP.get_interface_value("lan0", "netmask").should == "255.255.255.0" - end - - it "should return a human readable netmask on Darwin" do - sample_output_file = File.dirname(__FILE__) + "/../data/darwin_ifconfig_single_interface" - - darwin_ifconfig_interface = File.new(sample_output_file).read() - - Facter::Util::IP.expects(:get_single_interface_output).with("en1").returns(darwin_ifconfig_interface) - Facter.stubs(:value).with(:kernel).returns("Darwin") - - Facter::Util::IP.get_interface_value("en1", "netmask").should == "255.255.255.0" - end - - it "should not get bonding master on interface aliases" do - Facter.stubs(:value).with(:kernel).returns("Linux") - - Facter::Util::IP.get_bonding_master("eth0:1").should be_nil - end - - [:freebsd, :netbsd, :openbsd, :sunos, :darwin, :"hp-ux"].each do |platform| - it "should require conversion from hex on #{platform}" do - Facter::Util::IP.convert_from_hex?(platform).should == true - end - end -end diff --git a/spec/unit/util/ip_spec.rb b/spec/unit/util/ip_spec.rb new file mode 100755 index 0000000..d87b4b9 --- /dev/null +++ b/spec/unit/util/ip_spec.rb @@ -0,0 +1,202 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/ip' + +describe Facter::Util::IP do + [:freebsd, :linux, :netbsd, :openbsd, :sunos, :darwin, :"hp-ux"].each do |platform| + it "should be supported on #{platform}" do + Facter::Util::IP.supported_platforms.should be_include(platform) + end + end + + it "should return a list of interfaces" do + Facter::Util::IP.should respond_to(:get_interfaces) + end + + it "should return an empty list of interfaces on an unknown kernel" do + Facter.stubs(:value).returns("UnknownKernel") + Facter::Util::IP.get_interfaces().should == [] + end + + it "should return a list with a single interface on Linux with a single interface" do + sample_output_file = File.dirname(__FILE__) + '/../data/linux_ifconfig_all_with_single_interface' + linux_ifconfig = File.new(sample_output_file).read() + Facter::Util::IP.stubs(:get_all_interface_output).returns(linux_ifconfig) + Facter::Util::IP.get_interfaces().should == ["eth0"] + end + + it "should return a list two interfaces on Darwin with two interfaces" do + sample_output_file = File.dirname(__FILE__) + '/../data/darwin_ifconfig_all_with_multiple_interfaces' + darwin_ifconfig = File.new(sample_output_file).read() + Facter::Util::IP.stubs(:get_all_interface_output).returns(darwin_ifconfig) + Facter::Util::IP.get_interfaces().should == ["lo0", "en0"] + end + + it "should return a list two interfaces on Solaris with two interfaces multiply reporting" do + sample_output_file = File.dirname(__FILE__) + '/../data/solaris_ifconfig_all_with_multiple_interfaces' + solaris_ifconfig = File.new(sample_output_file).read() + Facter::Util::IP.stubs(:get_all_interface_output).returns(solaris_ifconfig) + Facter::Util::IP.get_interfaces().should == ["lo0", "e1000g0"] + end + + it "should return a list three interfaces on HP-UX with three interfaces multiply reporting" do + sample_output_file = File.dirname(__FILE__) + '/../data/hpux_netstat_all_interfaces' + hpux_netstat = File.new(sample_output_file).read() + Facter::Util::IP.stubs(:get_all_interface_output).returns(hpux_netstat) + Facter::Util::IP.get_interfaces().should == ["lan1", "lan0", "lo0"] + end + + it "should return a value for a specific interface" do + Facter::Util::IP.should respond_to(:get_interface_value) + end + + it "should not return interface information for unsupported platforms" do + Facter.stubs(:value).with(:kernel).returns("bleah") + Facter::Util::IP.get_interface_value("e1000g0", "netmask").should == [] + end + + it "should return ipaddress information for Solaris" do + sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface" + solaris_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("SunOS") + + Facter::Util::IP.get_interface_value("e1000g0", "ipaddress").should == "172.16.15.138" + end + + it "should return netmask information for Solaris" do + sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface" + solaris_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("SunOS") + + Facter::Util::IP.get_interface_value("e1000g0", "netmask").should == "255.255.255.0" + end + + it "should return calculated network information for Solaris" do + sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface" + solaris_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.stubs(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("SunOS") + + Facter::Util::IP.get_network_value("e1000g0").should == "172.16.15.0" + end + + it "should return ipaddress information for HP-UX" do + sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" + hpux_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("HP-UX") + + Facter::Util::IP.get_interface_value("lan0", "ipaddress").should == "168.24.80.71" + end + + it "should return macaddress information for HP-UX" do + sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" + hpux_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("HP-UX") + + Facter::Util::IP.get_interface_value("lan0", "macaddress").should == "00:13:21:BD:9C:B7" + end + + it "should return netmask information for HP-UX" do + sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" + hpux_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("HP-UX") + + Facter::Util::IP.get_interface_value("lan0", "netmask").should == "255.255.255.0" + end + + it "should return calculated network information for HP-UX" do + sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" + hpux_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.stubs(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("HP-UX") + + Facter::Util::IP.get_network_value("lan0").should == "168.24.80.0" + end + + it "should return interface information for FreeBSD supported via an alias" do + sample_output_file = File.dirname(__FILE__) + "/../data/6.0-STABLE_FreeBSD_ifconfig" + ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("fxp0").returns(ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("FreeBSD") + + Facter::Util::IP.get_interface_value("fxp0", "macaddress").should == "00:0e:0c:68:67:7c" + end + + it "should return macaddress information for OS X" do + sample_output_file = File.dirname(__FILE__) + "/../data/Mac_OS_X_10.5.5_ifconfig" + ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("en1").returns(ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("Darwin") + + Facter::Util::IP.get_interface_value("en1", "macaddress").should == "00:1b:63:ae:02:66" + end + + it "should return all interfaces correctly on OS X" do + sample_output_file = File.dirname(__FILE__) + "/../data/Mac_OS_X_10.5.5_ifconfig" + ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_all_interface_output).returns(ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("Darwin") + + Facter::Util::IP.get_interfaces().should == ["lo0", "gif0", "stf0", "en0", "fw0", "en1", "vmnet8", "vmnet1"] + end + + it "should return a human readable netmask on Solaris" do + sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface" + solaris_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("SunOS") + + Facter::Util::IP.get_interface_value("e1000g0", "netmask").should == "255.255.255.0" + end + + it "should return a human readable netmask on HP-UX" do + sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" + hpux_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("HP-UX") + + Facter::Util::IP.get_interface_value("lan0", "netmask").should == "255.255.255.0" + end + + it "should return a human readable netmask on Darwin" do + sample_output_file = File.dirname(__FILE__) + "/../data/darwin_ifconfig_single_interface" + + darwin_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("en1").returns(darwin_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("Darwin") + + Facter::Util::IP.get_interface_value("en1", "netmask").should == "255.255.255.0" + end + + it "should not get bonding master on interface aliases" do + Facter.stubs(:value).with(:kernel).returns("Linux") + + Facter::Util::IP.get_bonding_master("eth0:1").should be_nil + end + + [:freebsd, :netbsd, :openbsd, :sunos, :darwin, :"hp-ux"].each do |platform| + it "should require conversion from hex on #{platform}" do + Facter::Util::IP.convert_from_hex?(platform).should == true + end + end +end diff --git a/spec/unit/util/loader.rb b/spec/unit/util/loader.rb deleted file mode 100755 index 0bb823e..0000000 --- a/spec/unit/util/loader.rb +++ /dev/null @@ -1,229 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/loader' - -describe Facter::Util::Loader do - def with_env(values) - old = {} - values.each do |var, value| - if old_val = ENV[var] - old[var] = old_val - end - ENV[var] = value - end - yield - values.each do |var, value| - if old.include?(var) - ENV[var] = old[var] - else - ENV.delete(var) - end - end - end - - it "should have a method for loading individual facts by name" do - Facter::Util::Loader.new.should respond_to(:load) - end - - it "should have a method for loading all facts" do - Facter::Util::Loader.new.should respond_to(:load_all) - end - - it "should have a method for returning directories containing facts" do - Facter::Util::Loader.new.should respond_to(:search_path) - end - - describe "when determining the search path" do - before do - @loader = Facter::Util::Loader.new - @settings = mock 'settings' - @settings.stubs(:value).returns "/eh" - end - - it "should include the facter subdirectory of all paths in ruby LOAD_PATH" do - dirs = $LOAD_PATH.collect { |d| File.join(d, "facter") } - paths = @loader.search_path - - dirs.each do |dir| - paths.should be_include(dir) - end - end - - it "should include all search paths registered with Facter" do - Facter.expects(:search_path).returns %w{/one /two} - paths = @loader.search_path - paths.should be_include("/one") - paths.should be_include("/two") - end - - describe "and the FACTERLIB environment variable is set" do - it "should include all paths in FACTERLIB" do - with_env "FACTERLIB" => "/one/path:/two/path" do - paths = @loader.search_path - %w{/one/path /two/path}.each do |dir| - paths.should be_include(dir) - end - end - end - end - end - - describe "when loading facts" do - before do - @loader = Facter::Util::Loader.new - @loader.stubs(:search_path).returns [] - end - - it "should load values from the matching environment variable if one is present" do - Facter.expects(:add).with("testing") - - with_env "facter_testing" => "yayness" do - @loader.load(:testing) - end - end - - it "should load any files in the search path with names matching the fact name" do - @loader.expects(:search_path).returns %w{/one/dir /two/dir} - FileTest.stubs(:exist?).returns false - FileTest.expects(:exist?).with("/one/dir/testing.rb").returns true - FileTest.expects(:exist?).with("/two/dir/testing.rb").returns true - - Kernel.expects(:load).with("/one/dir/testing.rb") - Kernel.expects(:load).with("/two/dir/testing.rb") - - @loader.load(:testing) - end - - it "should load any ruby files in directories matching the fact name in the search path" do - @loader.expects(:search_path).returns %w{/one/dir} - FileTest.stubs(:exist?).returns false - FileTest.expects(:directory?).with("/one/dir/testing").returns true - - Dir.expects(:entries).with("/one/dir/testing").returns %w{two.rb} - - Kernel.expects(:load).with("/one/dir/testing/two.rb") - - @loader.load(:testing) - end - - it "should not load files that don't end in '.rb'" do - @loader.expects(:search_path).returns %w{/one/dir} - FileTest.stubs(:exist?).returns false - FileTest.expects(:directory?).with("/one/dir/testing").returns true - - Dir.expects(:entries).with("/one/dir/testing").returns %w{one} - - Kernel.expects(:load).never - - @loader.load(:testing) - end - end - - describe "when loading all facts" do - before do - @loader = Facter::Util::Loader.new - @loader.stubs(:search_path).returns [] - - FileTest.stubs(:directory?).returns true - end - - it "should skip directories that do not exist" do - @loader.expects(:search_path).returns %w{/one/dir} - - FileTest.expects(:directory?).with("/one/dir").returns false - - Dir.expects(:entries).with("/one/dir").never - - @loader.load_all - end - - it "should load all files in all search paths" do - @loader.expects(:search_path).returns %w{/one/dir /two/dir} - - Dir.expects(:entries).with("/one/dir").returns %w{a.rb b.rb} - Dir.expects(:entries).with("/two/dir").returns %w{c.rb d.rb} - - %w{/one/dir/a.rb /one/dir/b.rb /two/dir/c.rb /two/dir/d.rb}.each { |f| Kernel.expects(:load).with(f) } - - @loader.load_all - end - - it "should load all files in all subdirectories in all search paths" do - @loader.expects(:search_path).returns %w{/one/dir /two/dir} - - Dir.expects(:entries).with("/one/dir").returns %w{a} - Dir.expects(:entries).with("/two/dir").returns %w{b} - - %w{/one/dir/a /two/dir/b}.each { |f| File.expects(:directory?).with(f).returns true } - - Dir.expects(:entries).with("/one/dir/a").returns %w{c.rb} - Dir.expects(:entries).with("/two/dir/b").returns %w{d.rb} - - %w{/one/dir/a/c.rb /two/dir/b/d.rb}.each { |f| Kernel.expects(:load).with(f) } - - @loader.load_all - end - - it "should not load files in the util subdirectory" do - @loader.expects(:search_path).returns %w{/one/dir} - - Dir.expects(:entries).with("/one/dir").returns %w{util} - - File.expects(:directory?).with("/one/dir/util").returns true - - Dir.expects(:entries).with("/one/dir/util").never - - @loader.load_all - end - - it "should not load files in a lib subdirectory" do - @loader.expects(:search_path).returns %w{/one/dir} - - Dir.expects(:entries).with("/one/dir").returns %w{lib} - - File.expects(:directory?).with("/one/dir/lib").returns true - - Dir.expects(:entries).with("/one/dir/lib").never - - @loader.load_all - end - - it "should not load files in '.' or '..'" do - @loader.expects(:search_path).returns %w{/one/dir} - - Dir.expects(:entries).with("/one/dir").returns %w{. ..} - - File.expects(:entries).with("/one/dir/.").never - File.expects(:entries).with("/one/dir/..").never - - @loader.load_all - end - - it "should not raise an exception when a file is unloadable" do - @loader.expects(:search_path).returns %w{/one/dir} - Dir.expects(:entries).with("/one/dir").returns %w{a.rb} - - Kernel.expects(:load).with("/one/dir/a.rb").raises(LoadError) - @loader.expects(:warn) - - lambda { @loader.load_all }.should_not raise_error - end - - it "should load all facts from the environment" do - Facter.expects(:add).with('one') - Facter.expects(:add).with('two') - - with_env "facter_one" => "yayness", "facter_two" => "boo" do - @loader.load_all - end - end - - it "should only load all facts one time" do - @loader.expects(:load_env).once - @loader.load_all - @loader.load_all - end - end -end diff --git a/spec/unit/util/loader_spec.rb b/spec/unit/util/loader_spec.rb new file mode 100755 index 0000000..0bb823e --- /dev/null +++ b/spec/unit/util/loader_spec.rb @@ -0,0 +1,229 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/loader' + +describe Facter::Util::Loader do + def with_env(values) + old = {} + values.each do |var, value| + if old_val = ENV[var] + old[var] = old_val + end + ENV[var] = value + end + yield + values.each do |var, value| + if old.include?(var) + ENV[var] = old[var] + else + ENV.delete(var) + end + end + end + + it "should have a method for loading individual facts by name" do + Facter::Util::Loader.new.should respond_to(:load) + end + + it "should have a method for loading all facts" do + Facter::Util::Loader.new.should respond_to(:load_all) + end + + it "should have a method for returning directories containing facts" do + Facter::Util::Loader.new.should respond_to(:search_path) + end + + describe "when determining the search path" do + before do + @loader = Facter::Util::Loader.new + @settings = mock 'settings' + @settings.stubs(:value).returns "/eh" + end + + it "should include the facter subdirectory of all paths in ruby LOAD_PATH" do + dirs = $LOAD_PATH.collect { |d| File.join(d, "facter") } + paths = @loader.search_path + + dirs.each do |dir| + paths.should be_include(dir) + end + end + + it "should include all search paths registered with Facter" do + Facter.expects(:search_path).returns %w{/one /two} + paths = @loader.search_path + paths.should be_include("/one") + paths.should be_include("/two") + end + + describe "and the FACTERLIB environment variable is set" do + it "should include all paths in FACTERLIB" do + with_env "FACTERLIB" => "/one/path:/two/path" do + paths = @loader.search_path + %w{/one/path /two/path}.each do |dir| + paths.should be_include(dir) + end + end + end + end + end + + describe "when loading facts" do + before do + @loader = Facter::Util::Loader.new + @loader.stubs(:search_path).returns [] + end + + it "should load values from the matching environment variable if one is present" do + Facter.expects(:add).with("testing") + + with_env "facter_testing" => "yayness" do + @loader.load(:testing) + end + end + + it "should load any files in the search path with names matching the fact name" do + @loader.expects(:search_path).returns %w{/one/dir /two/dir} + FileTest.stubs(:exist?).returns false + FileTest.expects(:exist?).with("/one/dir/testing.rb").returns true + FileTest.expects(:exist?).with("/two/dir/testing.rb").returns true + + Kernel.expects(:load).with("/one/dir/testing.rb") + Kernel.expects(:load).with("/two/dir/testing.rb") + + @loader.load(:testing) + end + + it "should load any ruby files in directories matching the fact name in the search path" do + @loader.expects(:search_path).returns %w{/one/dir} + FileTest.stubs(:exist?).returns false + FileTest.expects(:directory?).with("/one/dir/testing").returns true + + Dir.expects(:entries).with("/one/dir/testing").returns %w{two.rb} + + Kernel.expects(:load).with("/one/dir/testing/two.rb") + + @loader.load(:testing) + end + + it "should not load files that don't end in '.rb'" do + @loader.expects(:search_path).returns %w{/one/dir} + FileTest.stubs(:exist?).returns false + FileTest.expects(:directory?).with("/one/dir/testing").returns true + + Dir.expects(:entries).with("/one/dir/testing").returns %w{one} + + Kernel.expects(:load).never + + @loader.load(:testing) + end + end + + describe "when loading all facts" do + before do + @loader = Facter::Util::Loader.new + @loader.stubs(:search_path).returns [] + + FileTest.stubs(:directory?).returns true + end + + it "should skip directories that do not exist" do + @loader.expects(:search_path).returns %w{/one/dir} + + FileTest.expects(:directory?).with("/one/dir").returns false + + Dir.expects(:entries).with("/one/dir").never + + @loader.load_all + end + + it "should load all files in all search paths" do + @loader.expects(:search_path).returns %w{/one/dir /two/dir} + + Dir.expects(:entries).with("/one/dir").returns %w{a.rb b.rb} + Dir.expects(:entries).with("/two/dir").returns %w{c.rb d.rb} + + %w{/one/dir/a.rb /one/dir/b.rb /two/dir/c.rb /two/dir/d.rb}.each { |f| Kernel.expects(:load).with(f) } + + @loader.load_all + end + + it "should load all files in all subdirectories in all search paths" do + @loader.expects(:search_path).returns %w{/one/dir /two/dir} + + Dir.expects(:entries).with("/one/dir").returns %w{a} + Dir.expects(:entries).with("/two/dir").returns %w{b} + + %w{/one/dir/a /two/dir/b}.each { |f| File.expects(:directory?).with(f).returns true } + + Dir.expects(:entries).with("/one/dir/a").returns %w{c.rb} + Dir.expects(:entries).with("/two/dir/b").returns %w{d.rb} + + %w{/one/dir/a/c.rb /two/dir/b/d.rb}.each { |f| Kernel.expects(:load).with(f) } + + @loader.load_all + end + + it "should not load files in the util subdirectory" do + @loader.expects(:search_path).returns %w{/one/dir} + + Dir.expects(:entries).with("/one/dir").returns %w{util} + + File.expects(:directory?).with("/one/dir/util").returns true + + Dir.expects(:entries).with("/one/dir/util").never + + @loader.load_all + end + + it "should not load files in a lib subdirectory" do + @loader.expects(:search_path).returns %w{/one/dir} + + Dir.expects(:entries).with("/one/dir").returns %w{lib} + + File.expects(:directory?).with("/one/dir/lib").returns true + + Dir.expects(:entries).with("/one/dir/lib").never + + @loader.load_all + end + + it "should not load files in '.' or '..'" do + @loader.expects(:search_path).returns %w{/one/dir} + + Dir.expects(:entries).with("/one/dir").returns %w{. ..} + + File.expects(:entries).with("/one/dir/.").never + File.expects(:entries).with("/one/dir/..").never + + @loader.load_all + end + + it "should not raise an exception when a file is unloadable" do + @loader.expects(:search_path).returns %w{/one/dir} + Dir.expects(:entries).with("/one/dir").returns %w{a.rb} + + Kernel.expects(:load).with("/one/dir/a.rb").raises(LoadError) + @loader.expects(:warn) + + lambda { @loader.load_all }.should_not raise_error + end + + it "should load all facts from the environment" do + Facter.expects(:add).with('one') + Facter.expects(:add).with('two') + + with_env "facter_one" => "yayness", "facter_two" => "boo" do + @loader.load_all + end + end + + it "should only load all facts one time" do + @loader.expects(:load_env).once + @loader.load_all + @loader.load_all + end + end +end diff --git a/spec/unit/util/macaddress.rb b/spec/unit/util/macaddress.rb deleted file mode 100755 index 98215c4..0000000 --- a/spec/unit/util/macaddress.rb +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/macaddress' - -describe "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}" - - describe "version #{version}" do - - describe Facter::Util::Macaddress::Darwin do - - describe ".default_interface" do - describe "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 - describe "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 - - describe "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/macaddress_spec.rb b/spec/unit/util/macaddress_spec.rb new file mode 100755 index 0000000..98215c4 --- /dev/null +++ b/spec/unit/util/macaddress_spec.rb @@ -0,0 +1,75 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/macaddress' + +describe "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}" + + describe "version #{version}" do + + describe Facter::Util::Macaddress::Darwin do + + describe ".default_interface" do + describe "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 + describe "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 + + describe "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/macosx.rb b/spec/unit/util/macosx.rb deleted file mode 100755 index 44ba460..0000000 --- a/spec/unit/util/macosx.rb +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/macosx' - -describe Facter::Util::Macosx do - it "should be able to retrieve profiler data as xml for a given data field" do - Facter::Util::Resolution.expects(:exec).with("/usr/sbin/system_profiler -xml foo").returns "yay" - Facter::Util::Macosx.profiler_xml("foo").should == "yay" - end - - it "should use PList to convert xml to data structures" do - Plist.expects(:parse_xml).with("foo").returns "bar" - - Facter::Util::Macosx.intern_xml("foo").should == "bar" - end - - describe "when collecting profiler data" do - it "should return the first value in the '_items' hash in the first value of the results of the system_profiler data, with the '_name' field removed, if the profiler returns data" do - @result = [ - '_items' => [ - {'_name' => "foo", "yay" => "bar"} - ] - ] - Facter::Util::Macosx.expects(:profiler_xml).with("foo").returns "eh" - Facter::Util::Macosx.expects(:intern_xml).with("eh").returns @result - Facter::Util::Macosx.profiler_data("foo").should == {"yay" => "bar"} - end - - it "should return nil if an exception is thrown during parsing of xml" do - Facter::Util::Macosx.expects(:profiler_xml).with("foo").returns "eh" - Facter::Util::Macosx.expects(:intern_xml).with("eh").raises "boo!" - Facter::Util::Macosx.profiler_data("foo").should be_nil - end - end - - it "should return the profiler data for 'SPHardwareDataType' as the hardware information" do - Facter::Util::Macosx.expects(:profiler_data).with("SPHardwareDataType").returns "eh" - Facter::Util::Macosx.hardware_overview.should == "eh" - end - - it "should return the profiler data for 'SPSoftwareDataType' as the os information" do - Facter::Util::Macosx.expects(:profiler_data).with("SPSoftwareDataType").returns "eh" - Facter::Util::Macosx.os_overview.should == "eh" - end - - describe "when working out software version" do - - before do - Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productName").returns "Mac OS X" - Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -buildVersion").returns "9J62" - end - - it "should have called sw_vers three times when determining software version" do - Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "10.5.7" - Facter::Util::Macosx.sw_vers - end - - it "should return a hash with the correct keys when determining software version" do - Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "10.5.7" - Facter::Util::Macosx.sw_vers.keys.sort.should == ["macosx_productName", - "macosx_buildVersion", - "macosx_productversion_minor", - "macosx_productversion_major", - "macosx_productVersion"].sort - end - - it "should split a product version of 'x.y.z' into separate hash entries correctly" do - Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "1.2.3" - sw_vers = Facter::Util::Macosx.sw_vers - sw_vers["macosx_productversion_major"].should == "1.2" - sw_vers["macosx_productversion_minor"].should == "3" - end - - it "should treat a product version of 'x.y' as 'x.y.0" do - Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "2.3" - Facter::Util::Macosx.sw_vers["macosx_productversion_minor"].should == "0" - end - end -end diff --git a/spec/unit/util/macosx_spec.rb b/spec/unit/util/macosx_spec.rb new file mode 100755 index 0000000..44ba460 --- /dev/null +++ b/spec/unit/util/macosx_spec.rb @@ -0,0 +1,81 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/macosx' + +describe Facter::Util::Macosx do + it "should be able to retrieve profiler data as xml for a given data field" do + Facter::Util::Resolution.expects(:exec).with("/usr/sbin/system_profiler -xml foo").returns "yay" + Facter::Util::Macosx.profiler_xml("foo").should == "yay" + end + + it "should use PList to convert xml to data structures" do + Plist.expects(:parse_xml).with("foo").returns "bar" + + Facter::Util::Macosx.intern_xml("foo").should == "bar" + end + + describe "when collecting profiler data" do + it "should return the first value in the '_items' hash in the first value of the results of the system_profiler data, with the '_name' field removed, if the profiler returns data" do + @result = [ + '_items' => [ + {'_name' => "foo", "yay" => "bar"} + ] + ] + Facter::Util::Macosx.expects(:profiler_xml).with("foo").returns "eh" + Facter::Util::Macosx.expects(:intern_xml).with("eh").returns @result + Facter::Util::Macosx.profiler_data("foo").should == {"yay" => "bar"} + end + + it "should return nil if an exception is thrown during parsing of xml" do + Facter::Util::Macosx.expects(:profiler_xml).with("foo").returns "eh" + Facter::Util::Macosx.expects(:intern_xml).with("eh").raises "boo!" + Facter::Util::Macosx.profiler_data("foo").should be_nil + end + end + + it "should return the profiler data for 'SPHardwareDataType' as the hardware information" do + Facter::Util::Macosx.expects(:profiler_data).with("SPHardwareDataType").returns "eh" + Facter::Util::Macosx.hardware_overview.should == "eh" + end + + it "should return the profiler data for 'SPSoftwareDataType' as the os information" do + Facter::Util::Macosx.expects(:profiler_data).with("SPSoftwareDataType").returns "eh" + Facter::Util::Macosx.os_overview.should == "eh" + end + + describe "when working out software version" do + + before do + Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productName").returns "Mac OS X" + Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -buildVersion").returns "9J62" + end + + it "should have called sw_vers three times when determining software version" do + Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "10.5.7" + Facter::Util::Macosx.sw_vers + end + + it "should return a hash with the correct keys when determining software version" do + Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "10.5.7" + Facter::Util::Macosx.sw_vers.keys.sort.should == ["macosx_productName", + "macosx_buildVersion", + "macosx_productversion_minor", + "macosx_productversion_major", + "macosx_productVersion"].sort + end + + it "should split a product version of 'x.y.z' into separate hash entries correctly" do + Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "1.2.3" + sw_vers = Facter::Util::Macosx.sw_vers + sw_vers["macosx_productversion_major"].should == "1.2" + sw_vers["macosx_productversion_minor"].should == "3" + end + + it "should treat a product version of 'x.y' as 'x.y.0" do + Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "2.3" + Facter::Util::Macosx.sw_vers["macosx_productversion_minor"].should == "0" + end + end +end diff --git a/spec/unit/util/manufacturer.rb b/spec/unit/util/manufacturer.rb deleted file mode 100644 index 07473db..0000000 --- a/spec/unit/util/manufacturer.rb +++ /dev/null @@ -1,132 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/manufacturer' - -describe Facter::Manufacturer do - before :each do - Facter.clear - end - - it "should return the system DMI table" do - Facter::Manufacturer.should respond_to(:get_dmi_table) - end - - it "should return nil on non-supported operating systems" do - Facter.stubs(:value).with(:kernel).returns("SomeThing") - Facter::Manufacturer.get_dmi_table().should be_nil - end - - it "should parse prtdiag output" do - Facter::Util::Resolution.stubs(:exec).returns("System Configuration: Sun Microsystems sun4u Sun SPARC Enterprise M3000 Server") - Facter::Manufacturer.prtdiag_sparc_find_system_info() - Facter.value(:manufacturer).should == "Sun Microsystems" - Facter.value(:productname).should == "Sun SPARC Enterprise M3000 Server" - end - - it "should strip white space on dmi output with spaces" do - sample_output_file = File.dirname(__FILE__) + "/../data/linux_dmidecode_with_spaces" - dmidecode_output = File.new(sample_output_file).read() - Facter::Manufacturer.expects(:get_dmi_table).returns(dmidecode_output) - Facter.fact(:kernel).stubs(:value).returns("Linux") - - query = { '[Ss]ystem [Ii]nformation' => [ { 'Product(?: Name)?:' => 'productname' } ] } - - Facter::Manufacturer.dmi_find_system_info(query) - Facter.value(:productname).should == "MS-6754" - end - - it "should handle output from smbios when run under sunos" do - sample_output_file = File.dirname(__FILE__) + "/../data/opensolaris_smbios" - smbios_output = File.new(sample_output_file).read() - Facter::Manufacturer.expects(:get_dmi_table).returns(smbios_output) - Facter.fact(:kernel).stubs(:value).returns("SunOS") - - query = { 'BIOS information' => [ { 'Release Date:' => 'reldate' } ] } - - Facter::Manufacturer.dmi_find_system_info(query) - Facter.value(:reldate).should == "12/01/2006" - end - - it "should not split on dmi keys containing the string Handle" do - dmidecode_output = <<-eos -Handle 0x1000, DMI type 16, 15 bytes -Physical Memory Array - Location: System Board Or Motherboard - Use: System Memory - Error Correction Type: None - Maximum Capacity: 4 GB - Error Information Handle: Not Provided - Number Of Devices: 123 - -Handle 0x001F - DMI type 127, 4 bytes. - End Of Table - eos - Facter::Manufacturer.expects(:get_dmi_table).returns(dmidecode_output) - Facter.fact(:kernel).stubs(:value).returns("Linux") - query = { 'Physical Memory Array' => [ { 'Number Of Devices:' => 'ramslots'}]} - Facter::Manufacturer.dmi_find_system_info(query) - Facter.value(:ramslots).should == "123" - end - - it "should match the key in the defined section and not the first one found" do - dmidecode_output = <<-eos -Handle 0x000C, DMI type 7, 19 bytes -Cache Information - Socket Designation: Internal L2 Cache - Configuration: Enabled, Socketed, Level 2 - Operational Mode: Write Back - Location: Internal - Installed Size: 4096 KB - Maximum Size: 4096 KB - Supported SRAM Types: - Burst - Installed SRAM Type: Burst - Speed: Unknown - Error Correction Type: Single-bit ECC - System Type: Unified - Associativity: 8-way Set-associative - -Handle 0x1000, DMI type 16, 15 bytes -Physical Memory Array - Location: System Board Or Motherboard - Use: System Memory - Error Correction Type: None - Maximum Capacity: 4 GB - Error Information Handle: Not Provided - Number Of Devices: 2 - -Handle 0x001F - DMI type 127, 4 bytes. - End Of Table - eos - Facter::Manufacturer.expects(:get_dmi_table).returns(dmidecode_output) - Facter.fact(:kernel).stubs(:value).returns("Linux") - query = { 'Physical Memory Array' => [ { 'Location:' => 'ramlocation'}]} - Facter::Manufacturer.dmi_find_system_info(query) - Facter.value(:ramlocation).should == "System Board Or Motherboard" - end - - def find_product_name(os) - output_file = case os - when "FreeBSD": File.dirname(__FILE__) + "/../data/freebsd_dmidecode" - when "SunOS" : File.dirname(__FILE__) + "/../data/opensolaris_smbios" - end - - output = File.new(output_file).read() - query = { '[Ss]ystem [Ii]nformation' => [ { 'Product(?: Name)?:' => "product_name_#{os}" } ] } - - Facter.fact(:kernel).stubs(:value).returns(os) - Facter::Manufacturer.expects(:get_dmi_table).returns(output) - - Facter::Manufacturer.dmi_find_system_info(query) - - return Facter.value("product_name_#{os}") - end - - it "should return the same result with smbios than with dmidecode" do - find_product_name("FreeBSD").should_not == nil - find_product_name("FreeBSD").should == find_product_name("SunOS") - end - -end diff --git a/spec/unit/util/manufacturer_spec.rb b/spec/unit/util/manufacturer_spec.rb new file mode 100644 index 0000000..07473db --- /dev/null +++ b/spec/unit/util/manufacturer_spec.rb @@ -0,0 +1,132 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/manufacturer' + +describe Facter::Manufacturer do + before :each do + Facter.clear + end + + it "should return the system DMI table" do + Facter::Manufacturer.should respond_to(:get_dmi_table) + end + + it "should return nil on non-supported operating systems" do + Facter.stubs(:value).with(:kernel).returns("SomeThing") + Facter::Manufacturer.get_dmi_table().should be_nil + end + + it "should parse prtdiag output" do + Facter::Util::Resolution.stubs(:exec).returns("System Configuration: Sun Microsystems sun4u Sun SPARC Enterprise M3000 Server") + Facter::Manufacturer.prtdiag_sparc_find_system_info() + Facter.value(:manufacturer).should == "Sun Microsystems" + Facter.value(:productname).should == "Sun SPARC Enterprise M3000 Server" + end + + it "should strip white space on dmi output with spaces" do + sample_output_file = File.dirname(__FILE__) + "/../data/linux_dmidecode_with_spaces" + dmidecode_output = File.new(sample_output_file).read() + Facter::Manufacturer.expects(:get_dmi_table).returns(dmidecode_output) + Facter.fact(:kernel).stubs(:value).returns("Linux") + + query = { '[Ss]ystem [Ii]nformation' => [ { 'Product(?: Name)?:' => 'productname' } ] } + + Facter::Manufacturer.dmi_find_system_info(query) + Facter.value(:productname).should == "MS-6754" + end + + it "should handle output from smbios when run under sunos" do + sample_output_file = File.dirname(__FILE__) + "/../data/opensolaris_smbios" + smbios_output = File.new(sample_output_file).read() + Facter::Manufacturer.expects(:get_dmi_table).returns(smbios_output) + Facter.fact(:kernel).stubs(:value).returns("SunOS") + + query = { 'BIOS information' => [ { 'Release Date:' => 'reldate' } ] } + + Facter::Manufacturer.dmi_find_system_info(query) + Facter.value(:reldate).should == "12/01/2006" + end + + it "should not split on dmi keys containing the string Handle" do + dmidecode_output = <<-eos +Handle 0x1000, DMI type 16, 15 bytes +Physical Memory Array + Location: System Board Or Motherboard + Use: System Memory + Error Correction Type: None + Maximum Capacity: 4 GB + Error Information Handle: Not Provided + Number Of Devices: 123 + +Handle 0x001F + DMI type 127, 4 bytes. + End Of Table + eos + Facter::Manufacturer.expects(:get_dmi_table).returns(dmidecode_output) + Facter.fact(:kernel).stubs(:value).returns("Linux") + query = { 'Physical Memory Array' => [ { 'Number Of Devices:' => 'ramslots'}]} + Facter::Manufacturer.dmi_find_system_info(query) + Facter.value(:ramslots).should == "123" + end + + it "should match the key in the defined section and not the first one found" do + dmidecode_output = <<-eos +Handle 0x000C, DMI type 7, 19 bytes +Cache Information + Socket Designation: Internal L2 Cache + Configuration: Enabled, Socketed, Level 2 + Operational Mode: Write Back + Location: Internal + Installed Size: 4096 KB + Maximum Size: 4096 KB + Supported SRAM Types: + Burst + Installed SRAM Type: Burst + Speed: Unknown + Error Correction Type: Single-bit ECC + System Type: Unified + Associativity: 8-way Set-associative + +Handle 0x1000, DMI type 16, 15 bytes +Physical Memory Array + Location: System Board Or Motherboard + Use: System Memory + Error Correction Type: None + Maximum Capacity: 4 GB + Error Information Handle: Not Provided + Number Of Devices: 2 + +Handle 0x001F + DMI type 127, 4 bytes. + End Of Table + eos + Facter::Manufacturer.expects(:get_dmi_table).returns(dmidecode_output) + Facter.fact(:kernel).stubs(:value).returns("Linux") + query = { 'Physical Memory Array' => [ { 'Location:' => 'ramlocation'}]} + Facter::Manufacturer.dmi_find_system_info(query) + Facter.value(:ramlocation).should == "System Board Or Motherboard" + end + + def find_product_name(os) + output_file = case os + when "FreeBSD": File.dirname(__FILE__) + "/../data/freebsd_dmidecode" + when "SunOS" : File.dirname(__FILE__) + "/../data/opensolaris_smbios" + end + + output = File.new(output_file).read() + query = { '[Ss]ystem [Ii]nformation' => [ { 'Product(?: Name)?:' => "product_name_#{os}" } ] } + + Facter.fact(:kernel).stubs(:value).returns(os) + Facter::Manufacturer.expects(:get_dmi_table).returns(output) + + Facter::Manufacturer.dmi_find_system_info(query) + + return Facter.value("product_name_#{os}") + end + + it "should return the same result with smbios than with dmidecode" do + find_product_name("FreeBSD").should_not == nil + find_product_name("FreeBSD").should == find_product_name("SunOS") + end + +end diff --git a/spec/unit/util/resolution.rb b/spec/unit/util/resolution.rb deleted file mode 100755 index 581d0e1..0000000 --- a/spec/unit/util/resolution.rb +++ /dev/null @@ -1,269 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/resolution' - -describe Facter::Util::Resolution do - it "should require a name" do - lambda { Facter::Util::Resolution.new }.should raise_error(ArgumentError) - end - - it "should have a name" do - Facter::Util::Resolution.new("yay").name.should == "yay" - end - - it "should have a method for setting the code" do - Facter::Util::Resolution.new("yay").should respond_to(:setcode) - end - - it "should support a timeout value" do - Facter::Util::Resolution.new("yay").should respond_to(:timeout=) - end - - it "should default to a timeout of 0 seconds" do - Facter::Util::Resolution.new("yay").limit.should == 0 - end - - it "should default to nil for code" do - Facter::Util::Resolution.new("yay").code.should be_nil - end - - it "should default to nil for interpreter" do - Facter::Util::Resolution.new("yay").interpreter.should be_nil - end - - it "should provide a 'limit' method that returns the timeout" do - res = Facter::Util::Resolution.new("yay") - res.timeout = "testing" - res.limit.should == "testing" - end - - describe "when setting the code" do - before do - @resolve = Facter::Util::Resolution.new("yay") - end - - it "should default to the detected interpreter if a string is provided" do - Facter::Util::Resolution::INTERPRETER = "/bin/bar" - @resolve.setcode "foo" - @resolve.interpreter.should == "/bin/bar" - end - - it "should set the code to any provided string" do - @resolve.setcode "foo" - @resolve.code.should == "foo" - end - - it "should set the code to any provided block" do - block = lambda { } - @resolve.setcode(&block) - @resolve.code.should equal(block) - end - - it "should prefer the string over a block" do - @resolve.setcode("foo") { } - @resolve.code.should == "foo" - end - - it "should fail if neither a string nor block has been provided" do - lambda { @resolve.setcode }.should raise_error(ArgumentError) - end - end - - it "should be able to return a value" do - Facter::Util::Resolution.new("yay").should respond_to(:value) - end - - describe "when returning the value" do - before do - @resolve = Facter::Util::Resolution.new("yay") - end - - describe "and setcode has not been called" do - it "should return nil" do - Facter::Util::Resolution.expects(:exec).with(nil, nil).never - @resolve.value.should be_nil - end - end - - describe "and the code is a string" do - describe "on windows" do - before do - Facter::Util::Resolution::WINDOWS = true - Facter::Util::Resolution::INTERPRETER = "cmd.exe" - end - - it "should return the result of executing the code with the interpreter" do - @resolve.setcode "/bin/foo" - Facter::Util::Resolution.expects(:exec).once.with("/bin/foo", "cmd.exe").returns "yup" - - @resolve.value.should == "yup" - end - - it "should return nil if the value is an empty string" do - @resolve.setcode "/bin/foo" - Facter::Util::Resolution.expects(:exec).once.returns "" - @resolve.value.should be_nil - end - end - - describe "on non-windows systems" do - before do - Facter::Util::Resolution::WINDOWS = false - Facter::Util::Resolution::INTERPRETER = "/bin/sh" - end - - it "should return the result of executing the code with the interpreter" do - @resolve.setcode "/bin/foo" - Facter::Util::Resolution.expects(:exec).once.with("/bin/foo", "/bin/sh").returns "yup" - - @resolve.value.should == "yup" - end - - it "should return nil if the value is an empty string" do - @resolve.setcode "/bin/foo" - Facter::Util::Resolution.expects(:exec).once.returns "" - @resolve.value.should be_nil - end - end - end - - describe "and the code is a block" do - it "should warn but not fail if the code fails" do - @resolve.setcode { raise "feh" } - @resolve.expects(:warn) - @resolve.value.should be_nil - end - - it "should return the value returned by the block" do - @resolve.setcode { "yayness" } - @resolve.value.should == "yayness" - end - - it "should return nil if the value is an empty string" do - @resolve.setcode { "" } - @resolve.value.should be_nil - end - - it "should return nil if the value is an empty block" do - @resolve.setcode { "" } - @resolve.value.should be_nil - end - - it "should use its limit method to determine the timeout, to avoid conflict when a 'timeout' method exists for some other reason" do - @resolve.expects(:timeout).never - @resolve.expects(:limit).returns "foo" - Timeout.expects(:timeout).with("foo") - - @resolve.setcode { sleep 2; "raise This is a test"} - @resolve.value - end - - it "should timeout after the provided timeout" do - @resolve.expects(:warn) - @resolve.timeout = 0.1 - @resolve.setcode { sleep 2; raise "This is a test" } - Thread.expects(:new).yields - - @resolve.value.should be_nil - end - - it "should waitall to avoid zombies if the timeout is exceeded" do - @resolve.stubs(:warn) - @resolve.timeout = 0.1 - @resolve.setcode { sleep 2; raise "This is a test" } - - Thread.expects(:new).yields - Process.expects(:waitall) - - @resolve.value - end - end - end - - it "should return its value when converted to a string" do - @resolve = Facter::Util::Resolution.new("yay") - @resolve.expects(:value).returns "myval" - @resolve.to_s.should == "myval" - end - - it "should allow the adding of confines" do - Facter::Util::Resolution.new("yay").should respond_to(:confine) - end - - it "should provide a method for returning the number of confines" do - @resolve = Facter::Util::Resolution.new("yay") - @resolve.confine "one" => "foo", "two" => "fee" - @resolve.length.should == 2 - end - - it "should return 0 confines when no confines have been added" do - Facter::Util::Resolution.new("yay").length.should == 0 - end - - it "should have a method for determining if it is suitable" do - Facter::Util::Resolution.new("yay").should respond_to(:suitable?) - end - - describe "when adding confines" do - before do - @resolve = Facter::Util::Resolution.new("yay") - end - - it "should accept a hash of fact names and values" do - lambda { @resolve.confine :one => "two" }.should_not raise_error - end - - it "should create a Util::Confine instance for every argument in the provided hash" do - Facter::Util::Confine.expects(:new).with("one", "foo") - Facter::Util::Confine.expects(:new).with("two", "fee") - - @resolve.confine "one" => "foo", "two" => "fee" - end - - end - - describe "when determining suitability" do - before do - @resolve = Facter::Util::Resolution.new("yay") - end - - it "should always be suitable if no confines have been added" do - @resolve.should be_suitable - end - - it "should be unsuitable if any provided confines return false" do - confine1 = mock 'confine1', :true? => true - confine2 = mock 'confine2', :true? => false - Facter::Util::Confine.expects(:new).times(2).returns(confine1).then.returns(confine2) - @resolve.confine :one => :two, :three => :four - - @resolve.should_not be_suitable - end - - it "should be suitable if all provided confines return true" do - confine1 = mock 'confine1', :true? => true - confine2 = mock 'confine2', :true? => true - Facter::Util::Confine.expects(:new).times(2).returns(confine1).then.returns(confine2) - @resolve.confine :one => :two, :three => :four - - @resolve.should be_suitable - end - end - - it "should have a class method for executing code" do - Facter::Util::Resolution.should respond_to(:exec) - end - - # It's not possible, AFAICT, to mock %x{}, so I can't really test this bit. - describe "when executing code" do - it "should fail if any interpreter other than /bin/sh is requested" do - lambda { Facter::Util::Resolution.exec("/something", "/bin/perl") }.should raise_error(ArgumentError) - end - - it "should execute the binary" do - Facter::Util::Resolution.exec("echo foo").should == "foo" - end - end -end diff --git a/spec/unit/util/resolution_spec.rb b/spec/unit/util/resolution_spec.rb new file mode 100755 index 0000000..581d0e1 --- /dev/null +++ b/spec/unit/util/resolution_spec.rb @@ -0,0 +1,269 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/resolution' + +describe Facter::Util::Resolution do + it "should require a name" do + lambda { Facter::Util::Resolution.new }.should raise_error(ArgumentError) + end + + it "should have a name" do + Facter::Util::Resolution.new("yay").name.should == "yay" + end + + it "should have a method for setting the code" do + Facter::Util::Resolution.new("yay").should respond_to(:setcode) + end + + it "should support a timeout value" do + Facter::Util::Resolution.new("yay").should respond_to(:timeout=) + end + + it "should default to a timeout of 0 seconds" do + Facter::Util::Resolution.new("yay").limit.should == 0 + end + + it "should default to nil for code" do + Facter::Util::Resolution.new("yay").code.should be_nil + end + + it "should default to nil for interpreter" do + Facter::Util::Resolution.new("yay").interpreter.should be_nil + end + + it "should provide a 'limit' method that returns the timeout" do + res = Facter::Util::Resolution.new("yay") + res.timeout = "testing" + res.limit.should == "testing" + end + + describe "when setting the code" do + before do + @resolve = Facter::Util::Resolution.new("yay") + end + + it "should default to the detected interpreter if a string is provided" do + Facter::Util::Resolution::INTERPRETER = "/bin/bar" + @resolve.setcode "foo" + @resolve.interpreter.should == "/bin/bar" + end + + it "should set the code to any provided string" do + @resolve.setcode "foo" + @resolve.code.should == "foo" + end + + it "should set the code to any provided block" do + block = lambda { } + @resolve.setcode(&block) + @resolve.code.should equal(block) + end + + it "should prefer the string over a block" do + @resolve.setcode("foo") { } + @resolve.code.should == "foo" + end + + it "should fail if neither a string nor block has been provided" do + lambda { @resolve.setcode }.should raise_error(ArgumentError) + end + end + + it "should be able to return a value" do + Facter::Util::Resolution.new("yay").should respond_to(:value) + end + + describe "when returning the value" do + before do + @resolve = Facter::Util::Resolution.new("yay") + end + + describe "and setcode has not been called" do + it "should return nil" do + Facter::Util::Resolution.expects(:exec).with(nil, nil).never + @resolve.value.should be_nil + end + end + + describe "and the code is a string" do + describe "on windows" do + before do + Facter::Util::Resolution::WINDOWS = true + Facter::Util::Resolution::INTERPRETER = "cmd.exe" + end + + it "should return the result of executing the code with the interpreter" do + @resolve.setcode "/bin/foo" + Facter::Util::Resolution.expects(:exec).once.with("/bin/foo", "cmd.exe").returns "yup" + + @resolve.value.should == "yup" + end + + it "should return nil if the value is an empty string" do + @resolve.setcode "/bin/foo" + Facter::Util::Resolution.expects(:exec).once.returns "" + @resolve.value.should be_nil + end + end + + describe "on non-windows systems" do + before do + Facter::Util::Resolution::WINDOWS = false + Facter::Util::Resolution::INTERPRETER = "/bin/sh" + end + + it "should return the result of executing the code with the interpreter" do + @resolve.setcode "/bin/foo" + Facter::Util::Resolution.expects(:exec).once.with("/bin/foo", "/bin/sh").returns "yup" + + @resolve.value.should == "yup" + end + + it "should return nil if the value is an empty string" do + @resolve.setcode "/bin/foo" + Facter::Util::Resolution.expects(:exec).once.returns "" + @resolve.value.should be_nil + end + end + end + + describe "and the code is a block" do + it "should warn but not fail if the code fails" do + @resolve.setcode { raise "feh" } + @resolve.expects(:warn) + @resolve.value.should be_nil + end + + it "should return the value returned by the block" do + @resolve.setcode { "yayness" } + @resolve.value.should == "yayness" + end + + it "should return nil if the value is an empty string" do + @resolve.setcode { "" } + @resolve.value.should be_nil + end + + it "should return nil if the value is an empty block" do + @resolve.setcode { "" } + @resolve.value.should be_nil + end + + it "should use its limit method to determine the timeout, to avoid conflict when a 'timeout' method exists for some other reason" do + @resolve.expects(:timeout).never + @resolve.expects(:limit).returns "foo" + Timeout.expects(:timeout).with("foo") + + @resolve.setcode { sleep 2; "raise This is a test"} + @resolve.value + end + + it "should timeout after the provided timeout" do + @resolve.expects(:warn) + @resolve.timeout = 0.1 + @resolve.setcode { sleep 2; raise "This is a test" } + Thread.expects(:new).yields + + @resolve.value.should be_nil + end + + it "should waitall to avoid zombies if the timeout is exceeded" do + @resolve.stubs(:warn) + @resolve.timeout = 0.1 + @resolve.setcode { sleep 2; raise "This is a test" } + + Thread.expects(:new).yields + Process.expects(:waitall) + + @resolve.value + end + end + end + + it "should return its value when converted to a string" do + @resolve = Facter::Util::Resolution.new("yay") + @resolve.expects(:value).returns "myval" + @resolve.to_s.should == "myval" + end + + it "should allow the adding of confines" do + Facter::Util::Resolution.new("yay").should respond_to(:confine) + end + + it "should provide a method for returning the number of confines" do + @resolve = Facter::Util::Resolution.new("yay") + @resolve.confine "one" => "foo", "two" => "fee" + @resolve.length.should == 2 + end + + it "should return 0 confines when no confines have been added" do + Facter::Util::Resolution.new("yay").length.should == 0 + end + + it "should have a method for determining if it is suitable" do + Facter::Util::Resolution.new("yay").should respond_to(:suitable?) + end + + describe "when adding confines" do + before do + @resolve = Facter::Util::Resolution.new("yay") + end + + it "should accept a hash of fact names and values" do + lambda { @resolve.confine :one => "two" }.should_not raise_error + end + + it "should create a Util::Confine instance for every argument in the provided hash" do + Facter::Util::Confine.expects(:new).with("one", "foo") + Facter::Util::Confine.expects(:new).with("two", "fee") + + @resolve.confine "one" => "foo", "two" => "fee" + end + + end + + describe "when determining suitability" do + before do + @resolve = Facter::Util::Resolution.new("yay") + end + + it "should always be suitable if no confines have been added" do + @resolve.should be_suitable + end + + it "should be unsuitable if any provided confines return false" do + confine1 = mock 'confine1', :true? => true + confine2 = mock 'confine2', :true? => false + Facter::Util::Confine.expects(:new).times(2).returns(confine1).then.returns(confine2) + @resolve.confine :one => :two, :three => :four + + @resolve.should_not be_suitable + end + + it "should be suitable if all provided confines return true" do + confine1 = mock 'confine1', :true? => true + confine2 = mock 'confine2', :true? => true + Facter::Util::Confine.expects(:new).times(2).returns(confine1).then.returns(confine2) + @resolve.confine :one => :two, :three => :four + + @resolve.should be_suitable + end + end + + it "should have a class method for executing code" do + Facter::Util::Resolution.should respond_to(:exec) + end + + # It's not possible, AFAICT, to mock %x{}, so I can't really test this bit. + describe "when executing code" do + it "should fail if any interpreter other than /bin/sh is requested" do + lambda { Facter::Util::Resolution.exec("/something", "/bin/perl") }.should raise_error(ArgumentError) + end + + it "should execute the binary" do + Facter::Util::Resolution.exec("echo foo").should == "foo" + end + end +end diff --git a/spec/unit/util/uptime.rb b/spec/unit/util/uptime.rb deleted file mode 100755 index 8d3980c..0000000 --- a/spec/unit/util/uptime.rb +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/uptime' - -describe Facter::Util::Uptime do - - describe ".get_uptime_seconds_unix" do - describe "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 - - describe "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 '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 - - describe "nor is 'sysctl kern.boottime'" do - before :each do - Facter::Util::Uptime.stubs(:uptime_sysctl_cmd).returns("cat \"#{@nonexistent_file}\"") - end - - it "should use 'kstat -p unix:::boot_time'" do - kstat_output_file = File.join(SPECDIR, 'fixtures', 'uptime', 'kstat_boot_time') # unix:0:system_misc:boot_time 1236919980 - Facter::Util::Uptime.stubs(:uptime_kstat_cmd).returns("cat \"#{kstat_output_file}\"") - Time.stubs(:now).returns Time.at(1236923580) #one hour later - Facter::Util::Uptime.get_uptime_seconds_unix.should == 60 * 60 - end - - describe "nor is 'kstat -p unix:::boot_time'" do - before :each do - Facter::Util::Uptime.stubs(:uptime_kstat_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 - - describe "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 -end diff --git a/spec/unit/util/uptime_spec.rb b/spec/unit/util/uptime_spec.rb new file mode 100755 index 0000000..8d3980c --- /dev/null +++ b/spec/unit/util/uptime_spec.rb @@ -0,0 +1,73 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/uptime' + +describe Facter::Util::Uptime do + + describe ".get_uptime_seconds_unix" do + describe "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 + + describe "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 '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 + + describe "nor is 'sysctl kern.boottime'" do + before :each do + Facter::Util::Uptime.stubs(:uptime_sysctl_cmd).returns("cat \"#{@nonexistent_file}\"") + end + + it "should use 'kstat -p unix:::boot_time'" do + kstat_output_file = File.join(SPECDIR, 'fixtures', 'uptime', 'kstat_boot_time') # unix:0:system_misc:boot_time 1236919980 + Facter::Util::Uptime.stubs(:uptime_kstat_cmd).returns("cat \"#{kstat_output_file}\"") + Time.stubs(:now).returns Time.at(1236923580) #one hour later + Facter::Util::Uptime.get_uptime_seconds_unix.should == 60 * 60 + end + + describe "nor is 'kstat -p unix:::boot_time'" do + before :each do + Facter::Util::Uptime.stubs(:uptime_kstat_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 + + describe "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 +end diff --git a/spec/unit/util/virtual.rb b/spec/unit/util/virtual.rb deleted file mode 100644 index 12ba0ac..0000000 --- a/spec/unit/util/virtual.rb +++ /dev/null @@ -1,151 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/virtual' - -describe Facter::Util::Virtual do - - after do - Facter.clear - end - it "should detect openvz" do - FileTest.stubs(:directory?).with("/proc/vz").returns(true) - Facter::Util::Virtual.should be_openvz - end - - it "should identify openvzhn when version file exists" do - Facter::Util::Virtual.stubs(:openvz?).returns(true) - FileTest.stubs(:exists?).with("/proc/vz/version").returns(true) - Facter::Util::Virtual.openvz_type().should == "openvzhn" - end - - it "should identify openvzve when no version file exists" do - Facter::Util::Virtual.stubs(:openvz?).returns(true) - FileTest.stubs(:exists?).with("/proc/vz/version").returns(false) - Facter::Util::Virtual.openvz_type().should == "openvzve" - end - - it "should identify Solaris zones when non-global zone" do - Facter::Util::Resolution.stubs(:exec).with("/sbin/zonename").returns("somezone") - Facter::Util::Virtual.should be_zone - end - - it "should not identify Solaris zones when global zone" do - Facter::Util::Resolution.stubs(:exec).with("/sbin/zonename").returns("global") - Facter::Util::Virtual.should_not be_zone - end - - it "should not detect vserver if no self status" do - FileTest.stubs(:exists?).with("/proc/self/status").returns(false) - Facter::Util::Virtual.should_not be_vserver - end - - it "should detect vserver when vxid present in process status" do - FileTest.stubs(:exists?).with("/proc/self/status").returns(true) - File.stubs(:read).with("/proc/self/status").returns("VxID: 42\n") - Facter::Util::Virtual.should be_vserver - end - - it "should detect vserver when s_context present in process status" do - FileTest.stubs(:exists?).with("/proc/self/status").returns(true) - File.stubs(:read).with("/proc/self/status").returns("s_context: 42\n") - Facter::Util::Virtual.should be_vserver - end - - it "should not detect vserver when vserver flags not present in process status" do - FileTest.stubs(:exists?).with("/proc/self/status").returns(true) - File.stubs(:read).with("/proc/self/status").returns("wibble: 42\n") - 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| - describe "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) - Facter::Util::Virtual.vserver_type().should == "vserver_host" - end - - it "should identify vserver_type as vserver when /proc/virtual does not exist" do - Facter::Util::Virtual.expects(:vserver?).returns(true) - FileTest.stubs(:exists?).with("/proc/virtual").returns(false) - Facter::Util::Virtual.vserver_type().should == "vserver" - end - - it "should detect xen when /proc/sys/xen exists" do - FileTest.expects(:exists?).with("/proc/sys/xen").returns(true) - Facter::Util::Virtual.should be_xen - end - - it "should detect xen when /sys/bus/xen exists" do - FileTest.expects(:exists?).with("/proc/sys/xen").returns(false) - FileTest.expects(:exists?).with("/sys/bus/xen").returns(true) - Facter::Util::Virtual.should be_xen - end - - it "should detect xen when /proc/xen exists" do - FileTest.expects(:exists?).with("/proc/sys/xen").returns(false) - FileTest.expects(:exists?).with("/sys/bus/xen").returns(false) - FileTest.expects(:exists?).with("/proc/xen").returns(true) - Facter::Util::Virtual.should be_xen - end - - it "should not detect xen when no sysfs/proc xen directories exist" do - FileTest.expects(:exists?).with("/proc/sys/xen").returns(false) - FileTest.expects(:exists?).with("/sys/bus/xen").returns(false) - FileTest.expects(:exists?).with("/proc/xen").returns(false) - Facter::Util::Virtual.should_not be_xen - end - - it "should detect kvm" do - FileTest.stubs(:exists?).with("/proc/cpuinfo").returns(true) - File.stubs(:read).with("/proc/cpuinfo").returns("model name : QEMU Virtual CPU version 0.9.1\n") - Facter::Util::Virtual.should be_kvm - 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 - end - - it "should identify FreeBSD jail when in jail" do - Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n security.jail.jailed").returns("1") - Facter::Util::Virtual.should be_jail - end - - it "should not identify FreeBSD jail when not in jail" do - Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n security.jail.jailed").returns("0") - Facter::Util::Virtual.should_not be_jail - end - - it "should detect hpvm on HP-UX" do - Facter.fact(:kernel).stubs(:value).returns("HP-UX") - Facter::Util::Resolution.stubs(:exec).with("/usr/bin/getconf MACHINE_MODEL").returns('ia64 hp server Integrity Virtual Machine') - Facter::Util::Virtual.should be_hpvm - end - - it "should not detect hpvm on HP-UX when not in hpvm" do - Facter.fact(:kernel).stubs(:value).returns("HP-UX") - Facter::Util::Resolution.stubs(:exec).with("/usr/bin/getconf MACHINE_MODEL").returns('ia64 hp server rx660') - Facter::Util::Virtual.should_not be_hpvm - end -end diff --git a/spec/unit/util/virtual_spec.rb b/spec/unit/util/virtual_spec.rb new file mode 100644 index 0000000..12ba0ac --- /dev/null +++ b/spec/unit/util/virtual_spec.rb @@ -0,0 +1,151 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/virtual' + +describe Facter::Util::Virtual do + + after do + Facter.clear + end + it "should detect openvz" do + FileTest.stubs(:directory?).with("/proc/vz").returns(true) + Facter::Util::Virtual.should be_openvz + end + + it "should identify openvzhn when version file exists" do + Facter::Util::Virtual.stubs(:openvz?).returns(true) + FileTest.stubs(:exists?).with("/proc/vz/version").returns(true) + Facter::Util::Virtual.openvz_type().should == "openvzhn" + end + + it "should identify openvzve when no version file exists" do + Facter::Util::Virtual.stubs(:openvz?).returns(true) + FileTest.stubs(:exists?).with("/proc/vz/version").returns(false) + Facter::Util::Virtual.openvz_type().should == "openvzve" + end + + it "should identify Solaris zones when non-global zone" do + Facter::Util::Resolution.stubs(:exec).with("/sbin/zonename").returns("somezone") + Facter::Util::Virtual.should be_zone + end + + it "should not identify Solaris zones when global zone" do + Facter::Util::Resolution.stubs(:exec).with("/sbin/zonename").returns("global") + Facter::Util::Virtual.should_not be_zone + end + + it "should not detect vserver if no self status" do + FileTest.stubs(:exists?).with("/proc/self/status").returns(false) + Facter::Util::Virtual.should_not be_vserver + end + + it "should detect vserver when vxid present in process status" do + FileTest.stubs(:exists?).with("/proc/self/status").returns(true) + File.stubs(:read).with("/proc/self/status").returns("VxID: 42\n") + Facter::Util::Virtual.should be_vserver + end + + it "should detect vserver when s_context present in process status" do + FileTest.stubs(:exists?).with("/proc/self/status").returns(true) + File.stubs(:read).with("/proc/self/status").returns("s_context: 42\n") + Facter::Util::Virtual.should be_vserver + end + + it "should not detect vserver when vserver flags not present in process status" do + FileTest.stubs(:exists?).with("/proc/self/status").returns(true) + File.stubs(:read).with("/proc/self/status").returns("wibble: 42\n") + 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| + describe "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) + Facter::Util::Virtual.vserver_type().should == "vserver_host" + end + + it "should identify vserver_type as vserver when /proc/virtual does not exist" do + Facter::Util::Virtual.expects(:vserver?).returns(true) + FileTest.stubs(:exists?).with("/proc/virtual").returns(false) + Facter::Util::Virtual.vserver_type().should == "vserver" + end + + it "should detect xen when /proc/sys/xen exists" do + FileTest.expects(:exists?).with("/proc/sys/xen").returns(true) + Facter::Util::Virtual.should be_xen + end + + it "should detect xen when /sys/bus/xen exists" do + FileTest.expects(:exists?).with("/proc/sys/xen").returns(false) + FileTest.expects(:exists?).with("/sys/bus/xen").returns(true) + Facter::Util::Virtual.should be_xen + end + + it "should detect xen when /proc/xen exists" do + FileTest.expects(:exists?).with("/proc/sys/xen").returns(false) + FileTest.expects(:exists?).with("/sys/bus/xen").returns(false) + FileTest.expects(:exists?).with("/proc/xen").returns(true) + Facter::Util::Virtual.should be_xen + end + + it "should not detect xen when no sysfs/proc xen directories exist" do + FileTest.expects(:exists?).with("/proc/sys/xen").returns(false) + FileTest.expects(:exists?).with("/sys/bus/xen").returns(false) + FileTest.expects(:exists?).with("/proc/xen").returns(false) + Facter::Util::Virtual.should_not be_xen + end + + it "should detect kvm" do + FileTest.stubs(:exists?).with("/proc/cpuinfo").returns(true) + File.stubs(:read).with("/proc/cpuinfo").returns("model name : QEMU Virtual CPU version 0.9.1\n") + Facter::Util::Virtual.should be_kvm + 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 + end + + it "should identify FreeBSD jail when in jail" do + Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n security.jail.jailed").returns("1") + Facter::Util::Virtual.should be_jail + end + + it "should not identify FreeBSD jail when not in jail" do + Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n security.jail.jailed").returns("0") + Facter::Util::Virtual.should_not be_jail + end + + it "should detect hpvm on HP-UX" do + Facter.fact(:kernel).stubs(:value).returns("HP-UX") + Facter::Util::Resolution.stubs(:exec).with("/usr/bin/getconf MACHINE_MODEL").returns('ia64 hp server Integrity Virtual Machine') + Facter::Util::Virtual.should be_hpvm + end + + it "should not detect hpvm on HP-UX when not in hpvm" do + Facter.fact(:kernel).stubs(:value).returns("HP-UX") + Facter::Util::Resolution.stubs(:exec).with("/usr/bin/getconf MACHINE_MODEL").returns('ia64 hp server rx660') + Facter::Util::Virtual.should_not be_hpvm + end +end diff --git a/spec/unit/util/vlans.rb b/spec/unit/util/vlans.rb deleted file mode 100755 index 0331234..0000000 --- a/spec/unit/util/vlans.rb +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/vlans' - -describe Facter::Util::Vlans do - it "should return a list of vlans on Linux" do - sample_output_file = File.dirname(__FILE__) + '/../data/linux_vlan_config' - linux_vlanconfig = File.new(sample_output_file).read(); - Facter::Util::Vlans.stubs(:get_vlan_config).returns(linux_vlanconfig) - Facter::Util::Vlans.get_vlans().should == %{400,300,200,100} - end -end diff --git a/spec/unit/util/vlans_spec.rb b/spec/unit/util/vlans_spec.rb new file mode 100755 index 0000000..0331234 --- /dev/null +++ b/spec/unit/util/vlans_spec.rb @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/vlans' + +describe Facter::Util::Vlans do + it "should return a list of vlans on Linux" do + sample_output_file = File.dirname(__FILE__) + '/../data/linux_vlan_config' + linux_vlanconfig = File.new(sample_output_file).read(); + Facter::Util::Vlans.stubs(:get_vlan_config).returns(linux_vlanconfig) + Facter::Util::Vlans.get_vlans().should == %{400,300,200,100} + end +end diff --git a/spec/unit/util/xendomains.rb b/spec/unit/util/xendomains.rb deleted file mode 100755 index dc7e178..0000000 --- a/spec/unit/util/xendomains.rb +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -require 'facter/util/xendomains' - -describe Facter::Util::Xendomains do - describe ".get_domains" do - it "should return a list of running Xen Domains on Xen0" do - sample_output_file = File.dirname(__FILE__) + '/../data/xendomains' - xen0_domains = File.read(sample_output_file) - Facter::Util::Resolution.stubs(:exec).with('/usr/sbin/xm list').returns(xen0_domains) - Facter::Util::Xendomains.get_domains.should == %{web01,mailserver} - end - - describe "when xm list isn't executable" do - it "should be nil" do - Facter::Util::Resolution.stubs(:exec).with('/usr/sbin/xm list').returns(nil) - Facter::Util::Xendomains.get_domains.should == nil - end - end - end -end diff --git a/spec/unit/util/xendomains_spec.rb b/spec/unit/util/xendomains_spec.rb new file mode 100755 index 0000000..dc7e178 --- /dev/null +++ b/spec/unit/util/xendomains_spec.rb @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +require 'facter/util/xendomains' + +describe Facter::Util::Xendomains do + describe ".get_domains" do + it "should return a list of running Xen Domains on Xen0" do + sample_output_file = File.dirname(__FILE__) + '/../data/xendomains' + xen0_domains = File.read(sample_output_file) + Facter::Util::Resolution.stubs(:exec).with('/usr/sbin/xm list').returns(xen0_domains) + Facter::Util::Xendomains.get_domains.should == %{web01,mailserver} + end + + describe "when xm list isn't executable" do + it "should be nil" do + Facter::Util::Resolution.stubs(:exec).with('/usr/sbin/xm list').returns(nil) + Facter::Util::Xendomains.get_domains.should == nil + end + end + end +end diff --git a/spec/unit/virtual.rb b/spec/unit/virtual.rb deleted file mode 100644 index a152b40..0000000 --- a/spec/unit/virtual.rb +++ /dev/null @@ -1,185 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -require 'facter' -require 'facter/util/virtual' -require 'facter/util/macosx' - -describe "Virtual fact" do - - after do - Facter.clear - end - - it "should be zone on Solaris when a zone" do - Facter.fact(:kernel).stubs(:value).returns("SunOS") - Facter::Util::Virtual.stubs(:zone?).returns(true) - Facter::Util::Virtual.stubs(:vserver?).returns(false) - Facter::Util::Virtual.stubs(:xen?).returns(false) - Facter.fact(:virtual).value.should == "zone" - end - - it "should be jail on FreeBSD when a jail in kvm" do - Facter.fact(:kernel).stubs(:value).returns("FreeBSD") - Facter::Util::Virtual.stubs(:jail?).returns(true) - Facter::Util::Virtual.stubs(:kvm?).returns(true) - Facter.fact(:virtual).value.should == "jail" - end - - it "should be hpvm on HP-UX when in HP-VM" do - Facter.fact(:kernel).stubs(:value).returns("HP-UX") - Facter::Util::Virtual.stubs(:hpvm?).returns(true) - Facter.fact(:virtual).value.should == "hpvm" - end - - describe "on Darwin" do - it "should be parallels with Parallels vendor id" do - Facter.fact(:kernel).stubs(:value).returns("Darwin") - Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor-id" => "0x1ab8" }) - Facter.fact(:virtual).value.should == "parallels" - end - - it "should be parallels with Parallels vendor name" do - Facter.fact(:kernel).stubs(:value).returns("Darwin") - Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor" => "Parallels" }) - Facter.fact(:virtual).value.should == "parallels" - end - - it "should be vmware with VMWare vendor id" do - Facter.fact(:kernel).stubs(:value).returns("Darwin") - Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor-id" => "0x15ad" }) - Facter.fact(:virtual).value.should == "vmware" - end - - it "should be vmware with VMWare vendor name" do - Facter.fact(:kernel).stubs(:value).returns("Darwin") - Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor" => "VMWare" }) - Facter.fact(:virtual).value.should == "vmware" - end - end - - describe "on Linux" do - before do - Facter::Util::Virtual.stubs(:zone?).returns(false) - Facter::Util::Virtual.stubs(:openvz?).returns(false) - Facter::Util::Virtual.stubs(:vserver?).returns(false) - Facter::Util::Virtual.stubs(:xen?).returns(false) - Facter::Util::Virtual.stubs(:kvm?).returns(false) - end - - it "should be parallels with Parallels vendor id from lspci" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter::Util::Resolution.stubs(:exec).with('lspci').returns("01:00.0 VGA compatible controller: Unknown device 1ab8:4005") - Facter.fact(:virtual).value.should == "parallels" - end - - it "should be parallels with Parallels vendor name from lspci" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter::Util::Resolution.stubs(:exec).with('lspci').returns("01:00.0 VGA compatible controller: Parallels Display Adapter") - Facter.fact(:virtual).value.should == "parallels" - end - - it "should be vmware with VMware vendor name from lspci" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter::Util::Resolution.stubs(:exec).with('lspci').returns("00:0f.0 VGA compatible controller: VMware Inc [VMware SVGA II] PCI Display Adapter") - Facter.fact(:virtual).value.should == "vmware" - end - - it "should be vmware with VMWare vendor name from dmidecode" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) - Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns("On Board Device 1 Information\nType: Video\nStatus: Disabled\nDescription: VMware SVGA II") - Facter.fact(:virtual).value.should == "vmware" - end - - it "should be parallels with Parallels vendor name from dmidecode" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) - Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns("On Board Device Information\nType: Video\nStatus: Disabled\nDescription: Parallels Video Adapter") - Facter.fact(:virtual).value.should == "parallels" - end - - it "should be vmware with VMWare vendor name from prtdiag" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) - Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns(nil) - Facter::Util::Resolution.stubs(:exec).with('prtdiag').returns("System Configuration: VMware, Inc. VMware Virtual Platform") - Facter.fact(:virtual).value.should == "vmware" - end - - it "should be parallels with Parallels vendor name from prtdiag" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) - Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns(nil) - Facter::Util::Resolution.stubs(:exec).with('prtdiag').returns("System Configuration: Parallels Virtual Platform") - Facter.fact(:virtual).value.should == "parallels" - end - end - -end - -describe "is_virtual fact" do - - after do - Facter.clear - end - - 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" - 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" - end - - it "should be false when running on physical" do - Facter.fact(:kernel).stubs(:value).returns("Linux") - Facter.fact(:virtual).stubs(:value).returns("physical") - 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" - 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" - 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" - 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" - 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" - end - - it "should be true when running on parallels" do - Facter.fact(:kernel).stubs(:value).returns("Darwin") - Facter.fact(:virtual).stubs(:value).returns("parallels") - Facter.fact(:is_virtual).value.should == "true" - end -end diff --git a/spec/unit/virtual_spec.rb b/spec/unit/virtual_spec.rb new file mode 100644 index 0000000..a152b40 --- /dev/null +++ b/spec/unit/virtual_spec.rb @@ -0,0 +1,185 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +require 'facter' +require 'facter/util/virtual' +require 'facter/util/macosx' + +describe "Virtual fact" do + + after do + Facter.clear + end + + it "should be zone on Solaris when a zone" do + Facter.fact(:kernel).stubs(:value).returns("SunOS") + Facter::Util::Virtual.stubs(:zone?).returns(true) + Facter::Util::Virtual.stubs(:vserver?).returns(false) + Facter::Util::Virtual.stubs(:xen?).returns(false) + Facter.fact(:virtual).value.should == "zone" + end + + it "should be jail on FreeBSD when a jail in kvm" do + Facter.fact(:kernel).stubs(:value).returns("FreeBSD") + Facter::Util::Virtual.stubs(:jail?).returns(true) + Facter::Util::Virtual.stubs(:kvm?).returns(true) + Facter.fact(:virtual).value.should == "jail" + end + + it "should be hpvm on HP-UX when in HP-VM" do + Facter.fact(:kernel).stubs(:value).returns("HP-UX") + Facter::Util::Virtual.stubs(:hpvm?).returns(true) + Facter.fact(:virtual).value.should == "hpvm" + end + + describe "on Darwin" do + it "should be parallels with Parallels vendor id" do + Facter.fact(:kernel).stubs(:value).returns("Darwin") + Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor-id" => "0x1ab8" }) + Facter.fact(:virtual).value.should == "parallels" + end + + it "should be parallels with Parallels vendor name" do + Facter.fact(:kernel).stubs(:value).returns("Darwin") + Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor" => "Parallels" }) + Facter.fact(:virtual).value.should == "parallels" + end + + it "should be vmware with VMWare vendor id" do + Facter.fact(:kernel).stubs(:value).returns("Darwin") + Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor-id" => "0x15ad" }) + Facter.fact(:virtual).value.should == "vmware" + end + + it "should be vmware with VMWare vendor name" do + Facter.fact(:kernel).stubs(:value).returns("Darwin") + Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor" => "VMWare" }) + Facter.fact(:virtual).value.should == "vmware" + end + end + + describe "on Linux" do + before do + Facter::Util::Virtual.stubs(:zone?).returns(false) + Facter::Util::Virtual.stubs(:openvz?).returns(false) + Facter::Util::Virtual.stubs(:vserver?).returns(false) + Facter::Util::Virtual.stubs(:xen?).returns(false) + Facter::Util::Virtual.stubs(:kvm?).returns(false) + end + + it "should be parallels with Parallels vendor id from lspci" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns("01:00.0 VGA compatible controller: Unknown device 1ab8:4005") + Facter.fact(:virtual).value.should == "parallels" + end + + it "should be parallels with Parallels vendor name from lspci" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns("01:00.0 VGA compatible controller: Parallels Display Adapter") + Facter.fact(:virtual).value.should == "parallels" + end + + it "should be vmware with VMware vendor name from lspci" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns("00:0f.0 VGA compatible controller: VMware Inc [VMware SVGA II] PCI Display Adapter") + Facter.fact(:virtual).value.should == "vmware" + end + + it "should be vmware with VMWare vendor name from dmidecode" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns("On Board Device 1 Information\nType: Video\nStatus: Disabled\nDescription: VMware SVGA II") + Facter.fact(:virtual).value.should == "vmware" + end + + it "should be parallels with Parallels vendor name from dmidecode" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns("On Board Device Information\nType: Video\nStatus: Disabled\nDescription: Parallels Video Adapter") + Facter.fact(:virtual).value.should == "parallels" + end + + it "should be vmware with VMWare vendor name from prtdiag" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('prtdiag').returns("System Configuration: VMware, Inc. VMware Virtual Platform") + Facter.fact(:virtual).value.should == "vmware" + end + + it "should be parallels with Parallels vendor name from prtdiag" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('prtdiag').returns("System Configuration: Parallels Virtual Platform") + Facter.fact(:virtual).value.should == "parallels" + end + end + +end + +describe "is_virtual fact" do + + after do + Facter.clear + end + + 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" + 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" + end + + it "should be false when running on physical" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter.fact(:virtual).stubs(:value).returns("physical") + 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" + 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" + 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" + 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" + 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" + end + + it "should be true when running on parallels" do + Facter.fact(:kernel).stubs(:value).returns("Darwin") + Facter.fact(:virtual).stubs(:value).returns("parallels") + Facter.fact(:is_virtual).value.should == "true" + end +end -- cgit