Permalink
Browse files

Merge branch 'ticket/master/12864_windows_primary_dns_suffix'

* ticket/master/12864_windows_primary_dns_suffix:
  (#12864) Windows: get primary DNS from registry
  • Loading branch information...
2 parents 51dcf4c + 5aa2a6f commit 1bd98fd94b5e577216b80789ee9f933dd5cc74f8 @kbarber kbarber committed May 3, 2012
Showing with 122 additions and 14 deletions.
  1. +10 −5 lib/facter/domain.rb
  2. +9 −0 lib/facter/util/registry.rb
  3. +29 −9 spec/unit/domain_spec.rb
  4. +74 −0 spec/unit/util/registry_spec.rb
View
@@ -52,12 +52,17 @@
Facter.add(:domain) do
confine :kernel => :windows
setcode do
- require 'facter/util/wmi'
+ require 'facter/util/registry'
domain = ""
- Facter::Util::WMI.execquery("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").each { |nic|
- domain = nic.DNSDomain
- break
- }
+ regvalue = Facter::Util::Registry.hklm_read('SYSTEM\CurrentControlSet\Services\Tcpip\Parameters', 'Domain')
+ domain = regvalue if regvalue
+ if domain == ""
+ require 'facter/util/wmi'
+ Facter::Util::WMI.execquery("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").each { |nic|
+ domain = nic.DNSDomain
+ break
+ }
+ end
domain
end
end
@@ -0,0 +1,9 @@
+module Facter::Util::Registry
+ class << self
+ def hklm_read(key, value)
+ require 'win32/registry'
+ reg = Win32::Registry::HKEY_LOCAL_MACHINE.open(key)
@joshcooper

joshcooper May 14, 2012

Member

@jeffweiss We should explicitly close the key, or use the block for open to read the value:

open(key) do |reg|
  reg[value]
end
@jeffweiss

jeffweiss May 14, 2012

Contributor

@joshcooper putting the reg[value] inside the block makes it a bit harder to mock for the tests.

I'll explicitly close the key.

+ reg[value]
+ end
+ end
+end
View
@@ -98,19 +98,39 @@
end
describe "on Windows" do
- it "should use the DNSDomain for the first nic where ip is enabled" do
+ before(:each) do
Facter.fact(:kernel).stubs(:value).returns("windows")
+ require 'facter/util/registry'
+ end
+ describe "with primary dns suffix" do
+ before(:each) do
+ Facter::Util::Registry.stubs(:hklm_read).returns('baz.com')
+ end
+ it "should get the primary dns suffix" do
+ Facter.fact(:domain).value.should == 'baz.com'
+ end
+ it "should not execute the wmi query" do
+ require 'facter/util/wmi'
+ Facter::Util::WMI.expects(:execquery).never
+ Facter.fact(:domain).value
+ end
+ end
+ describe "without primary dns suffix" do
+ before(:each) do
+ Facter::Util::Registry.stubs(:hklm_read).returns('')
+ end
+ it "should use the DNSDomain for the first nic where ip is enabled" do
+ nic = stubs 'nic'
+ nic.stubs(:DNSDomain).returns("foo.com")
- nic = stubs 'nic'
- nic.stubs(:DNSDomain).returns("foo.com")
-
- nic2 = stubs 'nic'
- nic2.stubs(:DNSDomain).returns("bar.com")
+ nic2 = stubs 'nic'
+ nic2.stubs(:DNSDomain).returns("bar.com")
- require 'facter/util/wmi'
- Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
+ require 'facter/util/wmi'
+ Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
- Facter.fact(:domain).value.should == 'foo.com'
+ Facter.fact(:domain).value.should == 'foo.com'
+ end
end
end
end
@@ -0,0 +1,74 @@
+#!/usr/bin/env rspec
+require 'spec_helper'
+require 'facter/operatingsystem'
+require 'facter/util/registry'
+
+describe Facter::Util::Registry do
+ describe "hklm_read", :if => Facter::Util::Config.is_windows? do
+ before(:all) do
+ require 'win32/registry'
+ end
+ describe "valid params" do
+ [ {:key => "valid_key", :value => "valid_value", :expected => "valid"},
+ {:key => "valid_key", :value => "", :expected => "valid"},
+ {:key => "valid_key", :value => nil, :expected => "invalid"},
+ {:key => "", :value => "valid_value", :expected => "valid"},
+ {:key => "", :value => "", :expected => "valid"},
+ {:key => "", :value => nil, :expected => "invalid"},
+ {:key => nil, :value => "valid_value", :expected => "invalid"},
+ {:key => nil, :value => "", :expected => "invalid"},
+ {:key => nil, :value => nil, :expected => "invalid"}
+ ].each do |scenario|
+ describe "with key #{scenario[:key] || "nil"} and value #{scenario[:value] || "nil"}" do
+ let :fake_registry_key do
+ fake = {}
+ fake[scenario[:value]] = scenario[:expected]
+ fake
+ end
+ it "should return #{scenario[:expected]} value" do
+ Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).returns(fake_registry_key)
+
+ Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value]).should == scenario[:expected]
+ end
+ end
+ end
+ end
+
+ describe "invalid params" do
+ [ {:key => "valid_key", :value => "invalid_value"},
+ {:key => "valid_key", :value => ""},
+ {:key => "valid_key", :value => nil},
+ ].each do |scenario|
+ describe "with valid key and value #{scenario[:value] || "nil"}" do
+ let :fake_registry_key do
+ {}
+ end
+ it "should raise an error" do
+ Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).returns(fake_registry_key)
+
+ Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value]).should raise_error
+ end
+ end
+ end
+ [ {:key => "invalid_key", :value => "valid_value"},
+ {:key => "invalid_key", :value => ""},
+ {:key => "invalid_key", :value => nil},
+ {:key => "", :value => "valid_value"},
+ {:key => "", :value => ""},
+ {:key => "", :value => nil},
+ {:key => nil, :value => "valid_value"},
+ {:key => nil, :value => ""},
+ {:key => nil, :value => nil}
+ ].each do |scenario|
+ describe "with invalid key #{scenario[:key] || "nil"} and value #{scenario[:value] || "nil"}" do
+ it "should raise an error" do
+ Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).raises(Win32::Registry::Error, 2)
+ expect do
+ Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value])
+ end.to raise_error Win32::Registry::Error
+ end
+ end
+ end
+ end
+ end
+end

0 comments on commit 1bd98fd

Please sign in to comment.