Class: Kitchen::Verifier::Base

Inherits:
Object
  • Object
show all
Includes:
Configurable, Logging
Defined in:
lib/kitchen/verifier/base.rb

Overview

Base class for a verifier.

Author:

Direct Known Subclasses

Busser, Dummy

Instance Attribute Summary

Attributes included from Configurable

#instance

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#banner, #debug, #error, #fatal, #info, #warn

Methods included from Configurable

#[], #bourne_shell?, #calculate_path, #config_keys, #diagnose, #diagnose_plugin, #finalize_config!, included, #name, #powershell_shell?, #remote_path_join, #unix_os?, #verify_dependencies, #windows_os?

Constructor Details

#initialize(config = {}) ⇒ Base

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

Parameters:

  • config (Hash) (defaults to: {})

    provided verifier configuration



57
58
59
# File 'lib/kitchen/verifier/base.rb', line 57

def initialize(config = {})
  init_config(config)
end

Class Method Details

.kitchen_verifier_api_version(version) ⇒ Object

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

rubocop:disable Style/TrivialAccessors

Examples:

setting an API version


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

      kitchen_verifier_api_version 2

    end
  end
end

Parameters:

  • version (Integer, String)

    a version number



185
186
187
# File 'lib/kitchen/verifier/base.rb', line 185

def self.kitchen_verifier_api_version(version)
  @api_version = version
end

Instance Method Details

#call(state) ⇒ Object

Runs the verifier on the instance.

Parameters:

  • state (Hash)

    mutable instance state

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kitchen/verifier/base.rb', line 65

def call(state)
  create_sandbox
  sandbox_dirs = Dir.glob(File.join(sandbox_path, "*"))

  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)
  end
rescue Kitchen::Transport::TransportFailed => ex
  raise ActionFailed, ex.message
ensure
  cleanup_sandbox
end

#cleanup_sandboxObject

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.



87
88
89
90
91
92
# File 'lib/kitchen/verifier/base.rb', line 87

def cleanup_sandbox
  return if sandbox_path.nil?

  debug("Cleaning up local sandbox in #{sandbox_path}")
  FileUtils.rmtree(sandbox_path)
end

#create_sandboxObject

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:

Examples:

overriding #create_sandbox


class MyVerifier < Kitchen::Verifier::Base
  def create_sandbox
    super
    # any further file copies, preparations, etc.
  end
end


112
113
114
115
116
117
# File 'lib/kitchen/verifier/base.rb', line 112

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

#init_commandString

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.

Returns:

  • (String)

    a command string



133
134
# File 'lib/kitchen/verifier/base.rb', line 133

def init_command
end

#install_commandString

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.

Returns:

  • (String)

    a command string



124
125
# File 'lib/kitchen/verifier/base.rb', line 124

def install_command
end

#prepare_commandString

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.

Returns:

  • (String)

    a command string



142
143
# File 'lib/kitchen/verifier/base.rb', line 142

def prepare_command
end

#run_commandString

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.

Returns:

  • (String)

    a command string



150
151
# File 'lib/kitchen/verifier/base.rb', line 150

def run_command
end

#sandbox_pathString

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

Returns:

  • (String)

    the absolute path to the sandbox directory

Raises:

  • (ClientError)

    if the sandbox directory has no yet been created by calling #create_sandbox



159
160
161
162
163
# File 'lib/kitchen/verifier/base.rb', line 159

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