summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorDavid Schmitt <david@dasz.at>2010-05-18 16:33:47 +0200
committertest branch <puppet-dev@googlegroups.com>2010-02-17 06:50:53 -0800
commita6b52bb01b0c73d36d6b40c263e7542cb5cfbfff (patch)
treebeb1ba6622aac8d28ddff619cc939b7444e3d1e0 /spec
parent533ef68e47a124182066432c11040bbb6120b658 (diff)
downloadpuppet-a6b52bb01b0c73d36d6b40c263e7542cb5cfbfff.tar.gz
puppet-a6b52bb01b0c73d36d6b40c263e7542cb5cfbfff.tar.xz
puppet-a6b52bb01b0c73d36d6b40c263e7542cb5cfbfff.zip
Avoid trying to lock on non-files
This is not supported on windows and makes little sense on POSIX
Diffstat (limited to 'spec')
-rwxr-xr-xspec/unit/util/file_locking.rb53
1 files changed, 47 insertions, 6 deletions
diff --git a/spec/unit/util/file_locking.rb b/spec/unit/util/file_locking.rb
index 70003faa1..35e7f781e 100755
--- a/spec/unit/util/file_locking.rb
+++ b/spec/unit/util/file_locking.rb
@@ -26,18 +26,23 @@ describe Puppet::Util::FileLocking do
end
describe "when acquiring a read lock" do
+ before do
+ File.stubs(:exists?).with('/file').returns true
+ File.stubs(:file?).with('/file').returns true
+ end
+
it "should use a global shared mutex" do
- sync = mock 'sync'
- sync.expects(:synchronize).with(Sync::SH)
- Puppet::Util.expects(:sync).with("/file").returns sync
+ @sync = mock 'sync'
+ @sync.expects(:synchronize).with(Sync::SH).once
+ Puppet::Util.expects(:sync).with('/file').returns @sync
Puppet::Util::FileLocking.readlock '/file'
end
it "should use a shared lock on the file" do
- sync = mock 'sync'
- sync.expects(:synchronize).yields
- Puppet::Util.expects(:sync).with("/file").returns sync
+ @sync = mock 'sync'
+ @sync.stubs(:synchronize).yields
+ Puppet::Util.expects(:sync).with('/file').returns @sync
fh = mock 'filehandle'
File.expects(:open).with("/file").yields fh
@@ -47,6 +52,22 @@ describe Puppet::Util::FileLocking do
Puppet::Util::FileLocking.readlock('/file') { |l| result = l }
result.should == "locked_fh"
end
+
+ it "should only work on regular files" do
+ File.expects(:file?).with('/file').returns false
+ proc { Puppet::Util::FileLocking.readlock('/file') }.should raise_error(ArgumentError)
+ end
+
+ it "should create missing files" do
+ @sync = mock 'sync'
+ @sync.stubs(:synchronize).yields
+ Puppet::Util.expects(:sync).with('/file').returns @sync
+
+ File.expects(:exists?).with('/file').returns false
+ File.expects(:open).with('/file').once
+
+ Puppet::Util::FileLocking.readlock('/file')
+ end
end
describe "when acquiring a write lock" do
@@ -54,10 +75,14 @@ describe Puppet::Util::FileLocking do
@sync = mock 'sync'
Puppet::Util.stubs(:sync).returns @sync
@sync.stubs(:synchronize).yields
+ File.stubs(:file?).with('/file').returns true
+ File.stubs(:exists?).with('/file').returns true
end
it "should fail if the parent directory does not exist" do
FileTest.expects(:directory?).with("/my/dir").returns false
+ File.stubs(:file?).with('/my/dir/file').returns true
+ File.stubs(:exists?).with('/my/dir/file').returns true
lambda { Puppet::Util::FileLocking.writelock('/my/dir/file') }.should raise_error(Puppet::DevError)
end
@@ -111,5 +136,21 @@ describe Puppet::Util::FileLocking do
f.print "foo"
end
end
+
+ it "should only work on regular files" do
+ File.expects(:file?).with('/file').returns false
+ proc { Puppet::Util::FileLocking.writelock('/file') }.should raise_error(ArgumentError)
+ end
+
+ it "should create missing files" do
+ @sync = mock 'sync'
+ @sync.stubs(:synchronize).yields
+ Puppet::Util.expects(:sync).with('/file').returns @sync
+
+ File.expects(:exists?).with('/file').returns false
+ File.expects(:open).with('/file', 'w', 0600).once
+
+ Puppet::Util::FileLocking.writelock('/file')
+ end
end
end