From 4529c10898d7ae822b2ddb22ae5d04fafeaff449 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 7 Jan 2013 21:17:12 -0500 Subject: [PATCH 01/10] Add fauxhai to gemspec Only lock to '~> 0.0' because fauxhai won't have a major change without a major version bump --- chefspec.gemspec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/chefspec.gemspec b/chefspec.gemspec index bfef7c1b..81a52da2 100644 --- a/chefspec.gemspec +++ b/chefspec.gemspec @@ -12,8 +12,10 @@ 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.0') s.add_dependency('minitest-chef-handler', '~> 0.6.0') s.add_dependency('rspec', '~> 2.11.0') end From 0d8d99705662b6a836c6ed0e3f51ed4c196dbf46 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 7 Jan 2013 21:18:30 -0500 Subject: [PATCH 02/10] Compile against Ruby 2.0 and Chef 10.16.4 --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b80c095b..ff951ec8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,9 @@ 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 From 4f32d0f216f030f143eb4750e15a275590a448a6 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 7 Jan 2013 21:56:08 -0500 Subject: [PATCH 03/10] Move :test and :development dependencies into the gemspec --- Gemfile | 14 +------------- chefspec.gemspec | 11 +++++++++++ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Gemfile b/Gemfile index 13c7cd7e..e45e65f8 100644 --- a/Gemfile +++ b/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 diff --git a/chefspec.gemspec b/chefspec.gemspec index 81a52da2..eb38760d 100644 --- a/chefspec.gemspec +++ b/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' @@ -18,4 +19,14 @@ Gem::Specification.new do |s| s.add_dependency('fauxhai', '~> 0.0') s.add_dependency('minitest-chef-handler', '~> 0.6.0') s.add_dependency('rspec', '~> 2.11.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 From d1816f2669b3dc888e49a4c16b90277e17c9d6b8 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 7 Jan 2013 21:57:21 -0500 Subject: [PATCH 04/10] Major version bump (necessary for conditional dependency on fauxhai) --- lib/chefspec/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chefspec/version.rb b/lib/chefspec/version.rb index 872acb0a..128fadc8 100644 --- a/lib/chefspec/version.rb +++ b/lib/chefspec/version.rb @@ -1,4 +1,4 @@ module ChefSpec # The gem version - VERSION = '0.9.0' + VERSION = '1.0.0' end From e2f21f656c5e45647b43b332936d2b6b1cbaefb7 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 7 Jan 2013 22:46:55 -0500 Subject: [PATCH 05/10] Upgrade rspec to 2.12.0 --- chefspec.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chefspec.gemspec b/chefspec.gemspec index eb38760d..c4833e9c 100644 --- a/chefspec.gemspec +++ b/chefspec.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.add_dependency('erubis', '>= 0') s.add_dependency('fauxhai', '~> 0.0') s.add_dependency('minitest-chef-handler', '~> 0.6.0') - s.add_dependency('rspec', '~> 2.11.0') + s.add_dependency('rspec', '~> 2.12.0') # Development Dependencies s.add_development_dependency('rake', '~> 0.9.2.2') From 2e6a9be7d1e6ecdfae7cac547965e6393fef6c02 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 7 Jan 2013 23:04:49 -0500 Subject: [PATCH 06/10] Delegate to fauxhai for node attributes (default remains chefspec) - Requires a major version bump (because fauxhai is expecting it) - sorry for tampering with the gemspec - **Is** backward compatible - Adds spec --- chefspec.gemspec | 2 +- lib/chefspec/chef_runner.rb | 25 +++++++++++++---------- spec/chefspec/chef_runner_spec.rb | 34 ++++++++++++++++++++++--------- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/chefspec.gemspec b/chefspec.gemspec index c4833e9c..98a9f401 100644 --- a/chefspec.gemspec +++ b/chefspec.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |s| s.add_dependency('chef', chef_version) s.add_dependency('erubis', '>= 0') - s.add_dependency('fauxhai', '~> 0.0') + s.add_dependency('fauxhai', '~> 0.1') s.add_dependency('minitest-chef-handler', '~> 0.6.0') s.add_dependency('rspec', '~> 2.12.0') diff --git a/lib/chefspec/chef_runner.rb b/lib/chefspec/chef_runner.rb index 3a3b7695..3a426f2e 100644 --- a/lib/chefspec/chef_runner.rb +++ b/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 diff --git a/spec/chefspec/chef_runner_spec.rb b/spec/chefspec/chef_runner_spec.rb index 72a5d9ca..f07d451e 100644 --- a/spec/chefspec/chef_runner_spec.rb +++ b/spec/chefspec/chef_runner_spec.rb @@ -63,16 +63,30 @@ module ChefSpec runner = ChefSpec::ChefRunner.new() {|node| node.set[:foo] = 'baz'} 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'} + # context "default ohai attributes" do + # 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} + it 'foo' do + node['os'].should == 'linux' + end + # 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 From 0fcee2eaba478a1c3ba8f54b8c719a5d5b1097bd Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 7 Jan 2013 23:11:44 -0500 Subject: [PATCH 07/10] Update README --- README.md | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index edaec52d..e16c5c4d 100644 --- a/README.md +++ b/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 From e39a62bea393ec8ea673138e1220f85b752e353b Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 7 Jan 2013 23:15:13 -0500 Subject: [PATCH 08/10] re-add failing tests (after fixing fauxhai) --- spec/chefspec/chef_runner_spec.rb | 41 ++++++++++++++----------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/spec/chefspec/chef_runner_spec.rb b/spec/chefspec/chef_runner_spec.rb index f07d451e..1a1a1484 100644 --- a/spec/chefspec/chef_runner_spec.rb +++ b/spec/chefspec/chef_runner_spec.rb @@ -63,30 +63,27 @@ module ChefSpec runner = ChefSpec::ChefRunner.new() {|node| node.set[:foo] = 'baz'} runner.node.foo.should == 'baz' end - # context "default ohai attributes" do - # 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 "default ohai attributes" do + 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} - it 'foo' do - node['os'].should == 'linux' - end - # 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'} + 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 From 8bb2ad51662c192d0c064998434ae8aa8cb7c31a Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 7 Jan 2013 23:23:26 -0500 Subject: [PATCH 09/10] Switch to 1.8 compatible hashes and add dependency on moneta --- chefspec.gemspec | 1 + lib/chefspec/chef_runner.rb | 2 +- spec/chefspec/chef_runner_spec.rb | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/chefspec.gemspec b/chefspec.gemspec index 98a9f401..1a4f5c13 100644 --- a/chefspec.gemspec +++ b/chefspec.gemspec @@ -18,6 +18,7 @@ Gem::Specification.new do |s| s.add_dependency('erubis', '>= 0') s.add_dependency('fauxhai', '~> 0.1') s.add_dependency('minitest-chef-handler', '~> 0.6.0') + s.add_dependency('moneta', '< 0.7.0') # https://github.com/opscode/chef/commit/c6b6103e3befa355c2645c35fc3b8ba0159375f0 s.add_dependency('rspec', '~> 2.12.0') # Development Dependencies diff --git a/lib/chefspec/chef_runner.rb b/lib/chefspec/chef_runner.rb index 3a426f2e..6c12b7e1 100644 --- a/lib/chefspec/chef_runner.rb +++ b/lib/chefspec/chef_runner.rb @@ -144,7 +144,7 @@ def to_s # # @param [Ohai::System] ohai The ohai instance to set fake attributes on def fake_ohai(ohai) - ::Fauxhai::Mocker.new(platform: @options[:platform], version: @options[:version]).data.each_pair do |attribute, value| + ::Fauxhai::Mocker.new(:platform => @options[:platform], :version => @options[:version]).data.each_pair do |attribute, value| ohai[attribute] = value end end diff --git a/spec/chefspec/chef_runner_spec.rb b/spec/chefspec/chef_runner_spec.rb index 1a1a1484..18a93733 100644 --- a/spec/chefspec/chef_runner_spec.rb +++ b/spec/chefspec/chef_runner_spec.rb @@ -64,7 +64,7 @@ module ChefSpec runner.node.foo.should == 'baz' end context "default ohai attributes" do - let(:node){ChefSpec::ChefRunner.new(platform:'chefspec', version:'0.6.1').node} + 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'} @@ -75,7 +75,7 @@ module ChefSpec specify{node['kernel']['machine'].should == 'i386'} end context "fauxhai delegation" do - let(:node){ChefSpec::ChefRunner.new(platform:'ubuntu', version:'12.04').node} + 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'} From 60038ca522df54146245fb47f413d34f8f68ba9e Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 10 Jan 2013 01:33:19 -0500 Subject: [PATCH 10/10] allow failures on 2.0 for now --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index ff951ec8..06e13cbb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,3 +8,6 @@ env: - CHEF_VERSION=0.10.10 - CHEF_VERSION=10.14.4 - CHEF_VERSION=10.16.4 +matrix: + allow_failures: + - rvm: 2.0.0