Skip to content

Commit

Permalink
Merge pull request #85 from sethvargo/fauxhai
Browse files Browse the repository at this point in the history
Integrate with fauxhai? 
Looks good, merging.
  • Loading branch information
ranjib committed Jan 24, 2013
2 parents 56a2426 + 60038ca commit 3e0990d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 49 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Expand Up @@ -2,8 +2,12 @@ rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0.0
env:
- CHEF_VERSION=0.9.18
- CHEF_VERSION=0.10.10
- CHEF_VERSION=10.14.4
- CHEF_VERSION=10.16.2
- CHEF_VERSION=10.16.4
matrix:
allow_failures:
- rvm: 2.0.0
14 changes: 1 addition & 13 deletions Gemfile
@@ -1,14 +1,2 @@
source 'http://rubygems.org'
source :rubygems
gemspec

group :test do
gem 'aruba', '~> 0.4.11'
gem 'cucumber', '~> 1.2.0'
gem 'i18n', '~> 0.6.0'
gem 'simplecov', '~> 0.6.4'
end

group :development do
gem 'rake', '~> 0.9.2.2'
gem 'yard', '~> 0.8.1'
end
29 changes: 16 additions & 13 deletions README.md
Expand Up @@ -195,19 +195,22 @@ declares the platform underneath `automatic_attrs`.
4 'I am running on a Commodore 64.'
```

### Missing attributes

Because Ohai runs a large number of plugins by default, many community cookbooks
will assume that a node attribute will be present, and will fail unless a value
is provided. Providing values for each of these attributes can detract from the
readability of your examples.

[Fauxhai](https://github.com/customink/fauxhai) from Seth Vargo is a promising
solution to this problem because it enables you to re-use sanitized Ohai
attribute profiles by name, rather than being required to provide each attribute
individually. For more on Fauxhai
[check out this blog post](http://technology.customink.com/blog/2012/08/03/testing-chef-cookbooks/)
from CustomInk.
### "Missing" attributes

[Fauxhai](https://github.com/customink/fauxhai) from Seth Vargo is now a dependency of ChefSpec. This means you leverage all the power of fauxhai (and it's community contributed ohai mocks) without additional configuration. Just specify the `platform` and `version` attributes when you instantiate your `ChefRunner`:

```ruby
1 chef_run = ChefSpec::ChefRunner.new(platform:'ubuntu', version:'12.04') do |node|
2 node['my_attribute'] = 'bar'
3 node['my_other_attribute'] = 'bar2'
4 end
5 chef_run.converge 'example::default'
```

This will include all the default attributes for Ubuntu Precise 12.04. By default, ChefSpec uses the built-in ChefSpec environment (which is minimally configured) for backward compatibility.

For more on Fauxhai
[check out this blog post](http://technology.customink.com/blog/2012/08/03/testing-chef-cookbooks/) from CustomInk.

## Search Results

Expand Down
16 changes: 15 additions & 1 deletion chefspec.gemspec
@@ -1,6 +1,7 @@
chef_version = ENV.key?('CHEF_VERSION') ? "= #{ENV['CHEF_VERSION']}" : ['>= 0.9.12']
lib = File.expand_path('../lib/', __FILE__)
$:.unshift lib unless $:.include?(lib)

require 'chefspec/version'
Gem::Specification.new do |s|
s.name = 'chefspec'
Expand All @@ -12,8 +13,21 @@ Gem::Specification.new do |s|
s.license = 'MIT'
s.require_path = 'lib'
s.files = Dir['lib/**/*.rb']

s.add_dependency('chef', chef_version)
s.add_dependency('erubis', '>= 0')
s.add_dependency('fauxhai', '~> 0.1')
s.add_dependency('minitest-chef-handler', '~> 0.6.0')
s.add_dependency('rspec', '~> 2.11.0')
s.add_dependency('moneta', '< 0.7.0') # https://github.com/opscode/chef/commit/c6b6103e3befa355c2645c35fc3b8ba0159375f0
s.add_dependency('rspec', '~> 2.12.0')

# Development Dependencies
s.add_development_dependency('rake', '~> 0.9.2.2')
s.add_development_dependency('yard', '~> 0.8.1')

# Testing Dependencies
s.add_development_dependency('aruba', '~> 0.4.11')
s.add_development_dependency('cucumber', '~> 1.2.0')
s.add_development_dependency('i18n', '~> 0.6.0')
s.add_development_dependency('simplecov', '~> 0.6.4')
end
25 changes: 14 additions & 11 deletions lib/chefspec/chef_runner.rb
@@ -1,6 +1,8 @@
require 'chef'
require 'chef/client'
require 'chef/cookbook_loader'
require 'fauxhai'

require 'chefspec/matchers/shared'

# ChefSpec allows you to write rspec examples for Chef recipes to gain faster feedback without the need to converge a
Expand All @@ -22,16 +24,18 @@ class ChefRunner
# @param [Hash] options The options for the new runner
# @option options [String] :cookbook_path The path to the chef cookbook(s) to be tested.
# @option options [Symbol] :log_level The log level to use (default is :warn)
# @option options [String] :platform The platform to load Ohai attributes from (must be present in fauxhai)
# @option options [String] :version The version of the platform to load Ohai attributes from (must be present in fauxhai)
# @yield [node] Configuration block for Chef::Node
def initialize(options={})
defaults = {:cookbook_path => default_cookbook_path, :log_level => :warn, :dry_run => false, :step_into => []}
options = {:cookbook_path => options} unless options.respond_to?(:to_hash) # backwards-compatibility
options = defaults.merge(options)
@options = defaults.merge(options)

the_runner = self
@resources = []
@step_into = options[:step_into]
@do_dry_run = options[:dry_run]
@step_into = @options[:step_into]
@do_dry_run = @options[:dry_run]

Chef::Resource.class_eval do
alias :old_run_action :run_action unless method_defined?(:old_run_action)
Expand Down Expand Up @@ -69,10 +73,10 @@ def run_action(*args)
Chef::Config[:solo] = true
Chef::Config[:cache_type] = "Memory"
Chef::Cookbook::FileVendor.on_create { |manifest| Chef::Cookbook::FileSystemFileVendor.new(manifest) }
Chef::Config[:cookbook_path] = options[:cookbook_path]
Chef::Config[:cookbook_path] = @options[:cookbook_path]
Chef::Config[:client_key] = nil
Chef::Log.verbose = true if Chef::Log.respond_to?(:verbose)
Chef::Log.level(options[:log_level])
Chef::Log.level(@options[:log_level])
@client = Chef::Client.new
fake_ohai(@client.ohai)
@client.load_node if @client.respond_to?(:load_node) # chef >= 10.14.0
Expand Down Expand Up @@ -136,13 +140,13 @@ def to_s
# does conditional execution based on these values or additional attributes you can set these via
# node.automatic_attrs.
#
# This method now relies on fauxhai to set node attributes.
#
# @param [Ohai::System] ohai The ohai instance to set fake attributes on
def fake_ohai(ohai)
{:os => 'chefspec', :os_version => ChefSpec::VERSION, :fqdn => 'chefspec.local', :domain => 'local',
:ipaddress => '127.0.0.1', :hostname => 'chefspec', :languages => Mash.new({"ruby" => "/usr/somewhere"}),
:kernel => Mash.new({:machine => 'i386'})}.each_pair do |attribute,value|
ohai[attribute] = value
end
::Fauxhai::Mocker.new(:platform => @options[:platform], :version => @options[:version]).data.each_pair do |attribute, value|
ohai[attribute] = value
end
end

# Infer the default cookbook path from the location of the calling spec.
Expand All @@ -160,7 +164,6 @@ def default_cookbook_path
def find_resource(type, name)
resources.find{|resource| resource_type(resource) == type and resource.name == name}
end

end

end
2 changes: 1 addition & 1 deletion lib/chefspec/version.rb
@@ -1,4 +1,4 @@
module ChefSpec
# The gem version
VERSION = '0.9.0'
VERSION = '1.0.0'
end
29 changes: 20 additions & 9 deletions spec/chefspec/chef_runner_spec.rb
Expand Up @@ -64,15 +64,26 @@ module ChefSpec
runner.node.foo.should == 'baz'
end
context "default ohai attributes" do
let(:node){ChefSpec::ChefRunner.new.node}
specify{node.os.should == 'chefspec'}
specify{node.languages['ruby'].should == "/usr/somewhere"}
specify{node.os_version.should == ChefSpec::VERSION}
specify{node.fqdn.should == 'chefspec.local'}
specify{node.domain.should == 'local'}
specify{node.ipaddress.should == '127.0.0.1'}
specify{node.hostname.should == 'chefspec'}
specify{node.kernel.machine.should == 'i386'}
let(:node){ChefSpec::ChefRunner.new(:platform => 'chefspec', :version => '0.6.1').node}
specify{node['os'].should == 'chefspec'}
specify{node['languages']['ruby'].should == "/usr/somewhere"}
specify{node['os_version'].should == '0.6.1'}
specify{node['fqdn'].should == 'chefspec.local'}
specify{node['domain'].should == 'local'}
specify{node['ipaddress'].should == '127.0.0.1'}
specify{node['hostname'].should == 'chefspec'}
specify{node['kernel']['machine'].should == 'i386'}
end
context "fauxhai delegation" do
let(:node){ChefSpec::ChefRunner.new(:platform => 'ubuntu', :version => '12.04').node}
specify{node['os'].should == 'linux'}
specify{node['languages']['ruby']['ruby_bin'].should == '/usr/local/bin/ruby'}
specify{node['os_version'].should == '3.2.0-26-generic'}
specify{node['fqdn'].should == 'fauxhai.local'}
specify{node['domain'].should == 'local'}
specify{node['ipaddress'].should == '10.0.0.2'}
specify{node['hostname'].should == 'Fauxhai'}
specify{node['kernel']['machine'].should == 'x86_64'}
end
end
describe "#converge" do
Expand Down

0 comments on commit 3e0990d

Please sign in to comment.