[Pkg-puppet-devel] [facter] 31/352: (maint) refactor processor specs, test sysfs fallback

Stig Sandbeck Mathisen ssm at debian.org
Sun Apr 6 22:21:28 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 3849295841085e37298a53acc6565e1c07cc1ddc
Author: Adrien Thebo <git at somethingsinistral.net>
Date:   Wed May 22 10:52:52 2013 -0700

    (maint) refactor processor specs, test sysfs fallback
    
    Conflicts:
    	spec/unit/processor_spec.rb
---
 spec/unit/processor_spec.rb | 204 +++++++++++++++++++++++---------------------
 1 file changed, 109 insertions(+), 95 deletions(-)

diff --git a/spec/unit/processor_spec.rb b/spec/unit/processor_spec.rb
index 2585ffb..200afbf 100755
--- a/spec/unit/processor_spec.rb
+++ b/spec/unit/processor_spec.rb
@@ -1,8 +1,8 @@
 #! /usr/bin/env ruby
 
 require 'facter/util/processor'
-require 'facter_spec/cpuinfo'
 require 'spec_helper'
+require 'facter_spec/cpuinfo'
 
 describe "Processor facts" do
   describe "on Windows" do
@@ -65,102 +65,110 @@ describe "Processor facts" do
     end
   end
 
-  describe "on Unixes" do
+  describe "on Linux" do
     include FacterSpec::Cpuinfo
 
-    before :each do
-      Facter.collection.internal_loader.load(:processor)
+    shared_context 'Linux processor stubs' do
+      before :each do
+        Facter.collection.internal_loader.load(:processor)
+
+        Facter.fact(:kernel).stubs(:value).returns 'Linux'
+        Facter.fact(:operatingsystem).stubs(:value).returns 'Linux'
+        File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
+      end
     end
 
-    it "should be 1 in SPARC fixture" do
-      Facter.fact(:kernel).stubs(:value).returns("Linux")
-      Facter.fact(:operatingsystem).stubs(:value).returns("Linux")
-      Facter.fact(:architecture).stubs(:value).returns("sparc")
-      File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
-      File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("sparc"))
+    shared_examples_for 'a /proc/cpuinfo based processor fact' do |processor_fact|
+      include_context 'Linux processor stubs'
 
-      Facter.fact(:processorcount).value.should == "1"
-    end
+      it "should be 1 in SPARC fixture" do
+        Facter.fact(:architecture).stubs(:value).returns("sparc")
+        File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("sparc"))
 
-    it "should be 2 in ppc64 fixture on Linux" do
-      Facter.fact(:kernel).stubs(:value).returns("Linux")
-      Facter.fact(:architecture).stubs(:value).returns("ppc64")
-      File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
-      File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("ppc64"))
+        Facter.fact(processor_fact).value.should == "1"
+      end
 
-      Facter.fact(:processorcount).value.should == "2"
-    end
+      it "should be 2 in ppc64 fixture on Linux" do
+        Facter.fact(:architecture).stubs(:value).returns("ppc64")
+        File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("ppc64"))
 
-    it "should be 2 in panda-armel fixture on Linux" do
-      Facter.fact(:kernel).stubs(:value).returns("Linux")
-      Facter.fact(:architecture).stubs(:value).returns("arm")
-      File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
-      File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("panda-armel"))
+        Facter.fact(processor_fact).value.should == "2"
+      end
 
-      Facter.fact(:processorcount).value.should == "2"
-    end
+      it "should be 2 in panda-armel fixture on Linux" do
+        Facter.fact(:architecture).stubs(:value).returns("arm")
+        File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("panda-armel"))
 
-    it "should be 1 in bbg3-armel fixture on Linux" do
-      Facter.fact(:kernel).stubs(:value).returns("Linux")
-      Facter.fact(:architecture).stubs(:value).returns("arm")
-      File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
-      File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("bbg3-armel"))
+        Facter.fact(processor_fact).value.should == "2"
+      end
 
