From 37047e5e6117e55769554bd2134ebb24de306adc Mon Sep 17 00:00:00 2001 From: vagrant Date: Wed, 6 Nov 2013 21:14:16 +0000 Subject: [PATCH] make tests work with old or recent JSON module serialization behavior. --- spec/spec_helper.rb | 14 +++++++++++++ spec/unit/solo_data_bag_create_spec.rb | 29 +++++++++++++++++--------- spec/unit/solo_data_bag_edit_spec.rb | 15 ++++++++----- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f68d194..8a402ea 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,20 @@ require 'fakefs/safe' require 'knife-solo_data_bag' + +def JSON_to_databag_item (json_string) + #This is a fix to handle differing JSON behavior across multiple JSON module versions + json_parsed = JSON.parse(json_string) + #should break these into their own tests depending on JSON::VERSION value? + if json_parsed.is_a? Chef::DataBagItem #old JSON deserialized it + item = json_parsed + elsif json_parsed.has_key?("json_class") #old serialized file format knife-solo_data_bag<=0.4.0 + item = Chef::DataBagItem.json_create json_parsed #method is destructive to json_parsed + else #basic hash from json file + item = Chef::DataBagItem.from_hash json_parsed + end +end + ['contexts', 'helpers', 'matchers'].each do |dir| Dir[File.expand_path(File.join(File.dirname(__FILE__),dir,'*.rb'))].each {|f| require f} end diff --git a/spec/unit/solo_data_bag_create_spec.rb b/spec/unit/solo_data_bag_create_spec.rb index c7693b5..7d619ea 100644 --- a/spec/unit/solo_data_bag_create_spec.rb +++ b/spec/unit/solo_data_bag_create_spec.rb @@ -46,10 +46,11 @@ it 'should create the data bag item' do @knife.run - JSON.parse(File.read(@item_path)).raw_data.should == @input_data + item = JSON_to_databag_item(File.read(@item_path)) + item.raw_data.should == @input_data end - context 'with --data-bag-path' do + context 'with --data/-bag-path' do before do @override_bags_path = '/opt/bags' @override_bag_path = "#{@override_bags_path}/bag_1" @@ -71,7 +72,8 @@ it 'should create the encrypted data bag item' do @knife.run - content = JSON.parse(File.read(@item_path)).raw_data + item = JSON_to_databag_item(File.read(@item_path)) + content = item.raw_data @input_data.keys.reject{|i| i == 'id'}.each do |k| content.should have_key k content[k].should_not == @input_data[k] @@ -91,7 +93,8 @@ it 'should create the encrypted data bag item' do @knife.run - content = JSON.parse(File.read(@item_path)).raw_data + item = JSON_to_databag_item(File.read(@item_path)) + content = item.raw_data @input_data.keys.reject{|i| i == 'id'}.each do |k| content.should have_key k content[k].should_not == @input_data[k] @@ -112,7 +115,8 @@ it 'creates the encrypted data bag item' do @knife.run - content = JSON.parse(File.read(@item_path)).raw_data + item = JSON_to_databag_item(File.read(@item_path)) + content = item.raw_data @input_data.keys.reject{|i| i == 'id'}.each do |k| content.should have_key k content[k].should_not == @input_data[k] @@ -133,7 +137,8 @@ it 'should create the data bag item' do @knife.run - JSON.parse(File.read(@item_path)).raw_data.should == @input_data + item = JSON_to_databag_item(File.read(@item_path)) + item.raw_data.should == @input_data end context 'when encrypting with -s or --secret' do @@ -144,7 +149,8 @@ it 'should create the encrypted data bag item' do @knife.run - content = JSON.parse(File.read(@item_path)).raw_data + item = JSON_to_databag_item(File.read(@item_path)) + content = item.raw_data @input_data.keys.reject{|i| i == 'id'}.each do |k| content.should have_key k content[k].should_not == @input_data[k] @@ -164,7 +170,8 @@ it 'should create the encrypted data bag item' do @knife.run - content = JSON.parse(File.read(@item_path)).raw_data + item = JSON_to_databag_item(File.read(@item_path)) + content = item.raw_data @input_data.keys.reject{|i| i == 'id'}.each do |k| content.should have_key k content[k].should_not == @input_data[k] @@ -185,7 +192,8 @@ it 'creates the encrypted data bag item' do @knife.run - content = JSON.parse(File.read(@item_path)).raw_data + item = JSON_to_databag_item(File.read(@item_path)) + content = item.raw_data @input_data.keys.reject{|i| i == 'id'}.each do |k| content.should have_key k content[k].should_not == @input_data[k] @@ -207,7 +215,8 @@ it 'creates the data bag item' do @knife.run - JSON.parse(File.read(@item_path)).raw_data.should == @input_data + item = JSON_to_databag_item(File.read(@item_path)) + item.raw_data.should == @input_data end end diff --git a/spec/unit/solo_data_bag_edit_spec.rb b/spec/unit/solo_data_bag_edit_spec.rb index 20ee547..62049fc 100644 --- a/spec/unit/solo_data_bag_edit_spec.rb +++ b/spec/unit/solo_data_bag_edit_spec.rb @@ -55,7 +55,8 @@ it 'should edit the data bag item' do @knife.run - JSON.parse(File.read(@item_path)).raw_data.should == @updated_data + item = JSON_to_databag_item(File.read(@item_path)) + item.raw_data.should == @updated_data end it 'should write pretty json' do @@ -83,7 +84,8 @@ it 'uses the data bag path from the override' do @knife.run - data = JSON.parse(File.read(@override_item_path)).raw_data + item = JSON_to_databag_item(File.read(@override_item_path)) + data = item.raw_data data.should == @updated_data end end @@ -98,7 +100,8 @@ it 'should edit the encrypted data bag item' do @knife.run - content = JSON.parse(File.read(@item_path)).raw_data + item = JSON_to_databag_item(File.read(@item_path)) + content = item.raw_data content['who'].should_not == @orig_data['who'] content['who'].should_not be_nil end @@ -118,7 +121,8 @@ it 'should edit the encrypted data bag item' do @knife.run - content = JSON.parse(File.read(@item_path)).raw_data + item = JSON_to_databag_item(File.read(@item_path)) + content = item.raw_data content['who'].should_not == @orig_data['who'] content['who'].should_not be_nil end @@ -140,7 +144,8 @@ it 'should edit the encrypted data bag item' do @knife.run - content = JSON.parse(File.read(@item_path)).raw_data + item = JSON_to_databag_item(File.read(@item_path)) + content = item.raw_data content['who'].should_not == @orig_data['who'] content['who'].should_not be_nil end