module Kitchen::Configurable
def self.included(base)
def self.included(base) base.extend(ClassMethods) end
def [](attr)
-
(Object)- value at configuration key
Parameters:
-
attr(Object) -- configuration key
def [](attr) config[attr] end
def bourne_shell?
-
(TrueClass, FalseClass)- true if `:shell_type` is `"bourne"` (or
def bourne_shell? shell_type = instance.platform.respond_to?(:shell_type) ? instance.platform.shell_type : nil ["bourne", nil].include?(shell_type) end
def calculate_path(path, opts = {})
-
(UserError)- if `config[:test_base_path]` is used and is not set
Returns:
-
(String)- path to the existing file or directory, or nil if file
Options Hash:
(**opts)-
:base_path(Symbol) -- a custom base path to search under, -
:type(Symbol) -- either `:file` or `:directory` (default)
Parameters:
-
opts(Hash) -- options -
path(String) -- the base path segment to search for
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
def config_keys
-
(Array)- array of configuration keys
def config_keys config.keys end
def deprecate_config!
- Api: - private
def deprecate_config! # We only want to display the deprecation list of config values once per execution on the default config. # This prevents the output of deprecations from being printed for each permutation of test suites. return if defined? @@has_been_warned_of_deprecations deprecated_attributes = LazyHash.new(self.class.deprecated_attributes, self) # Remove items from hash when not provided in the loaded config or when the rendered message is nil @deprecated_config = deprecated_attributes.delete_if { |attr, obj| !provided_config.key?(attr) || obj.nil? } unless deprecated_config.empty? warning = Util.outdent!(<<-MSG) Deprecated configuration detected: #{deprecated_config.keys.join("\n")} Run 'kitchen doctor' for details. MSG Error.warn_on_stderr(warning) # Set global var that the deprecation message has been printed @@has_been_warned_of_deprecations = true end end
def diagnose
-
(Hash)- a diagnostic hash
def diagnose result = {} config_keys.sort.each { |k| result[k] = config[k] } result end
def diagnose_plugin
-
(Hash)- a diagnostic hash
def diagnose_plugin result = {} result[:name] = name result.merge!(self.class.diagnose) result end
def env_wrapped(code)
def env_wrapped(code) code_parts = resolve_proxy_settings_from_config code_parts << shell_env_var("TEST_KITCHEN", 1) code_parts << shell_env_var("CI", ENV["CI"]) if ENV["CI"] code_parts << shell_env_var("CHEF_LICENSE", ENV["CHEF_LICENSE"]) if ENV["CHEF_LICENSE"] ENV.select { |key, value| key.start_with?("TKENV_") }.each do |key, value| env_var_name = "#{key}".sub!("TKENV_", "") code_parts << shell_env_var(env_var_name, value) end code_parts << code code_parts.join("\n") end
def expand_paths!
- Api: - private
def expand_paths! root_path = config[:kitchen_root] || Dir.pwd expanded_paths = LazyHash.new(self.class.expanded_paths, self).to_hash expanded_paths.each do |key, should_expand| next if !should_expand || config[key].nil? || config[key] == false config[key] = if config[key].is_a?(Array) config[key].map { |path| File.expand_path(path, root_path) } else File.expand_path(config[key], root_path) end end end
def export_proxy(env, type)
- Api: - private
Parameters:
-
code(String) -- the type of proxy to export, one of 'http', 'https' or 'ftp' -
env(Array) -- the environment to modify
def export_proxy(env, type) env << shell_env_var(type.to_s, ENV[type.to_s]) if ENV[type.to_s] env << shell_env_var(type.upcase.to_s, ENV[type.upcase.to_s]) if ENV[type.upcase.to_s] end
def finalize_config!(instance)
-
(ClientError)- if instance parameter is nil
Returns:
-
(self)- itself, for use in chaining
Parameters:
-
instance(Instance) -- an associated instance
def finalize_config!(instance) raise ClientError, "Instance must be provided to #{self}" if instance.nil? @instance = instance expand_paths! deprecate_config! validate_config! load_needed_dependencies! self end
def init_config(config)
- Api: - private
Parameters:
-
config(Hash) -- initial provided configuration
def init_config(config) @config = LazyHash.new(config, self) @provided_config = config.dup self.class.defaults.each do |attr, value| @config[attr] = value unless @config.key?(attr) end end
def load_needed_dependencies!
- Api: - private
Raises:
-
(ClientError)- if any library loading fails or any of the
Other tags:
- Example: overriding `#load_needed_dependencies!` -
def load_needed_dependencies! # this method may be left unimplemented if that is applicable end
def logger
- Api: - private
Returns:
-
(Logger)- the instance's logger or Test Kitchen's common logger
def logger instance ? instance.logger : Kitchen.logger end
def name
-
(String)- name of this plugin
def name self.class.name.split("::").last end
def powershell_shell?
-
(TrueClass, FalseClass)- true if `:shell_type` is `"powershell"`
def powershell_shell? shell_type = instance.platform.respond_to?(:shell_type) ? instance.platform.shell_type : nil ["powershell"].include?(shell_type) end
def proxy_config_setting_present?(protocol)
def proxy_config_setting_present?(protocol) config.key?(protocol) && !config[protocol].nil? && !config[protocol].empty? end
def proxy_from_config?
def proxy_from_config? proxy_setting_keys.any? do |protocol| !config[protocol].nil? end end
def proxy_from_environment?
def proxy_from_environment? proxy_setting_keys.any? do |protocol| !ENV[protocol.downcase.to_s].nil? || !ENV[protocol.upcase.to_s].nil? end end
def proxy_setting_keys
def proxy_setting_keys %i{http_proxy https_proxy ftp_proxy no_proxy} end
def reload_ps1_path
- Api: - private
Returns:
-
(String)- a powershell command to reload the `PATH` environment
def reload_ps1_path [ "$env:PATH = try {", "[System.Environment]::GetEnvironmentVariable('PATH','Machine')", "} catch { $env:PATH }\n\n", ].join("\n") end
def remote_path_join(*parts)
-
(String)- joined path for instance's os_type
def remote_path_join(*parts) path = File.join(*parts) windows_os? ? path.tr("/", "\\") : path.tr("\\", "/") end
def resolve_proxy_settings_from_config
def resolve_proxy_settings_from_config proxy_setting_keys.each_with_object([]) do |protocol, set_env| if !config.key?(protocol) || config[protocol].nil? export_proxy(set_env, protocol) elsif proxy_config_setting_present?(protocol) set_env << shell_env_var(protocol.downcase.to_s, config[protocol]) set_env << shell_env_var(protocol.upcase.to_s, config[protocol]) end end end
def shell_env_var(name, value)
- Api: - private
Returns:
-
(String)- shell variable assignment
Parameters:
-
value(String) -- variable value -
name(String) -- variable name
def shell_env_var(name, value) if powershell_shell? shell_var("env:#{name}", value) else "#{shell_var(name, value)}; export #{name}" end end
def shell_var(name, value)
- Api: - private
Returns:
-
(String)- shell variable assignment
Parameters:
-
value(String) -- variable value -
name(String) -- variable name
def shell_var(name, value) if powershell_shell? %{$#{name} = "#{value}"} else %{#{name}="#{value}"} end end
def unix_os?
-
(TrueClass, FalseClass)- true if `:os_type` is `"unix"` (or
def unix_os? os_type = instance.platform.respond_to?(:os_type) ? instance.platform.os_type : nil ["unix", nil].include?(os_type) end
def validate_config!
- Api: - private
def validate_config! self.class.validations.each do |attr, block| block.call(attr, config[attr], self) end end
def verify_dependencies
-
(UserError)- if the plugin will not be able to perform or if a
def verify_dependencies # this method may be left unimplemented if that is applicable end
def windows_os?
-
(TrueClass, FalseClass)- true if `:os_type` is `"windows"`
def windows_os? os_type = instance.platform.respond_to?(:os_type) ? instance.platform.os_type : nil ["windows"].include?(os_type) end
def wrap_shell_code(code)
- Api: - private
Returns:
-
(String)- wrapped shell code
Parameters:
-
code(String) -- the shell code to be wrapped
def wrap_shell_code(code) return env_wrapped(code) if powershell_shell? Util.wrap_command((env_wrapped code)) end