class Kitchen::Verifier::Awspec

Awspec verifier for Kitchen.

Public Instance Methods

awspec_commands() click to toggle source
# 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_cmd() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 207
def bundler_cmd
  config[:bundler_path] ? "#{config[:bundler_path]}/bundle" : '$(which bundle)'
end
bundler_local_cmd() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 211
def bundler_local_cmd
  config[:bundler_path] ? "#{config[:bundler_path]}/bundle" : 'bundle'
end
call(state) click to toggle source

(see Base#call)

# 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) click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 286
def chef_data_dir?(base, file)
  file =~ %r{^#{base}/(data|data_bags|environments|nodes|roles)/}
end
color() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 239
def color
  config[:color] ? '-c' : nil
end
create_sandbox() click to toggle source

(see Base#create_sandbox)

Calls superclass method
# File lib/kitchen/verifier/awspec.rb, line 71
def create_sandbox
  super
  prepare_suites
end
custom_awspec_command() click to toggle source
# 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_vars() click to toggle source
# 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_installed() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 170
def fi_test_awspec_installed
  config[:test_awspec_installed] ? 'fi' : nil
end
gem_proxy_parm() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 235
def gem_proxy_parm
  http_proxy ? "--http-proxy #{http_proxy}" : nil
end
http_proxy() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 227
def http_proxy
  config[:http_proxy]
end
https_proxy() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 231
def https_proxy
  config[:https_proxy]
end
install_awspec() click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# 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_bundler() click to toggle source

private

# 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_command() click to toggle source
# 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_gemfile() click to toggle source
# 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_files() click to toggle source

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.

@return [Array<String>] array of suite files @api private

# 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) click to toggle source
# 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_suites() click to toggle source

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

# 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_gemfile() click to toggle source
# 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_path() click to toggle source
# 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_cmd() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 215
def rspec_bash_cmd
  config[:rspec_path] ? "#{config[:rspec_path]}/rspec" : '$(which rspec)'
end
rspec_cmd() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 183
def rspec_cmd
  "#{rspec_path}rspec"
end
rspec_commands() click to toggle source
# 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_path() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 219
def rspec_path
  config[:rspec_path] ? "#{config[:rspec_path]}/" : nil
end
rspec_path_option() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 223
def rspec_path_option
  config[:rspec_path] ? "--rspec-path #{config[:rspec_path]}/" : nil
end
run_command() click to toggle source

for legacy drivers.

# File lib/kitchen/verifier/awspec.rb, line 60
def run_command
  sleep_if_set
  awspec_commands
end
sandbox_suites_dir() click to toggle source

@return [String] path to suites directory under sandbox path @api private

# File lib/kitchen/verifier/awspec.rb, line 317
def sandbox_suites_dir
  File.join(sandbox_path, 'suites')
end
setup_cmd() click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 65
def setup_cmd
  sleep_if_set
  install_command
end
shellout(command) click to toggle source
# 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_set() click to toggle source

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

@api private

# 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) click to toggle source
# File lib/kitchen/verifier/awspec.rb, line 197
def sudo_env(pm)
  # TODO: handle proxies
  pm
end
test_awspec_installed() click to toggle source
# 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