diff options
author | Ho-Sheng Hsiao <hosh@opscode.com> | 2013-02-28 15:51:25 -0800 |
---|---|---|
committer | John Keiser <jkeiser@opscode.com> | 2013-06-07 13:12:27 -0700 |
commit | fac10f8731aef1c3a8e29b9b59eea66ff8ba615a (patch) | |
tree | c492d2b5aeea41217f9c3bd940d4badd37aec305 /spec/unit/chef_fs | |
parent | 1bef87b28bbddf2b838a2fa6a081fab0c2140386 (diff) | |
download | chef-fac10f8731aef1c3a8e29b9b59eea66ff8ba615a.tar.gz |
[CORE] Bypass chef_object inflation and normalize raw requests instead
- Factored out api_request() from knife_essentials to a class method
available everywhere
- Added #raw_request to handle GET requests without chef object inflation
- Added RestListEntry#chef_hash to pull a raw_request()
- RestListEntry#read will now normalize from #chef_hash
- Added RestListDir#chef_collection to make it easier to customize and test
- Updated unit tests to mock #chef_hash and #chef_collection instead of using a
@rest mock, where appropriate
Diffstat (limited to 'spec/unit/chef_fs')
-rw-r--r-- | spec/unit/chef_fs/file_system/chef_server_root_dir_spec.rb | 38 | ||||
-rw-r--r-- | spec/unit/chef_fs/file_system/data_bags_dir_spec.rb | 112 |
2 files changed, 89 insertions, 61 deletions
diff --git a/spec/unit/chef_fs/file_system/chef_server_root_dir_spec.rb b/spec/unit/chef_fs/file_system/chef_server_root_dir_spec.rb index b2dbde2d11..a09c50abcb 100644 --- a/spec/unit/chef_fs/file_system/chef_server_root_dir_spec.rb +++ b/spec/unit/chef_fs/file_system/chef_server_root_dir_spec.rb @@ -20,6 +20,24 @@ require 'spec_helper' require 'chef/chef_fs/file_system/chef_server_root_dir' describe Chef::ChefFS::FileSystem::ChefServerRootDir do + + let(:should_receive_children) { endpoint.should_receive(:chef_collection).once.and_return(chef_collection) } + let(:should_receive_read) { endpoint_leaf.should_receive(:chef_hash).once.and_return(chef_hash) } + let(:should_throw_404) do + nonexistent_child.should_receive(:raw_request). + with("#{endpoint_name}/blah"). + once.and_raise(Net::HTTPServerException.new(nil,Net::HTTPResponse.new(nil,'404',nil))) + end + + let(:chef_collection) do + { + "achild" => "http://opscode.com/achild", + "bchild" => "http://opscode.com/bchild" + } + end + + let(:chef_hash) { { 'a' => 'b' } } + shared_examples 'a json endpoint dir leaf' do it 'parent is endpoint' do endpoint_leaf.parent.should == endpoint @@ -41,10 +59,7 @@ describe Chef::ChefFS::FileSystem::ChefServerRootDir do endpoint_leaf.exists?.should be_true end it 'read returns content' do - @rest.should_receive(:get_rest).with("#{endpoint_name}/#{endpoint_leaf_name}").once.and_return( - { - 'a' => 'b' - }) + should_receive_read endpoint_leaf.read.should == '{ "name": "achild", "a": "b" @@ -52,6 +67,7 @@ describe Chef::ChefFS::FileSystem::ChefServerRootDir do end end + shared_examples 'a json rest endpoint dir' do it 'parent is root' do endpoint.parent.should == root_dir @@ -81,17 +97,13 @@ describe Chef::ChefFS::FileSystem::ChefServerRootDir do endpoint.can_have_child?('blah', true).should be_false endpoint.can_have_child?('blah.json', true).should be_false end - let(:should_receive_children) { - @rest.should_receive(:get_rest).with(endpoint_name).once.and_return( - { - "achild" => "http://opscode.com/achild", - "bchild" => "http://opscode.com/bchild" - }) - } + + it 'has correct children' do should_receive_children endpoint.children.map { |child| child.name }.should =~ %w(achild.json bchild.json) end + context 'achild in endpoint.children' do let(:endpoint_leaf_name) { 'achild' } let(:endpoint_leaf) do @@ -121,7 +133,7 @@ describe Chef::ChefFS::FileSystem::ChefServerRootDir do nonexistent_child.dir?.should be_false end it 'read returns NotFoundError' do - @rest.should_receive(:get_rest).with("#{endpoint_name}/blah").once.and_raise(Net::HTTPServerException.new(nil,Net::HTTPResponse.new(nil,'404',nil))) + should_throw_404 expect { nonexistent_child.read }.to raise_error(Chef::ChefFS::FileSystem::NotFoundError) end end @@ -135,10 +147,12 @@ describe Chef::ChefFS::FileSystem::ChefServerRootDir do :client_key => 'key' }, 'everything') } + before(:each) do @rest = double("rest") Chef::REST.stub(:new).with('url','username','key') { @rest } end + context 'the root directory' do it 'has no parent' do root_dir.parent.should == nil diff --git a/spec/unit/chef_fs/file_system/data_bags_dir_spec.rb b/spec/unit/chef_fs/file_system/data_bags_dir_spec.rb index 2b01031e23..02a37c8733 100644 --- a/spec/unit/chef_fs/file_system/data_bags_dir_spec.rb +++ b/spec/unit/chef_fs/file_system/data_bags_dir_spec.rb @@ -28,46 +28,59 @@ describe Chef::ChefFS::FileSystem::DataBagsDir do :client_key => 'key' }, 'everything') } - let(:data_bags_dir) { root_dir.child('data_bags') } - let(:should_list_data_bags) do - @rest.should_receive(:get_rest).with('data').once.and_return( - { - "achild" => "http://opscode.com/achild", - "bchild" => "http://opscode.com/bchild" - }) - end - before(:each) do - @rest = double("rest") - Chef::REST.stub(:new).with('url','username','key') { @rest } - end - it 'has / as parent' do - data_bags_dir.parent.should == root_dir - end - it 'is a directory' do - data_bags_dir.dir?.should be_true - end - it 'exists' do - data_bags_dir.exists?.should be_true - end - it 'has name data_bags' do - data_bags_dir.name.should == 'data_bags' - end - it 'has path /data_bags' do - data_bags_dir.path.should == '/data_bags' - end - it 'has path_for_printing remote/data_bags' do - data_bags_dir.path_for_printing.should == 'remote/data_bags' + let(:data_bags_dir) do + root_dir.child('data_bags').tap do |artifact| + artifact.stub(:chef_collection).and_return(chef_collection) + end end - it 'has correct children' do - should_list_data_bags - data_bags_dir.children.map { |child| child.name }.should =~ %w(achild bchild) + + let(:should_list_data_bags) { } # noop. Remove before committing + + let(:chef_collection) do + { + "achild" => "http://opscode.com/achild", + "bchild" => "http://opscode.com/bchild" + } end - it 'can have directories as children' do - data_bags_dir.can_have_child?('blah', true).should be_true + + let(:item_collection) do + { + "aitem" => "http://opscode.com/achild", + "bitem" => "http://opscode.com/bchild" + } end - it 'cannot have files as children' do - data_bags_dir.can_have_child?('blah', false).should be_false + + context 'code contract' do + + it 'has / as parent' do + data_bags_dir.parent.should == root_dir + end + it 'is a directory' do + data_bags_dir.dir?.should be_true + end + it 'exists' do + data_bags_dir.exists?.should be_true + end + it 'has name data_bags' do + data_bags_dir.name.should == 'data_bags' + end + it 'has path /data_bags' do + data_bags_dir.path.should == '/data_bags' + end + it 'has path_for_printing remote/data_bags' do + data_bags_dir.path_for_printing.should == 'remote/data_bags' + end + it 'has correct children' do + should_list_data_bags + data_bags_dir.children.map { |child| child.name }.should =~ %w(achild bchild) + end + it 'can have directories as children' do + data_bags_dir.can_have_child?('blah', true).should be_true + end + it 'cannot have files as children' do + data_bags_dir.can_have_child?('blah', false).should be_false + end end shared_examples_for 'a data bag item' do @@ -90,11 +103,14 @@ describe Chef::ChefFS::FileSystem::DataBagsDir do it 'has correct path_for_printing' do data_bag_item.path_for_printing.should == "remote/data_bags/#{data_bag_dir_name}/#{data_bag_item_name}" end + it 'reads correctly' do - @rest.should_receive(:get_rest).with("data/#{data_bag_dir_name}/#{data_bag_item_short_name}").once.and_return({ - 'a' => 'b' - }) - data_bag_item.read.should == '{ + data_bag_item.should_receive(:raw_request). + with("data/#{data_bag_dir_name}/#{data_bag_item_short_name}"). + once.and_return({'a' => 'b'}.to_json) + + data_bag_item.read.should == +'{ "id": "aitem", "a": "b" }' @@ -102,13 +118,8 @@ describe Chef::ChefFS::FileSystem::DataBagsDir do end shared_examples_for 'a data bag' do - let(:should_list_data_bag_items) do - @rest.should_receive(:get_rest).with("data/#{data_bag_dir_name}").once.and_return( - { - "aitem" => "http://opscode.com/achild", - "bitem" => "http://opscode.com/bchild" - }) - end + let(:should_list_data_bag_items) { data_bag_dir.should_receive(:chef_collection).once.and_return(item_collection) } + it 'has /data as a parent' do data_bag_dir.parent.should == data_bags_dir end @@ -174,7 +185,11 @@ describe Chef::ChefFS::FileSystem::DataBagsDir do nonexistent_child.dir?.should be_false end it 'read returns NotFoundError' do - @rest.should_receive(:get_rest).with("data/#{data_bag_dir_name}/blah").once.and_raise(Net::HTTPServerException.new(nil,Net::HTTPResponse.new(nil,'404',nil))) + nonexistent_child.should_receive(:raw_request). + with("data/#{data_bag_dir_name}/blah"). + once. + and_raise(Net::HTTPServerException.new(nil,Net::HTTPResponse.new(nil,'404',nil))) + expect { nonexistent_child.read }.to raise_error(Chef::ChefFS::FileSystem::NotFoundError) end end @@ -182,7 +197,6 @@ describe Chef::ChefFS::FileSystem::DataBagsDir do context 'achild from data_bags.children' do let(:data_bag_dir) do - should_list_data_bags data_bags_dir.children.select { |child| child.name == 'achild' }.first end let(:data_bag_dir_name) { 'achild' } |