-      Facter.fact(:processorcount).value.should == "1"
-    end
+      it "should be 1 in bbg3-armel fixture on Linux" do
+        Facter.fact(:architecture).stubs(:value).returns("arm")
+        File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("bbg3-armel"))
 
-    it "should be 1 in beaglexm-armel fixture on Linux" do
-      Facter.fact(:kernel).stubs(:value).returns("Linux")
-      Facter.fact(:architecture).stubs(:value).returns("arm")
-      File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
-      File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("beaglexm-armel"))
+        Facter.fact(processor_fact).value.should == "1"
+      end
 
-      Facter.fact(:processorcount).value.should == "1"
-    end
+      it "should be 1 in beaglexm-armel fixture on Linux" do
+        Facter.fact(:architecture).stubs(:value).returns("arm")
+        File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("beaglexm-armel"))
 
-    it "should be 1 in amd64solo fixture on Linux" do
-      Facter.fact(:kernel).stubs(:value).returns("Linux")
-      Facter.fact(:architecture).stubs(:value).returns("amd64")
-      File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
-      File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("amd64solo"))
+        Facter.fact(processor_fact).value.should == "1"
+      end
 
-      Facter.fact(:processorcount).value.should == "1"
-    end
+      it "should be 1 in amd64solo fixture on Linux" do
+        Facter.fact(:architecture).stubs(:value).returns("amd64")
+        File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("amd64solo"))
 
-    it "should be 2 in amd64dual fixture on Linux" do
-      Facter.fact(:kernel).stubs(:value).returns("Linux")
-      Facter.fact(:architecture).stubs(:value).returns("amd64")
-      File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
-      File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("amd64dual"))
+        Facter.fact(processor_fact).value.should == "1"
+      end
 
-      Facter.fact(:processorcount).value.should == "2"
-    end
+      it "should be 2 in amd64dual fixture on Linux" do
+        Facter.fact(:architecture).stubs(:value).returns("amd64")
+        File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("amd64dual"))
 
-    it "should be 3 in amd64tri fixture on Linux" do
-      Facter.fact(:kernel).stubs(:value).returns("Linux")
-      Facter.fact(:architecture).stubs(:value).returns("amd64")
-      File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
-      File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("amd64tri"))
+        Facter.fact(processor_fact).value.should == "2"
+      end
+
+      it "should be 3 in amd64tri fixture on Linux" do
+        Facter.fact(:architecture).stubs(:value).returns("amd64")
+        File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("amd64tri"))
+
+        Facter.fact(processor_fact).value.should == "3"
+      end
 
-      Facter.fact(:processorcount).value.should == "3"
+      it "should be 4 in amd64quad fixture on Linux" do
+        Facter.fact(:architecture).stubs(:value).returns("amd64")
+        File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("amd64quad"))
+
+        Facter.fact(processor_fact).value.should == "4"
+      end
     end
 
-    it "should be 4 in amd64quad fixture on Linux" do
-      Facter.fact(:kernel).stubs(:value).returns("Linux")
-      Facter.fact(:architecture).stubs(:value).returns("amd64")
-      File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
-      File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("amd64quad"))
+    it_behaves_like 'a /proc/cpuinfo based processor fact', :processorcount
 
-      Facter.fact(:processorcount).value.should == "4"
+    describe 'when /proc/cpuinfo returns 0 processors (#2945)' do
+      include_context 'Linux processor stubs'
+      before do
+        File.stubs(:readlines).with("/proc/cpuinfo").returns([])
+      end
+
+      it 'enumerates sysfs for the processorcount' do
+        File.stubs(:exists?).with('/sys/devices/system/cpu').returns(true)
+        Dir.stubs(:glob).with("/sys/devices/system/cpu/cpu[0-9]*").returns(%w{
+          /sys/devices/system/cpu/cpu0
+          /sys/devices/system/cpu/cpu1
+        })
+
+        Facter.fact(:processorcount).value.should == '2'
+      end
     end
+  end
 
-    it "should be 4 in amd64quad fixture on Linux" do
-      Facter.fact(:kernel).stubs(:value).returns("Linux")
-      Facter.fact(:architecture).stubs(:value).returns("amd64")
-      File.stubs(:exists?).with("/proc/cpuinfo").returns(true)
-      File.stubs(:readlines).with("/proc/cpuinfo").returns(cpuinfo_fixture_readlines("amd64twentyfour"))
 
