class Kitchen::Verifier::Base

Base class for a verifier.

@author Fletcher Nichol <fnichol@nichol.ca>

Public Class Methods

kitchen_verifier_api_version(version) click to toggle source

Sets the API version for this verifier. If the verifier does not set this value, then `nil` will be used and reported.

Sets the API version for this verifier

@example setting an API version

module Kitchen
  module Verifier
    class NewVerifier < Kitchen::Verifier::Base

      kitchen_verifier_api_version 2

    end
  end
end

@param version [Integer,String] a version number

# File lib/kitchen/verifier/base.rb, line 202
def self.kitchen_verifier_api_version(version)
  @api_version = version
end
new(config = {}) click to toggle source

Creates a new Verifier object using the provided configuration data which will be merged with any default configuration.

@param config [Hash] provided verifier configuration

# File lib/kitchen/verifier/base.rb, line 58
def initialize(config = {})
  init_config(config)
end

Public Instance Methods

call(state) click to toggle source

Runs the verifier on the instance.

@param state [Hash] mutable instance state @raise [ActionFailed] if the action could not be completed

# File lib/kitchen/verifier/base.rb, line 66
def call(state)
  create_sandbox

  instance.transport.connection(state) do |conn|
    conn.execute(install_command)
    conn.execute(init_command)
    info("Transferring files to #{instance.to_str}")
    conn.upload(sandbox_dirs, config[:root_path])
    debug("Transfer complete")
    conn.execute(prepare_command)
    conn.execute(run_command)

    info("Downloading files from #{instance.to_str}")
    config[:downloads].to_h.each do |remotes, local|
      debug("Downloading #{Array(remotes).join(", ")} to #{local}")
      conn.download(remotes, local)
    end
    debug("Download complete")
  end
rescue Kitchen::Transport::TransportFailed => ex
  raise ActionFailed, ex.message
ensure
  cleanup_sandbox
end
cleanup_sandbox() click to toggle source

Deletes the sandbox path. Without calling this method, the sandbox path will persist after the process terminates. In other words, cleanup is explicit. This method is safe to call multiple times.

# File lib/kitchen/verifier/base.rb, line 102
def cleanup_sandbox
  return if sandbox_path.nil?

  debug("Cleaning up local sandbox in #{sandbox_path}")
  FileUtils.rmtree(sandbox_path)
end
create_sandbox() click to toggle source

Creates a temporary directory on the local workstation into which verifier related files and directories can be copied or created. The contents of this directory will be copied over to the instance before invoking the verifier's run command. After this method completes, it is expected that the contents of the sandbox is complete and ready for copy to the remote instance.

Note: any subclasses would be well advised to call super first when overriding this method, for example:

@example overriding `#create_sandbox`

class MyVerifier < Kitchen::Verifier::Base
  def create_sandbox
    super
    # any further file copies, preparations, etc.
  end
end
# File lib/kitchen/verifier/base.rb, line 127
def create_sandbox
  @sandbox_path = Dir.mktmpdir("#{instance.name}-sandbox-")
  File.chmod(0755, sandbox_path)
  info("Preparing files for transfer")
  debug("Creating local sandbox in #{sandbox_path}")
end
doctor(state) click to toggle source

Check system and configuration for common errors.

@param state [Hash] mutable instance state @returns [Boolean] Return true if a problem is found.

# File lib/kitchen/verifier/base.rb, line 95
def doctor(state)
  false
end
init_command() click to toggle source

Generates a command string which will perform any data initialization or configuration required after the verifier software is installed but before the sandbox has been transferred to the instance. If no work is required, then `nil` will be returned.

@return [String] a command string

# File lib/kitchen/verifier/base.rb, line 147
def init_command; end
install_command() click to toggle source

Generates a command string which will install and configure the verifier software on an instance. If no work is required, then `nil` will be returned.

@return [String] a command string

# File lib/kitchen/verifier/base.rb, line 139
def install_command; end
prepare_command() click to toggle source

Generates a command string which will perform any commands or configuration required just before the main verifier run command but after the sandbox has been transferred to the instance. If no work is required, then `nil` will be returned.

@return [String] a command string

# File lib/kitchen/verifier/base.rb, line 155
def prepare_command; end
run_command() click to toggle source

Generates a command string which will invoke the main verifier command on the prepared instance. If no work is required, then `nil` will be returned.

@return [String] a command string

# File lib/kitchen/verifier/base.rb, line 162
def run_command; end
sandbox_dirs() click to toggle source

Returns the list of items in the sandbox directory

@return [String] the absolute path of sandbox directory files

# File lib/kitchen/verifier/base.rb, line 179
def sandbox_dirs
  Util.list_directory(sandbox_path)
end
sandbox_path() click to toggle source

Returns the absolute path to the sandbox directory or raises an exception if `#create_sandbox` has not yet been called.

@return [String] the absolute path to the sandbox directory @raise [ClientError] if the sandbox directory has no yet been created

by calling `#create_sandbox`
# File lib/kitchen/verifier/base.rb, line 170
def sandbox_path
  @sandbox_path ||= raise ClientError, "Sandbox directory has not yet " \
     "been created. Please run #{self.class}#create_sandox before " \
     "trying to access the path."
end

Private Instance Methods

prefix_command(script) click to toggle source

Conditionally prefixes a command with a command prefix. This should generally be done after a command has been conditionally prefixed by sudo as certain platforms, such as Cisco Nexus, require all commands to be run with a prefix to obtain outbound network access.

@param command [String] command to be prefixed @return [String] the command, conditionally prefixed with the configured prefix @api private

# File lib/kitchen/verifier/base.rb, line 244
def prefix_command(script)
  config[:command_prefix] ? "#{config[:command_prefix]} #{script}" : script
end
shell_code_from_file(vars, file) click to toggle source

Builds a complete command given a variables String preamble and a file containing shell code.

@param vars [String] shell variables, as a String @param file [String] file basename (without extension) containing

shell code

@return [String] command @api private

# File lib/kitchen/verifier/base.rb, line 216
def shell_code_from_file(vars, file)
  src_file = File.join(
    File.dirname(__FILE__),
    %w{.. .. .. support},
    file + (powershell_shell? ? ".ps1" : ".sh")
  )

  wrap_shell_code([vars, "", IO.read(src_file)].join("\n"))
end
sudo(script) click to toggle source

Conditionally prefixes a command with a sudo command.

@param command [String] command to be prefixed @return [String] the command, conditionaly prefixed with sudo @api private

# File lib/kitchen/verifier/base.rb, line 231
def sudo(script)
  config[:sudo] ? "#{config[:sudo_command]} #{script}" : script
end