Class: Kitchen::Verifier::Awspec

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/verifier/awspec.rb

Overview

Awspec verifier for Kitchen.

Instance Method Summary collapse

Instance Method Details

#awspec_commandsObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/kitchen/verifier/awspec.rb', line 76

def awspec_commands
  if custom_awspec_command
    shellout custom_awspec_command
  else
    if config[:additional_awspec_command]
      c = config[:additional_awspec_command]
      shellout c
    end
    c = rspec_commands
    shellout c
  end
end

#bundler_cmdObject



207
208
209
# File 'lib/kitchen/verifier/awspec.rb', line 207

def bundler_cmd
  config[:bundler_path] ? "#{config[:bundler_path]}/bundle" : '$(which bundle)'
end

#bundler_local_cmdObject



211
212
213
# File 'lib/kitchen/verifier/awspec.rb', line 211

def bundler_local_cmd
  config[:bundler_path] ? "#{config[:bundler_path]}/bundle" : 'bundle'
end

#call(state) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/kitchen/verifier/awspec.rb', line 49

def call(state)
  info("[#{name}] Verify on instance=#{instance} with state=#{state}")
  sleep_if_set
  merge_state_to_env(state)
  config[:default_path] = Dir.pwd if config[:default_path] == '/tmp/kitchen'
  install_command
  awspec_commands
  debug("[#{name}] Verify completed.")
end

#chef_data_dir?(base, file) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
# File 'lib/kitchen/verifier/awspec.rb', line 286

