Skip to content

Commit

Permalink
CHEF-2899: Ensure proper roles/node json when activesupport is present
Browse files Browse the repository at this point in the history
Added activesupport to its own group in bundler so it can be loaded on
machines for chef compatibility testing
  • Loading branch information
Mark Mzyk authored and seth committed Feb 2, 2012
1 parent 1fb9424 commit 9e11fdb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions chef/Gemfile
Expand Up @@ -3,6 +3,7 @@ source :rubygems
gemspec

gem "dep_selector", :group => :development, :platform => "ruby"
gem "activesupport", :group => :compat_testing, :platform => "ruby"

platforms :mswin, :mingw do
gem "ffi", "1.0.9"
Expand Down
3 changes: 2 additions & 1 deletion chef/lib/chef/node.rb
Expand Up @@ -495,7 +495,8 @@ def to_json(*a)
"chef_type" => "node",
"default" => default_attrs,
"override" => override_attrs,
"run_list" => run_list.run_list
#Render correctly for run_list items so malformed json does not result
"run_list" => run_list.run_list.map { |item| item.to_s }
}
result["_rev"] = couchdb_rev if couchdb_rev
result.to_json(*a)
Expand Down
10 changes: 8 additions & 2 deletions chef/lib/chef/role.rb
Expand Up @@ -168,8 +168,14 @@ def to_hash
"default_attributes" => @default_attributes,
"override_attributes" => @override_attributes,
"chef_type" => "role",
"run_list" => run_list,
"env_run_lists" => env_run_lists_without_default

#Render to_json correctly for run_list items (both run_list and evn_run_lists)
#so malformed json does not result
"run_list" => run_list.run_list.map { |item| item.to_s },
"env_run_lists" => env_run_lists_without_default.inject({}) do |accumulator, (k, v)|
accumulator[k] = v.map { |x| x.to_s }
accumulator
end
}
result["_rev"] = couchdb_rev if couchdb_rev
result
Expand Down
14 changes: 12 additions & 2 deletions chef/spec/unit/node_spec.rb
Expand Up @@ -17,6 +17,7 @@
#

require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
require 'ostruct'

describe Chef::Node do
before(:each) do
Expand Down Expand Up @@ -543,7 +544,7 @@
end

describe "json" do
it "should serialize itself as json" do
it "should serialize itself as json", :json => true do
@node.find_file("test.example.com")
json = Chef::JSONCompat.to_json(@node)
json.should =~ /json_class/
Expand All @@ -555,7 +556,16 @@
json.should =~ /run_list/
end

it "should deserialize itself from json" do
it 'should serialze valid json with a run list', :json => true do
#This test came about because activesupport mucks with Chef json serialization
#Test should pass with and without Activesupport
@node.run_list << {"type" => "role", "name" => 'Cthulu'}
@node.run_list << {"type" => "role", "name" => 'Hastur'}
json = Chef::JSONCompat.to_json(@node)
json.should =~ /\"run_list\":\[\"role\[Cthulu\]\",\"role\[Hastur\]\"\]/
end

it "should deserialize itself from json", :json => true do
@node.find_file("test.example.com")
json = Chef::JSONCompat.to_json(@node)
serialized_node = Chef::JSONCompat.from_json(json)
Expand Down
10 changes: 7 additions & 3 deletions chef/spec/unit/role_spec.rb
Expand Up @@ -161,7 +161,7 @@
end
end

describe "when serialized as JSON" do
describe "when serialized as JSON", :json => true do
before(:each) do
@role.name('mars_volta')
@role.description('Great band!')
Expand Down Expand Up @@ -192,6 +192,8 @@
end

it "should include 'run_list'" do
#Activesupport messes with Chef json formatting
#This test should pass with and without activesupport
@serialized_role.should =~ /"run_list":\["recipe\[one\]","recipe\[two\]","role\[a\]"\]/
end

Expand All @@ -201,7 +203,9 @@
@serialized_role = Chef::JSONCompat.from_json(Chef::JSONCompat.to_json(@role), :create_additions => false)
end

it "includes the per-environment run lists in the " do
it "includes the per-environment run lists" do
#Activesupport messes with Chef json formatting
#This test should pass with and without activesupport
@serialized_role["env_run_lists"]["production"].should == ['role[monitoring]', 'role[auditing]', 'role[apache]']
@serialized_role["env_run_lists"]["dev"].should == ["role[nginx]"]
end
Expand All @@ -213,7 +217,7 @@
end
end

describe "when created from JSON" do
describe "when created from JSON", :json => true do
before(:each) do
@role.name('mars_volta')
@role.description('Great band!')
Expand Down
14 changes: 14 additions & 0 deletions chef/tasks/rspec.rb
Expand Up @@ -39,6 +39,20 @@
t.pattern = FileList['spec/functional/**/*_spec.rb']
end

desc "Run the rspec tests with activesupport loaded"
RSpec::Core::RakeTask.new(:spec_activesupport) do |t|
#require activesupport to make sure it is on the system and fail early if not found
#this has no affect on rspec, which still has to load it itself
begin
require 'active_support/core_ext'
rescue LoadError
raise "ActiveSupport not found on system, it is needed to run these tests"
end

t.rspec_opts = ['--options', "\"#{CHEF_ROOT}/.rspec\"", "--require active_support/core_ext"]
t.pattern = FileList['spec/unit/**/*_spec.rb']
end

namespace :spec do
desc "Run all specs in spec directory with RCov"
RSpec::Core::RakeTask.new(:rcov) do |t|
Expand Down

0 comments on commit 9e11fdb

Please sign in to comment.