Brando__
Brando__

Reputation: 365

Chef - Search within test kitchen context

I'm managing some scripts that can only be run on one server. I've decided the best way to single out one host that is assigned to a given role is by doing a search and then choosing the first element in the array returned.

Like this:

q = "roles:thumper"
arr = search(:node, q, filter_result: { fqdn: ['fqdn'] }).map { |n| n['fqdn'] }

 # => ["1.example", "2.example"]

arr.first 
# "1.example"

This works well in reality and chef-shell, but has difficulties in Test Kitchen where I am being returned an HTTP 500 on the search command.

     ================================================================================
     Recipe Compile Error in /tmp/kitchen/cache/cookbooks/test/recipes/full.rb
     ================================================================================

     Net::HTTPFatalError
     -------------------
     500 "Internal Server Error"

I am using chef_zero with the vagrant driver. Is there anything special that must be done to let search functionality work in this type of scenario?

Integration json:

{
  "id": "thumper",
  "environment": "food",
  "run_list": ["role["thumper]"],
  "automatic": {
    "fqdn": "thumper",
    "ipaddress": "10.10.10.10",
    "roles": ["thumper"],
    "environment": "food"
  }
}

Fixtures data is just calling the relevant cookbooks

include_recipe 'test_helpers'
include_recipe 'role_thumper'

Upvotes: 1

Views: 388

Answers (1)

coderanger
coderanger

Reputation: 54249

The underlying issue is probably that you aren't setting up the fixture node data correctly, or maybe some piece of the partial search API not working correctly under Zero. However this is mostly moot as this method for server election is unsafe and you shouldn't use it. At a minimum you need to sort the returned array in some way as search results in Chef are not stable to any external reference point (it sorts on a field called "object ID" but this is not exposed in the API). More to the point, this can easily result in a server being demoted but not realizing it until the next time Chef runs. Depending on how serious you are about the "only run in one place" rule you either need some slightly firmer code on the Chef side, or better yet use a tool actually designed for cluster coordination like Consul, ZooKeeper, or Etcd.

Upvotes: 1

Related Questions