[Pkg-puppet-devel] [facter] 31/61: (#16668) Use WMI to collect macaddress fact

Stig Sandbeck Mathisen ssm at debian.org
Mon Nov 4 15:01:55 UTC 2013


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

ssm pushed a commit to branch master
in repository facter.

commit 0ddf0012a69d1b624aa1d9e0a2fa3a8ab2b67211
Author: Josh Cooper <josh at puppetlabs.com>
Date:   Mon Jul 29 15:57:13 2013 -0700

    (#16668) Use WMI to collect macaddress fact
    
    Previously, facter would report on the first macaddress associated
    with any interface that had IP enabled. This could easily be an
    interface with a high cost metric, or an IPv6 only interface.
    
    This commit refactors the macaddress fact to use the same ordered list
    of network adapters as is done for ipaddress, to ensure we return a
    consistent set of facts.
---
 lib/facter/util/macaddress.rb     |   12 ++----
 spec/unit/util/macaddress_spec.rb |   76 ++++++++++++++++++++++++++++++++-----
 2 files changed, 69 insertions(+), 19 deletions(-)

diff --git a/lib/facter/util/macaddress.rb b/lib/facter/util/macaddress.rb
index 82470ec..0309971 100644
--- a/lib/facter/util/macaddress.rb
+++ b/lib/facter/util/macaddress.rb
@@ -33,16 +33,10 @@ module Facter::Util::Macaddress
 
   module Windows
     def macaddress
-      require 'facter/util/wmi'
+      require 'facter/util/ip/windows'
 
-      query = "select MACAddress from Win32_NetworkAdapterConfiguration where IPEnabled = True"
-
-      ether = nil
-      Facter::Util::WMI.execquery(query).each do |nic|
-        ether = nic.MacAddress
-        break
-      end
-      ether
+      adapter = Facter::Util::IP::Windows.get_preferred_network_adapters.first
+      adapter ? adapter.MACAddress : nil
     end
     module_function :macaddress
   end
diff --git a/spec/unit/util/macaddress_spec.rb b/spec/unit/util/macaddress_spec.rb
index c16d7a5..863ed11 100755
--- a/spec/unit/util/macaddress_spec.rb
+++ b/spec/unit/util/macaddress_spec.rb
@@ -81,19 +81,75 @@ describe "Darwin", :unless => Facter::Util::Config.is_windows? do
   end
 end
 
-describe "Windows" do
-  it "should return the first macaddress" do
-    Facter.fact(:kernel).stubs(:value).returns("windows")
+describe "The macaddress fact" do
+  context "on Windows" do
+    require 'facter/util/wmi'
+    require 'facter/util/registry'
 
-    nic = stubs 'nic'
-    nic.stubs(:MacAddress).returns("00:0C:29:0C:9E:9F")
+    let(:settingId0) { '{4AE6B55C-6DD6-427D-A5BB-13535D4BE926}' }
+    let(:settingId1) { '{38762816-7957-42AC-8DAA-3B08D0C857C7}' }
+    let(:nic_bindings) { ["\\Device\\#{settingId0}", "\\Device\\#{settingId1}" ] }
 
-    nic2 = stubs 'nic'
-    nic2.stubs(:MacAddress).returns("00:0C:29:0C:9E:AF")
+    before :each do
+      Facter.fact(:kernel).stubs(:value).returns(:windows)
+      Facter::Util::Registry.stubs(:hklm_read).returns(nic_bindings)
+    end
 
-    require 'facter/util/wmi'
-    Facter::Util::WMI.stubs(:execquery).with("select MACAddress from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
+    describe "when you have no active network adapter" do
+      it "should return nil if there are no active (or any) network adapters" do
+        Facter::Util::WMI.expects(:execquery).returns([])
+
+        Facter.value(:macaddress).should == nil
+      end
+    end
+
+    describe "when you have one network adapter" do
+      it "should return properly" do
+        network1 = mock('network1')
+        network1.expects(:MACAddress).returns("00:0C:29:0C:9E:9F")
+        Facter::Util::WMI.expects(:execquery).returns([network1])
+
+        Facter.value(:macaddress).should == "00:0C:29:0C:9E:9F"
+      end
+    end
 
-    Facter.fact(:macaddress).value.should == "00:0C:29:0C:9E:9F"
+    describe "when you have more than one network adapter" do
+      it "should return the macaddress of the adapter with the lowest IP connection metric (best connection)" do
+        network1 = mock('network1')
+        network1.expects(:IPConnectionMetric).returns(10)
+        network2 = mock('network2')
+        network2.expects(:MACAddress).returns("00:0C:29:0C:9E:AF")
+        network2.expects(:IPConnectionMetric).returns(5)
+        Facter::Util::WMI.expects(:execquery).returns([network1, network2])
+
+        Facter.value(:macaddress).should == "00:0C:29:0C:9E:AF"
+      end
+
+      context "when the IP connection metric is the same" do
+        it "should return the macaddress of the adapter with the lowest binding order" do
+          network1 = mock('network1', :MACAddress => "23:24:df:12:12:00")
+          network1.expects(:SettingID).returns(settingId0)
+          network1.expects(:IPConnectionMetric).returns(5)
+          network2 = mock('network2')
+          network2.expects(:SettingID).returns(settingId1)
+          network2.expects(:IPConnectionMetric).returns(5)
+          Facter::Util::WMI.expects(:execquery).returns([network1, network2])
+
+          Facter.value(:macaddress).should == "23:24:df:12:12:00"
+        end
+
+        it "should return the macaddress of the adapter with the lowest MACAddress when multiple adapters have the same IP connection metric when the lowest MACAddress is not first" do
+          network1 = stub('network1', :MACAddress => "23:24:df:12:12:00")
+          network1.expects(:SettingID).returns(settingId1)
+          network1.expects(:IPConnectionMetric).returns(5)
+          network2 = stub('network2', :MACAddress => "23:24:df:12:12:11")
+          network2.expects(:SettingID).returns(settingId0)
+          network2.expects(:IPConnectionMetric).returns(5)
+          Facter::Util::WMI.expects(:execquery).returns([network1, network2])
+
+          Facter.value(:macaddress).should == "23:24:df:12:12:11"
+        end
+      end
+    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