summaryrefslogtreecommitdiff
path: root/features/steps/run_client_steps.rb
blob: 0ecf85ec00d567a1143acd4c13b694701820928e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 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 'chef/shell_out'
require 'chef/mixin/shell_out'

include Chef::Mixin::ShellOut

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

def chef_client_command_string
  @log_level ||= ENV["LOG_LEVEL"] ? ENV["LOG_LEVEL"] : "error"
  @chef_args ||= ""
  @config_file ||= File.expand_path(File.join(configdir, 'client.rb'))

  "#{File.join(File.dirname(__FILE__), "..", "..", "chef", "bin", "chef-client")} -l #{@log_level} -c #{@config_file} #{@chef_args}"
end

###
# When
###
When /^I run the chef\-client$/ do
  status = Chef::Mixin::Command.popen4(chef_client_command_string()) do |p, i, o, e|
    @stdout = o.gets(nil)
    @stderr = e.gets(nil)
  end
  @status = status
end

When "I run the chef-client for no more than '$timeout' seconds" do |timeout|
  cmd = shell_out("#{CHEF_CLIENT} -l info -i 1 -s 1 -c #{File.expand_path(File.join(configdir, 'client.rb'))}", :timeout => timeout.to_i)
  @status = cmd.status
end

When /^I run the chef\-client again$/ do
  When "I run the chef-client"
end

When /^I run the chef\-client with '(.+)'$/ do |args|
  @chef_args = args
  When "I run the chef-client"
end

When "I run the chef-client with '$options' and the '$config_file' config" do |options, config_file|
  @config_file = File.expand_path(File.join(configdir, "#{config_file}.rb"))
  @chef_args = options
  When "I run the chef-client"
end

When /^I run the chef\-client with '(.+)' for '(.+)' seconds$/ do |args, run_for|
  @chef_args = args
  When "I run the chef-client for '#{run_for}' seconds"
end

When /^I run the chef\-client for '(.+)' seconds$/ do |run_for|
  # Normal behavior depends on the run_interval/recipes/default.rb to count down
  # and exit subordinate chef-client after two runs. However, we will forcably
  # kill the client if that didn't work.
  begin
    stdout_filename = "/tmp/chef.run_interval.stdout.#{$$}.txt"
    stderr_filename = "/tmp/chef.run_interval.stderr.#{$$}.txt"
    client_pid = Process.fork do
      STDOUT.reopen(File.open(stdout_filename, "w"))
      STDERR.reopen(File.open(stderr_filename, "w"))
      exec chef_client_command_string()
      exit 2
    end
  
    killer_pid = Process.fork {
      sleep run_for.to_i

      # Send KILL to the child chef-client. Due to OHAI-223, where ohai sometimes
      # ignores/doesn't exit correctly on receipt of SIGINT, brutally kill the
      # subprocess.
      begin
        Process.kill("KILL", client_pid)
      rescue Errno::ESRCH
        # Kill didn't work; the process exited while we were waiting, like
        # it's supposed to.
      end
    }

    Process.waitpid2(killer_pid)
    @status = Process.waitpid2(client_pid).last
    
    # Read these in so they can be used in later steps.
    @stdout = IO.read(stdout_filename)
    @stderr = IO.read(stderr_filename)
  ensure
    # clean up after ourselves.
    File.delete(stdout_filename)
    File.delete(stderr_filename)
  end
end

When /^I run the chef\-client at log level '(.+)'$/ do |log_level|
  @log_level = log_level.to_sym
  When "I run the chef-client"
end

When 'I run the chef-client with json attributes' do
  @log_level = :debug
  @chef_args = "-j #{File.join(FEATURES_DATA, 'json_attribs', 'attribute_settings.json')}"
  When "I run the chef-client"
end

When "I run the chef-client with json attributes '$json_file_basename'" do |json_file_basename|
  @log_level = :debug
  @chef_args = "-j #{File.join(FEATURES_DATA, 'json_attribs', "#{json_file_basename}.json")}"
  When "I run the chef-client"
end

When /^I run the chef\-client with config file '(.+)'$/ do |config_file|
  @config_file = config_file
  When "I run the chef-client"
end

