lib/kitchen/driver/base.rb in test-kitchen-1.0.0.alpha.7 vs lib/kitchen/driver/base.rb in test-kitchen-1.0.0.beta.1
- old
+ new
@@ -20,50 +20,44 @@
module Kitchen
module Driver
- # Value object to track a shell command that will be passed to Kernel.exec
- # for execution.
+ # Base class for a driver.
#
# @author Fletcher Nichol <fnichol@nichol.ca>
- class LoginCommand
-
- attr_reader :cmd_array, :options
-
- def initialize(cmd_array, options = {})
- @cmd_array = cmd_array
- @options = options
- end
- end
-
- # Base class for a driver. A driver is responsible for carrying out the
- # lifecycle activities of an instance, such as creating, converging, and
- # destroying an instance.
- #
- # @author Fletcher Nichol <fnichol@nichol.ca>
class Base
include ShellOut
include Logging
- attr_writer :instance
+ attr_accessor :instance
class << self
attr_reader :serial_actions
end
def initialize(config = {})
- @config = config
+ @config = LazyDriverHash.new(config, self)
self.class.defaults.each do |attr, value|
@config[attr] = value unless @config[attr]
end
+ end
+
+ def validate_config!
Array(self.class.validations).each do |tuple|
- tuple.last.call(tuple.first, config[tuple.first])
+ tuple.last.call(tuple.first, config[tuple.first], self)
end
end
+ # Returns the name of this driver, suitable for display in a CLI.
+ #
+ # @return [String] name of this driver
+ def name
+ self.class.name.split('::').last
+ end
+
# Provides hash-like access to configuration keys.
#
# @param attr [Object] configuration key
# @return [Object] value at configuration key
def [](attr)
@@ -119,11 +113,11 @@
# documented dependency is missing from the system
def verify_dependencies ; end
protected
- attr_reader :config, :instance
+ attr_reader :config
ACTION_METHODS = %w{create converge setup verify destroy}.
map(&:to_sym).freeze
def logger
@@ -178,25 +172,26 @@
else
Hash.new
end
end
- def self.default_config(attr, value)
- defaults[attr] = value
+ def self.default_config(attr, value = nil, &block)
+ defaults[attr] = block_given? ? block : value
end
def self.validations
@validations
end
def self.required_config(attr, &block)
@validations = [] if @validations.nil?
if ! block_given?
klass = self
- block = lambda do |attr, value|
+ block = lambda do |attr, value, driver|
if value.nil? || value.to_s.empty?
- raise UserError, "#{klass}#config[:#{attr}] cannot be blank"
+ attribute = "#{klass}#{driver.instance.to_str}#config[:#{attr}]"
+ raise UserError, "#{attribute} cannot be blank"
end
end
end
@validations << [attr, block]
end
@@ -208,9 +203,31 @@
end
end
@serial_actions ||= []
@serial_actions += methods
+ end
+
+ # A modifed Hash object that may contain procs as a value which must be
+ # executed in the context of a Driver object.
+ #
+ # @author Fletcher Nichol <fnichol@nichol.ca>
+ class LazyDriverHash < SimpleDelegator
+
+ def initialize(obj, driver)
+ @driver = driver
+ super(obj)
+ end
+
+ def [](key)
+ proc_or_val = __getobj__[key]
+
+ if proc_or_val.respond_to?(:call)
+ proc_or_val.call(@driver)
+ else
+ proc_or_val
+ end
+ end
end
end
end
end