Skip to content

Instantly share code, notes, and snippets.

@pikesley
Created August 15, 2012 13:33
Show Gist options
  • Save pikesley/3360194 to your computer and use it in GitHub Desktop.
Save pikesley/3360194 to your computer and use it in GitHub Desktop.
chef_environments and cucumber-chef

You know what would make cucumber-chef even more SUPERAWESOME? Support for Chef environments. I've tried (oh, how I've tried) to put support for this directly into the code, but chef_environments are apparently not like other attributes, so until I can work that out, here's a compromise solution.

Note: I lifted the stuff inside the step_definition pretty much directly from Jon Cowie's excellent knife-flip plugin. Thanks Jon :)

@environment_test
Feature: We can set the node's chef_environment correctly
Background:
* I have a server called "banana"
* "banana" should not be persistant
* "banana" is running "ubuntu" "lucid"
* "banana" has been provisioned
* the following databags have been updated:
| databag | databag_path |
| users | support/data_bags/users |
* the "users" recipe has been added to the "banana" run list
* the "env_tester" recipe has been added to the "banana" run list
* the chef-client has been run on "banana"
* I ssh to "banana" with the following credentials:
| username | keyfile |
| cucumber-chef | support/id_rsa-cucumber-chef |
* "banana" has been put into the "production" environment # << INTERESTING BIT
Scenario: Environment should be correct
* I run "cat ~/env"
* I should see "production" in the output # << ALSO INTERESTING BIT
Given /^"(.*?)" has been put into the "(.*?)" environment$/ do |name, env|
searcher = Chef::Search::Query.new
result = searcher.search(:node, "name:%s" % [ name ])
node = result.first.first
node.chef_environment env
node.save
end
execute "show our environment" do
command "echo %s > /home/cucumber-chef/env" % [
node.chef_environment
]
end

This relies on the following assumptions:

  • The environment(s) must exist (make them in the WebUI, create from files with cc-knife, whatever)
  • The 'users' cookbook must work correctly (comes from the Stephen Nelson-Smith book)

The first time we run this, it will fail because

   * "banana" has been put into the "production" environment         # teamserver/step_definitions/teamserver_steps.rb:1
    undefined method `chef_environment' for nil:NilClass (NoMethodError)
    /home/ubuntu/features/teamserver/step_definitions/teamserver_steps.rb:6

As far as I can tell, it fails because the node only gets saved to the server after convergence, so the search returns nothing. However when we run it again, it will pass, because the node now exists. Or maybe on the third time, or the fourth. I'm encountering some weird race condition here. But once it works, it seems to continue working.

We can then edit that feature, changing 'production' for 'staging' (or whatever other environments we've defined) and it will continue to work.

EDIT: No, doesn't work reliably. Race condition smacking me in the nuts. Bah.

Also note that * "banana" has been put into the "production" environment has to be the last step in the Background, otherwise the first run fails completely and the node doesn't get saved.

Hope somebody finds this useful (or it inspires somebody to bake environment support right into cucumber-chef).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment