Chef does non sequential recipe execution - ruby

I followed tutorial http://gettingstartedwithchef.com/, chapter 1.
My run list is
"run_list": [ "recipe[apt]", "recipe[phpap]" ]
My default recipe of phpap cookbook
include_recipe "apache2"
include_recipe "build-essential"
include_recipe "openssl"
include_recipe "mysql::client"
include_recipe "mysql::server"
include_recipe "php"
include_recipe "php::module_mysql"
include_recipe "apache2::mod_php5"
include_recipe "mysql::ruby"
Dependencies of my cookbook
depends "apache2"
depends "mysql"
depends "php"
depends "database"
My repo has following downloaded cookbooks
apache2 aws database openssl phpap xml
apt build-essential mysql php postgresql xfs
I use chef-solo. My host has outdated apt repo info.
Old apt repo should not be a problem because the first recipe in my run list
updates it. But chef ignores apt recipe and starts from mysql one.
See log
dan#mywp3:~/chef-repo$ sudo chef-solo -c solo.rb -j web.json
Starting Chef Client, version 11.6.2
Compiling Cookbooks...
[2013-10-27T00:59:28+04:00] WARN: Cloning resource attributes for service[apache2] from prior resource (CHEF-3694)
[2013-10-27T00:59:28+04:00] WARN: Previous service[apache2]: /home/dan/chef-repo/cookbooks/apache2/recipes/default.rb:24:in `from_file'
[2013-10-27T00:59:28+04:00] WARN: Current service[apache2]: /home/dan/chef-repo/cookbooks/apache2/recipes/default.rb:210:in `from_file'
[2013-10-27T00:59:28+04:00] WARN: Cloning resource attributes for directory[/var/cache/local/preseeding] from prior resource (CHEF-3694)
[2013-10-27T00:59:28+04:00] WARN: Previous directory[/var/cache/local/preseeding]: /home/dan/chef-repo/cookbooks/apt/recipes/default.rb:76:in `block in from_file'
[2013-10-27T00:59:28+04:00] WARN: Current directory[/var/cache/local/preseeding]: /home/dan/chef-repo/cookbooks/mysql/recipes/server.rb:44:in `from_file'
[2013-10-27T00:59:28+04:00] WARN: Cloning resource attributes for directory[/var/lib/mysql] from prior resource (CHEF-3694)
[2013-10-27T00:59:28+04:00] WARN: Previous directory[/var/lib/mysql]: /home/dan/chef-repo/cookbooks/mysql/recipes/server.rb:117:in `block in from_file'
[2013-10-27T00:59:28+04:00] WARN: Current directory[/var/lib/mysql]: /home/dan/chef-repo/cookbooks/mysql/recipes/server.rb:117:in `block in from_file'
[2013-10-27T00:59:28+04:00] WARN: Cloning resource attributes for template[/etc/mysql/my.cnf] from prior resource (CHEF-3694)
[2013-10-27T00:59:28+04:00] WARN: Previous template[/etc/mysql/my.cnf]: /home/dan/chef-repo/cookbooks/mysql/recipes/server.rb:134:in `from_file'
[2013-10-27T00:59:28+04:00] WARN: Current template[/etc/mysql/my.cnf]: /home/dan/chef-repo/cookbooks/mysql/recipes/server.rb:194:in `from_file'
Recipe: mysql::client
* package[mysql-client] action install
================================================================================
Error executing action `install` on resource 'package[mysql-client]'
================================================================================
Chef::Exceptions::Exec
----------------------
apt-get -q -y install mysql-client=5.5.32-0ubuntu0.12.04.1 returned 100, expected 0
Cookbook Trace:
---------------
/home/dan/chef-repo/cookbooks/mysql/recipes/ruby.rb:44:in `block in from_file'
/home/dan/chef-repo/cookbooks/mysql/recipes/ruby.rb:43:in `each'
/home/dan/chef-repo/cookbooks/mysql/recipes/ruby.rb:43:in `from_file'
/home/dan/chef-repo/cookbooks/phpap/recipes/default.rb:20:in `from_file'
Resource Declaration:
---------------------
# In /home/dan/chef-repo/cookbooks/mysql/recipes/client.rb
46: package name
47: end
Compiled Resource:
------------------
# Declared in /home/dan/chef-repo/cookbooks/mysql/recipes/client.rb:46:in `block in from_file'
package("mysql-client") do
action :install
retries 0
retry_delay 2
package_name "mysql-client"
version "5.5.32-0ubuntu0.12.04.1"
cookbook_name :mysql
recipe_name "client"
end
================================================================================
Recipe Compile Error in /home/dan/chef-repo/cookbooks/phpap/recipes/default.rb
================================================================================
Chef::Exceptions::Exec
----------------------
package[mysql-client] (mysql::client line 46) had an error: Chef::Exceptions::Exec: apt-get -q -y install mysql-client=5.5.32-0ubuntu0.12.04.1 returned 100, expected 0
Cookbook Trace:
---------------
/home/dan/chef-repo/cookbooks/mysql/recipes/ruby.rb:44:in `block in from_file'
/home/dan/chef-repo/cookbooks/mysql/recipes/ruby.rb:43:in `each'
/home/dan/chef-repo/cookbooks/mysql/recipes/ruby.rb:43:in `from_file'
/home/dan/chef-repo/cookbooks/phpap/recipes/default.rb:20:in `from_file'
Relevant File Content:
----------------------
/home/dan/chef-repo/cookbooks/mysql/recipes/ruby.rb:
37: when 'rhel'
38: resources('yum_key[RPM-GPG-KEY-percona]').run_action(:add)
39: resources('yum_repository[percona]').run_action(:add)
40: end
41: end
42:
43: node['mysql']['client']['packages'].each do |name|
44>> resources("package[#{name}]").run_action(:install)
45: end
46:
47: chef_gem 'mysql'
48:
[2013-10-27T00:59:30+04:00] ERROR: Running exception handlers
[2013-10-27T00:59:30+04:00] ERROR: Exception handlers complete
[2013-10-27T00:59:30+04:00] FATAL: Stacktrace dumped to /home/dan/chef-solo/chef-stacktrace.out
Chef Client failed. 0 resources updated
[2013-10-27T00:59:30+04:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
The host runs ubuntu 12.04.
I tried to put my own recipes before phpap in the run list but without any success.
I can workaround this with manual placing apt-get update in bash script right before chef-solo.
Is it mysql cookbook so bad? As far as I know chef ALWAYS follows run-list strait forward.

In general chef will always honor the run list. However sometimes you might want to run a resource before any others (like to configure a package manager, apt/yum/etc). What seems to be happening in the mysql ruby recipe is that the recipe is explicitly running some package installs at compile time (before chef has begun to execute resources). They fail as the apt recipe hasn't been run yet (none have).
There's a good opscode post explaining compile/execute phases here.
The offending code in mysql::ruby is
node['mysql']['client']['packages'].each do |name|
resources("package[#{name}]").run_action(:install)
end
The run_action(:install) is telling chef to run this now (at compile time).

I got phpap recipe without failure on bare ubuntu 12.04 (without any manual pre update) with following fix.
I removed recipes "build-essential" and "openssl" from phpap one.
After that change chef executes recipes as expected.
But weird behavior left unexplained.

vagrant destroy -f && vagrant up // destroy everything and start over => OK

Related

Error installing passenger: rake requires Ruby version >= 2.0.0. : Chef recipe error

I am trying to install passenger gem using chef recipe. Install methos is 'source'.
The ruby version installed is 2.2.3, and the gem binary is also specified as the same version.
But I am getting this error
STDERR: ERROR: Error installing passenger:
rake requires Ruby version >= 2.0.0.
When I tried to install directly in to the test VM it worked without any errors.
Any idea?
Chef Recipe
if node['nginx']['passenger']['install_method'] == 'package'
package node['nginx']['package_name']
package 'passenger'
elsif node['nginx']['passenger']['install_method'] == 'source'
gem_package 'passenger' do
action :install
version node['nginx']['passenger']['version']
gem_binary node['nginx']['passenger']['gem_binary'] if node['nginx']['passenger']['gem_binary']
end
node.run_state['nginx_configure_flags'] =
node.run_state['nginx_configure_flags'] | ["--add-module=#{node['nginx']['passenger']['root']}/ext/nginx"]
end
Test kitchen Log given below
Recipe: nginx::passenger
* apt_package[libcurl4-gnutls-dev] action install
- install version 7.35.0-1ubuntu2.17 of package libcurl4-gnutls-dev
* gem_package[passenger] action install
================================================================================
Error executing action `install` on resource 'gem_package[passenger]'
================================================================================
Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of /home/local/.rvm/rubies/ruby-2.2.3/bin/gem install passenger -q --no-rdoc --no-ri -v "4.0.57" ----
STDOUT:
STDERR: ERROR: Error installing passenger:
rake requires Ruby version >= 2.0.0.
---- End output of /home/local/.rvm/rubies/ruby-2.2.3/bin/gem install passenger -q --no-rdoc --no-ri -v "4.0.57" ----
Ran /home/local/.rvm/rubies/ruby-2.2.3/bin/gem install passenger -q --no-rdoc --no-ri -v "4.0.57" returned 1
Resource Declaration:
---------------------
# In /tmp/kitchen/cache/cookbooks/nginx/recipes/passenger.rb
39: gem_package 'passenger' do
40: action :install
41: version node['nginx']['passenger']['version']
42: gem_binary node['nginx']['passenger']['gem_binary'] if node['nginx']['passenger']['gem_binary']
43: end
44:
Compiled Resource:
------------------
# Declared in /tmp/kitchen/cache/cookbooks/nginx/recipes/passenger.rb:39:in `from_file'
gem_package("passenger") do
package_name "passenger"
action [:install]
retries 0
retry_delay 2
default_guard_interpreter :default
version "4.0.57"
declared_type :gem_package
cookbook_name "nginx"
recipe_name "passenger"
gem_binary "/home/local/.rvm/rubies/ruby-2.2.3/bin/gem"
end
Platform:
---------
x86_64-linux
Recipe: postfix::_common
* service[postfix] action restart
- restart service service[postfix]
Running handlers:
[2018-09-28T20:37:47+00:00] ERROR: Running exception handlers
Running handlers complete
[2018-09-28T20:37:47+00:00] ERROR: Exception handlers complete
Chef Client failed. 25 resources updated in 01 minutes 42 seconds
[2018-09-28T20:37:47+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
[2018-09-28T20:37:47+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2018-09-28T20:37:47+00:00] ERROR: gem_package[passenger] (nginx::passenger line 39) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
---- Begin output of /home/local/.rvm/rubies/ruby-2.2.3/bin/gem install passenger -q --no-rdoc --no-ri -v "4.0.57" ----
STDOUT:
STDERR: ERROR: Error installing passenger:
rake requires Ruby version >= 2.0.0.
---- End output of /home/local/.rvm/rubies/ruby-2.2.3/bin/gem install passenger -q --no-rdoc --no-ri -v "4.0.57" ----
Ran /home/local/.rvm/rubies/ruby-2.2.3/bin/gem install passenger -q --no-rdoc --no-ri -v "4.0.57" returned 1
[2018-09-28T20:37:47+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

Chef Client failed. 0 resources updated in 01 seconds - ERROR: wrong number of arguments (given 1, expected 3)

I am trying to run a simple Hazelcast cluster on Amazon EC2 following the official code sample, every step seems to be OK but when running vagrant up --provider=aws ( under the header "Let's do this thing" ) it fails as shown:
==> hazelcast2: - ohai (5.2.0)
==> hazelcast2: Installing Cookbook Gems:
==> hazelcast2: Compiling Cookbooks...
==> hazelcast2: [2017-10-24T20:03:06+00:00] INFO: The plugin path /etc/chef/ohai/plugins does not exist. Skipping...
==> hazelcast2: [2017-10-24T20:03:06+00:00] WARN:
[DEPRECATION] Plugin at /tmp/vagrant-chef/ohai/cookbook_plugins/ohai/hint.rb is a version 6 plugin.
Version 6 plugins will not be supported in Chef/Ohai 14. Please upgrade your plugin to version 7 plugin format.
For more information visit here: docs.chef.io/ohai_custom.html
==> hazelcast2:
==> hazelcast2: Running handlers:
==> hazelcast2: [2017-10-24T20:03:06+00:00] ERROR: Running exception handlers
==> hazelcast2: Running handlers complete
==> hazelcast2:
==> hazelcast2: [2017-10-24T20:03:06+00:00] ERROR: Exception handlers complete
==> hazelcast2: Chef Client failed. 0 resources updated in 01 seconds
==> hazelcast2: [2017-10-24T20:03:06+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
==> hazelcast2: [2017-10-24T20:03:06+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
==> hazelcast2: [2017-10-24T20:03:06+00:00] ERROR: wrong number of arguments (given 1, expected 3)
==> hazelcast2: [2017-10-24T20:03:06+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully
(exit code 1)
==> hazelcast2: An error occurred. The error will be shown after all tasks complete.
An error occurred while executing multiple actions in parallel.
Any errors that occurred are shown below.
An error occurred while executing the action on the 'hazelcast1' machine.
Please handle this error then try again:
Chef never successfully completed! Any errors should be visible in the output above.
Please fix your recipes so that they properly complete.
An error occurred while executing the action on the 'hazelcast2' machine.
Please handle this error then try again:
Chef never successfully completed! Any errors should be visible in the output above.
Please fix your recipes so that they properly complete.
/var/chef/cache/chef-stacktrace.out:
Generated at 2017-10-24 21:36:55 +0000
ArgumentError: wrong number of arguments (given 1, expected 3)
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/dsl/plugin/versionvi.rb:27:in `initialize'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/loader.rb:223:in `new'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/loader.rb:223:in `load_v7_plugin'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/loader.rb:93:in `block in load_additional'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/loader.rb:90:in `collect'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/loader.rb:90:in `load_additional'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/system.rb:116:in `run_additional_plugins'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/run_context/cookbook_compiler.rb:119:in `compile_ohai_plugins'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/run_context/cookbook_compiler.rb:73:in `compile'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/run_context.rb:192:in `load'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/policy_builder/expand_node_object.rb:97:in `setup_run_context'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/client.rb:513:in `setup_run_context'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/client.rb:281:in `run'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application.rb:291:in `block in fork_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application.rb:279:in `fork'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application.rb:279:in `fork_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application.rb:244:in `block in run_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/local_mode.rb:44:in `with_server_connectivity'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application.rb:232:in `run_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application/client.rb:469:in `sleep_then_run_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application/client.rb:458:in `block in interval_run_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application/client.rb:457:in `loop'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application/client.rb:457:in `interval_run_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application/client.rb:441:in `run_application'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application.rb:59:in `run'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/lib/chef/application/solo.rb:225:in `run'
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/chef-13.5.3/bin/chef-solo:25:in `<top (required)>'
/usr/bin/chef-solo:58:in `load'
/usr/bin/chef-solo:58:in `<main>'
Ohai version:
ohai -version
Ohai: 13.5.0
Java jdk not installed (when it should be according to the receipt):
java -version
The program 'java' can be found in the following packages:
* default-jre
* gcj-5-jre-headless
* openjdk-8-jre-headless
* gcj-4.8-jre-headless
* gcj-4.9-jre-headless
* openjdk-9-jre-headless
Try: sudo apt install <selected package>
Following the Stacktrace I got to the point where I believe it has something to do with the fact that the function:
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/dsl/plugin/versionvi.rb:27:in `initialize':
def initialize(controller, plugin_path, plugin_dir_path)
super(controller.data)
#controller = controller
#version = :version6
#source = plugin_path
#plugin_dir_path = plugin_dir_path
end
... requires 3 parameters and is called with only one in:
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/loader.rb:223:in 'new' in the function:
/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/loader.rb:223:in `load_v7_plugin'
def load_v7_plugin(plugin_class)
plugin = plugin_class.new(#controller.data)
collect_provides(plugin)
plugin
end
I repeated the steps on an Ubuntu 14.04 and on an Ubuntu 16.04 VM and I am still stuck with the same Error, any idea what I am doing wrong?
P.S. I am just following the steps in the sample.
Thank you
The important bit is probably this:
[DEPRECATION] Plugin at /tmp/vagrant-chef/ohai/cookbook_plugins/ohai/hint.rb is a version 6 plugin.
Version 6 plugins will not be supported in Chef/Ohai 14. Please upgrade your plugin to version 7 plugin format.
For more information visit here: docs.chef.io/ohai_custom.html
Figure out what is installing that plugin and check if anything is in the same folder and is an invalid plugin.

chef windows cookbook compile error, cannot find file windows/error

On a chef-client run, I'm seeing this windows cookbook compile error, as show below. What is it trying to require in that it can't find, and how to fix? Is 'windows/error' a ruby gem or something else?
================================================================================
Recipe Compile Error in c:/chef/cache/cookbooks/windows/libraries/windows_privileged.rb
================================================================================
LoadError
---------
cannot load such file -- windows/error
Cookbook Trace:
---------------
c:/chef/cache/cookbooks/windows/libraries/windows_privileged.rb:24:in `<top (required)>'
Relevant File Content:
----------------------
c:/chef/cache/cookbooks/windows/libraries/windows_privileged.rb:
17: # distributed under the License is distributed on an "AS IS" BASIS,
18: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19: # See the License for the specific language governing permissions and
20: # limitations under the License.
21: #
22:
23: if RUBY_PLATFORM =~ /mswin|mingw32|windows/
24>> require 'windows/error'
25: require 'windows/registry'
26: require 'windows/process'
27: require 'windows/security'
28: end
29:
30: #helpers for Windows API calls that require privilege adjustments
31: class Chef
32: class WindowsPrivileged
33: if RUBY_PLATFORM =~ /mswin|mingw32|windows/
Running handlers:
[2016-02-08T09:31:54-08:00] ERROR: Running exception handlers
Running handlers complete
[2016-02-08T09:31:54-08:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated in 01 minutes 44 seconds
Found solution with help from a friend.
chef-client versions > 12.5 will need to install a gem
gem install windows-pr
To find this solution, go to the cookbook github repo and search in the "issues" there. That's the method, here's the direct link:
https://github.com/chef-cookbooks/windows/issues/276

Install gem from S3 in OpsWorks Chef recipe

I need net-ssh and net-scp as part of a custom OpsWorks chef recipe.
Getting occasional failures from rubygems.org failing to provide the gems, so I would like to host them myself on S3.
chef_gem has the 'source' argument, but it seems to require the local file exist prior to chef being started (so I can't download the file immediately before chef_gem using remote_file)
$gemSsh = "#{Chef::Config[:file_cache_path]}/net-ssh.gem"
$gemScp = "#{Chef::Config[:file_cache_path]}/net-scp.gem"
remote_file $gemSsh do
source "https://s3-us-west-2.amazonaws.com/****/net-ssh-2.9.1.gem"
action :nothing
end.run_action(:create)
remote_file $gemScp do
source "https://s3-us-west-2.amazonaws.com/****/net-scp-1.2.1.gem"
action :nothing
end.run_action(:create)
chef_gem "net-ssh" do
action :nothing
source $gemSsh
end.run_action(:install)
chef_gem "net-scp" do
action :nothing
source $gemScp
end.run_action(:install)
(Note: the run_action(:install) is based on comments here https://tickets.opscode.com/browse/CHEF-4843)
This fails with the following error:
NoMethodError
-------------
undefined method `name' for "/var/lib/aws/opsworks/cache.stage2/net-scp.gem":String
Cookbook Trace:
---------------
/var/lib/aws/opsworks/cache.stage2/cookbooks/opsworks_commons/libraries/monkey_patch_rubygems_provider.rb:55:in `install'
/var/lib/aws/opsworks/cache.stage2/cookbooks/****/recipes/default.rb:24:in `from_file'
You can use "--local" flag which is provided by gem install (You can find other options with gem install --help).
Basic command would be somethig like gem install --local path_to_gem/filename.gem. So your recipe in that case would be:
....
chef_gem "net-ssh" do
action :nothing
options("--local #{$gemSsh}")
end.run_action(:install)
chef_gem "net-scp" do
action :nothing
options("--local #{$gemScp}")
end.run_action(:install)

Is it possible (or even recommended) to mock a Ruby require statement in ChefSpec?

I've been writing ChefSpec unit test suites for the past couple of weeks and have managed to do quite a bit with it, but I have found a scenario that got me stumped. I have a recipe that includes the 'aws' cookbook's default recipe, which in turn installs the right_aws and then proceeds to require it.
But the ChefSpec runner seems to have problems with it, spitting out a Ruby LoadError:
LoadError
---------
cannot load such file -- right_aws
Cookbook Trace:
---------------
/var/folders/0r/cg1hmpkj2nb3wh6slrg1hkhm0000gn/T/d20140612-36208-q1ecjj/cookbooks/aws/recipes/default.rb:25:in `from_file'
/var/folders/0r/cg1hmpkj2nb3wh6slrg1hkhm0000gn/T/d20140612-36208-q1ecjj/cookbooks/acmecorp-postgresql/recipes/server.rb:71:in `from_file'
Relevant File Content:
----------------------
/var/folders/0r/cg1hmpkj2nb3wh6slrg1hkhm0000gn/T/d20140612-36208-q1ecjj/cookbooks/aws/recipes/default.rb:
18: #
19:
20: chef_gem "right_aws" do
21: version node['aws']['right_aws_version']
22: action :install
23: end
24:
25>> require 'right_aws'
26:
Is there a way to mock that require 'right_aws' line from the 'aws' cookbook in my test? Is it recommended?
Is it better to simply install the right_aws gem on the system running ChefSpec tests?
You have a few options:
You can ensure right_aws is available by installing it directly on your machine or with a Gemfile (as noted in the question comments)
You can mock the call to include_recipe directly (if you don't need the included recipe for your test)
allow_any_instance_of(Chef::Recipe).to receive(:include_recipe)
allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).with('aws').and_return(true)
Mock the requires call itself.
allow(Kernel).to receive(:require)
allow(Kernel).to receive(:require).with("right_aws").and_return(true)
If possible, I'd recommend #2

Resources