diff options
author | Claire McQuin <claire@getchef.com> | 2015-09-17 09:47:08 -0700 |
---|---|---|
committer | Kartik Null Cating-Subramanian <ksubramanian@chef.io> | 2015-09-18 13:18:23 -0400 |
commit | 9703782bad3ddfad90b21ca5a225d4bd2e393f0d (patch) | |
tree | fff6a404d4be3764e5f50c95a1160b85dc452acc | |
parent | 2cadf9cd40e4c19741121bf2df180941918107bb (diff) | |
download | chef-9703782bad3ddfad90b21ca5a225d4bd2e393f0d.tar.gz |
Don't add win_evt logger when on nano.
-rw-r--r-- | lib/chef/config.rb | 3 | ||||
-rw-r--r-- | lib/chef/platform/query_helpers.rb | 24 | ||||
-rw-r--r-- | spec/unit/platform/query_helpers_spec.rb | 75 |
3 files changed, 93 insertions, 9 deletions
diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 6382af14c2..a43985f691 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -55,7 +55,8 @@ class Chef default :event_loggers do evt_loggers = [] - if ChefConfig.windows? and not Chef::Platform.windows_server_2003? + if ChefConfig.windows? && !(Chef::Platform.windows_server_2003? || + Chef::Platform.windows_nano_server?) evt_loggers << :win_evt end evt_loggers diff --git a/lib/chef/platform/query_helpers.rb b/lib/chef/platform/query_helpers.rb index 2dd33ea190..9ba8d2261d 100644 --- a/lib/chef/platform/query_helpers.rb +++ b/lib/chef/platform/query_helpers.rb @@ -36,6 +36,28 @@ class Chef is_server_2003 end + def windows_nano_server? + return false unless windows? + require 'win32/registry' + + # This method may be called before ohai runs (e.g., it may be used to + # determine settings in config.rb). Chef::Win32::Registry.new uses + # node attributes to verify the machine architecture which aren't + # accessible before ohai runs. + nano = nil + key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Server\\ServerLevels" + access = ::Win32::Registry::KEY_QUERY_VALUE | 0x0100 # nano is 64-bit only + begin + ::Win32::Registry::HKEY_LOCAL_MACHINE.open(key, access) do |reg| + nano = reg["NanoServer"] + end + rescue ::Win32::Registry::Error + # If accessing the registry key failed, then we're probably not on + # nano. Fail through. + end + return nano == 1 + end + def supports_powershell_execution_bypass?(node) node[:languages] && node[:languages][:powershell] && node[:languages][:powershell][:version].to_i >= 3 @@ -52,7 +74,7 @@ class Chef Gem::Version.new(node[:languages][:powershell][:version]) >= Gem::Version.new("5.0.10018.0") end - + def dsc_refresh_mode_disabled?(node) require 'chef/util/powershell/cmdlet' cmdlet = Chef::Util::Powershell::Cmdlet.new(node, "Get-DscLocalConfigurationManager", :object) diff --git a/spec/unit/platform/query_helpers_spec.rb b/spec/unit/platform/query_helpers_spec.rb index f33bfa128b..2843f80c60 100644 --- a/spec/unit/platform/query_helpers_spec.rb +++ b/spec/unit/platform/query_helpers_spec.rb @@ -21,7 +21,7 @@ require 'spec_helper' describe "Chef::Platform#windows_server_2003?" do it "returns false early when not on windows" do allow(ChefConfig).to receive(:windows?).and_return(false) - expect(Chef::Platform).not_to receive(:require) + expect(Chef::Platform).not_to receive(:require) expect(Chef::Platform.windows_server_2003?).to be_falsey end @@ -31,7 +31,69 @@ describe "Chef::Platform#windows_server_2003?" do end end -describe 'Chef::Platform#supports_dsc?' do +describe "Chef::Platform#windows_nano_server?" do + let(:key) { "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Server\\ServerLevels" } + let(:access) { 0x0001 | 0x0100 } + let(:hive) { double("Win32::Registry::HKEY_LOCAL_MACHINE") } + let(:registry) { double("Win32::Registry") } + + before do + Object.send(:remove_const, 'Win32') if defined?(Win32) + Win32 = Module.new + Win32::Registry = Class.new + Win32::Registry::Error = Class.new(RuntimeError) + Win32::Registry::HKEY_LOCAL_MACHINE = hive + Win32::Registry::KEY_QUERY_VALUE = 0x0001 + end + + it "returns false early when not on windows" do + allow(ChefConfig).to receive(:windows?).and_return(false) + expect(Chef::Platform).to_not receive(:require) + expect(Chef::Platform.windows_nano_server?).to be false + end + + it "returns true when the registry value is 1" do + allow(ChefConfig).to receive(:windows?).and_return(true) + allow(Chef::Platform).to receive(:require).with('win32/registry') + expect(Win32::Registry::HKEY_LOCAL_MACHINE).to receive(:open). + with(key, access). + and_yield(registry) + expect(registry).to receive(:[]).with("NanoServer").and_return(1) + expect(Chef::Platform.windows_nano_server?).to be true + end + + it "returns false when the registry value is not 1" do + allow(ChefConfig).to receive(:windows?).and_return(true) + allow(Chef::Platform).to receive(:require).with('win32/registry') + expect(Win32::Registry::HKEY_LOCAL_MACHINE).to receive(:open). + with(key, access). + and_yield(registry) + expect(registry).to receive(:[]).with("NanoServer").and_return(0) + expect(Chef::Platform.windows_nano_server?).to be false + end + + it "returns false when the registry value does not exist" do + allow(ChefConfig).to receive(:windows?).and_return(true) + allow(Chef::Platform).to receive(:require).with('win32/registry') + expect(Win32::Registry::HKEY_LOCAL_MACHINE).to receive(:open). + with(key, access). + and_yield(registry) + expect(registry).to receive(:[]).with("NanoServer"). + and_raise(Win32::Registry::Error, "The system cannot find the file specified.") + expect(Chef::Platform.windows_nano_server?).to be false + end + + it "returns false when the registry key does not exist" do + allow(ChefConfig).to receive(:windows?).and_return(true) + allow(Chef::Platform).to receive(:require).with('win32/registry') + expect(Win32::Registry::HKEY_LOCAL_MACHINE).to receive(:open). + with(key, access). + and_raise(Win32::Registry::Error, "The system cannot find the file specified.") + expect(Chef::Platform.windows_nano_server?).to be false + end +end + +describe 'Chef::Platform#supports_dsc?' do it 'returns false if powershell is not present' do node = Chef::Node.new expect(Chef::Platform.supports_dsc?(node)).to be_falsey @@ -54,7 +116,7 @@ describe 'Chef::Platform#supports_dsc?' do end end -describe 'Chef::Platform#supports_dsc_invoke_resource?' do +describe 'Chef::Platform#supports_dsc_invoke_resource?' do it 'returns false if powershell is not present' do node = Chef::Node.new expect(Chef::Platform.supports_dsc_invoke_resource?(node)).to be_falsey @@ -79,7 +141,7 @@ describe 'Chef::Platform#dsc_refresh_mode_disabled?' do let(:node) { instance_double('Chef::Node') } let(:cmdlet) { instance_double('Chef::Util::Powershell::Cmdlet') } let(:cmdlet_result) { instance_double('Chef::Util::Powershell::CmdletResult')} - + it "returns true when RefreshMode is Disabled" do expect(Chef::Util::Powershell::Cmdlet).to receive(:new). with(node, "Get-DscLocalConfigurationManager", :object). @@ -88,14 +150,13 @@ describe 'Chef::Platform#dsc_refresh_mode_disabled?' do expect(cmdlet_result).to receive(:return_value).and_return({ 'RefreshMode' => 'Disabled' }) expect(Chef::Platform.dsc_refresh_mode_disabled?(node)).to be true end - + it "returns false when RefreshMode is not Disabled" do expect(Chef::Util::Powershell::Cmdlet).to receive(:new). with(node, "Get-DscLocalConfigurationManager", :object). and_return(cmdlet) expect(cmdlet).to receive(:run!).and_return(cmdlet_result) expect(cmdlet_result).to receive(:return_value).and_return({ 'RefreshMode' => 'LaLaLa' }) - expect(Chef::Platform.dsc_refresh_mode_disabled?(node)).to be false + expect(Chef::Platform.dsc_refresh_mode_disabled?(node)).to be false end end - |