diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2014-05-30 20:11:59 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2014-05-30 20:11:59 -0700 |
commit | 67294ec99faa6832d6ab99d7e905bf890900bc28 (patch) | |
tree | b3faf423c16610a5282f7f23923ec2c43e084690 | |
parent | 57de4f18c8fa3f2e1a0760d2a2d48a35094cd407 (diff) | |
download | chef-67294ec99faa6832d6ab99d7e905bf890900bc28.tar.gz |
adding disable_fs unit tests
still need enable_fs unit tests
-rw-r--r-- | lib/chef/provider/mount/solaris.rb | 24 | ||||
-rw-r--r-- | spec/unit/provider/mount/solaris_spec.rb | 157 |
2 files changed, 105 insertions, 76 deletions
diff --git a/lib/chef/provider/mount/solaris.rb b/lib/chef/provider/mount/solaris.rb index a5b1d5db20..a9e2234664 100644 --- a/lib/chef/provider/mount/solaris.rb +++ b/lib/chef/provider/mount/solaris.rb @@ -99,7 +99,8 @@ class Chef passstr = pass == 0 ? "-" : pass optstr = (actual_options.nil? || actual_options.empty?) ? "-" : actual_options.join(',') - Tempfile.open("vfstab", "etc") do |f| + # FIXME: permissions + etc_tempfile do |f| f.write(IO.read(VFSTAB)) f.puts("#{device}\t-\t#{mount_point}\t#{fstype}\t#{passstr}\t#{autostr}\t#{optstr}") f.close @@ -112,7 +113,7 @@ class Chef found = false ::File.readlines(VFSTAB).reverse_each do |line| - if !found && line =~ /^#{device_vfstab_regex}\s+[-\/\w]+\s+#{Regexp.escape(mount_point)}/ + if !found && line =~ /^#{device_vfstab_regex}\s+\S+\s+#{Regexp.escape(mount_point)}/ found = true Chef::Log.debug("#{new_resource} is removed from vfstab") next @@ -120,13 +121,24 @@ class Chef contents << line end - Tempfile.open("vfstab", "etc") do |f| - f.write(contents.reverse) - f.close - FileUtils.mv f.path, VFSTAB + if found + # FIXME: permissions + etc_tempfile do |f| + f.write(contents.reverse.join('')) + f.close + FileUtils.mv f.path, VFSTAB + end + else + # this is likely some kind of internal error, since we should only call disable_fs when there + # the filesystem we want to disable is enabled. + Chef::Log.warn("#{new_resource} did not find the mountpoint to disable in the vfstab") end end + def etc_tempfile + yield Tempfile.open("vfstab", "/etc") + end + def mount_options_unchanged? current_resource.fstype == fstype and current_resource.options == options and diff --git a/spec/unit/provider/mount/solaris_spec.rb b/spec/unit/provider/mount/solaris_spec.rb index 9ac079a11c..e3f5ec555f 100644 --- a/spec/unit/provider/mount/solaris_spec.rb +++ b/spec/unit/provider/mount/solaris_spec.rb @@ -532,97 +532,114 @@ describe Chef::Provider::Mount::Solaris do end end - describe "when enabling the fs" do - it "should enable if enabled isn't true" do - @current_resource.enabled(false) +# describe "when enabling the fs" do +# it "should enable if enabled isn't true" do +# @current_resource.enabled(false) +# +# @fstab = StringIO.new +# File.stub(:open).with("/etc/fstab", "a").and_yield(@fstab) +# provider.enable_fs +# @fstab.string.should match(%r{^/dev/sdz1\s+/tmp/foo\s+ext3\s+defaults\s+0\s+2\s*$}) +# end +# +# it "should not enable if enabled is true and resources match" do +# @current_resource.enabled(true) +# @current_resource.fstype("ext3") +# @current_resource.options(["defaults"]) +# @current_resource.dump(0) +# @current_resource.pass(2) +# File.should_not_receive(:open).with("/etc/fstab", "a") +# +# provider.enable_fs +# end +# +# it "should enable if enabled is true and resources do not match" do +# @current_resource.enabled(true) +# @current_resource.fstype("auto") +# @current_resource.options(["defaults"]) +# @current_resource.dump(0) +# @current_resource.pass(2) +# @fstab = StringIO.new +# File.stub(:readlines).and_return([]) +# File.should_receive(:open).once.with("/etc/fstab", "w").and_yield(@fstab) +# File.should_receive(:open).once.with("/etc/fstab", "a").and_yield(@fstab) +# +# provider.enable_fs +# end +# end - @fstab = StringIO.new - File.stub(:open).with("/etc/fstab", "a").and_yield(@fstab) - provider.enable_fs - @fstab.string.should match(%r{^/dev/sdz1\s+/tmp/foo\s+ext3\s+defaults\s+0\s+2\s*$}) - end + describe "when disabling the fs" do + context "in the typical case" do + let(:other_mount) { "/dev/dsk/c0t2d0s0 /dev/rdsk/c0t2d0s0 / ufs 2 yes -" } - it "should not enable if enabled is true and resources match" do - @current_resource.enabled(true) - @current_resource.fstype("ext3") - @current_resource.options(["defaults"]) - @current_resource.dump(0) - @current_resource.pass(2) - File.should_not_receive(:open).with("/etc/fstab", "a") + let(:this_mount) { "/dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes -" } - provider.enable_fs - end + let(:vfstab_file_contents) { [other_mount, this_mount].join("\n") } - it "should enable if enabled is true and resources do not match" do - @current_resource.enabled(true) - @current_resource.fstype("auto") - @current_resource.options(["defaults"]) - @current_resource.dump(0) - @current_resource.pass(2) - @fstab = StringIO.new - File.stub(:readlines).and_return([]) - File.should_receive(:open).once.with("/etc/fstab", "w").and_yield(@fstab) - File.should_receive(:open).once.with("/etc/fstab", "a").and_yield(@fstab) + before do + provider.stub(:etc_tempfile).and_yield(Tempfile.open("vfstab")) + provider.disable_fs + end - provider.enable_fs + it "should leave the other mountpoint alone" do + IO.read(vfstab_file.path).should match(Regexp.escape(other_mount)) + end + + it "should disable the mountpoint we care about" do + IO.read(vfstab_file.path).should_not match(Regexp.escape(this_mount)) + end end - end - describe "when disabling the fs" do - it "should disable if enabled is true" do - @current_resource.enabled(true) + context "when there is a commented out line" do + let(:other_mount) { "/dev/dsk/c0t2d0s0 /dev/rdsk/c0t2d0s0 / ufs 2 yes -" } - other_mount = "/dev/sdy1 /tmp/foo ext3 defaults 1 2\n" - this_mount = "/dev/sdz1 /tmp/foo ext3 defaults 1 2\n" + let(:this_mount) { "/dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes -" } - @fstab_read = [this_mount, other_mount] - File.stub(:readlines).with("/etc/fstab").and_return(@fstab_read) - @fstab_write = StringIO.new - File.stub(:open).with("/etc/fstab", "w").and_yield(@fstab_write) + let(:comment) { "#/dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes -" } - provider.disable_fs - @fstab_write.string.should match(Regexp.escape(other_mount)) - @fstab_write.string.should_not match(Regexp.escape(this_mount)) - end + let(:vfstab_file_contents) { [other_mount, this_mount, comment].join("\n") } - it "should disable if enabled is true and ignore commented lines" do - @current_resource.enabled(true) + before do + provider.stub(:etc_tempfile).and_yield(Tempfile.open("vfstab")) + provider.disable_fs + end - fstab_read = [%q{/dev/sdy1 /tmp/foo ext3 defaults 1 2}, - %q{/dev/sdz1 /tmp/foo ext3 defaults 1 2}, - %q{#/dev/sdz1 /tmp/foo ext3 defaults 1 2}] - fstab_write = StringIO.new + it "should leave the other mountpoint alone" do + IO.read(vfstab_file.path).should match(/^#{Regexp.escape(other_mount)}/) + end - File.stub(:readlines).with("/etc/fstab").and_return(fstab_read) - File.stub(:open).with("/etc/fstab", "w").and_yield(fstab_write) + it "should disable the mountpoint we care about" do + IO.read(vfstab_file.path).should_not match(/^#{Regexp.escape(this_mount)}/) + end - provider.disable_fs - fstab_write.string.should match(%r{^/dev/sdy1 /tmp/foo ext3 defaults 1 2$}) - fstab_write.string.should match(%r{^#/dev/sdz1 /tmp/foo ext3 defaults 1 2$}) - fstab_write.string.should_not match(%r{^/dev/sdz1 /tmp/foo ext3 defaults 1 2$}) + it "should keep the comment" do + IO.read(vfstab_file.path).should match(/^#{Regexp.escape(comment)}/) + end end - it "should disable only the last entry if enabled is true" do - @current_resource.stub(:enabled).and_return(true) - fstab_read = ["/dev/sdz1 /tmp/foo ext3 defaults 1 2\n", - "/dev/sdy1 /tmp/foo ext3 defaults 1 2\n", - "/dev/sdz1 /tmp/foo ext3 defaults 1 2\n"] + context "when there is a duplicated line" do + let(:other_mount) { "/dev/dsk/c0t2d0s0 /dev/rdsk/c0t2d0s0 / ufs 2 yes -" } - fstab_write = StringIO.new - File.stub(:readlines).with("/etc/fstab").and_return(fstab_read) - File.stub(:open).with("/etc/fstab", "w").and_yield(fstab_write) + let(:this_mount) { "/dev/dsk/c0t2d0s7 /dev/rdsk/c0t2d0s7 /mnt/foo ufs 2 yes -" } - provider.disable_fs - fstab_write.string.should == "/dev/sdz1 /tmp/foo ext3 defaults 1 2\n/dev/sdy1 /tmp/foo ext3 defaults 1 2\n" - end + let(:vfstab_file_contents) { [this_mount, other_mount, this_mount].join("\n") } - it "should not disable if enabled is false" do - @current_resource.stub(:enabled).and_return(false) + before do + provider.stub(:etc_tempfile).and_yield(Tempfile.open("vfstab")) + provider.disable_fs + end - File.stub(:readlines).with("/etc/fstab").and_return([]) - File.should_not_receive(:open).and_yield(@fstab) + it "should leave the other mountpoint alone" do + IO.read(vfstab_file.path).should match(/^#{Regexp.escape(other_mount)}/) + end - provider.disable_fs + it "should still match the duplicated mountpoint" do + IO.read(vfstab_file.path).should match(/^#{Regexp.escape(this_mount)}/) + end + + it "should have removed the last line" do + IO.read(vfstab_file.path).should eql( "#{this_mount}\n#{other_mount}\n" ) + end end end end |