Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Integrate with fauxhai? #85

Merged
merged 10 commits into from

5 participants

@sethvargo
Owner

@acrmp what are you thoughts on integrating chefspec with fauxhai?

By integrate, I mean fauxhai will become a dependency of chefspec, and chefspec will load attributes from fauxhai where available.

@acrmp
Collaborator

Hi Seth,

Cool idea. I think that would really help new users getting started, rather than having to puzzle through why an attribute is not available. Any other feedback folks?

If you could put together a pull request for this that would be awesome.

Separately I was also toying with the idea of consuming Fauxhai's node attribute data from foodcritic, to flag references to Ohai attributes that are not available on all supported platforms.

Cheers,

Andrew.

@sethvargo
Owner

Hey Andrew,

I'm actually currently on my phone, but I can try and put a PR together this weekend.

In terms of consuming Fauxhai's node attributes for FC, I think that'd be an awesome idea! Let me know what I can do to help.

Have a great weekend,
Seth

@sethvargo
Owner

@acrmp I've converted this to a PR and I'm waiting on a Travis build

@sethvargo
Owner

@acrmp the build failure looks like someone Ruby 2.0 specific

@miketheman

@sethvargo you might want to add to the bottom of .travis.yml:

matrix:
  allow_failures:
    - rvm: 2.0.0
@tmatilai

Some of these commits fix build problems of the current master, so it would be nice to have this merged. Or at least cherry pick all the dependency and Travis stuff.

@sethvargo
Owner

@tmatilai I think @acrmp has just been swamped lately. I'm sure he'll take a look when he gets a chance.

@tmatilai

Yeah, just a tiny friendly poke. ;)

@ranjib ranjib merged commit 3e0990d into sethvargo:master
@sethvargo sethvargo deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 8, 2013
  1. Add fauxhai to gemspec

    authored
    Only lock to '~> 0.0' because fauxhai won't have a major change without a major version bump
  2. Upgrade rspec to 2.12.0

    authored
  3. Delegate to fauxhai for node attributes (default remains chefspec)

    authored
    - Requires a major version bump (because fauxhai is expecting it) - sorry for tampering with the gemspec
    - **Is** backward compatible
    - Adds spec
  4. Update README

    authored
Commits on Jan 10, 2013
This page is out of date. Refresh to see the latest.
View
6 .travis.yml
@@ -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
View
14 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
View
29 README.md
@@ -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
View
16 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'
@@ -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
View
25 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
@@ -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)
@@ -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
@@ -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.
@@ -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
View
2  lib/chefspec/version.rb
@@ -1,4 +1,4 @@
module ChefSpec
# The gem version
- VERSION = '0.9.0'
+ VERSION = '1.0.0'
end
View
29 spec/chefspec/chef_runner_spec.rb
@@ -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
Something went wrong with that request. Please try again.