diff options
author | Lamont Granquist <lamont@opscode.com> | 2013-03-22 16:01:08 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@opscode.com> | 2013-03-22 16:01:08 -0700 |
commit | db72ae9f28a3eaf27c8e8af5815f7b030186ef07 (patch) | |
tree | 055e49530c404d70ac4b998999630797722ef0dc /spec/unit/provider/template_spec.rb | |
parent | 675e0c7cca37a32189fd00cdf421363c7d0d2f46 (diff) | |
download | chef-db72ae9f28a3eaf27c8e8af5815f7b030186ef07.tar.gz |
more spec test work
Diffstat (limited to 'spec/unit/provider/template_spec.rb')
-rw-r--r-- | spec/unit/provider/template_spec.rb | 202 |
1 files changed, 15 insertions, 187 deletions
diff --git a/spec/unit/provider/template_spec.rb b/spec/unit/provider/template_spec.rb index 321864b4d8..4d9b6183ac 100644 --- a/spec/unit/provider/template_spec.rb +++ b/spec/unit/provider/template_spec.rb @@ -40,199 +40,27 @@ describe Chef::Provider::Template do it_behaves_like Chef::Provider::File -# before(:each) do -# @cookbook_repo = File.expand_path(File.join(CHEF_SPEC_DATA, "cookbooks")) -# Chef::Cookbook::FileVendor.on_create { |manifest| Chef::Cookbook::FileSystemFileVendor.new(manifest, @cookbook_repo) } -# -# @node = Chef::Node.new -# cl = Chef::CookbookLoader.new(@cookbook_repo) -# cl.load_cookbooks -# @cookbook_collection = Chef::CookbookCollection.new(cl) -# @events = Chef::EventDispatch::Dispatcher.new -# @run_context = Chef::RunContext.new(@node, @cookbook_collection, @events) -# -# @rendered_file_location = Dir.tmpdir + '/openldap_stuff.conf' -# -# @resource = Chef::Resource::Template.new(@rendered_file_location) -# @resource.cookbook_name = 'openldap' -# -# @provider = Chef::Provider::Template.new(@resource, @run_context) -# @current_resource = @resource.dup -# @provider.current_resource = @current_resource -# @access_controls = mock("access controls") -# @provider.stub!(:access_controls).and_return(@access_controls) -# passwd_struct = if windows? -# Struct::Passwd.new("root", "x", 0, 0, "/root", "/bin/bash") -# else -# Struct::Passwd.new("root", "x", 0, 0, "root", "/root", "/bin/bash") -# end -# group_struct = mock("Group Ent", :name => "root", :passwd => "x", :gid => 0) -# Etc.stub!(:getpwuid).and_return(passwd_struct) -# Etc.stub!(:getgrgid).and_return(group_struct) -# end - - describe "when creating the template" do - - before do - end + context "when creating the template" do - after do - FileUtils.rm(@rendered_file_location) if ::File.exist?(@rendered_file_location) - end + let(:node) { double('Chef::Node') } + let(:events) { double('Chef::Events').as_null_object } # mock all the methods + let(:run_context) { double('Chef::RunContext', :node => node, :events => events) } + let(:enclosing_directory) { File.expand_path(File.join(CHEF_SPEC_DATA, "templates")) } + let(:resource_path) { File.expand_path(File.join(enclosing_directory, "seattle.txt")) } - it "finds the template file in the coobook cache if it isn't local" do - @provider.template_location.should == CHEF_SPEC_DATA + '/cookbooks/openldap/templates/default/openldap_stuff.conf.erb' - end + # Subject - it "finds the template file locally if it is local" do - @resource.local(true) - @resource.source('/tmp/its_on_disk.erb') - @provider.template_location.should == '/tmp/its_on_disk.erb' + let(:provider) do + provider = described_class.new(resource, run_context) + provider.stub!(:content).and_return(content) + provider end it "stops executing when the local template source can't be found" do - @access_controls.stub!(:requires_changes?).and_return(false) - @resource.source "invalid.erb" - @resource.local true - lambda { @provider.run_action(:create) } .should raise_error Chef::Mixin::WhyRun::ResourceRequirements::Assertion::AssertionFailure - end - - it "should use the cookbook name if defined in the template resource" do - @resource.cookbook_name = 'apache2' - @resource.cookbook('openldap') - @resource.source "test.erb" - @provider.template_location.should == CHEF_SPEC_DATA + '/cookbooks/openldap/templates/default/test.erb' - end - - describe "when the target file does not exist" do - it "creates the template with the rendered content" do - @access_controls.stub!(:requires_changes?).and_return(true) - @access_controls.should_receive(:set_all!) - @node.normal[:slappiness] = "a warm gun" - @provider.should_receive(:backup) - @provider.run_action(:create) - IO.read(@rendered_file_location).should == "slappiness is a warm gun" - @resource.should be_updated_by_last_action - end - - it "should set the file access control as specified in the resource" do - @access_controls.stub!(:requires_changes?).and_return(false) - @access_controls.should_receive(:set_all!) - @resource.owner("adam") - @resource.group("wheel") - @resource.mode(00644) - @provider.run_action(:create) - @resource.should be_updated_by_last_action - end - - it "creates the template with the rendered content for the create if missing action" do - @access_controls.stub!(:requires_changes?).and_return(true) - @access_controls.should_receive(:set_all!) - @node.normal[:slappiness] = "happiness" - @provider.should_receive(:backup) - @provider.run_action(:create_if_missing) - IO.read(@rendered_file_location).should == "slappiness is happiness" - @resource.should be_updated_by_last_action - end - - context "and no access control settings are set on the resource" do - context "on a Unix system" do - before do - Chef::Platform.stub!(:windows?).and_return(false) - end - - it "sets access control metadata on the new resource" do - @access_controls.stub!(:requires_changes?).and_return(false) - @access_controls.should_receive(:set_all!) - @node.normal[:slappiness] = "happiness" - @provider.should_receive(:backup) - @provider.run_action(:create) - IO.read(@rendered_file_location).should == "slappiness is happiness" - @resource.should be_updated_by_last_action - - # Veracity of actual data checked in functional tests - @resource.owner.should be_a_kind_of(String) - @resource.group.should be_a_kind_of(String) - @resource.mode.should be_a_kind_of(String) - end - end - end - end - - describe "when the target file has the wrong content" do - before do - File.open(@rendered_file_location, "w+") { |f| f.print "blargh" } - end - - it "overwrites the file with the updated content when the create action is run" do - @node.normal[:slappiness] = "a warm gun" - @access_controls.stub!(:requires_changes?).and_return(false) - @access_controls.should_receive(:set_all!) - @provider.should_receive(:backup) - @provider.run_action(:create) - IO.read(@rendered_file_location).should == "slappiness is a warm gun" - @resource.should be_updated_by_last_action - end - - it "should set the file access control as specified in the resource" do - @access_controls.stub!(:requires_changes?).and_return(true) - @access_controls.should_receive(:set_all!) - @resource.owner("adam") - @resource.group("wheel") - @resource.mode(00644) - @provider.should_receive(:backup) - @provider.run_action(:create) - @resource.should be_updated_by_last_action - end - - it "doesn't overwrite the file when the create if missing action is run" do - @access_controls.stub!(:requires_changes?).and_return(false) - @access_controls.should_not_receive(:set_all!) - @node.normal[:slappiness] = "a warm gun" - @provider.should_not_receive(:backup) - @provider.run_action(:create_if_missing) - IO.read(@rendered_file_location).should == "blargh" - @resource.should_not be_updated_by_last_action - end - end - - describe "when the target has the correct content" do - before do - File.open(@rendered_file_location, "w") { |f| f.print "slappiness is a warm gun" } - @current_resource.checksum('4ff94a87794ed9aefe88e734df5a66fc8727a179e9496cbd88e3b5ec762a5ee9') - @access_controls = mock("access controls") - @provider.stub!(:access_controls).and_return(@access_controls) - end - - it "does not backup the original or overwrite it" do - @node.normal[:slappiness] = "a warm gun" - @access_controls.stub!(:requires_changes?).and_return(false) - @provider.should_not_receive(:backup) - FileUtils.should_not_receive(:mv) - @provider.run_action(:create) - @resource.should_not be_updated_by_last_action - end - - it "does not backup the original or overwrite it on create if missing" do - @node.normal[:slappiness] = "a warm gun" - @access_controls.stub!(:requires_changes?).and_return(false) - @provider.should_not_receive(:backup) - FileUtils.should_not_receive(:mv) - @provider.run_action(:create) - @resource.should_not be_updated_by_last_action - end - - it "sets the file access controls if they have diverged" do - @provider.stub!(:backup).and_return(true) - @access_controls.stub!(:requires_changes?).and_return(true) - @access_controls.should_receive(:set_all!) - @resource.owner("adam") - @resource.group("wheel") - @resource.mode(00644) - @provider.should_receive(:backup) - @provider.run_action(:create) - @resource.should be_updated_by_last_action - end + setup_normal_file + content.stub!(:template_location).and_return("/baz/bar/foo") + File.stub(:exists?).with("/baz/bar/foo").and_return(false) + lambda { provider.run_action(:create) }.should raise_error Chef::Mixin::WhyRun::ResourceRequirements::Assertion::AssertionFailure end end |