Mock Chef::ReservedNames::Win32::Version.new in Ch

2019-07-25 02:37发布

问题:

Assuming, I have the following recipe:

install_iis:

require 'chef/win32/version'
windows_version = Chef::ReservedNames::Win32::Version.new

node.set['iis']['components'] = [
  'IIS-HttpErrors',
  'IIS-HttpRedirect',
  'IIS-HttpLogging',
  'IIS-LoggingLibraries',
  'IIS-RequestMonitor',
  'WAS-WindowsActivationService',
  'WAS-ProcessModel',
  'IIS-StaticContent',
  'IIS-DefaultDocument',
  'IIS-DirectoryBrowsing'
]

include_recipe 'iis::default'
include_recipe 'iis::mod_aspnet'
include_recipe 'iis::mod_auth_basic'
include_recipe 'iis::mod_auth_windows'
include_recipe 'iis::mod_compress_static'
include_recipe 'iis::mod_security'
include_recipe 'iis::mod_management'

if windows_version.windows_server_2012_r2?
  include_recipe 'iis::mod_aspnet45'
  include_recipe 'iis::mod_tracing'
  include_recipe 'iis::mod_application_initialization'
end

I want to be able to test, using chefspec, if the include_recipe methods are running (everything in the if block).


After reading:

Standard RSpec applies so allow(Chef::ReservedNames::Win32::Version).to receive(:new).and_return(double('fake version')) or similar.

Source: Mock Chef::ReservedNames::Win32::Version.new in Chef unit/rspec test?

I've attempted to modified my install_iis_spec to mock the Chef::ReservedNames::Win32::Version. My spec file now looks like the following:

install_iis_spec:

recipes_2012 = [
  'iis::mod_aspnet45',
  'iis::mod_tracing',
  'iis::mod_application_initialization'
]

context 'when on "Windows Server 2012 R2"' do
  before do
    allow(Chef::ReservedNames::Win32::Version).to receive(:new).and_return(double('6.3'))
  end

  let(:chef_run) do
    runner = ChefSpec::SoloRunner.new(platform: 'windows', version: '2012R2')
    runner.converge(described_recipe)
  end

  should_include_recipe(recipes_2012)

  it 'converges successfully' do
    expect { chef_run }.to_not raise_error
  end
end

Note1: Assume that the should_include_recipe method works as intended.

Note2: After seeing double('fake version'), I assume I should be putting the value '6.3'.

Although, when I run chef exec rspec spec/unit/recipes/install_iis_spec.rb I get the following error:

Console Error:

my_recipe::install_iis when on "Windows Server 2012 R2" runs the 'iis::mod_aspnet45' recipe
  Failure/Error: runner.converge(described_recipe)
    #<Double "6.3"> received unexpected message :windows_server_2008? with (no args)
 # C:/Users/me/AppData/Local/Temp/d20161018-26632-iyzn4f/cookbooks/iis/libraries/helper.rb:44:in `older_than_windows2008r2?'
 # C:\Users\me\AppData\Local\Temp\d20161018-26632-iyzn4f\cookbooks\iis\recipes\default.rb:22:in `from_file'
 # C:\Users\me\AppData\Local\Temp\d20161018-26632-iyzn4f\cookbooks\my_recipe\recipes\install_iis.rb:23:in `from_file'
 # ./spec/unit/recipes/install_iis_spec.rb:61:in `block (3 levels) in <top (required)>'
 # ./spec/spec_helper.rb:7:in `block (2 levels) in should_include_recipe'

Ref: cookbooks/iis/libraries/helper.rb:44:in 'older_than_windows2008r2?'.


What value do I have to put, in double('fake version'), in order to target Windows Server 2012R2?

Is there a list of supported versions?

回答1:

The '6.3' is just a label for the fake version object. You need to tell it how to respond to methods. In this, it's pretty easy: double('fake version', :windows_server_2008? => false) (or true if you want to pretend to be true). Normally you would do this in a before block, like this:

before do
  allow(Chef::ReservedNames::Win32::Version).to receive(:new).and_return(double('fake version', :windows_server_2008? => false))
end

You can find more info on how to use RSpec in the RSpec documentation or in any of several thousand tutorials I'm sure are available via Google.