class Kitchen::Diagnostic

Combines and compiles diagnostic information about a Test Kitchen configuration suitable for support and troubleshooting.

@author Fletcher Nichol <fnichol@nichol.ca>

Attributes

instances[R]

@return [Array<#diagnose>,Hash] an Array of instances that respond to

`#diagnose` or an error Hash

@api private

loader[R]

@return [#diagnose,Hash] a loader instance that responds to `#diagnose`

or an error Hash

@api private

result[R]

@return [Hash] a result hash @api private

Public Class Methods

new(options = {}) click to toggle source

Constructs a new Diagnostic object with an optional loader and optional instances array.

@param options [Hash] optional configuration @option options [#diagnose,Hash] :loader a loader instance that responds

to `#diagnose` or an error Hash

@option options [Array<#diagnose>,Hash] :instances an Array of instances

that respond to `#diagnose` or an error Hash

@option options [true,false] :plugins whether or not plugins should be

returned
# File lib/kitchen/diagnostic.rb, line 37
def initialize(options = {})
  @loader = options.fetch(:loader, nil)
  @instances = options.fetch(:instances, [])
  @plugins = options.fetch(:plugins, false)
  @result = {}
end

Public Instance Methods

read() click to toggle source

Returns a Hash with stringified keys containing diagnostic information.

@return [Hash] a configuration Hash

# File lib/kitchen/diagnostic.rb, line 47
def read
  prepare_common
  prepare_plugins
  prepare_loader
  prepare_instances

  Util.stringified_hash(result)
end

Private Instance Methods

error_hash?(obj) click to toggle source

Determins whether or not the object is an error hash. An error hash is defined as a Hash containing an `:error` key.

@param obj [Object] an object @return [true,false] whether or not the object is an error hash @api private

# File lib/kitchen/diagnostic.rb, line 134
def error_hash?(obj)
  obj.is_a?(Hash) && obj.key?(:error)
end
prepare_common() click to toggle source

Adds common information to the result Hash.

@api private

# File lib/kitchen/diagnostic.rb, line 75
def prepare_common
  result[:timestamp] = Time.now.gmtime.to_s
  result[:kitchen_version] = Kitchen::VERSION
end
prepare_instances() click to toggle source

Adds instance information to the result Hash.

@api private

# File lib/kitchen/diagnostic.rb, line 119
def prepare_instances
  result[:instances] = {}
  if error_hash?(instances)
    result[:instances][:error] = instances[:error]
  else
    Array(instances).each { |i| result[:instances][i.name] = i.diagnose }
  end
end
prepare_loader() click to toggle source

Adds loader information to the result Hash.

@api private

# File lib/kitchen/diagnostic.rb, line 83
def prepare_loader
  if error_hash?(loader)
    result[:loader] = loader
  else
    result[:loader] = loader.diagnose if loader
  end
end
prepare_plugins() click to toggle source

Adds plugin information to the result Hash.

@api private

# File lib/kitchen/diagnostic.rb, line 94
def prepare_plugins
  return unless @plugins

  if error_hash?(instances)
    result[:plugins] = { error: instances[:error] }
  elsif instances.empty?
    result[:plugins] = {}
  else
    plugins = {
      driver: [], provisioner: [], transport: [], verifier: []
    }
    instances.map(&:diagnose_plugins).each do |plugin_hash|
      plugin_hash.each { |type, plugin| plugins[type] << plugin }
    end
    plugins.each do |type, list|
      plugins[type] =
        Hash[list.uniq.map { |hash| [hash.delete(:name), hash] }]
    end
    result[:plugins] = plugins
  end
end