user3481957
user3481957

Reputation: 161

Testing Chef search queries in recipes using Test Kitchen

In a related post I asked about creating a comma separated list of hosts from a Chef query. The problem though is that I'm using test-kitchen which doesn't use the Chef server so that was why my query was not returning any hosts.

My question is then, how do you test recipes that rely on Chef queries in test kitchen? Preloading data into a file or something along those lines won't expose any bugs in the knife query itself. Is there a busser that would allow me to run queries against the Chef Server?

Update: Below is the code to support the strange results I'm getting from the Chef query.

Recipe:

# Create output conf file
indexers = search("node", "role:#{node['forwarder']['indexer_role']}")

template '/opt/splunkforwarder/etc/system/local/outputs.conf' do
  source "system_local_outputs.erb"
  owner 'nobody'
  group 'nobody'
  mode 0600
  action :create
variables(
  :indexers  => indexers
)
end

Template ERB

[tcpout]
defaultGroup = default-autolb-group

[tcpout:default-autolb-group]
disabled = false
server = <%= @indexers.map {|n| "#{n}:5501" }.join(",") %>

attributes/default.rb

default['forwarder']['indexer_role'] = 'splunk_indexer'

Command line query inside Docker instance to test if forwarders are configured correctly

sudo /opt/splunkforwarder/bin/splunk list forward-server -auth admin:<password>

Active forwards:
None
Configured but inactive forwards:
node[splunk-001-indexer.example.com]:5501
node[splunk-002-indexer.example.com]:5501
node[splunk-003-indexer.example.com]:5501
node[splunk-004-indexer.example.com]:5501
node[splunk-005-indexer.example.com]:5501
node[splunk-006-indexer.example.com]:5501
node[splunk-007-indexer.example.com]:5501
node[splunk-008-indexer.example.com]:5501
node[splunk-009-indexer.example.com]:5501
node[splunk-010-indexer.example.com]:5501
node[splunk-011-indexer.example.com]:5501
node[splunk-012-indexer.example.com]:5501

Finally, the contents of outputs.conf file

sudo cat /opt/splunkforwarder/etc/system/local/outputs.conf
[tcpout]
defaultGroup = default-autolb-group

[tcpout:default-autolb-group]
disabled = false
server = node[dspe-splunk-001-indexer.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-002.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-003-indexer.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-004-indexer.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-005-indexer.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-006-indexer.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-007-indexer.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-008.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-009-indexer.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-010-indexer.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-011-indexer.pbp.gq1.yahoo.com]:5501,node[dspe-splunk-012-indexer.pbp.gq1.yahoo.com]:5501

Upvotes: 0

Views: 1050

Answers (2)

user3481957
user3481957

Reputation: 161

Actually Fauxhai turns out to provide the ability to gather data from the Chef server instead of mocking the data up. I just stumbled across this entry:

Alternatively, if you do not want to mock the data, Fauxhai provides a fetch mechanism for collecting "real" Ohai data from a remote server or local file. Maybe you want to test against the fully-replicated environment for a front-facing server in your pool. Just pass in the url option instead of a platform:

The fetch method supports all the same options as the Net-SSH command, such as :user, :password, :key_file, etc.

The fetch method will cache the JSON file in a temporary path on your local machine. Similar to gems like VCR, this allows Fauxhai to use the cached copy, making your test suite run faster. You can optionally force a cache miss by passing the :force_cache_miss => true option to the fetch initializer. Because this is real data, there may be a security concern. Secure your laptop accordingly.

Upvotes: 0

StephenKing
StephenKing

Reputation: 37630

You can mock search data (for nodes) by putting JSON files below test/integration/nodes/:

An example file looks as follows:

{
  "id":       "testhost",
  "name":     "testhost.example.com",
  "chef_type": "node",
  "json_class": "Chef::Node",
  "run_list": [],
  "chef_environment": "production",
  "automatic": {
    "fqdn": "testhost.example.com",
    "ip": "127.0.0.1"
  }
}

Upvotes: 2

Related Questions