I am using Windows helpers in a cookbook to determine the version of Windows that it is running on and different resources execute depending which version it is.
I am wondering if there is a trick to mocking this check so that I can use Chefspec to write unit tests?
In my recipe:
require 'chef/win32/version'
version = Chef::ReservedNames::Win32::Version.new
if version.windows_server_2016?
The error I’m seeing when I try to run a basic spec on the recipe:
/opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/ffi-1.9.17/lib/ffi/library.rb:147:in
`block in ffi_lib': Could not open library 'kernel32': kernel32: cannot open
shared object file: No such file or directory. (LoadError)
Could not open library 'libkernel32.so': libkernel32.so: cannot open shared
object file: No such file or directory
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/ffi-
1.9.17/lib/ffi/library.rb:100:in `map'
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/ffi-
1.9.17/lib/ffi/library.rb:100:in `ffi_lib'
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-
12.18.31/lib/chef/win32/api/system.rb:135:in `<module:System>'
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-
12.18.31/lib/chef/win32/api/system.rb:24:in `<module:API>'
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-
12.18.31/lib/chef/win32/api/system.rb:23:in `<module:Win32>'
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-
12.18.31/lib/chef/win32/api/system.rb:22:in `<class:Chef>'
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-
12.18.31/lib/chef/win32/api/system.rb:21:in `<top (required)>'
...
Hello,
I am seeing the same issue. Where you able to figure out a way forward with ChefSpec?
When trying to use
require 'chef/win32/version'
windows_version = Chef::ReservedNames::Win32::Version.new
# apply user policy for 2012+
if windows_version.windows_server_2012?
expected no Exception, got #<LoadError: Could not open library 'kernel32': kernel32: cannot open shared object file: No such fil...library 'libkernel32.so': libkernel32.so: cannot open shared object file: No such file or directory> with backtrace:
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/ffi-1.10.0/lib/ffi/library.rb:145:in block in ffi_lib' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/ffi-1.10.0/lib/ffi/library.rb:99:in
map'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/ffi-1.10.0/lib/ffi/library.rb:99:in ffi_lib' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/win32/api/system.rb:135:in
module:System'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/win32/api/system.rb:24:in <module:API>' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/win32/api/system.rb:23:in
module:Win32'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/win32/api/system.rb:22:in <class:Chef>' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/win32/api/system.rb:21:in
<top (required)>'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/win32/version.rb:20:in <top (required)>' # /tmp/d20190130-27460-7ivt75/cookbooks/windows_security_baselines/recipes/security_baseline_policies.rb:15:in
from_file'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/mixin/from_file.rb:34:in instance_eval' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/mixin/from_file.rb:34:in
from_file'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/cookbook_version.rb:199:in load_recipe' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/run_context.rb:350:in
load_recipe'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/run_context/cookbook_compiler.rb:166:in block in compile_recipes' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/run_context/cookbook_compiler.rb:163:in
each'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/run_context/cookbook_compiler.rb:163:in compile_recipes' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/extensions/chef/run_context/cookbook_compiler.rb:62:in
compile_recipes'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/run_context/cookbook_compiler.rb:79:in compile' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/run_context.rb:199:in
load'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/policy_builder/expand_node_object.rb:97:in setup_run_context' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chef-14.10.9/lib/chef/client.rb:515:in
setup_run_context'
# /home/aish/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/solo_runner.rb:113:in converge' # ./cookbooks/windows_security_baselines/spec/unit/recipes/security_baseline_policies_spec.rb:17:in
block (4 levels) in <top (required)>'
# ./cookbooks/windows_security_baselines/spec/unit/recipes/security_baseline_policies_spec.rb:21:in block (5 levels) in <top (required)>' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/expect_exception.rb:15:in
matches?'
# ./cookbooks/windows_security_baselines/spec/unit/recipes/security_baseline_policies_spec.rb:21:in block (4 levels) in <top (required)>' # /home/aish/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/api/core.rb:18:in
block in module:Core'