diff options
author | danielsdeleo <dan@getchef.com> | 2014-08-08 15:55:28 -0700 |
---|---|---|
committer | danielsdeleo <dan@getchef.com> | 2014-08-10 09:40:36 -0700 |
commit | 11f7b2c725db4384787f76c4c5229f98c8694e95 (patch) | |
tree | ec99bf199d8a9fe14c90aab2e234a219b2bd447c | |
parent | 674cc65dc68b9d5f0eaa36f2409c6c12becf2c11 (diff) | |
download | chef-11f7b2c725db4384787f76c4c5229f98c8694e95.tar.gz |
Fix expected format of JSON errors in integration tests
The format was previously not stable because chef-zero (or other test
code) would `require 'json'` and undo ffi-yajl's monkey patches to the
JSON gem. We would probably be better off if we can get out of the
business of monkeypatching JSON or attempting to provide compatibility
at all, in which case this commit could be reverted.
-rw-r--r-- | lib/chef/json_compat.rb | 1 | ||||
-rw-r--r-- | spec/integration/knife/diff_spec.rb | 8 | ||||
-rw-r--r-- | spec/integration/knife/download_spec.rb | 19 | ||||
-rw-r--r-- | spec/integration/knife/upload_spec.rb | 48 | ||||
-rw-r--r-- | spec/support/shared/integration/integration_helper.rb | 2 | ||||
-rw-r--r-- | spec/unit/environment_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/knife/data_bag_from_file_spec.rb | 2 |
7 files changed, 34 insertions, 48 deletions
diff --git a/lib/chef/json_compat.rb b/lib/chef/json_compat.rb index 2dbb607d9b..0f182508b9 100644 --- a/lib/chef/json_compat.rb +++ b/lib/chef/json_compat.rb @@ -18,6 +18,7 @@ # Wrapper class for interacting with JSON. require 'ffi_yajl' +require 'json' require 'ffi_yajl/json_gem' # XXX: parts of chef require JSON gem's Hash#to_json monkeypatch class Chef diff --git a/spec/integration/knife/diff_spec.rb b/spec/integration/knife/diff_spec.rb index 8d13d86a99..62b491d6ef 100644 --- a/spec/integration/knife/diff_spec.rb +++ b/spec/integration/knife/diff_spec.rb @@ -309,7 +309,9 @@ EOM when_the_repository 'has an environment with bad JSON' do before { file 'environments/x.json', '{' } it 'knife diff reports an error and does a textual diff' do - knife('diff /environments/x.json').should_succeed(/- "name": "x"/, :stderr => "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: A JSON text must at least contain two octets!\n") + error_text = "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: parse error: premature EOF" + error_match = Regexp.new(Regexp.escape(error_text)) + knife('diff /environments/x.json').should_succeed(/- "name": "x"/, :stderr => error_match) end end end @@ -590,7 +592,9 @@ EOM when_the_repository 'has an environment with bad JSON' do before { file 'environments/x.json', '{' } it 'knife diff reports an error and does a textual diff' do - knife('diff /environments/x.json').should_succeed(/- "name": "x"/, :stderr => "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: A JSON text must at least contain two octets!\n") + error_text = "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: parse error: premature EOF" + error_match = Regexp.new(Regexp.escape(error_text)) + knife('diff /environments/x.json').should_succeed(/- "name": "x"/, :stderr => error_match) end end end diff --git a/spec/integration/knife/download_spec.rb b/spec/integration/knife/download_spec.rb index 4788a564b7..2ef5dc03c1 100644 --- a/spec/integration/knife/download_spec.rb +++ b/spec/integration/knife/download_spec.rb @@ -541,7 +541,13 @@ EOM file 'environments/x.json', '{' end it 'knife download succeeds' do - knife('download /environments/x.json').should_succeed "Updated /environments/x.json\n", :stderr => "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: A JSON text must at least contain two octets!\n" + warning = <<-EOH +WARN: Parse error reading #{path_to('environments/x.json')} as JSON: parse error: premature EOF + { + (right here) ------^ + +EOH + knife('download /environments/x.json').should_succeed "Updated /environments/x.json\n", :stderr => warning knife('diff --name-status /environments/x.json').should_succeed '' end end @@ -1033,17 +1039,6 @@ EOM environment 'x', {} end - when_the_repository 'has an environment with bad JSON' do - before do - file 'environments/x.json', '{' - end - - it 'knife download succeeds' do - knife('download /environments/x.json').should_succeed "Updated /environments/x.json\n", :stderr => "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: A JSON text must at least contain two octets!\n" - knife('diff --name-status /environments/x.json').should_succeed '' - end - end - when_the_repository 'has the same environment with the wrong name in the file' do before do file 'environments/x.json', { 'name' => 'y' } diff --git a/spec/integration/knife/upload_spec.rb b/spec/integration/knife/upload_spec.rb index 90666f4fc9..d8db27f73b 100644 --- a/spec/integration/knife/upload_spec.rb +++ b/spec/integration/knife/upload_spec.rb @@ -605,8 +605,24 @@ EOM end it 'knife upload tries and fails' do - knife('upload /environments/x.json').should_fail "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: A JSON text must at least contain two octets!\nERROR: /environments/x.json failed to write: Parse error reading JSON: A JSON text must at least contain two octets!\n" - knife('diff --name-status /environments/x.json').should_succeed "M\t/environments/x.json\n", :stderr => "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: A JSON text must at least contain two octets!\n" + error1 = <<-EOH +WARN: Parse error reading #{path_to('environments/x.json')} as JSON: parse error: premature EOF + { + (right here) ------^ + +ERROR: /environments/x.json failed to write: Parse error reading JSON: parse error: premature EOF + { + (right here) ------^ +EOH + + warn = <<-EOH +WARN: Parse error reading #{path_to('environments/x.json')} as JSON: parse error: premature EOF + { + (right here) ------^ + +EOH + knife('upload /environments/x.json').should_fail(error1) + knife('diff --name-status /environments/x.json').should_succeed("M\t/environments/x.json\n", :stderr => warn) end end @@ -632,15 +648,6 @@ EOM end when_the_chef_server 'is empty' do - when_the_repository 'has an environment with bad JSON' do - before do - file 'environments/x.json', '{' - end - it 'knife upload tries and fails' do - knife('upload /environments/x.json').should_fail "ERROR: /environments failed to create_child: Parse error reading JSON creating child 'x.json': A JSON text must at least contain two octets!\n" - knife('diff --name-status /environments/x.json').should_succeed "A\t/environments/x.json\n" - end - end when_the_repository 'has an environment with the wrong name in the file' do before do @@ -1140,16 +1147,6 @@ EOM environment 'x', {} end - when_the_repository 'has an environment with bad JSON' do - before do - file 'environments/x.json', '{' - end - it 'knife upload tries and fails' do - knife('upload /environments/x.json').should_fail "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: A JSON text must at least contain two octets!\nERROR: /environments/x.json failed to write: Parse error reading JSON: A JSON text must at least contain two octets!\n" - knife('diff --name-status /environments/x.json').should_succeed "M\t/environments/x.json\n", :stderr => "WARN: Parse error reading #{path_to('environments/x.json')} as JSON: A JSON text must at least contain two octets!\n" - end - end - when_the_repository 'has the same environment with the wrong name in the file' do before do file 'environments/x.json', { 'name' => 'y' } @@ -1172,15 +1169,6 @@ EOM end when_the_chef_server 'is empty' do - when_the_repository 'has an environment with bad JSON' do - before do - file 'environments/x.json', '{' - end - it 'knife upload tries and fails' do - knife('upload /environments/x.json').should_fail "ERROR: /environments failed to create_child: Parse error reading JSON creating child 'x.json': A JSON text must at least contain two octets!\n" - knife('diff --name-status /environments/x.json').should_succeed "A\t/environments/x.json\n" - end - end when_the_repository 'has an environment with the wrong name in the file' do before do diff --git a/spec/support/shared/integration/integration_helper.rb b/spec/support/shared/integration/integration_helper.rb index 9b0f17d378..5369b56f5b 100644 --- a/spec/support/shared/integration/integration_helper.rb +++ b/spec/support/shared/integration/integration_helper.rb @@ -26,7 +26,7 @@ require 'chef/config' #require 'chef_zero/rspec' require 'support/shared/integration/chef_zero_support' -require 'json' +require 'chef/json_compat' require 'support/shared/integration/knife_support' require 'support/shared/integration/app_server_support' require 'spec_helper' diff --git a/spec/unit/environment_spec.rb b/spec/unit/environment_spec.rb index 0e230e46ed..5a2c400d3c 100644 --- a/spec/unit/environment_spec.rb +++ b/spec/unit/environment_spec.rb @@ -420,7 +420,7 @@ describe Chef::Environment do "description" => "desc", "chef_type" => "environment" } - IO.should_receive(:read).with(File.join(Chef::Config[:environment_path], 'foo.json')).and_return(JSON.dump(environment_hash)) + IO.should_receive(:read).with(File.join(Chef::Config[:environment_path], 'foo.json')).and_return(Chef::JSONCompat.to_json(environment_hash)) environment = Chef::Environment.load('foo') environment.should be_a_kind_of(Chef::Environment) diff --git a/spec/unit/knife/data_bag_from_file_spec.rb b/spec/unit/knife/data_bag_from_file_spec.rb index 8537045164..1ad6b4712c 100644 --- a/spec/unit/knife/data_bag_from_file_spec.rb +++ b/spec/unit/knife/data_bag_from_file_spec.rb @@ -21,7 +21,6 @@ require 'spec_helper' require 'chef/data_bag_item' require 'chef/encrypted_data_bag_item' require 'tempfile' -require 'json' Chef::Knife::DataBagFromFile.load_deps @@ -86,7 +85,6 @@ describe Chef::Knife::DataBagFromFile do end it "loads all from a folder and saves" do - dir = File.dirname(@db_file.path) @knife.name_args = [ 'bag_name', @db_folder ] @knife.loader.should_receive(:load_from).with("data_bags", 'bag_name', @db_file.path).and_return(@plain_data) @knife.loader.should_receive(:load_from).with("data_bags", 'bag_name', @db_file2.path).and_return(@plain_data) |