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



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/kitchen/busser.rb', line 145

def run_cmd
  @run_cmd ||= if local_suite_files.empty?
    nil
  else
    run_cmd  = []
    run_cmd << busser_setup_env
    run_cmd << "#{sudo}#{config[:busser_bin]} test"

    # use Bourne (/bin/sh) as Bash does not exist on all Unix flavors
    "sh -c '#{run_cmd.join('; ')}'"
  end
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
# File 'lib/kitchen/busser.rb', line 97

def setup_cmd
  @setup_cmd ||= if local_suite_files.empty?
    nil
  else
    setup_cmd  = []
    setup_cmd << busser_setup_env
    setup_cmd << "if ! #{sudo}#{config[:ruby_bindir]}/gem list busser -i >/dev/null"
    setup_cmd << "then #{sudo}#{config[:ruby_bindir]}/gem install #{gem_install_args}"
    setup_cmd << "fi"
    setup_cmd << "gem_bindir=`#{config[:ruby_bindir]}/ruby -rrubygems -e \"puts Gem.bindir\"`"
    setup_cmd << "#{sudo}${gem_bindir}/busser setup"
    setup_cmd << "#{sudo}#{config[:busser_bin]} plugin install #{plugins.join(' ')}"

    # use Bourne (/bin/sh) as Bash does not exist on all Unix flavors
    "sh -c '#{setup_cmd.join('; ')}'"
  end
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.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/kitchen/busser.rb', line 123

def sync_cmd
  @sync_cmd ||= if local_suite_files.empty?
    nil
  else
    sync_cmd  = []
    sync_cmd << busser_setup_env
    sync_cmd << "#{sudo}#{config[:busser_bin]} suite cleanup"
    sync_cmd << "#{local_suite_files.map { |f| stream_file(f, remote_file(f, config[:suite_name])) }.join("; ")}"
    sync_cmd << "#{helper_files.map { |f| stream_file(f, remote_file(f, "helpers")) }.join("; ")}"

    # use Bourne (/bin/sh) as Bash does not exist on all Unix flavors
    "sh -c '#{sync_cmd.join('; ')}'"
  end
end