-      Facter.fact(:processorcount).value.should == "24"
+  describe "on Unixes" do
+    before :each do
+      Facter.collection.internal_loader.load(:processor)
     end
 
     it "should be 2 on dual-processor Darwin box" do
@@ -240,37 +248,43 @@ describe "Processor facts" do
 
       Facter.fact(:processorcount).value.should == "16"
     end
+  end
 
-    describe "on solaris" do
-      before :all do
-        @fixture_kstat_sparc  = File.read(fixtures('processorcount','solaris-sparc-kstat-cpu-info'))
-        @fixture_kstat_x86_64 = File.read(fixtures('processorcount','solaris-x86_64-kstat-cpu-info'))
-      end
+  describe "on solaris" do
+    before :each do
+      Facter::Util::Processor.stubs(:kernel_fact_value).returns :sunos
+      Facter.fact(:kernel).stubs(:value).returns(:sunos)
+      Facter.collection.internal_loader.load(:processor)
+    end
 
-      let(:kstat_sparc) { @fixture_kstat_sparc }
-      let(:kstat_x86_64) { @fixture_kstat_x86_64 }
+    before :all do
+      @fixture_kstat_sparc  = File.read(fixtures('processorcount','solaris-sparc-kstat-cpu-info'))
+      @fixture_kstat_x86_64 = File.read(fixtures('processorcount','solaris-x86_64-kstat-cpu-info'))
+    end
 
-      %w{ 5.5.1 5.6 5.7 }.each do |release|
-        %w{ sparc x86_64 }.each do |arch|
-          it "uses kstat on release #{release} (#{arch})" do
-            Facter.fact(:kernel).stubs(:value).returns(:sunos)
-            Facter.stubs(:value).with(:kernelrelease).returns(release)
+    let(:kstat_sparc) { @fixture_kstat_sparc }
+    let(:kstat_x86_64) { @fixture_kstat_x86_64 }
 
-            Facter::Util::Resolution.expects(:exec).with("/usr/bin/kstat cpu_info").returns(self.send("kstat_#{arch}".intern))
-            Facter.fact(:processorcount).value.should == 8
-          end
+    %w{ 5.8 5.9 5.10 5.11 }.each do |release|
+      %w{ sparc x86_64 }.each do |arch|
+        it "uses kstat on release #{release} (#{arch})" do
+          Facter.stubs(:value).with(:kernelrelease).returns(release)
+
+          Facter::Util::Resolution.expects(:exec).with("/usr/sbin/psrinfo").never
+          Facter::Util::Resolution.expects(:exec).with("/usr/bin/kstat cpu_info").returns(self.send("kstat_#{arch}".intern))
+          Facter.fact(:processorcount).value.should == 8
         end
       end
+    end
 
-      %w{ 5.8 5.9 5.10 5.11 }.each do |release|
-        it "uses psrinfo on release #{release}" do
-          Facter.fact(:kernel).stubs(:value).returns(:sunos)
-          Facter.stubs(:value).with(:kernelrelease).returns(release)
+    %w{ 5.5.1 5.6 5.7 }.each do |release|
+      it "uses psrinfo on release #{release}" do
+        Facter.stubs(:value).with(:kernelrelease).returns(release)
 
-          fixture_data = File.read(fixtures('processorcount','solaris-psrinfo'))
-          Facter::Util::Resolution.expects(:exec).with("/usr/sbin/psrinfo").returns(fixture_data)
-          Facter.fact(:processorcount).value.should == 24
-        end
+        fixture_data = File.read(fixtures('processorcount','solaris-psrinfo'))
+        Facter::Util::Resolution.expects(:exec).with("/usr/bin/kstat cpu_info").never
+        Facter::Util::Resolution.expects(:exec).with("/usr/sbin/psrinfo").returns(fixture_data)
+        Facter.fact(:processorcount).value.should == 24
       end
     end
   end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list