Skip to content

Commit

Permalink
More fixes for CHEF-1292: updated comments, refactored feature tests.…
Browse files Browse the repository at this point in the history
… Added

some require's
  • Loading branch information
Tim Hinderliter committed Nov 24, 2010
1 parent 12dce10 commit f6e94c1
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 20 deletions.
13 changes: 9 additions & 4 deletions chef-server-api/app/controllers/application.rb
Expand Up @@ -119,13 +119,18 @@ def get_available_recipes
available_recipes
end

# Use Chef's JSON conversion library for sending JSON instead of the
# default Merb, which calls obj.to_json. Fixes CHEF-1292/PL-538.
# Fix CHEF-1292/PL-538; cause Merb to pass the max nesting constant into
# obj.to_json, which it calls by default based on the original request's
# accept headers and the type passed into Merb::Controller.display
#
# TODO: tim, 2010-11-24: would be nice to instead have Merb call
# Chef::JSON.to_json, instead of obj.to_json, but changing that
# behavior is convoluted in Merb. This override is assuming that
# Merb is eventually calling obj.to_json, which takes the :max_nesting
# option.
override! :display
def display(obj)
super(obj, nil, {:max_nesting => Chef::JSON::JSON_MAX_NESTING})
#super.display(Chef::JSON.to_json(obj))
#Chef::JSON.to_json(obj)
end

end
Expand Down
1 change: 0 additions & 1 deletion chef/lib/chef/json.rb
Expand Up @@ -41,7 +41,6 @@ def from_json(source, opts = {})
end

def to_json(obj, opts = nil)
#::JSON.generate(obj, opts_add_max_nesting(opts))
obj.to_json(opts_add_max_nesting(opts))
end

Expand Down
7 changes: 7 additions & 0 deletions chef/lib/chef/provider/deploy/revision.rb
@@ -1,6 +1,9 @@
#
# Author:: Daniel DeLeo (<dan@kallistec.com>)
# Author:: Tim Hinderliter (<tim@opscode.com>)
# Author:: Seth Falcon (<seth@opscode.com>)
# Copyright:: Copyright (c) 2009 Daniel DeLeo
# Copyright:: Copyright (c) 2010 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,6 +19,10 @@
# limitations under the License.
#

require 'chef/provider'
require 'chef/provider/deploy'
require 'chef/json'

class Chef
class Provider
class Deploy
Expand Down
17 changes: 9 additions & 8 deletions chef/lib/chef/shef.rb
Expand Up @@ -15,17 +15,18 @@
# limitations under the License.
#

require "singleton"
require "pp"
require "etc"
require "mixlib/cli"
require 'singleton'
require 'pp'
require 'etc'
require 'mixlib/cli'

require 'chef/version'
require "chef/client"
require "chef/config"
require 'chef/client'
require 'chef/config'

require "chef/shef/shef_session"
require "chef/shef/ext"
require 'chef/shef/shef_session'
require 'chef/shef/ext'
require 'chef/json'

# = Shef
# Shef is Chef in an IRB session. Shef can interact with a Chef server via the
Expand Down
1 change: 1 addition & 0 deletions chef/lib/chef/shef/ext.rb
Expand Up @@ -23,6 +23,7 @@
require 'chef/shef/shef_session'
require 'chef/shef/model_wrapper'
require 'chef/shef/shef_rest'
require 'chef/json'

module Shef
module Extensions
Expand Down
7 changes: 4 additions & 3 deletions features/api/nodes/deep_node_show_save.feature
@@ -1,13 +1,14 @@
@api @api_nodes @nodes_show @json_recusion @pl_538
@api @api_nodes @nodes_show @json_recusion @pl_538 @chef_1292
Feature: Save and show a node with deep structure via the API
In order to verify that I can save and show very deep structure in a node object
As a Developer
I want to show the details for a specific node

Scenario: Show a really deep node
Scenario: Show a deeply nested node
Given I am an administrator
And a 'node' named 'really_deep_node' exists
When I 'GET' the path '/nodes/really_deep_node'
Then I should not get an exception
And the inflated response should respond to 'name' with 'really_deep_node'
And the inflated response should respond to 'deep_array' and match '.*10,\"really_deep_string\".*' as json
And the 'deep_array' component has depth of '50' levels
And the 'deep_hash' component has depth of '50' levels
11 changes: 7 additions & 4 deletions features/steps/fixture_steps.rb
Expand Up @@ -210,17 +210,20 @@
end,
'really_deep_node' => Proc.new do
array = []
hash = {}
max_levels = 50
num_level = 0
begin
array = [num_level, "really_deep_string", array]

max_levels.times do |num_level|
array = [num_level, "really_deep_string_in_array", array]
hash = {"really_deep_string_in_hash_#{num_level}" => hash}
num_level += 1
end while num_level < max_levels
end

n = Chef::Node.new
n.name 'really_deep_node'
n.run_list << "deep_node_recipe"
n.deep_array = array
n.deep_hash = hash
n
end
},
Expand Down
30 changes: 30 additions & 0 deletions features/steps/response_steps.rb
@@ -1,3 +1,28 @@

# Walk array/hash to determine maximum depth. A scalar (anything but an
# Array or Hash) has depth 0.
def count_structure_levels(obj)
if obj.respond_to?(:keys)
# empty hash also has depth 0.
max_depth = 0
obj.keys.each do |key|
child_levels = 1 + count_structure_levels(obj[key])
max_depth = [max_depth, child_levels].max
end
max_depth
elsif obj.is_a?(Array)
# empty array also has depth 0.
max_depth = 0
obj.each do |child|
child_levels = 1 + count_structure_levels(child)
max_depth = [max_depth, child_levels].max
end
max_depth
else
0
end
end

Then /^I should get a '(.+)' exception$/ do |exception|
self.exception.message.to_s.should == exception
end
Expand Down Expand Up @@ -149,6 +174,11 @@
Chef::JSON.to_json(self.inflated_response.to_hash[method]).should =~ /#{regex}/m
end

#And the 'deep_array' component has depth of '50' levels
Then /^the '(.+)' component has depth of '(.+)' levels$/ do |method, levels|
count_structure_levels(self.inflated_response.to_hash[method]).should == levels.to_i
end

Then /^the fields in the inflated response should match the '(.+)'$/ do |stash_name|
self.inflated_response.each do |k,v|
unless k =~ /^_/ || k == 'couchrest-type'
Expand Down

0 comments on commit f6e94c1

Please sign in to comment.