Module: Kitchen::Configurable

Included in:
Driver::Base, Driver::SSHBase, Provisioner::Base, Transport::Base, Verifier::Base
Defined in:
lib/kitchen/configurable.rb

Overview

A mixin for providing configuration-related behavior such as default config (static, computed, inherited), required config, local path expansion, etc.

Author:

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#instanceKitchen::Instance (readonly)

Returns the associated instance.

Returns:



37
38
39
# File 'lib/kitchen/configurable.rb', line 37

def instance
  @instance
end

Class Method Details

.included(base) ⇒ Object



32
33
34
# File 'lib/kitchen/configurable.rb', line 32

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#[](attr) ⇒ Object

Provides hash-like access to configuration keys.

Parameters:

  • attr (Object)

    configuration key

Returns:

  • (Object)

    value at configuration key



65
66
67
# File 'lib/kitchen/configurable.rb', line 65

def [](attr)
  config[attr]
end

#bourne_shell?TrueClass, FalseClass

Returns true if :shell_type is "bourne" (or unset, for backwards compatability).

Returns:

  • (TrueClass, FalseClass)

    true if :shell_type is "bourne" (or unset, for backwards compatability)



71
72
73
# File 'lib/kitchen/configurable.rb', line 71

def bourne_shell?
  ["bourne", nil].include?(instance.platform.shell_type)
end

#calculate_path(path, opts = {}) ⇒ String

Find an appropriate path to a file or directory, based on graceful fallback rules or returns nil if path cannot be determined.

Given an instance with suite named "server", a test_base_path of "/a/b", and a path segement of "roles" then following will be tried in order (first match that exists wins):

  1. /a/b/server/roles
  2. /a/b/roles
  3. $PWD/roles

Parameters:

  • path (String)

    the base path segment to search for

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

    options

Options Hash (opts):

  • :type (Symbol)

    either :file or :directory (default)

  • :base_path (Symbol)

    a custom base path to search under, default uses value from config[:test_base_path]

Returns:

  • (String)

    path to the existing file or directory, or nil if file or directory was not found

Raises:

  • (UserError)

    if config[:test_base_path] is used and is not set



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/kitchen/configurable.rb', line 94

def calculate_path(path, opts = {})
  type = opts.fetch(:type, :directory)
  base = opts.fetch(:base_path) do
    config.fetch(:test_base_path) do |key|
      raise UserError, "#{key} is not found in #{self}"
    end
  end

  [
    File.join(base, instance.suite.name, path),
    File.join(base, path),
    File.join(Dir.pwd, path)
  ].find do |candidate|
    type == :directory ? File.directory?(candidate) : File.file?(candidate)
  end
end

#config_keysArray

Returns an array of configuration keys.

Returns:

  • (Array)

    array of configuration keys



114
115
116
# File 'lib/kitchen/configurable.rb', line 114

def config_keys
  config.keys
end

#diagnoseHash

Returns a Hash of configuration and other useful diagnostic information.

Returns:

  • (Hash)

    a diagnostic hash



121
122
123
124
125
# File 'lib/kitchen/configurable.rb', line 121

def diagnose
  result = Hash.new
  config_keys.sort.each { |k| result[k] = config[k] }
  result
end

#diagnose_pluginHash

Returns a Hash of configuration and other useful diagnostic information associated with the plugin itself (such as loaded version, class name, etc.).

Returns:

  • (Hash)

    a diagnostic hash



132
133
134
135
136
137
# File 'lib/kitchen/configurable.rb', line 132

def diagnose_plugin
  result = Hash.new
  result[:name] = name
  result.merge!(self.class.diagnose)
  result
end

#finalize_config!(instance) ⇒ self

A lifecycle method that should be invoked when the object is about ready to be used. A reference to an Instance is required as configuration dependant data may be access through an Instance. This also acts as a hook point where the object may wish to perform other last minute checks, validations, or configuration expansions.

Parameters:

  • instance (Instance)

    an associated instance

Returns:

  • (self)

    itself, for use in chaining

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kitchen/configurable.rb', line 48

def finalize_config!(instance)
  if instance.nil?
    raise ClientError, "Instance must be provided to #{self}"
  end

  @instance = instance
  expand_paths!
  validate_config!
  load_needed_dependencies!

  self
end

#nameString

Returns the name of this plugin, suitable for display in a CLI.

Returns:

  • (String)

    name of this plugin



142
143
144
# File 'lib/kitchen/configurable.rb', line 142

def name
  self.class.name.split("::").last
end

#powershell_shell?TrueClass, FalseClass

Returns true if :shell_type is "powershell".

Returns:

  • (TrueClass, FalseClass)

    true if :shell_type is "powershell"



147
148
149
# File 'lib/kitchen/configurable.rb', line 147

def powershell_shell?
  ["powershell"].include?(instance.platform.shell_type)
end

#remote_path_join(*parts) ⇒ String

Builds a file path based on the :os_type ("windows" or "unix").

Returns:

  • (String)

    joined path for instance's os_type



154
155
156
157
# File 'lib/kitchen/configurable.rb', line 154

def remote_path_join(*parts)
  path = File.join(*parts)
  windows_os? ? path.gsub("/", "\\") : path.gsub("\\", "/")
end

#unix_os?TrueClass, FalseClass

Returns true if :os_type is "unix" (or unset, for backwards compatibility).

Returns:

  • (TrueClass, FalseClass)

    true if :os_type is "unix" (or unset, for backwards compatibility)



161
162
163
# File 'lib/kitchen/configurable.rb', line 161

def unix_os?
  ["unix", nil].include?(instance.platform.os_type)
end

#verify_dependenciesObject

Performs whatever tests that may be required to ensure that this plugin will be able to function in the current environment. This may involve checking for the presence of certain directories, software installed, etc.

Raises:

  • (UserError)

    if the plugin will not be able to perform or if a documented dependency is missing from the system



172
173
174
# File 'lib/kitchen/configurable.rb', line 172

def verify_dependencies
  # this method may be left unimplemented if that is applicable
end

#windows_os?TrueClass, FalseClass

Returns true if :os_type is "windows".

Returns:

  • (TrueClass, FalseClass)

    true if :os_type is "windows"



177
178
179
# File 'lib/kitchen/configurable.rb', line 177

def windows_os?
  ["windows"].include?(instance.platform.os_type)
end