summaryrefslogtreecommitdiff
path: root/spec/unit/run_lock_spec.rb
diff options
context:
space:
mode:
authorClaire McQuin <claire@getchef.com>2014-05-01 14:38:07 -0700
committerClaire McQuin <claire@getchef.com>2014-05-05 09:25:05 -0700
commita20d6162f11154f248503f4145afff3a511afc98 (patch)
treeec4e924cdbd12b9014bddb4fa9261a7d6a44a3f5 /spec/unit/run_lock_spec.rb
parent561b564d28e12731434513f1783cd519690e144b (diff)
downloadchef-a20d6162f11154f248503f4145afff3a511afc98.tar.gz
add option to abandon chef run if blocked by another for too long
Diffstat (limited to 'spec/unit/run_lock_spec.rb')
-rw-r--r--spec/unit/run_lock_spec.rb98
1 files changed, 98 insertions, 0 deletions
diff --git a/spec/unit/run_lock_spec.rb b/spec/unit/run_lock_spec.rb
index de302dc533..9738f76650 100644
--- a/spec/unit/run_lock_spec.rb
+++ b/spec/unit/run_lock_spec.rb
@@ -35,6 +35,104 @@ describe Chef::RunLock do
end
end
+ describe "acquire" do
+ let(:lockfile) { "/tmp/chef-client-running.pid" }
+ subject(:runlock) { Chef::RunLock.new(lockfile) }
+
+ def stub_unblocked_run
+ runlock.stub(:test).and_return(true)
+ end
+
+ def stub_blocked_run(duration)
+ runlock.stub(:test).and_return(false)
+ runlock.stub(:wait) { sleep(duration) }
+ runlock.stub(:runpid).and_return(666) # errors read blocking pid
+ end
+
+ describe "when Chef::Config[:run_lock_timeout] is not set (set to default)" do
+ describe "and the lockfile is not locked by another client run" do
+ it "should not wait" do
+ stub_unblocked_run
+ Chef::RunLock.any_instance.should_not_receive(:wait)
+ runlock.acquire
+ end
+ end
+
+ describe "and the lockfile is locked by another client run" do
+ it "should wait for the lock to be released" do
+ stub_blocked_run(0.001)
+ runlock.should_receive(:wait)
+ runlock.acquire
+ end
+ end
+ end
+
+ describe "when Chef::Config[:run_lock_timeout] is set to 0" do
+ before(:each) do
+ @default_timeout = Chef::Config[:run_lock_timeout]
+ Chef::Config[:run_lock_timeout] = 0
+ end
+
+ after(:each) do
+ Chef::Config[:run_lock_timeout] = @default_timeout
+ end
+
+ describe "and the lockfile is not locked by another client run" do
+ it "should acquire the lock" do
+ stub_unblocked_run
+ runlock.should_not_receive(:wait)
+ runlock.acquire
+ end
+ end
+
+ describe "and the lockfile is locked by another client run" do
+ it "should raise Chef::Exceptions::RunLockTimeout" do
+ stub_blocked_run(0.001)
+ runlock.should_not_receive(:wait)
+ expect{ runlock.acquire }.to raise_error(Chef::Exceptions::RunLockTimeout)
+ end
+ end
+ end
+
+ describe "when Chef::Config[:run_lock_timeout] is set to >0" do
+ before(:each) do
+ @default_timeout = Chef::Config[:run_lock_timeout]
+ @timeout = 0.1
+ Chef::Config[:run_lock_timeout] = @timeout
+ end
+
+ after(:each) do
+ Chef::Config[:run_lock_timeout] = @default_timeout
+ end
+
+ describe "and the lockfile is not locked by another client run" do
+ it "should acquire the lock" do
+ stub_unblocked_run
+ runlock.should_not_receive(:wait)
+ runlock.acquire
+ end
+ end
+
+ describe "and the lockfile is locked by another client run" do
+ describe "and the lock is released before the timeout expires" do
+ it "should acquire the lock" do
+ stub_blocked_run(@timeout/2.0)
+ runlock.should_receive(:wait)
+ expect{ runlock.acquire }.not_to raise_error
+ end
+ end
+
+ describe "and the lock is not released before the timeout expires" do
+ it "should raise a RunLockTimeout exception" do
+ stub_blocked_run(2.0)
+ runlock.should_receive(:wait)
+ expect{ runlock.acquire }.to raise_error(Chef::Exceptions::RunLockTimeout)
+ end
+ end
+ end
+ end
+ end
+
# See also: spec/functional/run_lock_spec
end