[Pkg-puppet-devel] [SCM] Packaging of Facter for debian branch, experimental_upstream, updated. 1.5.9-54-g5a4eeed

Adrien Thebo adrien.thebo at gmail.com
Wed Jun 22 08:51:48 UTC 2011


The following commit has been merged in the experimental_upstream branch:
commit c2ff8330f4d705ded3f251e170df4bd973af3d43
Author: Adrien Thebo <adrien at puppetlabs.com>
Date:   Tue Mar 15 17:29:19 2011 -0700

    (#5135) Refactored physicalprocessorcount
    
     - Refactored code to make testing simpler

diff --git a/lib/facter/physicalprocessorcount.rb b/lib/facter/physicalprocessorcount.rb
index 144183c..cd5777c 100644
--- a/lib/facter/physicalprocessorcount.rb
+++ b/lib/facter/physicalprocessorcount.rb
@@ -39,9 +39,11 @@ Facter.add('physicalprocessorcount') do
       lookup_pattern = "#{sysfs_cpu_directory}" +
         "/cpu*/topology/physical_package_id"
 
-      Facter::Util::Resolution.exec(
-        "cat #{lookup_pattern}"
-      ).scan(/\d+/).uniq.size
+      ids = Dir.glob(lookup_pattern).collect { |f| Facter::Util::Resolution.exec("cat #{f}")}
+
+      ids = ids.join if ids.is_a?(Array)
+      ids.scan(/\d+/).uniq.size
+
     else
       #
       # Try to count number of CPUs using the proc file system next ...
@@ -49,9 +51,9 @@ Facter.add('physicalprocessorcount') do
       # We assume that /proc/cpuinfo has what we need and is so then we need
       # to make sure that we only count unique entries ...
       #
-      Facter::Util::Resolution.exec(
-        "grep 'physical.\\+:' /proc/cpuinfo"
-      ).scan(/\d+/).uniq.size
+      str = Facter::Util::Resolution.exec("grep 'physical.\\+:' /proc/cpuinfo")
+
+      if not str.nil? then str.scan(/\d+/).uniq.size; end
     end
   end
 end
diff --git a/spec/unit/physicalprocessorcount_spec.rb b/spec/unit/physicalprocessorcount_spec.rb
new file mode 100644
index 0000000..260788b
--- /dev/null
+++ b/spec/unit/physicalprocessorcount_spec.rb
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+require 'facter'
+
+describe "Physical processor count facts" do
+    before do
+        Facter.loadfacts
+    end
+    before do
+        Facter.clear
+    end
+    it "should return one physical CPU" do
+        Facter.fact(:kernel).stubs(:value).returns("Linux")
+        File.stubs(:exists?).with('/sys/devices/system/cpu').returns(true)
+        Dir.stubs(:glob).with("/sys/devices/system/cpu/cpu*/topology/physical_package_id").returns("/sys/devices/system/cpu/cpu0/topology/physical_package_id")
+        Facter::Util::Resolution.stubs(:exec).with("cat /sys/devices/system/cpu/cpu0/topology/physical_package_id").returns("0")
+
+        Facter.fact(:physicalprocessorcount).value.should == 1
+    end
+
+    it "should return four physical CPUs" do
+        Facter.fact(:kernel).stubs(:value).returns("Linux")
+        File.stubs(:exists?).with('/sys/devices/system/cpu').returns(true)
+        Dir.stubs(:glob).with("/sys/devices/system/cpu/cpu*/topology/physical_package_id").returns(%w{
+          /sys/devices/system/cpu/cpu0/topology/physical_package_id
+          /sys/devices/system/cpu/cpu1/topology/physical_package_id
+          /sys/devices/system/cpu/cpu2/topology/physical_package_id
+          /sys/devices/system/cpu/cpu3/topology/physical_package_id
+        })
+
+        Facter::Util::Resolution.stubs(:exec).with("cat /sys/devices/system/cpu/cpu0/topology/physical_package_id").returns("0")
+        Facter::Util::Resolution.stubs(:exec).with("cat /sys/devices/system/cpu/cpu1/topology/physical_package_id").returns("1")
+        Facter::Util::Resolution.stubs(:exec).with("cat /sys/devices/system/cpu/cpu2/topology/physical_package_id").returns("2")
+        Facter::Util::Resolution.stubs(:exec).with("cat /sys/devices/system/cpu/cpu3/topology/physical_package_id").returns("3")
+
+        Facter.fact(:physicalprocessorcount).value.should == 4
+    end
+end

-- 
Packaging of Facter for debian



More information about the Pkg-puppet-devel mailing list