def chef_data_dir?(base, file)
  file =~ %r{^#{base}/(data|data_bags|environments|nodes|roles)/}
end

#colorObject



239
240
241
# File 'lib/kitchen/verifier/awspec.rb', line 239

def color
  config[:color] ? '-c' : nil
end

#create_sandboxObject



71
72
73
74
# File 'lib/kitchen/verifier/awspec.rb', line 71

def create_sandbox
  super
  prepare_suites
end

#custom_awspec_commandObject



202
203
204
205
# File 'lib/kitchen/verifier/awspec.rb', line 202

def custom_awspec_command
  return config[:custom_awspec_command] if config[:custom_awspec_command]
  config[:awspec_command]
end

#env_varsObject



187
188
189
190
191
192
193
194
195
# File 'lib/kitchen/verifier/awspec.rb', line 187

def env_vars
  return nil if config[:env_vars].none?
  cmd = nil
  config[:env_vars].map do |k, v|
    info("Environment variable #{k} value #{v}")
    ENV[k.to_s] = v.to_s
  end
  cmd
end

#fi_test_awspec_installedObject



170
171
172
# File 'lib/kitchen/verifier/awspec.rb', line 170

def fi_test_awspec_installed
  config[:test_awspec_installed] ? 'fi' : nil
end

#gem_proxy_parmObject



235
236
237
# File 'lib/kitchen/verifier/awspec.rb', line 235

def gem_proxy_parm
  http_proxy ? "--http-proxy #{http_proxy}" : nil
end

#http_proxyObject



227
228
229
# File 'lib/kitchen/verifier/awspec.rb', line 227

def http_proxy
  config[:http_proxy]
end

#https_proxyObject



231
232
233
# File 'lib/kitchen/verifier/awspec.rb', line 231

def https_proxy
  config[:https_proxy]
end

#install_awspecObject

rubocop:disable Metrics/CyclomaticComplexity



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
# File 'lib/kitchen/verifier/awspec.rb', line 110

def install_awspec
  if config[:test_awspec_installed]
    begin
      require 'awspec'
      return
    rescue LoadError
      info('awspec not installed installing ...')
    end
  end
  unless config[:gemfile]
    gemfile = "#{config[:default_path]}/Gemfile"
    unless File.exist?(gemfile)
      File.open(gemfile, 'w') do |f|
        f.write("source 'https://rubygems.org'\ngem 'net-ssh','~> 2.9.4'\ngem 'awspec'")
      end
    end
  end
  gemfile = config[:gemfile] if config[:gemfile]
  begin
    shellout "#{bundler_local_cmd} install --gemfile='#{gemfile}'"
  rescue
    raise ActionFailed, 'Awspec install failed'
  end
  nil
end

#install_bundlerObject

private



103
104
105
106
107
# File 'lib/kitchen/verifier/awspec.rb', line 103

def install_bundler
  require 'bundler'
rescue LoadError
  shellout `gem install --no-ri --no-rdoc  bundler`
end

#install_commandObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kitchen/verifier/awspec.rb', line 89

def install_command
  info('Installing with custom install command') if config[:custom_install_command]
  return config[:custom_install_command] if config[:custom_install_command]
  info('Installing bundler and awspec locally on workstation')
  if config[:additional_install_command]
    c = config[:additional_install_command]
    shellout c
  end
  install_bundler
  install_awspec
end

#install_gemfileObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/kitchen/verifier/awspec.rb', line 136

def install_gemfile
  if config[:gemfile]
    <<-INSTALL
    #{read_gemfile}
    INSTALL
  else
    <<-INSTALL
    #{sudo('rm')} -f #{config[:default_path]}/Gemfile
    #{sudo('echo')} "source 'https://rubygems.org'" >> #{config[:default_path]}/Gemfile
    #{sudo('echo')} "gem 'net-ssh','~> 2.9'"  >> #{config[:default_path]}/Gemfile
    #{sudo('echo')} "gem 'awspec'" >> #{config[:default_path]}/Gemfile
    INSTALL
  end
end

#local_suite_filesArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an Array of test suite filenames for the related suite currently residing on the local workstation. Any special provisioner-specific directories (such as a Chef roles/ directory) are excluded.

Returns:

  • (Array<String>)

    array of suite files



296
297
298
299
300
301
302
# File 'lib/kitchen/verifier/awspec.rb', line 296

def local_suite_files
  base = File.join(config[:test_base_path], config[:suite_name])
  glob = File.join(base, '*/**/*')
  Dir.glob(glob).reject do |f|
    chef_data_dir?(base, f) || File.directory?(f)
  end
end

#merge_state_to_env(state) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/kitchen/verifier/awspec.rb', line 266

def merge_state_to_env(state)
  env_state = { :environment => {} }
  env_state[:environment]['KITCHEN_INSTANCE'] = instance.name
  env_state[:environment]['KITCHEN_PLATFORM'] = instance.platform.name
  env_state[:environment]['KITCHEN_SUITE'] = instance.suite.name
  state.each_pair do |key, value|
    env_state[:environment]['KITCHEN_' + key.to_s.upcase] = value.to_s
    ENV['KITCHEN_' + key.to_s.upcase] = value.to_s
    info("Environment variable #{'KITCHEN_' + key.to_s.upcase} value #{value}")
  end
  # if using a driver that uses transport expose those too
  %w(username password ssh_key port).each do |key|
    next if instance.transport[key.to_sym].nil?
    value = instance.transport[key.to_sym].to_s
    ENV['KITCHEN_' + key.to_s.upcase] = value
    info("Transport Environment variable #{'KITCHEN_' + key.to_s.upcase} value #{value}")
  end
  config[:shellout_opts].merge!(env_state)
end

#prepare_suitesObject

Copies all test suite files into the suites directory in the sandbox.



305
306
307
308
309
310
311
312
313
# File 'lib/kitchen/verifier/awspec.rb', line 305

def prepare_suites
  base = File.join(config[:test_base_path], config[:suite_name])
  debug("Creating local sandbox of all test suite files in #{base}")
  local_suite_files.each do |src|
    dest = File.join(sandbox_suites_dir, src.sub("#{base}/", ''))
    FileUtils.mkdir_p(File.dirname(dest))
    FileUtils.cp(src, dest, :preserve => true)
  end
end

#read_gemfileObject



151
152
153
154
155
156
157
158
159
# File 'lib/kitchen/verifier/awspec.rb', line 151

def read_gemfile
  data = "#{sudo('rm')} -f #{config[:default_path]}/Gemfile\n"
  f = File.open(config[:gemfile], 'r')
  f.each_line do |line|
    data = "#{data}#{sudo('echo')} \"#{line}\" >> #{config[:default_path]}/Gemfile\n"
  end
  f.close
  data
end

#remove_default_pathObject



161
162
163
164
# File 'lib/kitchen/verifier/awspec.rb', line 161

def remove_default_path
  info('Removing default path') if config[:remove_default_path]
  config[:remove_default_path] ? "rm -rf #{config[:default_path]}" : nil
end

#rspec_bash_cmdObject



215
216
217
# File 'lib/kitchen/verifier/awspec.rb', line 215

def rspec_bash_cmd
  config[:rspec_path] ? "#{config[:rspec_path]}/rspec" : '$(which rspec)'
end

#rspec_cmdObject



183
184
185
# File 'lib/kitchen/verifier/awspec.rb', line 183

def rspec_cmd
  "#{rspec_path}rspec"
end

#rspec_commandsObject



174
175
176
177
178
179
180
181
# File 'lib/kitchen/verifier/awspec.rb', line 174

def rspec_commands
  info('Running Awspec')
  if config[:default_pattern]
    info("Using default pattern #{config[:default_path]}/spec/*_spec.rb")
    config[:patterns] = ["#{config[:default_path]}/spec/*_spec.rb"]
  end
  config[:patterns].map { |s| "#{env_vars} #{sudo_env(rspec_cmd)} #{color} -f #{config[:format]} --default-path '#{config[:default_path]}' #{config[:extra_flags]} -P '#{s}'" }.join(';')
end

#rspec_pathObject



219
220
221
# File 'lib/kitchen/verifier/awspec.rb', line 219

def rspec_path
  config[:rspec_path] ? "#{config[:rspec_path]}/" : nil
end

#rspec_path_optionObject



223
224
225
# File 'lib/kitchen/verifier/awspec.rb', line 223

def rspec_path_option
  config[:rspec_path] ? "--rspec-path #{config[:rspec_path]}/" : nil
end

#run_commandObject

for legacy drivers.



60
61
62
63
# File 'lib/kitchen/verifier/awspec.rb', line 60

def run_command
  sleep_if_set
  awspec_commands
end

#sandbox_suites_dirString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns path to suites directory under sandbox path.

Returns:

  • (String)

    path to suites directory under sandbox path



317
318
319
# File 'lib/kitchen/verifier/awspec.rb', line 317

def sandbox_suites_dir
  File.join(sandbox_path, 'suites')
end

#setup_cmdObject



65
66
67
68
# File 'lib/kitchen/verifier/awspec.rb', line 65

def setup_cmd
  sleep_if_set
  install_command
end

#shellout(command) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/kitchen/verifier/awspec.rb', line 253

def shellout(command)
  command = command.strip
  info("Running command: #{command}")
  cmd = Mixlib::ShellOut.new(command, config[:shellout_opts])
  cmd.live_stream = config[:live_stream]
  cmd.run_command
  begin
    cmd.error!
  rescue Mixlib::ShellOut::ShellCommandFailed
    raise ActionFailed, "Command #{command.inspect} failed for #{instance.to_str}"
  end
end

#sleep_if_setObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sleep for a period of time, if a value is set in the config.



246
247
248
249
250
251
# File 'lib/kitchen/verifier/awspec.rb', line 246

def sleep_if_set
  config[:sleep].to_i.times do
    print '.'
    sleep 1
  end
end

#sudo_env(pm) ⇒ Object



197
198
199
200
# File 'lib/kitchen/verifier/awspec.rb', line 197

def sudo_env(pm)
  # TODO: handle proxies
  pm
end

#test_awspec_installedObject



166
167
168
# File 'lib/kitchen/verifier/awspec.rb', line 166

def test_awspec_installed
  config[:test_awspec_installed] ? "if [ $(#{sudo('gem')} list awspec -i) == 'false' ]; then" : nil
end