[Pkg-puppet-devel] [facter] 30/61: (#16668) Use WMI to collect netmask 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 8ab8b985953b0f9654e5bd86c3a02648d9828e24
Author: Rob Reynolds <rob at puppetlabs.com>
Date:   Wed Jul 24 17:01:27 2013 -0700

    (#16668) Use WMI to collect netmask fact
    
    Previously, Windows hosts didn't report the netmask fact.
---
 lib/facter/netmask.rb     |   19 ++++++++++
 spec/unit/netmask_spec.rb |   84 ++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 98 insertions(+), 5 deletions(-)

diff --git a/lib/facter/netmask.rb b/lib/facter/netmask.rb
index 710ebe4..9f749b3 100644
--- a/lib/facter/netmask.rb
+++ b/lib/facter/netmask.rb
@@ -21,3 +21,22 @@ Facter.add("netmask") do
     Facter::NetMask.get_netmask
   end
 end
+
+Facter.add(:netmask) do
+  confine :kernel => :windows
+  setcode do
+    require 'facter/util/ip/windows'
+
+    mask = nil
+
+    adapters = Facter::Util::IP::Windows.get_preferred_network_adapters
+    adapters.find do |nic|
+      nic.IPSubnet.any? do |subnet|
+        mask = subnet if Facter::Util::IP::Windows.valid_ipv4_address?(subnet)
+        mask
+      end
+    end
+
+    mask
+  end
+end
diff --git a/spec/unit/netmask_spec.rb b/spec/unit/netmask_spec.rb
index ce77eef..25896b3 100644
--- a/spec/unit/netmask_spec.rb
+++ b/spec/unit/netmask_spec.rb
@@ -13,12 +13,12 @@ shared_examples_for "netmask from ifconfig output" do |platform, address, fixtur
   end
 end
 
-describe "netmask fact" do
-  before :each do
-    Facter.fact(:kernel).stubs(:value).returns("Linux")
-  end
-
+describe "The netmask fact" do
   context "on Linux" do
+    before :each do
+      Facter.fact(:kernel).stubs(:value).returns("Linux")
+    end
+
     example_behavior_for "netmask from ifconfig output",
       "Archlinux (net-tools 1.60)", "255.255.255.0",
       "ifconfig_net_tools_1.60.txt"
@@ -26,4 +26,78 @@ describe "netmask fact" do
       "Ubuntu 12.04", "255.255.255.255",
       "ifconfig_ubuntu_1204.txt"
   end
+
+  context "on Windows" do
+    require 'facter/util/wmi'
+    require 'facter/util/registry'
+
+    let(:settingId0) { '{4AE6B55C-6DD6-427D-A5BB-13535D4BE926}' }
+    let(:settingId1) { '{38762816-7957-42AC-8DAA-3B08D0C857C7}' }
+    let(:nic_bindings) { ["\\Device\\#{settingId0}", "\\Device\\#{settingId1}" ] }
+
+    before :each do
+      Facter.fact(:kernel).stubs(:value).returns(:windows)
+      Facter::Util::Registry.stubs(:hklm_read).returns(nic_bindings)
+    end
+
+    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(:netmask).should == nil
+      end
+    end
+
+    describe "when you have one network adapter" do
+      it "should return properly" do
+        network1 = mock('network1')
+        network1.expects(:IPSubnet).returns(["255.255.255.0", "48","2"])
+        Facter::Util::WMI.expects(:execquery).returns([network1])
+
+        Facter.value(:netmask).should == "255.255.255.0"
+      end
+    end
+
+    describe "when you have more than one network adapter" do
+      it "should return the netmask 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(:IPConnectionMetric).returns(5)
+        network2.expects(:IPSubnet).returns(["255.255.0.0", "48","2"])
+        Facter::Util::WMI.expects(:execquery).returns([network1, network2])
+
+        Facter.value(:netmask).should == "255.255.0.0"
+      end
+
+      context "when the IP connection metric is the same" do
+        it "should return the netmask of the adapter with the lowest binding order" do
+          network1 = mock('network1')
+          network1.expects(:SettingID).returns(settingId0)
+          network1.expects(:IPConnectionMetric).returns(5)
+          network1.expects(:IPSubnet).returns(["255.255.255.0", "48","64"])
+          network2 = mock('network2')
+          network2.expects(:SettingID).returns(settingId1)
+          network2.expects(:IPConnectionMetric).returns(5)
+
+          Facter::Util::WMI.expects(:execquery).returns([network1, network2])
+
+          Facter.value(:netmask).should =="255.255.255.0"
+        end
+
+        it "should return the netmask of the adapter with the lowest binding even if the adapter is not first" do
+          network1 = mock('network1')
+          network1.expects(:SettingID).returns(settingId1)
+          network1.expects(:IPConnectionMetric).returns(5)
+          network2 = mock('network2')
+          network2.expects(:SettingID).returns(settingId0)
+          network2.expects(:IPConnectionMetric).returns(5)
+          network2.expects(:IPSubnet).returns(["255.255.0.0", "48","2"])
+          Facter::Util::WMI.expects(:execquery).returns([network1, network2])
+
+          Facter.value(:netmask).should =="255.255.0.0"
+        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