I am trying to write some test for my recipe using ChefSpec.
This is piece of code I am trying to test:
file node[:storm][:job_dir] + node[:storm_work_gen][:jar_name] do
owner node[:storm][:user]
group node[:storm][:user]
action :delete
only_if { File.exist? node[:storm][:job_dir]+node[:storm_work_gen][:jar_name] }
end
Here the value of node[:storm_work_gen][:jar_name]
is coming from a environment file. Content of my env file is:
# coding: UTF-8
name 'sro_work_gen_dev'
description 'Sro Work Generator Environment for dev cluster'
override_attributes()
default_attributes(
storm_work_gen: {
cache_prop: {
expire_time: '30',
max_record_size: '100'
},
parallelism_hint: {
kafka_spout_brq_sfq_ph: '1',
kafka_spout_ftl_ph: '1',
data_marshaller_ph: '1',
data_processor_ph: '1',
item_lookup_ph: '1',
rule_applier_ph: '1',
worlist_writer_ph: '1'
},
num_workers: '2',
Topology_Name: 'WorkGen-tplgy-json-version',
Kafka_Zookeepers: '...',
Kafka_Broker: '...',
repo_url: '...',
jar_name: 'wlm-workgenerator-topology-0.0.1-SNAPSHOT.jar'
}
)
And my spec looks like this:
describe 'storm_wlm_deploy::_artifact' do
# let(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe) }
let(:chef_run) do
ChefSpec::SoloRunner.new do |node|
env = Chef::Environment.new
env.name 'storm_work_gen'
allow(node).to receive(:chef_environment).and_return(env.name)
allow(Chef::Environment).to receive(:load).and_return(env)
end.converge(described_recipe)
end
it 'delete the jar:/opt/swlm/storm_jobs/wlm-workgenerator-topology-0.0.1-SNAPSHOT.jar if it exists' do
expect(chef_run).to delete_file('/opt/swlm/storm_jobs/wlm-workgenerator-topology-0.0.1-SNAPSHOT.jar')
end
end
The spec fails with message:
NoMethodError
-------------
undefined method `[]' for nil:NilClass
at line file node[:storm][:job_dir] + node[:storm_work_gen][:jar_name] do
I want to know how to get node[:storm_work_gen][:jar_name] from the env file when chefSpec runs
You are setting up a mock environment, but you aren't actually telling Chef to use it for anything. Also you aren't putting any data in it. While it has the same name as the environment in your file, the two have nothing to do with each other.