chefspec: Cannot test cross-platform cookbooks
This is something @sethvargo and I discussed privately and I thought I should open an issue here. Seth, feel free to give a more cogent explanation & close it if you feel it can’t be solved.
Basically, attempting to test cookbooks intended for one platform (e.g. Windows) on another (e.g. Mac OS X) does not seem to work, because there is no way to stub basic Kernel functionality like ‘require’.
Concrete example:
require 'spec_helper'
describe 'foo::windows_web_server' do
let(:chef_run) { ChefSpec::Runner.new(platform: 'windows', version: '2008R2').converge(described_recipe) }
before(:each) do
stub_const("RUBY_PLATFORM", "windows") # you'll see what this is about below
end
# expectations below here don't actually matter
end
When run on a Mac, this spec fails, because the library from the Windows cookbook is loaded as part of the recipe-under-test, and the following happens:
Failures:
1) foo::windows_web_server includes the _windows_base role
Failure/Error: let(:chef_run) { ChefSpec::Runner.new(platform: 'windows', version: '2008R2').converge(described_recipe) }
LoadError:
cannot load such file -- win32/registry
# /var/folders/56/vlf96yyx30vdx5qlq6n4pkxw0000gp/T/d20140401-74327-qv3myy/cookbooks/windows/libraries/registry_helper.rb:26:in `<top (required)>'
# ./spec/unit/windows_web_server_spec.rb:4:in `block (2 levels) in <top (required)>'
# ./spec/unit/windows_web_server_spec.rb:10:in `block (2 levels) in <top (required)>'
Is this just a limitation in unit testing?
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 28 (10 by maintainers)
FWIW, here’s something that works in WSL, testing cookbooks that normally converge on Windows:
Credits to @goodrum for many of the bits in there.
This is how to get around it: