From ca716484915abf9a3220d898e458531151130910 Mon Sep 17 00:00:00 2001 From: Bertrand Paquet Date: Thu, 4 Jul 2013 22:17:06 +0200 Subject: [CHEF-2928] Allow to use array in role_path --- spec/unit/role_spec.rb | 67 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 4 deletions(-) (limited to 'spec/unit/role_spec.rb') diff --git a/spec/unit/role_spec.rb b/spec/unit/role_spec.rb index 764d586903..17a0e54921 100644 --- a/spec/unit/role_spec.rb +++ b/spec/unit/role_spec.rb @@ -244,6 +244,11 @@ describe Chef::Role do end end + ROLE_DSL=<<-EOR +name "ceiling_cat" +description "like Aliens, but furry" +EOR + describe "when loading from disk" do it "should return a Chef::Role object from JSON" do File.should_receive(:exists?).with(File.join(Chef::Config[:role_path], 'lolcat.json')).exactly(1).times.and_return(true) @@ -256,10 +261,6 @@ describe Chef::Role do File.should_receive(:exists?).with(File.join(Chef::Config[:role_path], 'lolcat.json')).exactly(1).times.and_return(false) File.should_receive(:exists?).with(File.join(Chef::Config[:role_path], 'lolcat.rb')).exactly(2).times.and_return(true) File.should_receive(:readable?).with(File.join(Chef::Config[:role_path], 'lolcat.rb')).exactly(1).times.and_return(true) - ROLE_DSL=<<-EOR -name "ceiling_cat" -description "like Aliens, but furry" -EOR IO.should_receive(:read).with(File.join(Chef::Config[:role_path], 'lolcat.rb')).and_return(ROLE_DSL) @role.should be_a_kind_of(Chef::Role) @role.class.from_disk("lolcat") @@ -271,5 +272,63 @@ EOR lambda {@role.class.from_disk("lolcat")}.should raise_error(Chef::Exceptions::RoleNotFound) end end + + describe "when loading from disk and role_path is an array" do + + before(:all) do + @old_config = Chef::Config[:role_path] + Chef::Config[:role_path] = ['/path1', '/path/path2'] + end + + it "should return a Chef::Role object from JSON" do + File.should_receive(:exists?).with(File.join('/path1', 'lolcat.json')).exactly(1).times.and_return(true) + IO.should_receive(:read).with(File.join('/path1', 'lolcat.json')).and_return('{"name": "ceiling_cat", "json_class": "Chef::Role" }') + @role.should be_a_kind_of(Chef::Role) + @role.class.from_disk("lolcat") + end + + it "should return a Chef::Role object from JSON when role is in the second path" do + File.should_receive(:exists?).with(File.join('/path1', 'lolcat.json')).exactly(1).times.and_return(false) + File.should_receive(:exists?).with(File.join('/path1', 'lolcat.rb')).exactly(1).times.and_return(false) + File.should_receive(:exists?).with(File.join('/path/path2', 'lolcat.json')).exactly(1).times.and_return(true) + IO.should_receive(:read).with(File.join('/path/path2', 'lolcat.json')).and_return('{"name": "ceiling_cat", "json_class": "Chef::Role" }') + @role.should be_a_kind_of(Chef::Role) + @role.class.from_disk("lolcat") + end + + it "should return a Chef::Role object from a Ruby DSL" do + File.should_receive(:exists?).with(File.join('/path1', 'lolcat.json')).exactly(1).times.and_return(false) + File.should_receive(:exists?).with(File.join('/path1', 'lolcat.rb')).exactly(2).times.and_return(true) + File.should_receive(:readable?).with(File.join('/path1', 'lolcat.rb')).exactly(1).times.and_return(true) + IO.should_receive(:read).with(File.join('/path1', 'lolcat.rb')).and_return(ROLE_DSL) + @role.should be_a_kind_of(Chef::Role) + @role.class.from_disk("lolcat") + end + + it "should return a Chef::Role object from a Ruby DSL when role is in the second path" do + File.should_receive(:exists?).with(File.join('/path1', 'lolcat.json')).exactly(1).times.and_return(false) + File.should_receive(:exists?).with(File.join('/path1', 'lolcat.rb')).exactly(1).times.and_return(false) + File.should_receive(:exists?).with(File.join('/path/path2', 'lolcat.json')).exactly(1).times.and_return(false) + File.should_receive(:exists?).with(File.join('/path/path2', 'lolcat.rb')).exactly(2).times.and_return(true) + File.should_receive(:readable?).with(File.join('/path/path2', 'lolcat.rb')).exactly(1).times.and_return(true) + IO.should_receive(:read).with(File.join('/path/path2', 'lolcat.rb')).and_return(ROLE_DSL) + @role.should be_a_kind_of(Chef::Role) + @role.class.from_disk("lolcat") + end + + it "should raise an exception if the file does not exist" do + File.should_receive(:exists?).with(File.join('/path1', 'lolcat.json')).exactly(1).times.and_return(false) + File.should_receive(:exists?).with(File.join('/path1', 'lolcat.rb')).exactly(1).times.and_return(false) + File.should_receive(:exists?).with(File.join('/path/path2', 'lolcat.json')).exactly(1).times.and_return(false) + File.should_receive(:exists?).with(File.join('/path/path2', 'lolcat.rb')).exactly(1).times.and_return(false) + lambda {@role.class.from_disk("lolcat")}.should raise_error(Chef::Exceptions::RoleNotFound) + end + + + after(:all) do + Chef::Config[:role_path] = @old_config + end + + end end -- cgit v1.2.1