Skip to content

Commit 0521e4e

Browse files
author
Tim Hinderliter
committed
Merge remote branch 'timh/CHEF-1835' into pl-master
2 parents b7f1739 + 61e96e4 commit 0521e4e

File tree

2 files changed

+55
-28
lines changed

2 files changed

+55
-28
lines changed

‎features/data/cookbooks/run_interval/recipes/default.rb‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Cookbook Name:: run_interval
33
# Recipe:: default
44
#
5-
# Copyright 2009, Opscode
5+
# Copyright 2009, 2010, Opscode
66
#
77
# Licensed under the Apache License, Version 2.0 (the "License");
88
# you may not use this file except in compliance with the License.
@@ -16,3 +16,12 @@
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
1818
#
19+
20+
# Force chef-client to exit once this cookbook has been applied twice.
21+
# The test depends on chef having run twice, so this number is tied to
22+
# run_interval.feature!
23+
24+
$run_interval_global ||= 2
25+
26+
$run_interval_global -= 1
27+
exit(2)if $run_interval_global == 0

‎features/steps/run_client_steps.rb‎

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@
2323

2424
CHEF_CLIENT=File.join(CHEF_PROJECT_ROOT,"chef","bin","chef-client")
2525

26+
defchef_client_command_string
27+
@log_level ||= ENV["LOG_LEVEL"] ? ENV["LOG_LEVEL"] : "error"
28+
@chef_args ||= ""
29+
@config_file ||= File.expand_path(File.join(configdir,'client.rb'))
30+
31+
"#{File.join(File.dirname(__FILE__),"..","..","chef","bin","chef-client")} -l #{@log_level} -c #{@config_file}#{@chef_args}"
32+
end
2633

2734
###
2835
# When
2936
###
3037
When/^I run the chef\-client$/do
31-
@log_level ||= ENV["LOG_LEVEL"] ? ENV["LOG_LEVEL"] : "error"
32-
@chef_args ||= ""
33-
@config_file ||= File.expand_path(File.join(configdir,'client.rb'))
34-
status=Chef::Mixin::Command.popen4(
35-
"#{File.join(File.dirname(__FILE__),"..","..","chef","bin","chef-client")} -l #{@log_level} -c #{@config_file}#{@chef_args}")do |p,i,o,e|
38+
status=Chef::Mixin::Command.popen4(chef_client_command_string())do |p,i,o,e|
3639
@stdout=o.gets(nil)
3740
@stderr=e.gets(nil)
3841
end
@@ -65,29 +68,44 @@
6568
end
6669

6770
When/^I run the chef\-client for '(.+)' seconds$/do |run_for|
68-
cid=Process.fork{
69-
sleeprun_for.to_i
70-
71-
client_pid=`ps ax | grep chef-client | grep -v grep | grep -v rake | grep -v cucumber | awk '{print $1 }'`.to_i
72-
73-
# Send a SIGINT to the child process so it has a chance to cleanly exit,
74-
# including flushing its stdout.
75-
Process.kill("INT",client_pid)
76-
77-
sleep1
78-
79-
# Send KILL to the child chef-client. Due to OHAI-223, where ohai sometimes
80-
# ignores/doesn't exit correctly on receipt of SIGINT, brutally kill the
81-
# subprocess.
82-
begin
83-
Process.kill("KILL",client_pid)
84-
rescueErrno::ESRCH
85-
# SIGINT worked above, so the KILL failed. Ignore it as this means things
86-
# are working the way they're supposed to.
71+
# Normal behavior depends on the run_interval/recipes/default.rb to count down
72+
# and exit subordinate chef-client after two runs. However, we will forcably
73+
# kill the client if that didn't work.
74+
begin
75+
stdout_filename="/tmp/chef.run_interval.stdout.#{$$}.txt"
76+
stderr_filename="/tmp/chef.run_interval.stderr.#{$$}.txt"
77+
client_pid=Process.forkdo
78+
STDOUT.reopen(File.open(stdout_filename,"w"))
79+
STDERR.reopen(File.open(stderr_filename,"w"))
80+
execchef_client_command_string()
81+
exit2
8782
end
88-
}
89-
When'I run the chef-client'
90-
Process.waitpid2(cid)
83+
84+
killer_pid=Process.fork{
85+
sleeprun_for.to_i
86+
87+
# Send KILL to the child chef-client. Due to OHAI-223, where ohai sometimes
88+
# ignores/doesn't exit correctly on receipt of SIGINT, brutally kill the
89+
# subprocess.
90+
begin
91+
Process.kill("KILL",client_pid)
92+
rescueErrno::ESRCH
93+
# Kill didn't work; the process exited while we were waiting, like
94+
# it's supposed to.
95+
end
96+
}
97+
98+
Process.waitpid2(killer_pid)
99+
@status=Process.waitpid2(client_pid).last
100+
101+
# Read these in so they can be used in later steps.
102+
@stdout=IO.read(stdout_filename)
103+
@stderr=IO.read(stderr_filename)
104+
ensure
105+
# clean up after ourselves.
106+
File.delete(stdout_filename)
107+
File.delete(stderr_filename)
108+
end
91109
end
92110

93111
When/^I run the chef\-client at log level '(.+)'$/do |log_level|

0 commit comments

Comments
(0)