When /^I run the chef\-client with logging to the file '(.+)'$/ do |log_file|
  
config_data = <<CONFIG
supportdir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
tmpdir = File.expand_path(File.join(File.dirname(__FILE__), "..", "tmp"))

log_level        :debug
log_location     File.join(tmpdir, "silly-monkey.log")
file_cache_path  File.join(tmpdir, "cache")
ssl_verify_mode  :verify_none
registration_url "http://127.0.0.1:4000"
template_url     "http://127.0.0.1:4000"
remotefile_url   "http://127.0.0.1:4000"
search_url       "http://127.0.0.1:4000"
role_url         "http://127.0.0.1:4000"
client_url       "http://127.0.0.1:4000"
chef_server_url  "http://127.0.0.1:4000"
validation_client_name "validator"
systmpdir = File.expand_path(File.join(Dir.tmpdir, "chef_integration"))
validation_key   File.join(systmpdir, "validation.pem")
client_key       File.join(systmpdir, "client.pem")
CONFIG
  
  @config_file = File.expand_path(File.join(File.dirname(__FILE__), '..', 'data', 'config', 'client-with-logging.rb'))  
  File.open(@config_file, "w") do |file|
    file.write(config_data)
  end

  self.cleanup_files << @config_file
  
  
  @status = Chef::Mixin::Command.popen4("#{File.join(File.dirname(__FILE__), "..", "..", "chef", "bin", "chef-client")} -c #{@config_file} #{@chef_args}") do |p, i, o, e|
    @stdout = o.gets(nil)
    @stderr = e.gets(nil)
  end
end

###
# Then
###
Then /^the run should exit '(.+)'$/ do |exit_code|
  if ENV['LOG_LEVEL'] == 'debug'
    puts @status.inspect
    puts @status.exitstatus
  end
  begin
    @status.exitstatus.should eql(exit_code.to_i)
  rescue 
    print_output
    raise
  end
  print_output if ENV["LOG_LEVEL"] == "debug"
end

Then "I print the debug log" do
  print_output
end

Then /^the run should exit from being signaled$/ do 
  begin
    @status.signaled?.should == true
  rescue 
    print_output
    raise
  end
  print_output if ENV["LOG_LEVEL"] == "debug"
end


def print_output
  puts "--- run stdout:"
  puts @stdout
  puts "--- run stderr:"
  puts @stderr
end

# Matcher for regular expression which uses normal string interpolation for
# the actual (target) value instead of expecting it, as stdout/stderr which
# get matched against may have lots of newlines, which looks ugly when
# inspected, as the newlines show up as \n
class NoInspectMatch
  def initialize(expected_regex)
    @expected_regex = expected_regex
  end
  def matches?(target)
    @target = target
    @target =~ @expected_regex
  end
  def failure_message
    "expected #{@target} should match #{@expected_regex}"
  end
  def negative_failure_message
    "expected #{@target} not to match #{@expected_regex}"
  end
end
def noinspect_match(expected_regex)
  NoInspectMatch.new(expected_regex)
end


Then /^'(.+)' should have '(.+)'$/ do |which, to_match|
  if which == "stdout" || which == "stderr"
    self.instance_variable_get("@#{which}".to_sym).should noinspect_match(/#{to_match}/m)
  else
    self.instance_variable_get("@#{which}".to_sym).should match(/#{to_match}/m)
  end    
end

Then /^'(.+)' should not have '(.+)'$/ do |which, to_match|
  to_match = Regexp.escape(to_match)
  if which == "stdout" || which == "stderr"
    self.instance_variable_get("@#{which}".to_sym).should_not noinspect_match(/#{to_match}/m)
  else
    self.instance_variable_get("@#{which}".to_sym).should_not match(/#{to_match}/m)
  end
end

Then /^'(.+)' should appear on '(.+)' '(.+)' times$/ do |to_match, which, count|
  seen_count = 0
  self.instance_variable_get("@#{which}".to_sym).split("\n").each do |line|
    seen_count += 1 if line =~ /#{to_match}/
  end
  seen_count.should == count.to_i
end

Then "I inspect the contents of the features tmpdir" do
  puts `ls -halpR #{tmpdir}`
end