summaryrefslogtreecommitdiff
path: root/spec/functional/run_lock_spec.rb
diff options
context:
space:
mode:
authorSeth Chisamore <schisamo@opscode.com>2012-10-30 10:39:35 -0400
committerSeth Chisamore <schisamo@opscode.com>2012-10-30 10:39:35 -0400
commit24dc69a9a97e82a6e4207de68d6dcc664178249b (patch)
tree19bb289c9f88b4bbab066bc56b95d6d222fd5c35 /spec/functional/run_lock_spec.rb
parent9348c1c9c80ee757354d624b7dc1b78ebc7605c4 (diff)
downloadchef-24dc69a9a97e82a6e4207de68d6dcc664178249b.tar.gz
[OC-3564] move core Chef to the repo root \o/ \m/
The opscode/chef repository now only contains the core Chef library code used by chef-client, knife and chef-solo!
Diffstat (limited to 'spec/functional/run_lock_spec.rb')
-rw-r--r--spec/functional/run_lock_spec.rb90
1 files changed, 90 insertions, 0 deletions
diff --git a/spec/functional/run_lock_spec.rb b/spec/functional/run_lock_spec.rb
new file mode 100644
index 0000000000..af9bd1aa1f
--- /dev/null
+++ b/spec/functional/run_lock_spec.rb
@@ -0,0 +1,90 @@
+#
+# Author:: Daniel DeLeo (<dan@opscode.com>)
+# Copyright:: Copyright (c) 2012 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+require File.expand_path('../../spec_helper', __FILE__)
+require 'chef/client'
+
+describe Chef::RunLock do
+
+ # This behavior is believed to work on windows, but the tests use UNIX APIs.
+ describe "when locking the chef-client run", :unix_only => true do
+ it "allows only one chef client run per lockfile" do
+ read, write = IO.pipe
+ run_lock = Chef::RunLock.new(:file_cache_path => "/var/chef/cache", :lockfile => "/tmp/chef-client-running.pid")
+ p1 = fork do
+ run_lock.acquire
+ write.puts 1
+ #puts "[#{Time.new.to_i % 100}] p1 (#{Process.pid}) running with lock"
+ sleep 2
+ write.puts 2
+ #puts "[#{Time.new.to_i % 100}] p1 (#{Process.pid}) releasing lock"
+ run_lock.release
+ end
+
+ sleep 0.5
+
+ p2 = fork do
+ run_lock.acquire
+ write.puts 3
+ #puts "[#{Time.new.to_i % 100}] p2 (#{Process.pid}) running with lock"
+ run_lock.release
+ end
+
+ Process.waitpid2(p1)
+ Process.waitpid2(p2)
+
+ write.close
+ order = read.read
+ read.close
+
+ order.should == "1\n2\n3\n"
+ end
+
+ it "clears the lock if the process dies unexpectedly" do
+ read, write = IO.pipe
+ run_lock = Chef::RunLock.new(:file_cache_path => "/var/chef/cache", :lockfile => "/tmp/chef-client-running.pid")
+ p1 = fork do
+ run_lock.acquire
+ write.puts 1
+ #puts "[#{Time.new.to_i % 100}] p1 (#{Process.pid}) running with lock"
+ sleep 1
+ write.puts 2
+ #puts "[#{Time.new.to_i % 100}] p1 (#{Process.pid}) releasing lock"
+ run_lock.release
+ end
+
+ p2 = fork do
+ run_lock.acquire
+ write.puts 3
+ #puts "[#{Time.new.to_i % 100}] p2 (#{Process.pid}) running with lock"
+ run_lock.release
+ end
+ Process.kill(:KILL, p1)
+
+ Process.waitpid2(p1)
+ Process.waitpid2(p2)
+
+ write.close
+ order = read.read
+ read.close
+
+ order.should =~ /3\Z/
+ end
+ end
+
+end
+