From 1507fba7fdeb853f0b8fbd2383457faa84ab176a Mon Sep 17 00:00:00 2001 From: Tim Hinderliter Date: Mon, 22 Nov 2010 15:13:25 -0800 Subject: Fix CHEF-1292/PL-538: make JSON.parse/to_json default recursion depth much higher than default of 19; wrap JSON with Chef::JSON (obj.to_json becomes Chef::JSON.to_json(obj); JSON.parse becomes Chef::JSON.from_json; JSON.pretty_generate becomes Chef::JSON.to_json_pretty); monkey-patch Merb::Request to handle this; override Merb::Controller.display to use this; many other modifications to to_json/from_json calls to use Chef::JSON --- chef-server-api/app/controllers/application.rb | 14 +++++- chef-server-api/config/router.rb | 33 ++++++++++++++ chef-server-webui/app/controllers/databag_items.rb | 4 +- chef-server-webui/app/controllers/nodes.rb | 6 +-- chef-server-webui/app/controllers/roles.rb | 16 +++---- chef/lib/chef/api_client.rb | 2 +- chef/lib/chef/application/client.rb | 2 +- chef/lib/chef/application/solo.rb | 2 +- chef/lib/chef/cookbook/metadata.rb | 4 +- chef/lib/chef/couchdb.rb | 2 +- chef/lib/chef/data_bag.rb | 2 +- chef/lib/chef/data_bag_item.rb | 2 +- chef/lib/chef/file_cache.rb | 2 +- chef/lib/chef/handler/json_file.rb | 2 +- chef/lib/chef/index_queue/consumer.rb | 4 +- chef/lib/chef/json.rb | 52 ++++++++++++++++++++++ chef/lib/chef/knife.rb | 12 ++--- chef/lib/chef/knife/bootstrap.rb | 2 +- chef/lib/chef/knife/client_bulk_delete.rb | 2 +- chef/lib/chef/knife/client_create.rb | 2 +- chef/lib/chef/knife/client_delete.rb | 2 +- chef/lib/chef/knife/client_edit.rb | 2 +- chef/lib/chef/knife/client_list.rb | 2 +- chef/lib/chef/knife/client_reregister.rb | 2 +- chef/lib/chef/knife/client_show.rb | 2 +- chef/lib/chef/knife/cookbook_create.rb | 2 +- chef/lib/chef/knife/cookbook_list.rb | 2 +- chef/lib/chef/knife/cookbook_metadata.rb | 2 +- chef/lib/chef/knife/cookbook_show.rb | 2 +- chef/lib/chef/knife/cookbook_site_share.rb | 2 +- chef/lib/chef/knife/cookbook_site_unshare.rb | 2 + chef/lib/chef/knife/data_bag_from_file.rb | 1 + chef/lib/chef/knife/ec2_instance_data.rb | 2 +- chef/lib/chef/knife/ec2_server_create.rb | 2 +- chef/lib/chef/knife/ec2_server_delete.rb | 2 +- chef/lib/chef/knife/ec2_server_list.rb | 2 +- chef/lib/chef/knife/node_bulk_delete.rb | 2 +- chef/lib/chef/knife/node_create.rb | 2 +- chef/lib/chef/knife/node_delete.rb | 2 +- chef/lib/chef/knife/node_edit.rb | 2 +- chef/lib/chef/knife/node_from_file.rb | 2 +- chef/lib/chef/knife/node_list.rb | 2 +- chef/lib/chef/knife/node_run_list_add.rb | 2 +- chef/lib/chef/knife/node_run_list_remove.rb | 2 +- chef/lib/chef/knife/node_show.rb | 2 +- chef/lib/chef/knife/rackspace_server_create.rb | 2 +- chef/lib/chef/knife/rackspace_server_delete.rb | 2 +- chef/lib/chef/knife/rackspace_server_list.rb | 2 +- chef/lib/chef/knife/role_bulk_delete.rb | 2 +- chef/lib/chef/knife/role_create.rb | 2 +- chef/lib/chef/knife/role_delete.rb | 2 +- chef/lib/chef/knife/role_edit.rb | 2 +- chef/lib/chef/knife/role_from_file.rb | 2 +- chef/lib/chef/knife/role_list.rb | 2 +- chef/lib/chef/knife/role_show.rb | 2 +- chef/lib/chef/knife/slicehost_images_list.rb | 2 +- chef/lib/chef/knife/slicehost_server_create.rb | 2 +- chef/lib/chef/knife/slicehost_server_delete.rb | 2 +- chef/lib/chef/knife/slicehost_server_list.rb | 2 +- chef/lib/chef/knife/terremark_server_create.rb | 2 +- chef/lib/chef/knife/terremark_server_delete.rb | 2 +- chef/lib/chef/knife/terremark_server_list.rb | 2 +- chef/lib/chef/node.rb | 3 +- chef/lib/chef/openid_registration.rb | 2 +- chef/lib/chef/provider/deploy/revision.rb | 2 +- chef/lib/chef/rest.rb | 14 +++--- chef/lib/chef/role.rb | 4 +- chef/lib/chef/shef.rb | 4 +- chef/lib/chef/shef/ext.rb | 4 +- chef/lib/chef/tasks/chef_repo.rake | 2 +- chef/lib/chef/webui_user.rb | 2 +- chef/spec/functional/tiny_server_spec.rb | 2 +- chef/spec/lib/chef/resource/zen_master.rb | 5 ++- chef/spec/tiny_server.rb | 2 +- chef/spec/unit/api_client_spec.rb | 2 +- chef/spec/unit/application/client_spec.rb | 4 +- chef/spec/unit/application/solo_spec.rb | 4 +- chef/spec/unit/checksum_spec.rb | 2 +- chef/spec/unit/cookbook/metadata_spec.rb | 4 +- chef/spec/unit/data_bag_item_spec.rb | 2 +- chef/spec/unit/data_bag_spec.rb | 2 +- chef/spec/unit/handler/json_file_spec.rb | 2 +- .../unit/knife/cookbook_metadata_from_file_spec.rb | 4 +- chef/spec/unit/knife/data_bag_show_spec.rb | 2 +- chef/spec/unit/mixin/deprecation_spec.rb | 2 +- chef/spec/unit/node_spec.rb | 6 +-- chef/spec/unit/openid_registration_spec.rb | 2 +- chef/spec/unit/resource_collection_spec.rb | 2 +- chef/spec/unit/resource_spec.rb | 2 +- chef/spec/unit/rest_spec.rb | 2 +- chef/spec/unit/role_spec.rb | 6 +-- chef/spec/unit/webui_user_spec.rb | 8 ++-- features/api/nodes/deep_node_show_save.feature | 13 ++++++ features/data/Rakefile | 2 +- features/data/node-load-test.rb | 2 +- features/steps/deploy_steps.rb | 2 +- features/steps/fixture_steps.rb | 19 +++++++- features/steps/response_steps.rb | 8 ++-- features/support/couchdb_replicate.rb | 6 +-- 99 files changed, 267 insertions(+), 139 deletions(-) create mode 100644 chef/lib/chef/json.rb create mode 100644 features/api/nodes/deep_node_show_save.feature diff --git a/chef-server-api/app/controllers/application.rb b/chef-server-api/app/controllers/application.rb index 72c1482087..0e2db2901e 100644 --- a/chef-server-api/app/controllers/application.rb +++ b/chef-server-api/app/controllers/application.rb @@ -22,6 +22,7 @@ require "chef/mixin/checksum" require "chef/cookbook_loader" require "mixlib/authentication/signatureverification" +require "chef/json" class Application < Merb::Controller @@ -102,11 +103,11 @@ class Application < Merb::Controller def access_denied raise Unauthorized, "You must authenticate first!" end - + def get_available_recipes all_cookbooks_list = Chef::CookbookVersion.cdb_list(true) available_recipes = all_cookbooks_list.sort{ |a,b| a.name.to_s <=> b.name.to_s }.inject([]) do |result, element| - element.recipes.sort.each do |r| + element.recipes.sort.each do |r| if r =~ /^(.+)::default$/ result << $1 else @@ -118,5 +119,14 @@ class Application < Merb::Controller 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. + 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 diff --git a/chef-server-api/config/router.rb b/chef-server-api/config/router.rb index b1d3c874a1..5883534c62 100644 --- a/chef-server-api/config/router.rb +++ b/chef-server-api/config/router.rb @@ -102,4 +102,37 @@ Merb::Router.prepare do match('/').to(:controller => 'main', :action =>'index').name(:top) + # Need to monkey patch Merb so that it inflates JSON input with a higher + # recursion depth allowed (the default is 19). See CHEF-1292/PL-538. + module Merb + class Request + # ==== Returns + # Hash:: Parameters from body if this is a JSON request. + # + # ==== Notes + # If the JSON object parses as a Hash, it will be merged with the + # parameters hash. If it parses to anything else (such as an Array, or + # if it inflates to an Object) it will be accessible via the inflated_object + # parameter. + # + # :api: private + def json_params + @json_params ||= begin + if Merb::Const::JSON_MIME_TYPE_REGEXP.match(content_type) + begin + # Call Chef's JSON utility instead of the default in Merb, + # JSON.parse. + jobj = Chef::JSON.from_json(raw_post) + jobj = jobj.to_mash if jobj.is_a?(Hash) + rescue JSON::ParserError + jobj = Mash.new + end + jobj.is_a?(Hash) ? jobj : { :inflated_object => jobj } + end + end + end + + end + end + end diff --git a/chef-server-webui/app/controllers/databag_items.rb b/chef-server-webui/app/controllers/databag_items.rb index 00310008f0..c3da721740 100644 --- a/chef-server-webui/app/controllers/databag_items.rb +++ b/chef-server-webui/app/controllers/databag_items.rb @@ -38,7 +38,7 @@ class DatabagItems < Application begin @databag_item = Chef::DataBagItem.new @databag_item.data_bag params[:databag_id] - @databag_item.raw_data = JSON.parse(params[:json_data]) + @databag_item.raw_data = Chef::JSON.from_json(params[:json_data]) raise ArgumentError, "Updating id is not allowed" unless @databag_item.raw_data['id'] == params[:id] #to be consistent with other objects, changing id is not allowed. @databag_item.save redirect(url(:databag_databag_items, :databag_id => params[:databag_id], :id => @databag_item.name), :message => { :notice => "Updated Databag Item #{@databag_item.name}" }) @@ -61,7 +61,7 @@ class DatabagItems < Application @databag_name = params[:databag_id] @databag_item = Chef::DataBagItem.new @databag_item.data_bag @databag_name - @databag_item.raw_data = JSON.parse(params[:json_data]) + @databag_item.raw_data = Chef::JSON.from_json(params[:json_data]) @databag_item.create redirect(url(:databag_databag_items, :databag_id => @databag_name), :message => { :notice => "Databag item created successfully" }) rescue => e diff --git a/chef-server-webui/app/controllers/nodes.rb b/chef-server-webui/app/controllers/nodes.rb index f2b9cbb30e..f28ccac668 100644 --- a/chef-server-webui/app/controllers/nodes.rb +++ b/chef-server-webui/app/controllers/nodes.rb @@ -89,14 +89,14 @@ class Nodes < Application begin @node = Chef::Node.new @node.name params[:name] - @node.normal_attrs = JSON.parse(params[:attributes]) + @node.normal_attrs = Chef::JSON.from_json(params[:attributes]) @node.run_list.reset!(params[:for_node] ? params[:for_node] : []) raise ArgumentError, "Node name cannot be blank" if (params[:name].nil? || params[:name].length==0) @node.create redirect(url(:nodes), :message => { :notice => "Created Node #{@node.name}" }) rescue => e Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") - @node.normal_attrs = JSON.parse(params[:attributes]) + @node.normal_attrs = Chef::JSON.from_json(params[:attributes]) @available_recipes = get_available_recipes @available_roles = Chef::Role.list.keys.sort @node.run_list params[:for_node] @@ -110,7 +110,7 @@ class Nodes < Application begin @node = Chef::Node.load(params[:id]) @node.run_list.reset!(params[:for_node] ? params[:for_node] : []) - @node.normal_attrs = JSON.parse(params[:attributes]) + @node.normal_attrs = Chef::JSON.from_json(params[:attributes]) @node.save @_message = { :notice => "Updated Node" } render :show diff --git a/chef-server-webui/app/controllers/roles.rb b/chef-server-webui/app/controllers/roles.rb index 12a663912b..bdf3186124 100644 --- a/chef-server-webui/app/controllers/roles.rb +++ b/chef-server-webui/app/controllers/roles.rb @@ -90,8 +90,8 @@ class Roles < Application @role.name(params[:name]) @role.run_list(params[:for_role] ? params[:for_role] : []) @role.description(params[:description]) if params[:description] != '' - @role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != '' - @role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != '' + @role.default_attributes(Chef::JSON.from_json(params[:default_attributes])) if params[:default_attributes] != '' + @role.override_attributes(Chef::JSON.from_json(params[:override_attributes])) if params[:override_attributes] != '' @role.create redirect(url(:roles), :message => { :notice => "Created Role #{@role.name}" }) rescue => e @@ -99,8 +99,8 @@ class Roles < Application @available_recipes = get_available_recipes @available_roles = Chef::Role.list.keys.sort @role = Chef::Role.new - @role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != '' - @role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != '' + @role.default_attributes(Chef::JSON.from_json(params[:default_attributes])) if params[:default_attributes] != '' + @role.override_attributes(Chef::JSON.from_json(params[:override_attributes])) if params[:override_attributes] != '' @run_list = Chef::RunList.new.reset!(Array(params[:for_role])) @_message = { :error => "Could not create role" } render :new @@ -113,8 +113,8 @@ class Roles < Application @role = Chef::Role.load(params[:id]) @role.run_list(params[:for_role] ? params[:for_role] : []) @role.description(params[:description]) if params[:description] != '' - @role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != '' - @role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != '' + @role.default_attributes(Chef::JSON.from_json(params[:default_attributes])) if params[:default_attributes] != '' + @role.override_attributes(Chef::JSON.from_json(params[:override_attributes])) if params[:override_attributes] != '' @role.save @_message = { :notice => "Updated Role" } render :show @@ -124,8 +124,8 @@ class Roles < Application @available_roles = Chef::Role.list.keys.sort @run_list = Chef::RunList.new.reset!( Array(params[:for_role])) Chef::Log.error(@run_list.inspect) - @role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != '' - @role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != '' + @role.default_attributes(Chef::JSON.from_json(params[:default_attributes])) if params[:default_attributes] != '' + @role.override_attributes(Chef::JSON.from_json(params[:override_attributes])) if params[:override_attributes] != '' @_message = {:error => "Could not update role #{params[:id]}"} render :edit end diff --git a/chef/lib/chef/api_client.rb b/chef/lib/chef/api_client.rb index 0dd7ee8042..d7eed5a16b 100644 --- a/chef/lib/chef/api_client.rb +++ b/chef/lib/chef/api_client.rb @@ -24,7 +24,7 @@ require 'chef/couchdb' require 'chef/certificate' require 'chef/index_queue' require 'extlib' -require 'json' +require 'chef/json' class Chef class ApiClient diff --git a/chef/lib/chef/application/client.rb b/chef/lib/chef/application/client.rb index 4d8addd9af..b8df754ece 100644 --- a/chef/lib/chef/application/client.rb +++ b/chef/lib/chef/application/client.rb @@ -181,7 +181,7 @@ class Chef::Application::Client < Chef::Application end begin - @chef_client_json = JSON.parse(json_io.read) + @chef_client_json = Chef::JSON.from_json(json_io.read) json_io.close unless json_io.closed? rescue JSON::ParserError => error Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2) diff --git a/chef/lib/chef/application/solo.rb b/chef/lib/chef/application/solo.rb index e1458195b8..35887d0d70 100644 --- a/chef/lib/chef/application/solo.rb +++ b/chef/lib/chef/application/solo.rb @@ -145,7 +145,7 @@ class Chef::Application::Solo < Chef::Application end begin - @chef_solo_json = JSON.parse(json_io.read) + @chef_solo_json = Chef::JSON.from_json(json_io.read) json_io.close unless json_io.closed? rescue JSON::ParserError => error Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2) diff --git a/chef/lib/chef/cookbook/metadata.rb b/chef/lib/chef/cookbook/metadata.rb index 2f31893ceb..79ef860b89 100644 --- a/chef/lib/chef/cookbook/metadata.rb +++ b/chef/lib/chef/cookbook/metadata.rb @@ -430,12 +430,12 @@ class Chef end def self.from_json(string) - o = JSON.parse(string) + o = Chef::JSON.from_json(string) self.from_hash(o) end def from_json(string) - o = JSON.parse(string) + o = Chef::JSON.from_json(string) from_hash(o) end diff --git a/chef/lib/chef/couchdb.rb b/chef/lib/chef/couchdb.rb index 27f4d2dc45..3dfd8ccbd8 100644 --- a/chef/lib/chef/couchdb.rb +++ b/chef/lib/chef/couchdb.rb @@ -21,7 +21,7 @@ require 'chef/config' require 'chef/rest' require 'chef/log' require 'digest/sha2' -require 'json' +require 'chef/json' # We want to fail on create if uuidtools isn't installed begin diff --git a/chef/lib/chef/data_bag.rb b/chef/lib/chef/data_bag.rb index 512293e717..f14812cf40 100644 --- a/chef/lib/chef/data_bag.rb +++ b/chef/lib/chef/data_bag.rb @@ -25,7 +25,7 @@ require 'chef/couchdb' require 'chef/data_bag_item' require 'chef/index_queue' require 'extlib' -require 'json' +require 'chef/json' class Chef class DataBag diff --git a/chef/lib/chef/data_bag_item.rb b/chef/lib/chef/data_bag_item.rb index d5fba01563..15d046efeb 100644 --- a/chef/lib/chef/data_bag_item.rb +++ b/chef/lib/chef/data_bag_item.rb @@ -25,7 +25,7 @@ require 'chef/couchdb' require 'chef/index_queue' require 'chef/data_bag' require 'extlib' -require 'json' +require 'chef/json' class Chef class DataBagItem diff --git a/chef/lib/chef/file_cache.rb b/chef/lib/chef/file_cache.rb index 9208f53d92..f558e67e9e 100644 --- a/chef/lib/chef/file_cache.rb +++ b/chef/lib/chef/file_cache.rb @@ -18,7 +18,7 @@ require 'chef/mixin/params_validate' require 'chef/mixin/create_path' require 'chef/exceptions' -require 'json' +require 'chef/json' require 'fileutils' class Chef diff --git a/chef/lib/chef/handler/json_file.rb b/chef/lib/chef/handler/json_file.rb index 580ede671b..8bac5232d0 100644 --- a/chef/lib/chef/handler/json_file.rb +++ b/chef/lib/chef/handler/json_file.rb @@ -41,7 +41,7 @@ class Chef build_report_dir savetime = Time.now.strftime("%Y%m%d%H%M%S") File.open(File.join(config[:path], "chef-run-report-#{savetime}.json"), "w") do |file| - file.puts JSON.pretty_generate(data) + file.puts Chef::JSON.to_json_pretty(data) end end diff --git a/chef/lib/chef/index_queue/consumer.rb b/chef/lib/chef/index_queue/consumer.rb index b7be42ed76..18e8d3b8fc 100644 --- a/chef/lib/chef/index_queue/consumer.rb +++ b/chef/lib/chef/index_queue/consumer.rb @@ -56,7 +56,7 @@ class Chef alias :start :run def call_action_for_message(message) - amqp_payload = JSON.parse(message[:payload], :create_additions => false, :max_nesting => false) + amqp_payload = Chef::JSON.from_json(message[:payload], :create_additions => false, :max_nesting => false) action = amqp_payload["action"].to_sym app_payload = amqp_payload["payload"] assert_method_whitelisted(action) @@ -73,4 +73,4 @@ class Chef end end -end \ No newline at end of file +end diff --git a/chef/lib/chef/json.rb b/chef/lib/chef/json.rb new file mode 100644 index 0000000000..1b1618c132 --- /dev/null +++ b/chef/lib/chef/json.rb @@ -0,0 +1,52 @@ +# +# Author:: Tim Hinderliter () +# Copyright:: Copyright (c) 2010 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Wrapper class for interacting with JSON. + +require 'json' + +class Chef + class JSON + JSON_MAX_NESTING = 1000 + + class < false) - output_parsed_again = JSON.parse(output.to_json, :create_additions => false) + object_parsed_again = Chef::JSON.from_json(Chef::JSON.to_json(object), :create_additions => false) + output_parsed_again = Chef::JSON.from_json(Chef::JSON.to_json(output), :create_additions => false) if object_parsed_again != output_parsed_again output.save self.msg("Saved #{output}") diff --git a/chef/lib/chef/knife/bootstrap.rb b/chef/lib/chef/knife/bootstrap.rb index 968c69076f..37a88f26a8 100644 --- a/chef/lib/chef/knife/bootstrap.rb +++ b/chef/lib/chef/knife/bootstrap.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' require 'tempfile' require 'erubis' diff --git a/chef/lib/chef/knife/client_bulk_delete.rb b/chef/lib/chef/knife/client_bulk_delete.rb index 9f7e0c8ace..a856c34dc3 100644 --- a/chef/lib/chef/knife/client_bulk_delete.rb +++ b/chef/lib/chef/knife/client_bulk_delete.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/api_client' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/client_create.rb b/chef/lib/chef/knife/client_create.rb index d25e77f4e2..963fe7bc60 100644 --- a/chef/lib/chef/knife/client_create.rb +++ b/chef/lib/chef/knife/client_create.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/api_client' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/client_delete.rb b/chef/lib/chef/knife/client_delete.rb index 2815369e5c..fa25b4188a 100644 --- a/chef/lib/chef/knife/client_delete.rb +++ b/chef/lib/chef/knife/client_delete.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/api_client' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/client_edit.rb b/chef/lib/chef/knife/client_edit.rb index 293e2ae904..cac55956de 100644 --- a/chef/lib/chef/knife/client_edit.rb +++ b/chef/lib/chef/knife/client_edit.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/api_client' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/client_list.rb b/chef/lib/chef/knife/client_list.rb index 1a0942ae4a..43ee6e4632 100644 --- a/chef/lib/chef/knife/client_list.rb +++ b/chef/lib/chef/knife/client_list.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/api_client' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/client_reregister.rb b/chef/lib/chef/knife/client_reregister.rb index e865b8bf66..257b72da39 100644 --- a/chef/lib/chef/knife/client_reregister.rb +++ b/chef/lib/chef/knife/client_reregister.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/api_client' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/client_show.rb b/chef/lib/chef/knife/client_show.rb index 7c84b6cfab..b8078ccf78 100644 --- a/chef/lib/chef/knife/client_show.rb +++ b/chef/lib/chef/knife/client_show.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/api_client' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/cookbook_create.rb b/chef/lib/chef/knife/cookbook_create.rb index 3098a27d9b..aed540ba6b 100644 --- a/chef/lib/chef/knife/cookbook_create.rb +++ b/chef/lib/chef/knife/cookbook_create.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' require 'uri' require 'chef/mixin/shell_out' diff --git a/chef/lib/chef/knife/cookbook_list.rb b/chef/lib/chef/knife/cookbook_list.rb index 9d201742d4..857431cbbc 100644 --- a/chef/lib/chef/knife/cookbook_list.rb +++ b/chef/lib/chef/knife/cookbook_list.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/cookbook_metadata.rb b/chef/lib/chef/knife/cookbook_metadata.rb index 8182e57502..f15f47b7d3 100644 --- a/chef/lib/chef/knife/cookbook_metadata.rb +++ b/chef/lib/chef/knife/cookbook_metadata.rb @@ -69,7 +69,7 @@ class Chef md.from_file(file) json_file = File.join(File.dirname(file), 'metadata.json') File.open(json_file, "w") do |f| - f.write(JSON.pretty_generate(md)) + f.write(Chef::JSON.to_json_pretty(md)) end generated = true Chef::Log.debug("Generated #{json_file}") diff --git a/chef/lib/chef/knife/cookbook_show.rb b/chef/lib/chef/knife/cookbook_show.rb index 7537c97cca..2b858e1569 100644 --- a/chef/lib/chef/knife/cookbook_show.rb +++ b/chef/lib/chef/knife/cookbook_show.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' require 'uri' class Chef diff --git a/chef/lib/chef/knife/cookbook_site_share.rb b/chef/lib/chef/knife/cookbook_site_share.rb index 3a2f7e5ec3..356902eed2 100644 --- a/chef/lib/chef/knife/cookbook_site_share.rb +++ b/chef/lib/chef/knife/cookbook_site_share.rb @@ -85,7 +85,7 @@ class Chef :cookbook => category_string }) - res = JSON.parse(http_resp.body) + res = Chef::JSON.from_json(http_resp.body) if http_resp.code.to_i != 201 if res['error_messages'] if res['error_messages'][0] =~ /Version already exists/ diff --git a/chef/lib/chef/knife/cookbook_site_unshare.rb b/chef/lib/chef/knife/cookbook_site_unshare.rb index e870dbb814..6c3736f7c1 100644 --- a/chef/lib/chef/knife/cookbook_site_unshare.rb +++ b/chef/lib/chef/knife/cookbook_site_unshare.rb @@ -18,6 +18,8 @@ # require 'chef/knife' +require 'chef/environment' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/data_bag_from_file.rb b/chef/lib/chef/knife/data_bag_from_file.rb index 2c8417a4f9..6a513e5dc5 100644 --- a/chef/lib/chef/knife/data_bag_from_file.rb +++ b/chef/lib/chef/knife/data_bag_from_file.rb @@ -19,6 +19,7 @@ require 'chef/knife' require 'chef/data_bag' require 'chef/data_bag_item' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/ec2_instance_data.rb b/chef/lib/chef/knife/ec2_instance_data.rb index b69a2330c1..43265b5cc2 100644 --- a/chef/lib/chef/knife/ec2_instance_data.rb +++ b/chef/lib/chef/knife/ec2_instance_data.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/ec2_server_create.rb b/chef/lib/chef/knife/ec2_server_create.rb index 3311786817..ae9c108acd 100644 --- a/chef/lib/chef/knife/ec2_server_create.rb +++ b/chef/lib/chef/knife/ec2_server_create.rb @@ -18,7 +18,7 @@ require 'socket' require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/ec2_server_delete.rb b/chef/lib/chef/knife/ec2_server_delete.rb index 161c558b37..1f1d45c55c 100644 --- a/chef/lib/chef/knife/ec2_server_delete.rb +++ b/chef/lib/chef/knife/ec2_server_delete.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/ec2_server_list.rb b/chef/lib/chef/knife/ec2_server_list.rb index 0711cbcf6a..7dabc4688d 100644 --- a/chef/lib/chef/knife/ec2_server_list.rb +++ b/chef/lib/chef/knife/ec2_server_list.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/node_bulk_delete.rb b/chef/lib/chef/knife/node_bulk_delete.rb index 1958c63836..621b8ae0ec 100644 --- a/chef/lib/chef/knife/node_bulk_delete.rb +++ b/chef/lib/chef/knife/node_bulk_delete.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/node_create.rb b/chef/lib/chef/knife/node_create.rb index 4f14d21ab8..004fea7df1 100644 --- a/chef/lib/chef/knife/node_create.rb +++ b/chef/lib/chef/knife/node_create.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/node_delete.rb b/chef/lib/chef/knife/node_delete.rb index 03113b6003..eef13a46c6 100644 --- a/chef/lib/chef/knife/node_delete.rb +++ b/chef/lib/chef/knife/node_delete.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/node_edit.rb b/chef/lib/chef/knife/node_edit.rb index 5460af5ab0..4ca64c6a59 100644 --- a/chef/lib/chef/knife/node_edit.rb +++ b/chef/lib/chef/knife/node_edit.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/node_from_file.rb b/chef/lib/chef/knife/node_from_file.rb index e644ca17a5..0020827d4e 100644 --- a/chef/lib/chef/knife/node_from_file.rb +++ b/chef/lib/chef/knife/node_from_file.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/node_list.rb b/chef/lib/chef/knife/node_list.rb index 6371b655b6..4a916a3096 100644 --- a/chef/lib/chef/knife/node_list.rb +++ b/chef/lib/chef/knife/node_list.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/node_run_list_add.rb b/chef/lib/chef/knife/node_run_list_add.rb index 9e1602a1e2..41bcf93670 100644 --- a/chef/lib/chef/knife/node_run_list_add.rb +++ b/chef/lib/chef/knife/node_run_list_add.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/node_run_list_remove.rb b/chef/lib/chef/knife/node_run_list_remove.rb index 7bdfc9a66b..a40488abf9 100644 --- a/chef/lib/chef/knife/node_run_list_remove.rb +++ b/chef/lib/chef/knife/node_run_list_remove.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/node_show.rb b/chef/lib/chef/knife/node_show.rb index 971354ba66..f9e8393e96 100644 --- a/chef/lib/chef/knife/node_show.rb +++ b/chef/lib/chef/knife/node_show.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/rackspace_server_create.rb b/chef/lib/chef/knife/rackspace_server_create.rb index 64e16b02f9..5c2347b8f0 100644 --- a/chef/lib/chef/knife/rackspace_server_create.rb +++ b/chef/lib/chef/knife/rackspace_server_create.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/rackspace_server_delete.rb b/chef/lib/chef/knife/rackspace_server_delete.rb index e6b133f7c6..db9f6b021a 100644 --- a/chef/lib/chef/knife/rackspace_server_delete.rb +++ b/chef/lib/chef/knife/rackspace_server_delete.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/rackspace_server_list.rb b/chef/lib/chef/knife/rackspace_server_list.rb index bae3548cc6..ea0a38f3ab 100644 --- a/chef/lib/chef/knife/rackspace_server_list.rb +++ b/chef/lib/chef/knife/rackspace_server_list.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/role_bulk_delete.rb b/chef/lib/chef/knife/role_bulk_delete.rb index 1e4b533c36..95268c799c 100644 --- a/chef/lib/chef/knife/role_bulk_delete.rb +++ b/chef/lib/chef/knife/role_bulk_delete.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/role_create.rb b/chef/lib/chef/knife/role_create.rb index e81b883fdb..d222c0e2ac 100644 --- a/chef/lib/chef/knife/role_create.rb +++ b/chef/lib/chef/knife/role_create.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/role' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/role_delete.rb b/chef/lib/chef/knife/role_delete.rb index 14e74ba110..989e1eec42 100644 --- a/chef/lib/chef/knife/role_delete.rb +++ b/chef/lib/chef/knife/role_delete.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/role' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/role_edit.rb b/chef/lib/chef/knife/role_edit.rb index 78cad69471..48194bde9b 100644 --- a/chef/lib/chef/knife/role_edit.rb +++ b/chef/lib/chef/knife/role_edit.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/role' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/role_from_file.rb b/chef/lib/chef/knife/role_from_file.rb index a12589c4df..ffa4aac6e5 100644 --- a/chef/lib/chef/knife/role_from_file.rb +++ b/chef/lib/chef/knife/role_from_file.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/role' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/role_list.rb b/chef/lib/chef/knife/role_list.rb index c617d4aa24..a02979fae4 100644 --- a/chef/lib/chef/knife/role_list.rb +++ b/chef/lib/chef/knife/role_list.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/role_show.rb b/chef/lib/chef/knife/role_show.rb index 952260ae27..14e34c8d0d 100644 --- a/chef/lib/chef/knife/role_show.rb +++ b/chef/lib/chef/knife/role_show.rb @@ -18,7 +18,7 @@ require 'chef/knife' require 'chef/node' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/slicehost_images_list.rb b/chef/lib/chef/knife/slicehost_images_list.rb index 1adf6edf6e..af9ad827d2 100644 --- a/chef/lib/chef/knife/slicehost_images_list.rb +++ b/chef/lib/chef/knife/slicehost_images_list.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/slicehost_server_create.rb b/chef/lib/chef/knife/slicehost_server_create.rb index 086fd387bc..19f802449b 100644 --- a/chef/lib/chef/knife/slicehost_server_create.rb +++ b/chef/lib/chef/knife/slicehost_server_create.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/slicehost_server_delete.rb b/chef/lib/chef/knife/slicehost_server_delete.rb index 2d065ea4af..e163a3ae61 100644 --- a/chef/lib/chef/knife/slicehost_server_delete.rb +++ b/chef/lib/chef/knife/slicehost_server_delete.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/slicehost_server_list.rb b/chef/lib/chef/knife/slicehost_server_list.rb index 17920094c3..64a16b11b1 100644 --- a/chef/lib/chef/knife/slicehost_server_list.rb +++ b/chef/lib/chef/knife/slicehost_server_list.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/terremark_server_create.rb b/chef/lib/chef/knife/terremark_server_create.rb index c32fd9315d..03136ab8c7 100644 --- a/chef/lib/chef/knife/terremark_server_create.rb +++ b/chef/lib/chef/knife/terremark_server_create.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' require 'tempfile' class Chef diff --git a/chef/lib/chef/knife/terremark_server_delete.rb b/chef/lib/chef/knife/terremark_server_delete.rb index aeb195c70d..8b3475c6fc 100644 --- a/chef/lib/chef/knife/terremark_server_delete.rb +++ b/chef/lib/chef/knife/terremark_server_delete.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' class Chef class Knife diff --git a/chef/lib/chef/knife/terremark_server_list.rb b/chef/lib/chef/knife/terremark_server_list.rb index 20dadbedff..3e90d4cfca 100644 --- a/chef/lib/chef/knife/terremark_server_list.rb +++ b/chef/lib/chef/knife/terremark_server_list.rb @@ -17,7 +17,7 @@ # require 'chef/knife' -require 'json' +require 'chef/json' require 'tempfile' class Chef diff --git a/chef/lib/chef/node.rb b/chef/lib/chef/node.rb index a0aed92258..6bb131a75f 100644 --- a/chef/lib/chef/node.rb +++ b/chef/lib/chef/node.rb @@ -32,7 +32,7 @@ require 'chef/run_list' require 'chef/node/attribute' require 'chef/index_queue' require 'extlib' -require 'json' +require 'chef/json' class Chef class Node @@ -596,6 +596,5 @@ class Chef self.from_file(attribute_filename) self end - end end diff --git a/chef/lib/chef/openid_registration.rb b/chef/lib/chef/openid_registration.rb index 82bee0798e..a97b34d1bc 100644 --- a/chef/lib/chef/openid_registration.rb +++ b/chef/lib/chef/openid_registration.rb @@ -21,7 +21,7 @@ require 'chef/mixin/params_validate' require 'chef/couchdb' require 'chef/index_queue' require 'digest/sha1' -require 'json' +require 'chef/json' class Chef class OpenIDRegistration diff --git a/chef/lib/chef/provider/deploy/revision.rb b/chef/lib/chef/provider/deploy/revision.rb index 00a30515ba..18c2c03f8a 100644 --- a/chef/lib/chef/provider/deploy/revision.rb +++ b/chef/lib/chef/provider/deploy/revision.rb @@ -56,7 +56,7 @@ class Chef def load_cache begin - JSON.parse(Chef::FileCache.load("revision-deploys/#{new_resource.name}")) + Chef::JSON.from_json(Chef::FileCache.load("revision-deploys/#{new_resource.name}")) rescue Chef::Exceptions::FileNotFound sorted_releases_from_filesystem end diff --git a/chef/lib/chef/rest.rb b/chef/lib/chef/rest.rb index b721a1bd27..052fa2a513 100644 --- a/chef/lib/chef/rest.rb +++ b/chef/lib/chef/rest.rb @@ -22,7 +22,7 @@ require 'net/https' require 'uri' -require 'json' +require 'chef/json' require 'tempfile' require 'chef/api_client' require 'chef/rest/auth_credentials' @@ -162,7 +162,7 @@ class Chef # # Will return the body of the response on success. def run_request(method, url, headers={}, data=false, limit=nil, raw=false) - json_body = data ? data.to_json : nil + json_body = data ? Chef::JSON.to_json(data) : nil headers = build_headers(method, url, headers, json_body, raw) tf = nil @@ -180,7 +180,7 @@ class Chef if res.kind_of?(Net::HTTPSuccess) if res['content-type'] =~ /json/ response_body = res.body.chomp - JSON.parse(response_body) + Chef::JSON.from_json(response_body) else if method == :HEAD true @@ -196,7 +196,7 @@ class Chef false else if res['content-type'] =~ /json/ - exception = JSON.parse(res.body) + exception = Chef::JSON.from_json(res.body) msg = "HTTP Request Returned #{res.code} #{res.message}: " msg << (exception["error"].respond_to?(:join) ? exception["error"].join(", ") : exception["error"].to_s) Chef::Log.warn(msg) @@ -208,7 +208,7 @@ class Chef # Runs an HTTP request to a JSON API. File Download not supported. def api_request(method, url, headers={}, data=false) - json_body = data ? data.to_json : nil + json_body = data ? Chef::JSON.to_json(data) : nil headers = build_headers(method, url, headers, json_body) retriable_rest_request(method, url, json_body, headers) do |rest_request| @@ -216,7 +216,7 @@ class Chef if response.kind_of?(Net::HTTPSuccess) if response['content-type'] =~ /json/ - JSON.parse(response.body.chomp) + Chef::JSON.from_json(response.body.chomp) else Chef::Log.warn("Expected JSON response, but got content-type '#{response['content-type']}'") response.body @@ -225,7 +225,7 @@ class Chef follow_redirect {api_request(:GET, create_url(redirect_location))} else if response['content-type'] =~ /json/ - exception = JSON.parse(response.body) + exception = Chef::JSON.from_json(response.body) msg = "HTTP Request Returned #{response.code} #{response.message}: " msg << (exception["error"].respond_to?(:join) ? exception["error"].join(", ") : exception["error"].to_s) Chef::Log.warn(msg) diff --git a/chef/lib/chef/role.rb b/chef/lib/chef/role.rb index bf0aa32b7a..802ad28752 100644 --- a/chef/lib/chef/role.rb +++ b/chef/lib/chef/role.rb @@ -25,7 +25,7 @@ require 'chef/couchdb' require 'chef/run_list' require 'chef/index_queue' require 'extlib' -require 'json' +require 'chef/json' class Chef class Role @@ -255,7 +255,7 @@ class Chef rb_file = File.join(Chef::Config[:role_path], "#{name}.rb") if File.exists?(js_file) || force == "json" - JSON.parse(IO.read(js_file)) + Chef::JSON.from_json(IO.read(js_file)) elsif File.exists?(rb_file) || force == "ruby" role = Chef::Role.new role.name(name) diff --git a/chef/lib/chef/shef.rb b/chef/lib/chef/shef.rb index e8a224f1e2..bcab49fb4b 100644 --- a/chef/lib/chef/shef.rb +++ b/chef/lib/chef/shef.rb @@ -163,7 +163,7 @@ module Shef end begin - @json_attribs = JSON.parse(json_io.read) + @json_attribs = Chef::JSON.from_json(json_io.read) rescue JSON::ParserError => error fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2) end @@ -322,4 +322,4 @@ FOOTER end -end \ No newline at end of file +end diff --git a/chef/lib/chef/shef/ext.rb b/chef/lib/chef/shef/ext.rb index a601e86786..77c63cfc07 100644 --- a/chef/lib/chef/shef/ext.rb +++ b/chef/lib/chef/shef/ext.rb @@ -327,13 +327,13 @@ E edited_data = Tempfile.open([filename, ".js"]) do |tempfile| tempfile.sync = true - tempfile.puts object.to_json + tempfile.puts Chef::JSON.to_json(object) system("#{Shef.editor.to_s} #{tempfile.path}") tempfile.rewind tempfile.read end - JSON.parse(edited_data) + Chef::JSON.from_json(edited_data) end desc "Find and edit API clients" diff --git a/chef/lib/chef/tasks/chef_repo.rake b/chef/lib/chef/tasks/chef_repo.rake index cae90c7e67..271a57ee6a 100644 --- a/chef/lib/chef/tasks/chef_repo.rake +++ b/chef/lib/chef/tasks/chef_repo.rake @@ -17,7 +17,7 @@ # require 'rubygems' -require 'json' +require 'chef/json' require 'chef' require 'chef/role' require 'chef/cookbook/metadata' diff --git a/chef/lib/chef/webui_user.rb b/chef/lib/chef/webui_user.rb index c582722fe8..46932d0f69 100644 --- a/chef/lib/chef/webui_user.rb +++ b/chef/lib/chef/webui_user.rb @@ -22,7 +22,7 @@ require 'chef/mixin/params_validate' require 'chef/couchdb' require 'chef/index_queue' require 'digest/sha1' -require 'json' +require 'chef/json' class Chef diff --git a/chef/spec/functional/tiny_server_spec.rb b/chef/spec/functional/tiny_server_spec.rb index 59a5f16501..370698f0c7 100644 --- a/chef/spec/functional/tiny_server_spec.rb +++ b/chef/spec/functional/tiny_server_spec.rb @@ -54,7 +54,7 @@ describe TinyServer::API do response[0].should == 404 response[1].should == {'Content-Type' => 'application/json'} response[2].should be_a_kind_of(String) - response_obj = JSON.parse(response[2]) + response_obj = Chef::JSON.from_json(response[2]) response_obj["message"].should == "no data matches the request for /no_such_thing" response_obj["available_routes"].should == {"GET"=>[], "PUT"=>[], "POST"=>[], "DELETE"=>[]} response_obj["request"].should == {"REQUEST_METHOD"=>"GET", "REQUEST_URI"=>"/no_such_thing"} diff --git a/chef/spec/lib/chef/resource/zen_master.rb b/chef/spec/lib/chef/resource/zen_master.rb index b3c2c7712a..2ecacd3f6a 100644 --- a/chef/spec/lib/chef/resource/zen_master.rb +++ b/chef/spec/lib/chef/resource/zen_master.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob () -# Copyright:: Copyright (c) 2008 Opscode, Inc. +# Copyright:: Copyright (c) 2008, 2010 Opscode, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,6 +16,9 @@ # limitations under the License. # +require 'chef/knife' +require 'chef/json' + class Chef class Resource class ZenMaster < Chef::Resource diff --git a/chef/spec/tiny_server.rb b/chef/spec/tiny_server.rb index 99b2887251..18b593ec8e 100644 --- a/chef/spec/tiny_server.rb +++ b/chef/spec/tiny_server.rb @@ -20,7 +20,7 @@ require 'rubygems' require 'rack' require 'thin' require 'singleton' -require 'json' +require 'chef/json' require 'open-uri' module TinyServer diff --git a/chef/spec/unit/api_client_spec.rb b/chef/spec/unit/api_client_spec.rb index 4b73d80575..eba41310c4 100644 --- a/chef/spec/unit/api_client_spec.rb +++ b/chef/spec/unit/api_client_spec.rb @@ -157,7 +157,7 @@ describe Chef::ApiClient do @client.public_key("crowes") @client.private_key("monkeypants") @client.admin(true) - @deserial = JSON.parse(@client.to_json) + @deserial = Chef::JSON.from_json(@client.to_json) end it "should deserialize to a Chef::ApiClient object" do diff --git a/chef/spec/unit/application/client_spec.rb b/chef/spec/unit/application/client_spec.rb index e92b2458ef..d5daec07d4 100644 --- a/chef/spec/unit/application/client_spec.rb +++ b/chef/spec/unit/application/client_spec.rb @@ -99,7 +99,7 @@ describe Chef::Application::Client, "reconfigure" do end it "should parse the json out of the file" do - JSON.should_receive(:parse).with(@json.read) + Chef::JSON.should_receive(:from_json).with(@json.read) @app.reconfigure end end @@ -109,7 +109,7 @@ describe Chef::Application::Client, "reconfigure" do Chef::Config[:json_attribs] = "/etc/chef/dna.json" @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true) @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json) - JSON.stub!(:parse).with(@json.read).and_raise(JSON::ParserError) + Chef::JSON.stub!(:from_json).with(@json.read).and_raise(JSON::ParserError) Chef::Application.stub!(:fatal!).and_return(true) end diff --git a/chef/spec/unit/application/solo_spec.rb b/chef/spec/unit/application/solo_spec.rb index 2a95174147..2ec1d3be4d 100644 --- a/chef/spec/unit/application/solo_spec.rb +++ b/chef/spec/unit/application/solo_spec.rb @@ -87,7 +87,7 @@ describe Chef::Application::Solo do end it "should parse the json out of the file" do - JSON.should_receive(:parse).with(@json.read) + Chef::JSON.should_receive(:from_json).with(@json.read) @app.reconfigure end end @@ -97,7 +97,7 @@ describe Chef::Application::Solo do Chef::Config[:json_attribs] = "/etc/chef/dna.json" @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true) @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json) - JSON.stub!(:parse).with(@json.read).and_raise(JSON::ParserError) + Chef::JSON.stub!(:from_json).with(@json.read).and_raise(JSON::ParserError) Chef::Application.stub!(:fatal!).and_return(true) end diff --git a/chef/spec/unit/checksum_spec.rb b/chef/spec/unit/checksum_spec.rb index d2740dae64..c8b405194f 100644 --- a/chef/spec/unit/checksum_spec.rb +++ b/chef/spec/unit/checksum_spec.rb @@ -92,7 +92,7 @@ describe Chef::Checksum do describe "when converted to json" do before do @checksum_as_json = @checksum.to_json - @checksum_as_hash_from_json = JSON.parse(@checksum_as_json, :create_additions => false) + @checksum_as_hash_from_json = Chef::JSON.from_json(@checksum_as_json, :create_additions => false) end it "contains the file's MD5 checksum" do diff --git a/chef/spec/unit/cookbook/metadata_spec.rb b/chef/spec/unit/cookbook/metadata_spec.rb index 9da3b0f84f..4b8420b149 100644 --- a/chef/spec/unit/cookbook/metadata_spec.rb +++ b/chef/spec/unit/cookbook/metadata_spec.rb @@ -515,11 +515,11 @@ describe Chef::Cookbook::Metadata do describe "serialize" do before(:each) do - @serial = JSON.parse(@meta.to_json) + @serial = Chef::JSON.from_json(@meta.to_json) end it "should serialize to a json hash" do - JSON.parse(@meta.to_json).should be_a_kind_of(Hash) + Chef::JSON.from_json(@meta.to_json).should be_a_kind_of(Hash) end %w{ diff --git a/chef/spec/unit/data_bag_item_spec.rb b/chef/spec/unit/data_bag_item_spec.rb index 661e2e7904..c13cca4abf 100644 --- a/chef/spec/unit/data_bag_item_spec.rb +++ b/chef/spec/unit/data_bag_item_spec.rb @@ -145,7 +145,7 @@ describe Chef::DataBagItem do before(:each) do @data_bag_item.data_bag('mars_volta') @data_bag_item.raw_data = { "id" => "octahedron", "snooze" => { "finally" => :world_will }} - @deserial = JSON.parse(@data_bag_item.to_json) + @deserial = Chef::JSON.from_json(@data_bag_item.to_json) end it "should deserialize to a Chef::DataBagItem object" do diff --git a/chef/spec/unit/data_bag_spec.rb b/chef/spec/unit/data_bag_spec.rb index 1f140b5f88..da8c792ea0 100644 --- a/chef/spec/unit/data_bag_spec.rb +++ b/chef/spec/unit/data_bag_spec.rb @@ -52,7 +52,7 @@ describe Chef::DataBag do describe "deserialize" do before(:each) do @data_bag.name('mars_volta') - @deserial = JSON.parse(@data_bag.to_json) + @deserial = Chef::JSON.from_json(@data_bag.to_json) end it "should deserialize to a Chef::DataBag object" do diff --git a/chef/spec/unit/handler/json_file_spec.rb b/chef/spec/unit/handler/json_file_spec.rb index 7a1c112360..6d3de0b970 100644 --- a/chef/spec/unit/handler/json_file_spec.rb +++ b/chef/spec/unit/handler/json_file_spec.rb @@ -52,7 +52,7 @@ describe Chef::Handler::JsonFile do it "saves run status data to a file as JSON" do @handler.should_receive(:build_report_dir) @handler.run_report_unsafe(@run_status) - reported_data = JSON.parse(@file_mock.string) + reported_data = Chef::JSON.from_json(@file_mock.string) reported_data['exception'].should == "Exception: Boy howdy!" reported_data['start_time'].should == @expected_time.iso8601 reported_data['end_time'].should == (@expected_time + 5).iso8601 diff --git a/chef/spec/unit/knife/cookbook_metadata_from_file_spec.rb b/chef/spec/unit/knife/cookbook_metadata_from_file_spec.rb index b3930689ef..9bee41facc 100644 --- a/chef/spec/unit/knife/cookbook_metadata_from_file_spec.rb +++ b/chef/spec/unit/knife/cookbook_metadata_from_file_spec.rb @@ -27,7 +27,7 @@ describe Chef::Knife::CookbookMetadataFromFile do @tgt = File.expand_path(File.join(CHEF_SPEC_DATA, "metadata", "quick_start", "metadata.json")) @knife = Chef::Knife::CookbookMetadataFromFile.new @knife.name_args = [ @src ] - @knife.stub!(:json_pretty_generate).and_return(true) + @knife.stub!(:to_json_pretty).and_return(true) @md = Chef::Cookbook::Metadata.new Chef::Cookbook::Metadata.stub(:new).and_return(@md) end @@ -56,7 +56,7 @@ describe Chef::Knife::CookbookMetadataFromFile do end it "should generate json from the metadata" do - JSON.should_receive(:pretty_generate).with(@md) + Chef::JSON.should_receive(:to_json_pretty).with(@md) @knife.run end diff --git a/chef/spec/unit/knife/data_bag_show_spec.rb b/chef/spec/unit/knife/data_bag_show_spec.rb index f72cae1987..1c4066991a 100644 --- a/chef/spec/unit/knife/data_bag_show_spec.rb +++ b/chef/spec/unit/knife/data_bag_show_spec.rb @@ -50,7 +50,7 @@ describe Chef::Knife::DataBagShow do Chef::DataBagItem.should_receive(:load).with('bag_o_data', 'an_item').and_return(data_item_content) @knife.run - JSON.parse(@stdout.string).should == data_item_content + Chef::JSON.from_json(@stdout.string).should == data_item_content end end diff --git a/chef/spec/unit/mixin/deprecation_spec.rb b/chef/spec/unit/mixin/deprecation_spec.rb index af0b27ed22..b78080971b 100644 --- a/chef/spec/unit/mixin/deprecation_spec.rb +++ b/chef/spec/unit/mixin/deprecation_spec.rb @@ -31,4 +31,4 @@ describe Chef::Mixin::Deprecation::DeprecatedInstanceVariable do @deprecated_ivar.to_sym.should == :value end -end \ No newline at end of file +end diff --git a/chef/spec/unit/node_spec.rb b/chef/spec/unit/node_spec.rb index 469720706c..8e896073ac 100644 --- a/chef/spec/unit/node_spec.rb +++ b/chef/spec/unit/node_spec.rb @@ -487,7 +487,7 @@ describe Chef::Node do describe "json" do it "should serialize itself as json" do @node.find_file("test.example.com") - json = @node.to_json() + json = Chef::JSON.to_json(@node) json.should =~ /json_class/ json.should =~ /name/ json.should =~ /normal/ @@ -498,8 +498,8 @@ describe Chef::Node do it "should deserialize itself from json" do @node.find_file("test.example.com") - json = @node.to_json - serialized_node = JSON.parse(json) + json = Chef::JSON.to_json(@node) + serialized_node = Chef::JSON.from_json(json) serialized_node.should be_a_kind_of(Chef::Node) serialized_node.name.should eql(@node.name) @node.each_attribute do |k,v| diff --git a/chef/spec/unit/openid_registration_spec.rb b/chef/spec/unit/openid_registration_spec.rb index 5187ee0d21..5f8421e9f7 100644 --- a/chef/spec/unit/openid_registration_spec.rb +++ b/chef/spec/unit/openid_registration_spec.rb @@ -56,7 +56,7 @@ describe Chef::OpenIDRegistration, "from_json" do oreg.name = "foobar" oreg.set_password("monkey") oreg_json = oreg.to_json - nreg = JSON.parse(oreg_json) + nreg = Chef::JSON.from_json(oreg_json) nreg.should be_a_kind_of(Chef::OpenIDRegistration) %w{name salt password validated}.each do |verify| nreg.send(verify.to_sym).should eql(oreg.send(verify.to_sym)) diff --git a/chef/spec/unit/resource_collection_spec.rb b/chef/spec/unit/resource_collection_spec.rb index e9ee2f86a8..578e5e8908 100644 --- a/chef/spec/unit/resource_collection_spec.rb +++ b/chef/spec/unit/resource_collection_spec.rb @@ -222,7 +222,7 @@ describe Chef::ResourceCollection do it "should deserialize itself from json" do @rc << @resource json = @rc.to_json - s_rc = JSON.parse(json) + s_rc = Chef::JSON.from_json(json) s_rc.should be_a_kind_of(Chef::ResourceCollection) s_rc[0].name.should eql(@resource.name) end diff --git a/chef/spec/unit/resource_spec.rb b/chef/spec/unit/resource_spec.rb index 72be22fa04..fdb3816788 100644 --- a/chef/spec/unit/resource_spec.rb +++ b/chef/spec/unit/resource_spec.rb @@ -204,7 +204,7 @@ describe Chef::Resource do describe "self.json_create" do it "should deserialize itself from json" do json = @resource.to_json - serialized_node = JSON.parse(json) + serialized_node = Chef::JSON.from_json(json) serialized_node.should be_a_kind_of(Chef::Resource) serialized_node.name.should eql(@resource.name) end diff --git a/chef/spec/unit/rest_spec.rb b/chef/spec/unit/rest_spec.rb index a2a697181b..1a79ea8075 100644 --- a/chef/spec/unit/rest_spec.rb +++ b/chef/spec/unit/rest_spec.rb @@ -204,7 +204,7 @@ describe Chef::REST do it "should inflate the body as to an object if JSON is returned" do @http_response.add_field("content-type", "application/json") - JSON.should_receive(:parse).with("ninja").and_return("ohai2u_success") + Chef::JSON.should_receive(:from_json).with("ninja").and_return("ohai2u_success") @rest.run_request(:GET, @url, {}).should == "ohai2u_success" end diff --git a/chef/spec/unit/role_spec.rb b/chef/spec/unit/role_spec.rb index 548faaac7c..fbb0b3c40a 100644 --- a/chef/spec/unit/role_spec.rb +++ b/chef/spec/unit/role_spec.rb @@ -115,11 +115,11 @@ describe Chef::Role do @role.run_list('one', 'two', 'role[a]') @role.default_attributes({ :el_groupo => 'nuevo' }) @role.override_attributes({ :deloused => 'in the comatorium' }) - @serial = @role.to_json + @serial = Chef::JSON.to_json(@role) end it "should serialize to a json hash" do - @role.to_json.should match(/^\{.+\}$/) + Chef::JSON.to_json(@role).should match(/^\{.+\}$/) end %w{ @@ -151,7 +151,7 @@ describe Chef::Role do @role.run_list('one', 'two', 'role[a]') @role.default_attributes({ 'el_groupo' => 'nuevo' }) @role.override_attributes({ 'deloused' => 'in the comatorium' }) - @deserial = JSON.parse(@role.to_json) + @deserial = Chef::JSON.from_json(Chef::JSON.to_json(@role)) end it "should deserialize to a Chef::Role object" do diff --git a/chef/spec/unit/webui_user_spec.rb b/chef/spec/unit/webui_user_spec.rb index 0e1c053013..4db9294b4e 100644 --- a/chef/spec/unit/webui_user_spec.rb +++ b/chef/spec/unit/webui_user_spec.rb @@ -217,8 +217,8 @@ describe Chef::WebUIUser do end it "sets its couchdb id when loading from the database" do - # reqs via REST eventually get to JSON.parse - webui_user = JSON.parse('{"salt":null,"name":"test_user","json_class":"Chef::WebUIUser","admin":false,"openid":null,"password":null,"chef_type":"webui_user","_id":"IdontNeedNoID"}') + # reqs via REST eventually get to Chef::JSON.from_json + webui_user = Chef::JSON.from_json('{"salt":null,"name":"test_user","json_class":"Chef::WebUIUser","admin":false,"openid":null,"password":null,"chef_type":"webui_user","_id":"IdontNeedNoID"}') webui_user.couchdb_id.should == "IdontNeedNoID" end @@ -230,9 +230,9 @@ describe Chef::WebUIUser do end it "sets the couchdb_rev when loading from the database" do - webui_user = JSON.parse('{"salt":null,"name":"test_user","json_class":"Chef::WebUIUser","admin":false,"openid":null,"password":null,"chef_type":"webui_user","_id":"IdontNeedNoID","_rev":"moto"}') + webui_user = Chef::JSON.from_json('{"salt":null,"name":"test_user","json_class":"Chef::WebUIUser","admin":false,"openid":null,"password":null,"chef_type":"webui_user","_id":"IdontNeedNoID","_rev":"moto"}') webui_user.couchdb_rev.should == "moto" end end -end \ No newline at end of file +end diff --git a/features/api/nodes/deep_node_show_save.feature b/features/api/nodes/deep_node_show_save.feature new file mode 100644 index 0000000000..309b7c4237 --- /dev/null +++ b/features/api/nodes/deep_node_show_save.feature @@ -0,0 +1,13 @@ +@api @api_nodes @nodes_show @json_recusion @pl_538 +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 + 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 diff --git a/features/data/Rakefile b/features/data/Rakefile index c7ddd15e1d..58ffa250d7 100644 --- a/features/data/Rakefile +++ b/features/data/Rakefile @@ -22,7 +22,7 @@ $: << File.join(File.dirname(__FILE__), "..", "..", "chef", "lib") require 'rubygems' require 'chef' -require 'json' +require 'chef/json' # Make sure you have loaded constants first require File.join(File.dirname(__FILE__), 'config', 'rake') diff --git a/features/data/node-load-test.rb b/features/data/node-load-test.rb index 0d559e7bb7..bc83a5f320 100755 --- a/features/data/node-load-test.rb +++ b/features/data/node-load-test.rb @@ -17,7 +17,7 @@ end Chef::Config.from_file("/etc/chef/client.rb") json_attrs = Hash.new if ARGV[3] - json_attrs = JSON.parse(IO.read(ARGV[3])) + json_attrs = Chef::JSON.from_json(IO.read(ARGV[3])) end Chef::Log.level = :info processes = Array.new diff --git a/features/steps/deploy_steps.rb b/features/steps/deploy_steps.rb index 2a3906f5ad..ace9b0b173 100644 --- a/features/steps/deploy_steps.rb +++ b/features/steps/deploy_steps.rb @@ -72,7 +72,7 @@ Then /^the callback named should have run$/ do |callback_files| hook_name = file.first.gsub(/\.rb$/, "") evidence_file = "deploy/current/app/" + hook_name expected_contents = {"hook_name" => hook_name, "env" => "production"} - actual_contents = JSON.parse(IO.read(File.join(tmpdir, evidence_file))) + actual_contents = Chef::JSON.from_json(IO.read(File.join(tmpdir, evidence_file))) expected_contents.should == actual_contents end end diff --git a/features/steps/fixture_steps.rb b/features/steps/fixture_steps.rb index 433826b1b5..539b866455 100644 --- a/features/steps/fixture_steps.rb +++ b/features/steps/fixture_steps.rb @@ -191,6 +191,21 @@ Before do n.name 'paradise' n.run_list << "version_test" n + end, + 'really_deep_node' => Proc.new do + array = [] + max_levels = 50 + num_level = 0 + begin + array = [num_level, "really_deep_string", array] + num_level += 1 + end while num_level < max_levels + + n = Chef::Node.new + n.name 'really_deep_node' + n.run_list << "deep_node_recipe" + n.deep_array = array + n end }, 'hash' => { @@ -264,14 +279,14 @@ Given /^an? '(.+)' named '(.+)' exists$/ do |stash_name, stash_key| :method => "POST", "HTTP_ACCEPT" => 'application/json', "CONTENT_TYPE" => 'application/json', - :input => @stash[stash_name].to_json + :input => Chef::JSON.to_json(@stash[stash_name]) }.merge(sign_request("POST", request_path, OpenSSL::PKey::RSA.new(IO.read("#{tmpdir}/client.pem")), "bobo"))) end end end Given /^sending the method '(.+)' to the '(.+)' with '(.+)'/ do |method, stash_name, update_value| - update_value = JSON.parse(update_value) if update_value =~ /^\[|\{/ + update_value = Chef::JSON.from_json(update_value) if update_value =~ /^\[|\{/ @stash[stash_name].send(method.to_sym, update_value) end diff --git a/features/steps/response_steps.rb b/features/steps/response_steps.rb index 37480b7108..99e627ed2d 100644 --- a/features/steps/response_steps.rb +++ b/features/steps/response_steps.rb @@ -40,12 +40,12 @@ end Then /^the inflated response should match '(.+)' as json$/ do |regex| puts self.inflated_response.inspect if ENV["DEBUG"] - self.inflated_response.to_json.should =~ /#{regex}/m + Chef::JSON.to_json(self.inflated_response).should =~ /#{regex}/m end Then /^the inflated responses key '(.+)' should match '(.+)' as json$/ do |key, regex| puts self.inflated_response.inspect if ENV["DEBUG"] - self.inflated_response[key].to_json.should =~ /#{regex}/m + Chef::JSON.to_json(self.inflated_response[key]).should =~ /#{regex}/m end Then /^the inflated responses key '(.+)' item '(\d+)' should be '(.+)'$/ do |key, index, to_equal| @@ -135,7 +135,7 @@ Then /^the inflated response should be a kind of '(.+)'$/ do |thing| end Then /^the inflated response should respond to '(.+)' with '(.+)'$/ do |method, to_match| - to_match = JSON.parse(to_match) if to_match =~ /^\[|\{/ + to_match = Chef::JSON.from_json(to_match) if to_match =~ /^\[|\{/ to_match = true if to_match == 'true' to_match = false if to_match == 'false' self.inflated_response.to_hash[method].should == to_match @@ -146,7 +146,7 @@ Then /^the inflated response should respond to '(.+)' and match '(.+)'$/ do |met end Then /^the inflated response should respond to '(.+)' and match '(.+)' as json$/ do |method, regex| - self.inflated_response.to_hash[method].to_json.should =~ /#{regex}/m + Chef::JSON.to_json(self.inflated_response.to_hash[method]).should =~ /#{regex}/m end Then /^the fields in the inflated response should match the '(.+)'$/ do |stash_name| diff --git a/features/support/couchdb_replicate.rb b/features/support/couchdb_replicate.rb index c44970e0bd..6459c3269e 100644 --- a/features/support/couchdb_replicate.rb +++ b/features/support/couchdb_replicate.rb @@ -27,7 +27,7 @@ require 'rubygems' require 'rest-client' require 'chef/log' -require 'json' +require 'chef/json' # Bulk GET all documents in the given db, using the given page size. # Calls the required block for each page size, passing in an array of @@ -48,7 +48,7 @@ def bulk_get_paged(db, page_size) # Pass :create_additions=>false so JSON parser does *not* expand # custom classes (such as Chef::Node, etc), and instead sticks only # to Array, Hash, String, etc. - paged_results = JSON.parse(paged_results_str, :create_additions => false) + paged_results = Chef::JSON.from_json(paged_results_str, :create_additions => false) paged_rows = paged_results['rows'] if paged_rows.length > 0 @@ -115,7 +115,7 @@ def replicate_dbs(replication_specs, delete_source_dbs = false) doc_in_row end - RestClient.post("#{target_db}/_bulk_docs", ({"docs" => paged_rows}).to_json, :content_type => "application/json") + RestClient.post("#{target_db}/_bulk_docs", Chef::JSON.to_json({"docs" => paged_rows}), :content_type => "application/json") end # Delete the source if asked to.. -- cgit v1.2.1 From adeefd88fb9a476269a3d133cbd31fe0fee9289e Mon Sep 17 00:00:00 2001 From: Tim Hinderliter Date: Mon, 22 Nov 2010 15:13:38 -0800 Subject: remove commented-out steps --- features/steps/request_steps.rb | 59 ----------------------------------------- 1 file changed, 59 deletions(-) diff --git a/features/steps/request_steps.rb b/features/steps/request_steps.rb index 92a57af280..199ff8d6c1 100644 --- a/features/steps/request_steps.rb +++ b/features/steps/request_steps.rb @@ -96,62 +96,3 @@ def call_as_admin(&block) Chef::Config[:client_key] = orig_client_key end end - -#When /^I dump the contents of the search index$/ do -# Given "I dump the contents of the search index" -#end -# - -# When /^I '(.+)' the path '(.+)'$/ do |http_method, request_uri| -# begin -# #if http_method.downcase == 'get' -# # self.api_response = @rest.get_rest(request_uri) -# #else -# #puts "test test test \n\n\n\n\n\n\n" -# @response = @rest.send("#{http_method}_rest".downcase.to_sym, request_uri) -# #end -# puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -# puts @response -# puts @response['content-type'] -# #puts self.api_response -# #puts self.api_response.inspect -# #self.inflated_response = self.api_response -# @inflated_response = @response#JSON.parse(response.body.to_s) -# puts "~~~~~~~~INFLATED RESPONSE~~~~~~~~~~~~" -# puts @inflated_response -# rescue -# self.exception = $! -# end -# end -# -# When /^I '(.+)' the '(.+)' to the path '(.+)'$/ do |http_method, stash_key, request_uri| -# begin -# #if http_method.downcase == 'post' -# # puts "post request" -# # self.api_response = @rest.post_rest(request_uri, @stash[stash_key]) -# # puts self.api_response -# #else -# puts "This is the request -- @stash[stash_key]:" -# puts @stash[stash_key].to_s -# @response = @rest.send("#{http_method}_rest".downcase.to_sym, request_uri, @stash[stash_key]) -# #end -# puts "This is the response:" -# #puts self.api_response.body.to_s -# puts @response -# #self.inflated_response = response -# @inflated_response = @response#JSON.parse(self.api_response.body.to_s) -# puts "~~~~~~~~INFLATED RESPONSE~~~~~~~~~~~~" -# puts @inflated_response -# rescue -# self.exception = $! -# end -# end -# -# When /^I authenticate as '(.+)'$/ do |reg| -# begin -# rest.authenticate(reg, 'tclown') -# rescue -# self.exception = $! -# end -# end -# -- cgit v1.2.1 From 9dca1e9e0cb9aefc6538fd98af9d57df5ac9d8c0 Mon Sep 17 00:00:00 2001 From: Tim Hinderliter Date: Mon, 22 Nov 2010 15:14:26 -0800 Subject: change comment to include CHEF- ticket number as well as PL- --- chef/lib/chef/json.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/chef/lib/chef/json.rb b/chef/lib/chef/json.rb index 1b1618c132..f83700dc40 100644 --- a/chef/lib/chef/json.rb +++ b/chef/lib/chef/json.rb @@ -24,8 +24,9 @@ class Chef JSON_MAX_NESTING = 1000 class < Date: Wed, 24 Nov 2010 13:29:14 -0800 Subject: More fixes for CHEF-1292: updated comments, refactored feature tests. Added some require's --- chef-server-api/app/controllers/application.rb | 13 +++++++---- chef/lib/chef/json.rb | 1 - chef/lib/chef/provider/deploy/revision.rb | 7 ++++++ chef/lib/chef/shef.rb | 17 ++++++++------- chef/lib/chef/shef/ext.rb | 1 + features/api/nodes/deep_node_show_save.feature | 7 +++--- features/steps/fixture_steps.rb | 11 ++++++---- features/steps/response_steps.rb | 30 ++++++++++++++++++++++++++ 8 files changed, 67 insertions(+), 20 deletions(-) diff --git a/chef-server-api/app/controllers/application.rb b/chef-server-api/app/controllers/application.rb index 0e2db2901e..7141bf91e5 100644 --- a/chef-server-api/app/controllers/application.rb +++ b/chef-server-api/app/controllers/application.rb @@ -119,13 +119,18 @@ class Application < Merb::Controller 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 diff --git a/chef/lib/chef/json.rb b/chef/lib/chef/json.rb index f83700dc40..24020356ba 100644 --- a/chef/lib/chef/json.rb +++ b/chef/lib/chef/json.rb @@ -41,7 +41,6 @@ class Chef end def to_json(obj, opts = nil) - #::JSON.generate(obj, opts_add_max_nesting(opts)) obj.to_json(opts_add_max_nesting(opts)) end diff --git a/chef/lib/chef/provider/deploy/revision.rb b/chef/lib/chef/provider/deploy/revision.rb index 18c2c03f8a..6b5848827a 100644 --- a/chef/lib/chef/provider/deploy/revision.rb +++ b/chef/lib/chef/provider/deploy/revision.rb @@ -1,6 +1,9 @@ # # Author:: Daniel DeLeo () +# Author:: Tim Hinderliter () +# Author:: Seth Falcon () # 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"); @@ -16,6 +19,10 @@ # limitations under the License. # +require 'chef/provider' +require 'chef/provider/deploy' +require 'chef/json' + class Chef class Provider class Deploy diff --git a/chef/lib/chef/shef.rb b/chef/lib/chef/shef.rb index bcab49fb4b..2710e7fbb1 100644 --- a/chef/lib/chef/shef.rb +++ b/chef/lib/chef/shef.rb @@ -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 diff --git a/chef/lib/chef/shef/ext.rb b/chef/lib/chef/shef/ext.rb index 77c63cfc07..6c5116f4a5 100644 --- a/chef/lib/chef/shef/ext.rb +++ b/chef/lib/chef/shef/ext.rb @@ -23,6 +23,7 @@ require 'chef/version' require 'chef/shef/shef_session' require 'chef/shef/model_wrapper' require 'chef/shef/shef_rest' +require 'chef/json' module Shef module Extensions diff --git a/features/api/nodes/deep_node_show_save.feature b/features/api/nodes/deep_node_show_save.feature index 309b7c4237..71290434b8 100644 --- a/features/api/nodes/deep_node_show_save.feature +++ b/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 diff --git a/features/steps/fixture_steps.rb b/features/steps/fixture_steps.rb index 539b866455..fb1b6a2cbb 100644 --- a/features/steps/fixture_steps.rb +++ b/features/steps/fixture_steps.rb @@ -194,17 +194,20 @@ Before do 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 }, diff --git a/features/steps/response_steps.rb b/features/steps/response_steps.rb index 99e627ed2d..989099d78d 100644 --- a/features/steps/response_steps.rb +++ b/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 @@ -149,6 +174,11 @@ Then /^the inflated response should respond to '(.+)' and match '(.+)' as json$/ 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' -- cgit v1.2.1 From 65f13122ee810278e29aef915b59d009a8029fbc Mon Sep 17 00:00:00 2001 From: Seth Falcon Date: Wed, 24 Nov 2010 16:05:03 -0800 Subject: Remove require accidentally added from merge from master environments are not yet in pl-master. --- chef/lib/chef/knife/cookbook_site_unshare.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/chef/lib/chef/knife/cookbook_site_unshare.rb b/chef/lib/chef/knife/cookbook_site_unshare.rb index 6c3736f7c1..9469cb073a 100644 --- a/chef/lib/chef/knife/cookbook_site_unshare.rb +++ b/chef/lib/chef/knife/cookbook_site_unshare.rb @@ -18,7 +18,6 @@ # require 'chef/knife' -require 'chef/environment' require 'chef/json' class Chef -- cgit v1.2.1 From 4422203ae7a2c711c31c95c529ee66bdffa363e1 Mon Sep 17 00:00:00 2001 From: Seth Falcon Date: Wed, 24 Nov 2010 16:05:52 -0800 Subject: Use Chef::JSON.to_json in Chef::IndexQueue PL-538 --- chef/lib/chef/index_queue/indexable.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/chef/lib/chef/index_queue/indexable.rb b/chef/lib/chef/index_queue/indexable.rb index c01bb0f748..03fb0916f1 100644 --- a/chef/lib/chef/index_queue/indexable.rb +++ b/chef/lib/chef/index_queue/indexable.rb @@ -1,6 +1,8 @@ # # Author:: Daniel DeLeo () +# Author:: Seth Falcon () # 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"); @@ -15,6 +17,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +require 'chef/json' class Chef module IndexQueue @@ -67,7 +70,8 @@ class Chef object_with_metadata = with_indexer_metadata(metadata) obj_id = object_with_metadata["id"] AmqpClient.instance.queue_for_object(obj_id) do |queue| - queue.publish({:action => :add, :payload => self.with_indexer_metadata(metadata)}.to_json) + obj = {:action => :add, :payload => self.with_indexer_metadata(metadata)} + queue.publish(Chef::JSON.to_json(obj)) end end @@ -76,7 +80,8 @@ class Chef object_with_metadata = with_indexer_metadata(metadata) obj_id = object_with_metadata["id"] AmqpClient.instance.queue_for_object(obj_id) do |queue| - queue.publish({:action => :delete, :payload => self.with_indexer_metadata(metadata)}.to_json) + obj = {:action => :delete, :payload => self.with_indexer_metadata(metadata)} + queue.publish(Chef::JSON.to_json(obj)) end end -- cgit v1.2.1 From 572e68ce90d59356a54f2b8ade7f715c20aadad4 Mon Sep 17 00:00:00 2001 From: Tim Hinderliter Date: Wed, 24 Nov 2010 16:40:02 -0800 Subject: for CHEF-1292/PL-538, modify search index_queue_spec to call the right JSON serialization functions in Chef::JSON --- chef/spec/unit/index_queue_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chef/spec/unit/index_queue_spec.rb b/chef/spec/unit/index_queue_spec.rb index 02cdd514e3..61382fb8ec 100644 --- a/chef/spec/unit/index_queue_spec.rb +++ b/chef/spec/unit/index_queue_spec.rb @@ -113,7 +113,7 @@ describe Chef::IndexQueue::Indexable do @queue = FauxQueue.new @publisher.should_receive(:queue_for_object).with("0000000-1111-2222-3333-444444444444").and_yield(@queue) @indexable_obj.add_to_index(:database => "couchdb@localhost,etc.", :id=>"0000000-1111-2222-3333-444444444444") - published_message = JSON.parse(@queue.published_message) + published_message = Chef::JSON.from_json(@queue.published_message) published_message.should == {"action" => "add", "payload" => {"item" => @item_as_hash, "type" => "indexable_test_harness", "database" => "couchdb@localhost,etc.", @@ -126,7 +126,7 @@ describe Chef::IndexQueue::Indexable do @publisher.should_receive(:queue_for_object).with("0000000-1111-2222-3333-444444444444").and_yield(@queue) @indexable_obj.delete_from_index(:database => "couchdb2@localhost", :id=>"0000000-1111-2222-3333-444444444444") - published_message = JSON.parse(@queue.published_message) + published_message = Chef::JSON.from_json(@queue.published_message) published_message.should == {"action" => "delete", "payload" => { "item" => @item_as_hash, "type" => "indexable_test_harness", "database" => "couchdb2@localhost", -- cgit v1.2.1