Class: Kitchen::Busser

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/busser.rb

Overview

Command string generator to interface with Busser. The commands that are generated are safe to pass to an SSH command or as an unix command argument (escaped in single quotes).

Author:

Instance Method Summary collapse

Constructor Details

#initialize(suite_name, opts = {}) ⇒ Busser

Constructs a new Busser command generator, given a suite name.

Parameters:

  • suite_name (String)

    name of suite on which to operate (Required)

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

    optional configuration

Options Hash (opts):

  • :kitchen_root (String)

    local path to the root of the project

  • :instance_ruby_bindir (String)

    path to the directory containing the Ruby binary on the remote instance

  • :sudo (TrueClass, FalseClass)

    whether or not to invoke sudo before commands requiring root access (default: true)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kitchen/busser.rb', line 41

def initialize(suite_name, opts = {})
  validate_options(suite_name)

  kitchen_root = opts.fetch(:kitchen_root) { Dir.pwd }
  test_base_path = opts.fetch(:test_base_path, Kitchen::DEFAULT_TEST_DIR)

  @config = Hash.new
  @config[:kitchen_root] = kitchen_root
  @config[:test_base_path] = File.expand_path(test_base_path, kitchen_root)
  @config[:suite_name] = suite_name
  @config[:sudo] = opts.fetch(:sudo, true)
  @config[:ruby_bindir] = opts.fetch(:ruby_bindir, DEFAULT_RUBY_BINDIR)
  @config[:root_path] = opts.fetch(:root_path, DEFAULT_ROOT_PATH)
  @config[:version] = opts.fetch(:version, "busser")
  @config[:busser_bin] = opts.fetch(:busser_bin, File.join(@config[:root_path], "bin/busser"))
end

Instance Method Details

#[](attr) ⇒ Object

Provides hash-like access to configuration keys.

Parameters:

  • attr (Object)

    configuration key

Returns:

  • (Object)

    value at configuration key



76
77
78
# File 'lib/kitchen/busser.rb', line 76

def [](attr)
  config[attr]
end

#config_keysArray

Returns an array of configuration keys.

Returns:

  • (Array)

    array of configuration keys



68
69
70
# File 'lib/kitchen/busser.rb', line 68

def config_keys
  config.keys
end

#diagnoseHash

Returns a Hash of configuration and other useful diagnostic information.

Returns:

  • (Hash)

    a diagnostic hash



83
84
85
86
87
# File 'lib/kitchen/busser.rb', line 83

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

#nameString

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

Returns:

  • (String)

    name of this busser



61
62
63
# File 'lib/kitchen/busser.rb', line 61

def name
  config[:suite_name]
end

#run_cmdString

Returns a command string which runs all Busser suite tests for the suite.

If no work needs to be performed, for example if there are no tests for the given suite, then nil will be returned.

Returns:

  • (String)

    a command string to run the test suites, or nil if no work needs to be performed



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/kitchen/busser.rb', line 151

def run_cmd
  return if local_suite_files.empty?

  cmd = <<-CMD.gsub(/^ {8}/, "")
    #{busser_setup_env}

    #{sudo(config[:busser_bin])} test
  CMD

  Util.wrap_command(cmd)
end

#setup_cmdString

Returns a command string which installs Busser, and installs all required Busser plugins for the suite.

If no work needs to be performed, for example if there are no tests for the given suite, then nil will be returned.

Returns:

  • (String)

    a command string to setup the test suite, or nil if no work needs to be performed



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/kitchen/busser.rb', line 97

def setup_cmd
  return if local_suite_files.empty?

  ruby    = "#{config[:ruby_bindir]}/ruby"
  gem     = sudo("#{config[:ruby_bindir]}/gem")
  busser  = sudo(config[:busser_bin])

  cmd = <<-CMD.gsub(/^ {8}/, "")
    #{busser_setup_env}
    gem_bindir=`#{ruby} -rrubygems -e "puts Gem.bindir"`

    if ! #{gem} list busser -i >/dev/null; then
      #{gem} install #{gem_install_args}
    fi
    #{sudo("${gem_bindir}")}/busser setup
    #{busser} plugin install #{plugins.join(" ")}
  CMD
  Util.wrap_command(cmd)
end

#sync_cmdString

Returns a command string which transfers all suite test files to the instance.

If no work needs to be performed, for example if there are no tests for the given suite, then nil will be returned.

Returns:

  • (String)

    a command string to transfer all suite test files, or nil if no work needs to be performed.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/kitchen/busser.rb', line 125

def sync_cmd
  return if local_suite_files.empty?

  cmd = <<-CMD.gsub(/^ {8}/, "")
    #{busser_setup_env}

    #{sudo(config[:busser_bin])} suite cleanup

  CMD
  local_suite_files.each do |f|
    cmd << stream_file(f, remote_file(f, config[:suite_name])).concat("\n")
  end
  helper_files.each do |f|
    cmd << stream_file(f, remote_file(f, "helpers")).concat("\n")
  end

  Util.wrap_command(cmd)
end