0

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

user3394555
  • 77
  • 10

2 Answers2

0

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.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • After I mock, I am trying to understand as to how i can mock node[:storm_work_gen][:jar_name] in the receipe when chefSpec runs, could you point out how I do this. – user3394555 Mar 30 '16 at 20:35
  • I would just set it directly in the node for the purposes of testing. Get rid of the env stub and add `node.set[:storm_work_gen][:jar_name] = 'whatever'` in that same block. – coderanger Mar 30 '16 at 20:44
0

For those of you who were looking for this solution but, not clear on how to configure the node value based on the above comments here's a breakdown based on the above example:

Initially: describe 'storm_wlm_deploy::_artifact' do 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

Updated: describe 'storm_wlm_deploy::_artifact' do let(:chef_run) do ChefSpec::SoloRunner.new do |node| node.set[:storm_work_gen][:jar_name] = 'wlm-workgenerator-topology-0.0.1-SNAPSHOT.jar' 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

SnellyBigoda
  • 1,535
  • 12
  • 10