1

I'm writing a cookbook that runs a partial search to find an attribute on other nodes. My chefspec test is failing with error ERROR: Connection refused connecting to localhost:443. The search is instantiated as below:

describe 'my_recipe::default' do
  let(:test1_node) do
    stub_node('test1.com', platform: 'redhat', version: '6.3') do |node|
      node.set['my_recipe']['id'] = 101
      node.set['chef_environment'] = 'production'
    end
  end

  let(:test2_node) do
    stub_node('test2.com', platform: 'redhat', version: '6.3') do |node|
      node.set['my_recipe']['id'] = 102
      node.set['chef_environment'] = 'production'
    end
  end

  before do
    stub_search("node", "my_recipe:* AND chef_environment:production").and_return([])
  end
  let(:chef_run) do
    ChefSpec::Runner.new do |node|
      env = Chef::Environment.new
      env.name 'production'

      node.stub(:chef_environment).and_return(env.name)
      Chef::Environment.stub(:load).and_return(env)
    end.converge(described_recipe)
  end
  it 'updates the file' do
    stub_search("node", "my_recipe:* AND chef_environment:production").and_return([test1_node,test2_node])
    expect(chef_run).to create_template(/conf/my_recipe.cfg")
  end
end

Am I stubbing this incorrectly?

zishe
  • 10,665
  • 12
  • 64
  • 103
raeshell
  • 13
  • 3

1 Answers1

1

stub_search is for stubbing Chef Search. Partial search is supported by a cookbook and therefore not part of Chef core. Partial search uses a different API endpoint and uses POST instead of GET for the protocol.

You'll need to stub Chef's API calls to partial search. stub_search will not work.

sethvargo
  • 26,739
  • 10
  • 86
  • 156
  • Can you please elaborate or provide a link? I have searched extensively and have not found something that will work. – raeshell Jun 02 '14 at 15:30
  • I've tried using Chef::Recipe.any_instance.stub(:search).with(:node, "my_recipe:*").and_return([{"hostname" => "node1.example.com" }, {"hostname" => "node2.example.com"}]) – raeshell Jun 02 '14 at 15:44
  • Search != Partial Search! – sethvargo Jun 02 '14 at 15:51
  • I wasn't understanding what you meant. Substituting the above 'search' with 'partial_search' was what I needed. – raeshell Jun 02 '14 at 19:03