Skip to content

Commit

Permalink
make tests work with old or recent JSON module serialization behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
vagrant committed Nov 6, 2013
1 parent 22d23f2 commit 37047e5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 19 additions & 10 deletions spec/unit/solo_data_bag_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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

Expand Down
15 changes: 10 additions & 5 deletions spec/unit/solo_data_bag_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 37047e5

Please sign in to comment.