Module: Beaker::DSL::Helpers

Included in:
Beaker::DSL
Defined in:
lib/beaker/dsl/helpers.rb

Overview

This is the heart of the Puppet Acceptance DSL. Here you find a helper to proxy commands to hosts, more commands to move files between hosts and execute remote scripts, confine test cases to certain hosts and prepare the state of a test case.

To mix this is into a class you need the following:

  • a method hosts that yields any hosts implementing Host‘s interface to act upon.

  • a method options that provides an options hash, see Options::OptionsHash

  • a method logger that yields a logger implementing Logger‘s interface.

  • the module Roles that provides access to the various hosts implementing Host‘s interface to act upon

  • the module Wrappers the provides convenience methods for Command creation

Constant Summary collapse

PUPPET_MODULE_INSTALL_IGNORE =
['.git', '.idea', '.vagrant', '.vendor', 'acceptance', 'spec', 'tests', 'log']

Instance Method Summary collapse

Instance Method Details

#apply_manifest(manifest, opts = {}, &block) ⇒ Object

Runs ‘puppet apply’ on default host, piping manifest through stdin

See Also:



994
995
996
# File 'lib/beaker/dsl/helpers.rb', line 994

def apply_manifest(manifest, opts = {}, &block)
  apply_manifest_on(default, manifest, opts, &block)
end

#apply_manifest_on(host, manifest, opts = {}, &block) ⇒ Object

Runs ‘puppet apply’ on a remote host, piping manifest through stdin

Parameters:

  • host (Host)

    The host that this command should be run on

  • manifest (String)

    The puppet manifest to apply

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Options to alter execution.

  • block (Block)

    This method will yield to a block of code passed by the caller; this can be used for additional validation, etc.

Options Hash (opts):

  • :silent (Boolean) — default: false

    Do not produce log output

  • :acceptable_exit_codes (Array<Fixnum>) — default: [0]

    An array (or range) of integer exit codes that should be considered acceptable. An error will be thrown if the exit code does not match one of the values in this list.

  • :environment (Hash{String=>String}) — default: {}

    These will be treated as extra environment variables that should be set before running the command.

  • :parseonly (Boolean) — default: false

    If this key is true, the “–parseonly” command line parameter will be passed to the ‘puppet apply’ command.

  • :trace (Boolean) — default: false

    If this key exists in the Hash, the “–trace” command line parameter will be passed to the ‘puppet apply’ command.

  • :acceptable_exit_codes (Array<Integer>) — default: [0]

    The list of exit codes that will NOT raise an error when found upon command completion. If provided, these values will be combined with those used in :catch_failures and :expect_failures to create the full list of passing exit codes.

  • :environment (Hash)

    Additional environment variables to be passed to the ‘puppet apply’ command

  • :catch_failures (Boolean) — default: false

    By default ‘puppet –apply` will exit with 0, which does not count as a test failure, even if there were errors or changes when applying the manifest. This option enables detailed exit codes and causes a test failure if `puppet –apply` indicates there was a failure during its execution.

  • :catch_changes (Boolean) — default: false

    This option enables detailed exit codes and causes a test failure if ‘puppet –apply` indicates that there were changes or failures during its execution.

  • :expect_changes (Boolean) — default: false

    This option enables detailed exit codes and causes a test failure if ‘puppet –apply` indicates that there were no resource changes during its execution.

  • :expect_failures (Boolean) — default: false

    This option enables detailed exit codes and causes a test failure if ‘puppet –apply` indicates there were no failure during its execution.

  • :future_parser (Boolean) — default: false

    This option enables the future parser option that is available from Puppet verion 3.2 By default it will use the ‘current’ parser.

  • :noop (Boolean) — default: false

    If this option exists, the the “–noop” command line parameter will be passed to the ‘puppet apply’ command.

  • :modulepath (String)

    The search path for modules, as a list of directories separated by the system path separator character. (The POSIX path separator is ‘:’, and the Windows path separator is ‘;’.)



919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/beaker/dsl/helpers.rb', line 919

def apply_manifest_on(host, manifest, opts = {}, &block)
  block_on host do | host |

    on_options = {}
    on_options[:acceptable_exit_codes] = Array(opts[:acceptable_exit_codes])

    puppet_apply_opts = {}
    puppet_apply_opts[:verbose] = nil
    puppet_apply_opts[:parseonly] = nil if opts[:parseonly]
    puppet_apply_opts[:trace] = nil if opts[:trace]
    puppet_apply_opts[:parser] = 'future' if opts[:future_parser]
    puppet_apply_opts[:modulepath] = opts[:modulepath] if opts[:modulepath]
    puppet_apply_opts[:noop] = nil if opts[:noop]

    # From puppet help:
    # "... an exit code of '2' means there were changes, an exit code of
    # '4' means there were failures during the transaction, and an exit
    # code of '6' means there were both changes and failures."
    if [opts[:catch_changes],opts[:catch_failures],opts[:expect_failures],opts[:expect_changes]].compact.length > 1
      raise(ArgumentError,
            'Cannot specify more than one of `catch_failures`, ' +
            '`catch_changes`, `expect_failures`, or `expect_changes` ' +
            'for a single manifest')
    end

    if opts[:catch_changes]
      puppet_apply_opts['detailed-exitcodes'] = nil

      # We're after idempotency so allow exit code 0 only.
      on_options[:acceptable_exit_codes] |= [0]
    elsif opts[:catch_failures]
      puppet_apply_opts['detailed-exitcodes'] = nil

      # We're after only complete success so allow exit codes 0 and 2 only.
      on_options[:acceptable_exit_codes] |= [0, 2]
    elsif opts[:expect_failures]
      puppet_apply_opts['detailed-exitcodes'] = nil

      # We're after failures specifically so allow exit codes 1, 4, and 6 only.
      on_options[:acceptable_exit_codes] |= [1, 4, 6]
    elsif opts[:expect_changes]
      puppet_apply_opts['detailed-exitcodes'] = nil

      # We're after changes specifically so allow exit code 2 only.
      on_options[:acceptable_exit_codes] |= [2]
    else
      # Either use the provided acceptable_exit_codes or default to [0]
      on_options[:acceptable_exit_codes] |= [0]
    end

    # Not really thrilled with this implementation, might want to improve it
    # later. Basically, there is a magic trick in the constructor of
    # PuppetCommand which allows you to pass in a Hash for the last value in
    # the *args Array; if you do so, it will be treated specially. So, here
    # we check to see if our caller passed us a hash of environment variables
    # that they want to set for the puppet command. If so, we set the final
    # value of *args to a new hash with just one entry (the value of which
    # is our environment variables hash)
    if opts.has_key?(:environment)
      puppet_apply_opts['ENV'] = opts[:environment]
    end

    file_path = host.tmpfile('apply_manifest.pp')
    create_remote_file(host, file_path, manifest + "\n")

    if host[:default_apply_opts].respond_to? :merge
      puppet_apply_opts = host[:default_apply_opts].merge( puppet_apply_opts )
    end

    on host, puppet('apply', file_path, puppet_apply_opts), on_options, &block
  end
end

#build_ignore_list(opts = {}) ⇒ Object

Build an array list of files/directories to ignore when pushing to remote host Automatically adds ‘..’ and ‘.’ to array. If not opts of :ignore list is provided it will use the static variable PUPPET_MODULE_INSTALL_IGNORE

Parameters:

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

Options Hash (opts):

  • :ignore_list (Array)

    A list of files/directories to ignore



1413
1414
1415
1416
1417
1418
1419
1420
1421
# File 'lib/beaker/dsl/helpers.rb', line 1413

def build_ignore_list(opts = {})
  ignore_list = opts[:ignore_list] || PUPPET_MODULE_INSTALL_IGNORE
  if !ignore_list.kind_of?(Array) || ignore_list.nil?
    raise ArgumentError "Ignore list must be an Array"
  end
  ignore_list << '.' unless ignore_list.include? '.'
  ignore_list << '..' unless ignore_list.include? '..'
  ignore_list
end

#check_for_package(host, package_name) ⇒ Boolean

Check to see if a package is installed on a remote host

Parameters:

  • host (Host)

    A host object

  • package_name (String)

    Name of the package to check for.

Returns:

  • (Boolean)

    true/false if the package is found



215
216
217
# File 'lib/beaker/dsl/helpers.rb', line 215

def check_for_package host, package_name
  host.check_for_package package_name
end

#confine(type, criteria, host_array = nil, &block) ⇒ Array<Host>

Note:

This will modify the TestCase#hosts member in place unless an array of hosts is passed into it and TestCase#logger yielding an object that responds like Logger#warn, as well as Outcomes#skip_test, and optionally TestCase#hosts.

Limit the hosts a test case is run against

Examples:

Basic usage to confine to debian OSes.

confine :to, :platform => 'debian'

Confining to anything but Windows and Solaris

confine :except, :platform => ['windows', 'solaris']

Using additional block to confine to Solaris global zone.

confine :to, :platform => 'solaris' do |solaris|
  on( solaris, 'zonename' ) =~ /global/
end

Parameters:

  • type (Symbol)

    The type of confinement to do. Valid parameters are :to to confine the hosts to only those that match criteria or :except to confine the test case to only those hosts that do not match criteria.

  • criteria (Hash{Symbol,String=>String,Regexp,Array<String,Regexp>})

    Specify the criteria with which a host should be considered for inclusion or exclusion. The key is any attribute of the host that will be yielded by Host#[]. The value can be any string/regex or array of strings/regexp. The values are compared using [Enumerable#any?] so that if one value of an array matches the host is considered a match for that criteria.

  • host_array (Array<Host>) (defaults to: nil)

    This creatively named parameter is an optional array of hosts to confine to. If not passed in, this method will modify TestCase#hosts in place.

  • block (Proc)

    Addition checks to determine suitability of hosts for confinement. Each host that is still valid after checking criteria is then passed in turn into this block. The block should return true if the host matches this additional criteria.

Returns:

  • (Array<Host>)

    Returns an array of hosts that are still valid targets for this tests case.

Raises:

  • (SkipTest)

    Raises skip test if there are no valid hosts for this test case after confinement.



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/beaker/dsl/helpers.rb', line 404

def confine(type, criteria, host_array = nil, &block)
  hosts_to_modify = host_array || hosts
  case type
  when :except
    hosts_to_modify = hosts_to_modify - select_hosts(criteria, hosts_to_modify, &block)
  when :to
    hosts_to_modify = select_hosts(criteria, hosts_to_modify, &block)
  else
    raise "Unknown option #{type}"
  end
  if hosts_to_modify.empty?
    logger.warn "No suitable hosts with: #{criteria.inspect}"
    skip_test 'No suitable hosts found'
  end
  self.hosts = hosts_to_modify
  hosts_to_modify
end

#confine_block(type, criteria, host_array = nil, &block) ⇒ Object

Ensures that host restrictions as specifid by type, criteria and host_array are confined to activity within the passed block. TestCase#hosts is reset after block has executed.

See Also:



427
428
429
430
431
432
433
434
435
436
437
# File 'lib/beaker/dsl/helpers.rb', line 427

def confine_block(type, criteria, host_array = nil, &block)
  begin
    original_hosts = self.hosts.dup
    confine(type, criteria, host_array)

    yield

  ensure
    self.hosts = original_hosts
  end
end

#copy_module_to(host, opts = {}) ⇒ Object Also known as: copy_root_module_to

Install local module for acceptance testing should be used as a presuite to ensure local module is copied to the hosts you want, particularly masters

Parameters:

  • host (Host, Array<Host>, String, Symbol)

    One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.

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

    a customizable set of options

Options Hash (opts):

  • :source (String) — default: './'

    The current directory where the module sits, otherwise will try

    and walk the tree to figure out
    
  • :module_name (String) — default: nil

    Name which the module should be installed under, please do not include author,

    if none is provided it will attempt to parse the metadata.json and then the Modulefile to determine
    the name of the module
    
  • :target_module_path (String) — default: host['distmoduledir']/modules

    Location where the module should be installed, will default

    to host['distmoduledir']/modules
    
  • :ignore_list (Array)

Raises:

  • (ArgumentError)

    if not host is provided or module_name is not provided and can not be found in Modulefile



1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
# File 'lib/beaker/dsl/helpers.rb', line 1320

def copy_module_to(host, opts = {})
  opts = {:source => './',
          :target_module_path => host['distmoduledir'],
          :ignore_list => PUPPET_MODULE_INSTALL_IGNORE}.merge(opts)
  ignore_list = build_ignore_list(opts)
  target_module_dir = opts[:target_module_path]
  if opts.has_key?(:module_name)
    module_name = opts[:module_name]
  else
    module_name = parse_for_modulename(opts[:source])
  end
  scp_to host, File.join(opts[:source]), File.join(target_module_dir, module_name), {:ignore => ignore_list}
end

#create_remote_file(hosts, file_path, file_content, opts = {}) ⇒ Result

Note:

This method uses Tempfile in Ruby’s STDLIB as well as #scp_to.

Create a remote file out of a string

Parameters:

  • hosts (Host, #do_scp_to)

    One or more hosts (or some object that responds like Host#do_scp_from.

  • file_path (String)

    A remote path to place file_content at.

  • file_content (String)

    The contents of the file to be placed.

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Options to alter execution.

Options Hash (opts):

  • :silent (Boolean) — default: false

    Do not produce log output

  • :acceptable_exit_codes (Array<Fixnum>) — default: [0]

    An array (or range) of integer exit codes that should be considered acceptable. An error will be thrown if the exit code does not match one of the values in this list.

  • :environment (Hash{String=>String}) — default: {}

    These will be treated as extra environment variables that should be set before running the command.

Returns:

  • (Result)

    Returns the result of the underlying SCP operation.



269
270
271
272
273
274
275
# File 'lib/beaker/dsl/helpers.rb', line 269

def create_remote_file(hosts, file_path, file_content, opts = {})
  Tempfile.open 'beaker' do |tempfile|
    File.open(tempfile.path, 'w') {|file| file.puts file_content }

    scp_to hosts, tempfile.path, file_path, opts
  end
end

#create_tmpdir_for_user(host, name = '/tmp/beaker', user = nil) ⇒ String

Create a temp directory on remote host owned by specified user.

the ownership of a temp directory. directory. Default value is ‘/tmp/beaker’ directory. If no username is specified, use ‘puppet master –configprint user` to obtain username from master. Raise RuntimeError if this puppet command returns a non-zero exit code.

Parameters:

  • host (Host)

    A single remote host on which to create and adjust

  • name (String) (defaults to: '/tmp/beaker')

    A remote path prefix for the new temp

  • user (String) (defaults to: nil)

    The name of user that should own the temp

Returns:

  • (String)

    Returns the name of the newly-created file.



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/beaker/dsl/helpers.rb', line 289

def create_tmpdir_for_user(host, name='/tmp/beaker', user=nil)
  if not user
    result = on(host, "puppet master --configprint user")
    if not result.exit_code == 0
      raise "`puppet master --configprint` failed, check that puppet is installed on #{host} or explicitly pass in a user name."
    end
    user = result.stdout.strip
  end

  if not on(host, "getent passwd #{user}").exit_code == 0
    raise "User #{user} does not exist on #{host}."
  end

  if defined? host.tmpdir
    dir = host.tmpdir(name)
    on host, "chown #{user}.#{user} #{dir}"
    return dir
  else
    raise "Host platform not supported by `create_tmpdir_for_user`."
  end
end

#curl_on(host, cmd, opts = {}, &block) ⇒ Object

Run a curl command on the provided host(s)

Parameters:

  • host (Host, Array<Host>, String, Symbol)

    One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.

  • cmd (String, Command)

    The curl command to execute on host.

  • block (Proc)

    Additional actions or assertions.

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Options to alter execution.

Options Hash (opts):

  • :silent (Boolean) — default: false

    Do not produce log output

  • :acceptable_exit_codes (Array<Fixnum>) — default: [0]

    An array (or range) of integer exit codes that should be considered acceptable. An error will be thrown if the exit code does not match one of the values in this list.

  • :environment (Hash{String=>String}) — default: {}

    These will be treated as extra environment variables that should be set before running the command.



1292
1293
1294
1295
1296
1297
1298
# File 'lib/beaker/dsl/helpers.rb', line 1292

def curl_on(host, cmd, opts = {}, &block)
  if options.is_pe? #check global options hash
    on host, "curl --sslv3 %s" % cmd, opts, &block
  else
    on host, "curl %s" % cmd, opts, &block
  end
end

#curl_with_retries(desc, host, url, desired_exit_codes, max_retries = 60, retry_interval = 1) ⇒ Object



1114
1115
1116
# File 'lib/beaker/dsl/helpers.rb', line 1114

def curl_with_retries(desc, host, url, desired_exit_codes, max_retries = 60, retry_interval = 1)
  retry_command(desc, host, "curl -m 1 #{url}", desired_exit_codes, max_retries, retry_interval)
end

#deploy_package_repo(host, path, name, version) ⇒ Object

Note:

To ensure the repo configs are available for deployment, you should run ‘rake pl:jenkins:deb_repo_configs` and `rake pl:jenkins:rpm_repo_configs` on your project checkout

Deploy packaging configurations generated by github.com/puppetlabs/packaging to a host.

Parameters:

  • host (Host)
  • path (String)

    The path to the generated repository config files. ex: /myproject/pkg/repo_configs

  • name (String)

    A human-readable name for the repository

  • version (String)

    The version of the project, as used by the packaging tools. This can be determined with ‘rake pl:print_build_params` from the packaging repo.



254
255
256
# File 'lib/beaker/dsl/helpers.rb', line 254

def deploy_package_repo host, path, name, version
  host.deploy_package_repo path, name, version
end

#exit_codeObject

Deprecated.

An proxy for the last Result#exit_code returned by a method that makes remote calls. Use the Result object returned by the method directly instead. For Usage see Result.



162
163
164
165
# File 'lib/beaker/dsl/helpers.rb', line 162

def exit_code
  return nil if @result.nil?
  @result.exit_code
end

#fact(name, opts = {}) ⇒ Object

Get a facter fact from the default host

See Also:



1280
1281
1282
# File 'lib/beaker/dsl/helpers.rb', line 1280

def fact(name, opts = {})
  fact_on(default, name, opts)
end

#fact_on(host, name, opts = {}) ⇒ Object

Get a facter fact from a provided host

Parameters:

  • host (Host, Array<Host>, String, Symbol)

    One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.

  • name (String)

    The name of the fact to query for

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Options to alter execution.

Options Hash (opts):

  • :silent (Boolean) — default: false

    Do not produce log output

  • :acceptable_exit_codes (Array<Fixnum>) — default: [0]

    An array (or range) of integer exit codes that should be considered acceptable. An error will be thrown if the exit code does not match one of the values in this list.

  • :environment (Hash{String=>String}) — default: {}

    These will be treated as extra environment variables that should be set before running the command.

Returns:

  • String The value of the fact ‘name’ on the provided host

Raises:

  • (FailTest)

    Raises an exception if call to facter fails



1269
1270
1271
1272
1273
1274
1275
1276
# File 'lib/beaker/dsl/helpers.rb', line 1269

def fact_on(host, name, opts = {})
  result = on host, facter(name, opts)
  if result.kind_of?(Array)
    result.map { |res| res.stdout.chomp }
  else
    result.stdout.chomp
  end
end

#get_module_name(author_module_name) ⇒ String?

Parse modulename from the pattern ‘Auther-ModuleName’

Parameters:

  • author_module_name (String)

    <Author>-<ModuleName> pattern

Returns:

  • (String, nil)


1386
1387
1388
1389
1390
1391
# File 'lib/beaker/dsl/helpers.rb', line 1386

def get_module_name(author_module_name)
  split_name = split_author_modulename(author_module_name)
  if split_name
    split_name[:module]
  end
end

#install_package(host, package_name, package_version = nil) ⇒ Result

Install a package on a host

Parameters:

  • host (Host)

    A host object

  • package_name (String)

    Name of the package to install

Returns:

  • (Result)

    An object representing the outcome of *install command*.



225
226
227
# File 'lib/beaker/dsl/helpers.rb', line 225

def install_package host, package_name, package_version = nil
  host.install_package package_name, '', package_version
end

#modify_tk_config(host, config_file_path, options_hash, replace = false) ⇒ Object

Note:

TrapperKeeper config files can be HOCON, JSON, or Ini. We don’t

Modify the given TrapperKeeper config file.

particularly care which of these the file named by ‘config_file_path` on the SUT actually is, just that the contents can be parsed into a map.

Parameters:

  • host (Host)

    A host object

  • options_hash (OptionsHash)

    New hash which will be merged into the given TrapperKeeper config.

  • config_file_path (String)

    Path to the TrapperKeeper config on the given host which is to be modified.

  • replace (Bool) (defaults to: false)

    If set true, instead of updating the existing TrapperKeeper configuration, replace it entirely with the contents of the given hash.



775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
# File 'lib/beaker/dsl/helpers.rb', line 775

def modify_tk_config(host, config_file_path, options_hash, replace=false)
  if options_hash.empty?
    return nil
  end

  new_hash = Beaker::Options::OptionsHash.new

  if replace
    new_hash.merge!(options_hash)
  else
    if not host.file_exist?( config_file_path )
      raise "Error: #{config_file_path} does not exist on #{host}"
    end
    file_string = host.exec( Command.new( "cat #{config_file_path}" )).stdout

    begin
      tk_conf_hash = read_tk_config_string(file_string)
    rescue RuntimeError
      raise "Error reading trapperkeeper config: #{config_file_path} at host: #{host}"
    end

    new_hash.merge!(tk_conf_hash)
    new_hash.merge!(options_hash)
  end

  file_string = JSON.dump(new_hash)
  create_remote_file host, config_file_path, file_string
end

#on(host, command, opts = {}, &block) ⇒ Result

The primary method for executing commands on some set of hosts.

Examples:

Most basic usage

on hosts, 'ls /tmp'

Allowing additional exit codes to pass

on agents, 'puppet agent -t', :acceptable_exit_codes => [0,2]

Using the returned result for any kind of checking

if on(host, 'ls -la ~').stdout =~ /\.bin/
  ...do some action...
end

Using TestCase helpers from within a test.

agents.each do |agent|
  on agent, 'cat /etc/puppet/puppet.conf' do
    assert_match stdout, /server = #{master}/, 'WTF Mate'
  end
end

Using a role (defined in a String) to identify the host

on "master", "echo hello"

Using a role (defined in a Symbol) to identify the host

on :dashboard, "echo hello"

Parameters:

  • host (Host, Array<Host>, String, Symbol)

    One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.

  • command (String, Command)

    The command to execute on host.

  • block (Proc)

    Additional actions or assertions.

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Options to alter execution.

Options Hash (opts):

  • :silent (Boolean) — default: false

    Do not produce log output

  • :acceptable_exit_codes (Array<Fixnum>) — default: [0]

    An array (or range) of integer exit codes that should be considered acceptable. An error will be thrown if the exit code does not match one of the values in this list.

  • :environment (Hash{String=>String}) — default: {}

    These will be treated as extra environment variables that should be set before running the command.

Returns:

  • (Result)

    An object representing the outcome of command.

Raises:

  • (FailTest)

    Raises an exception if command obviously fails.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/beaker/dsl/helpers.rb', line 79

def on(host, command, opts = {}, &block)
  block_on host do | host |
    cur_command = command
    if command.is_a? Command
      cur_command = command.cmd_line(host)
    end
    cmd_opts = {}
    #add any additional environment variables to the command
    if opts[:environment]
      cmd_opts['ENV'] = opts[:environment]
    end
    @result = host.exec(Command.new(cur_command.to_s, [], cmd_opts), opts)

    # Also, let additional checking be performed by the caller.
    if block_given?
      case block.arity
        #block with arity of 0, just hand back yourself
        when 0
          yield self
        #block with arity of 1 or greater, hand back the result object
        else
          yield @result
      end
    end
    @result
  end
end

#parse_for_modulename(root_module_dir) ⇒ String

Parse root directory of a module for module name Searches for metadata.json and then if none found, Modulefile and parses for the Name attribute

Parameters:

  • root_module_dir (String)

Returns:

  • (String)

    module name



1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
# File 'lib/beaker/dsl/helpers.rb', line 1359

def parse_for_modulename(root_module_dir)
  module_name = nil
  if File.exists?("#{root_module_dir}/metadata.json")
    logger.debug "Attempting to parse Modulename from metadata.json"
    module_json = JSON.parse(File.read "#{root_module_dir}/metadata.json")
    if(module_json.has_key?('name'))
      module_name = get_module_name(module_json['name'])
    end
  end
  if !module_name && File.exists?("#{root_module_dir}/Modulefile")
    logger.debug "Attempting to parse Modulename from Modulefile"
    if /^name\s+'?(\w+-\w+)'?\s*$/i.match(File.read("#{root_module_dir}/Modulefile"))
      module_name = get_module_name(Regexp.last_match[1])
    end
  end
  if !module_name
    logger.debug "Unable to determine name, returning null"
  end
  module_name
end

#parse_for_moduleroot(possible_module_directory) ⇒ String?

Recursive method for finding the module root Assumes that a Modulefile exists

Parameters:

  • possible_module_directory (String)

    will look for Modulefile and if none found go up one level and try again until root is reached

Returns:

  • (String, nil)


1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
# File 'lib/beaker/dsl/helpers.rb', line 1342

def parse_for_moduleroot(possible_module_directory)
  if File.exists?("#{possible_module_directory}/Modulefile")
    possible_module_directory
  elsif possible_module_directory === '/'
    logger.error "At root, can't parse for another directory"
    nil
  else
    logger.debug "No Modulefile found at #{possible_module_directory}, moving up"
    parse_for_moduleroot File.expand_path(File.join(possible_module_directory,'..'))
  end
end

#port_open_within?(host, port = 8140, seconds = 120) ⇒ Boolean

Blocks until the port is open on the host specified, returns false on failure

Returns:

  • (Boolean)


847
848
849
850
851
# File 'lib/beaker/dsl/helpers.rb', line 847

def port_open_within?( host, port = 8140, seconds = 120 )
  repeat_for( seconds ) do
    host.port_open?( port )
  end
end

#puppet_group(host) ⇒ Object

Note:

This method assumes puppet is installed on the host.

Return the name of the puppet group.

Parameters:

  • host (Host)

    One object that acts like a Beaker::Host



488
489
490
# File 'lib/beaker/dsl/helpers.rb', line 488

def puppet_group(host)
  return host.puppet('master')['user']
end

#puppet_module_install(opts = {}) ⇒ Object

Copy a puppet module from a given source to all hosts under test.



356
357
358
# File 'lib/beaker/dsl/helpers.rb', line 356

def puppet_module_install opts = {}
  puppet_module_install_on(hosts, opts)
end

#puppet_module_install_on(host, opts = {}) ⇒ Object

Copy a puppet module from a given source to all hosts under test. Assumes each host under test has an associated ‘distmoduledir’ (set in the host configuration YAML file).

Parameters:

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

Options Hash (opts):

  • :source (String)

    The location on the test runners box where the files are found

  • :module_name (String)

    The name of the module to be copied over



348
349
350
351
352
# File 'lib/beaker/dsl/helpers.rb', line 348

def puppet_module_install_on(host, opts = {})
  block_on host do | h |
    on h, puppet("module install #{opts[:module_name]}")
  end
end

#puppet_user(host) ⇒ Object

Note:

This method assumes puppet is installed on the host.

Return the name of the puppet user.

Parameters:

  • host (Host)

    One object that acts like a Beaker::Host



478
479
480
# File 'lib/beaker/dsl/helpers.rb', line 478

def puppet_user(host)
  return host.puppet('master')['group']
end

#retry_command(desc, host, command, desired_exit_codes = 0, max_retries = 60, retry_interval = 1, verbose = false) ⇒ Object



1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
# File 'lib/beaker/dsl/helpers.rb', line 1118

def retry_command(desc, host, command, desired_exit_codes = 0,
                  max_retries = 60, retry_interval = 1, verbose = false)
  log_prefix = host.log_prefix
  logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{command}"
  logger.debug "  Trying command #{max_retries} times."
  logger.debug ".", add_newline=false
  desired_exit_codes = [desired_exit_codes].flatten
  result = on host, command, {:acceptable_exit_codes => (0...127), :silent => !verbose}
  num_retries = 0
  until desired_exit_codes.include?(result.exit_code)
    sleep retry_interval
    result = on host, command, {:acceptable_exit_codes => (0...127), :silent => !verbose}
    num_retries += 1
    logger.debug ".", add_newline=false
    if (num_retries > max_retries)
      logger.debug "  Command \`#{command}\` failed."
      fail("Command \`#{command}\` failed.")
    end
  end
  logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{command} ostensibly successful."
end

#run_agent_on(host, arg = '--no-daemonize --verbose --onetime --test', options = {}, &block) ⇒ Object

Deprecated.


999
1000
1001
1002
1003
1004
# File 'lib/beaker/dsl/helpers.rb', line 999

def run_agent_on(host, arg='--no-daemonize --verbose --onetime --test',
                 options={}, &block)
  block_on host do | host |
    on host, puppet_agent(arg), options, &block
  end
end

#run_script(script, opts = {}, &block) ⇒ Object

Move a local script to default host and execute it

See Also:



337
338
339
# File 'lib/beaker/dsl/helpers.rb', line 337

def run_script(script, opts = {}, &block)
  run_script_on(default, script, opts, &block)
end

#run_script_on(host, script, opts = {}, &block) ⇒ Result

Note:

this relies on #on and #scp_to

Move a local script to a remote host and execute it

Parameters:

  • host (Host, #do_scp_to)

    One or more hosts (or some object that responds like Host#do_scp_from.

  • script (String)

    A local path to find an executable script at.

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Options to alter execution.

  • block (Proc)

    Additional tests to run after script has executed

Options Hash (opts):

  • :silent (Boolean) — default: false

    Do not produce log output

  • :acceptable_exit_codes (Array<Fixnum>) — default: [0]

    An array (or range) of integer exit codes that should be considered acceptable. An error will be thrown if the exit code does not match one of the values in this list.

  • :environment (Hash{String=>String}) — default: {}

    These will be treated as extra environment variables that should be set before running the command.

Returns:

  • (Result)

    Returns the result of the underlying SCP operation.



322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/beaker/dsl/helpers.rb', line 322

def run_script_on(host, script, opts = {}, &block)
  # this is unsafe as it uses the File::SEPARATOR will be set to that
  # of the coordinator node.  This works for us because we use cygwin
  # which will properly convert the paths.  Otherwise this would not
  # work for running tests on a windows machine when the coordinator
  # that the harness is running on is *nix. We should use
  # {Beaker::Host#temp_path} instead. TODO
  remote_path = File.join("", "tmp", File.basename(script))

  scp_to host, script, remote_path
  on host, remote_path, opts, &block
end

#scp_from(host, from_path, to_path, opts = {}) ⇒ Result

Note:

If using Host for the hosts scp is not required on the system as it uses Ruby’s net/scp library. The net-scp gem however is required (and specified in the gemspec).

Move a file from a remote to a local path

Parameters:

  • host (Host, #do_scp_from)

    One or more hosts (or some object that responds like Host#do_scp_from.

  • from_path (String)

    A remote path to a file.

  • to_path (String)

    A local path to copy from_path to.

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Options to alter execution.

Options Hash (opts):

  • :silent (Boolean) — default: false

    Do not produce log output

  • :acceptable_exit_codes (Array<Fixnum>) — default: [0]

    An array (or range) of integer exit codes that should be considered acceptable. An error will be thrown if the exit code does not match one of the values in this list.

  • :environment (Hash{String=>String}) — default: {}

    These will be treated as extra environment variables that should be set before running the command.

Returns:

  • (Result)

    Returns the result of the SCP operation



180
181
182
183
184
185
186
# File 'lib/beaker/dsl/helpers.rb', line 180

def scp_from host, from_path, to_path, opts = {}
  block_on host do | host |
    @result = host.do_scp_from(from_path, to_path, opts)
    @result.log logger
    @result
  end
end

#scp_to(host, from_path, to_path, opts = {}) ⇒ Result

Note:

If using Host for the hosts scp is not required on the system as it uses Ruby’s net/scp library. The net-scp gem however is required (and specified in the gemspec.

Move a local file to a remote host

Parameters:

  • host (Host, #do_scp_to)

    One or more hosts (or some object that responds like Host#do_scp_to.

  • from_path (String)

    A local path to a file.

  • to_path (String)

    A remote path to copy from_path to.

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Options to alter execution.

Options Hash (opts):

  • :silent (Boolean) — default: false

    Do not produce log output

  • :acceptable_exit_codes (Array<Fixnum>) — default: [0]

    An array (or range) of integer exit codes that should be considered acceptable. An error will be thrown if the exit code does not match one of the values in this list.

  • :environment (Hash{String=>String}) — default: {}

    These will be treated as extra environment variables that should be set before running the command.

Returns:

  • (Result)

    Returns the result of the SCP operation



201
202
203
204
205
206
207
# File 'lib/beaker/dsl/helpers.rb', line 201

def scp_to host, from_path, to_path, opts = {}
  block_on host do | host |
    @result = host.do_scp_to(from_path, to_path, opts)
    @result.log logger
    @result
  end
end

#select_hosts(criteria, host_array = nil, &block) ⇒ Array<Host>

Return a set of hosts that meet the given criteria

Parameters:

  • criteria (Hash{Symbol,String=>String,Regexp,Array<String,Regexp>})

    Specify the criteria with which a host should be considered for inclusion. The key is any attribute of the host that will be yielded by Host#[]. The value can be any string/regex or array of strings/regexp. The values are compared using [Enumerable#any?] so that if one value of an array matches the host is considered a match for that criteria.

  • host_array (Array<Host>) (defaults to: nil)

    This creatively named parameter is an optional array of hosts to confine to. If not passed in, this method will modify TestCase#hosts in place.

  • block (Proc)

    Addition checks to determine suitability of hosts for selection. Each host that is still valid after checking criteria is then passed in turn into this block. The block should return true if the host matches this additional criteria.

Returns:

  • (Array<Host>)

    Returns an array of hosts that meet the provided criteria



457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/beaker/dsl/helpers.rb', line 457

def select_hosts(criteria, host_array = nil, &block)
  hosts_to_select_from = host_array || hosts
  criteria.each_pair do |property, value|
    hosts_to_select_from = hosts_to_select_from.select do |host|
      inspect_host host, property, value
    end
  end
  if block_given?
    hosts_to_select_from = hosts_to_select_from.select do |host|
      yield host
    end
  end
  hosts_to_select_from
end

#shell(command, opts = {}, &block) ⇒ Result

The method for executing commands on the default host

Examples:

Most basic usage

shell 'ls /tmp'

Allowing additional exit codes to pass

shell 'puppet agent -t', :acceptable_exit_codes => [0,2]

Using the returned result for any kind of checking

if shell('ls -la ~').stdout =~ /\.bin/
  ...do some action...
end

Using TestCase helpers from within a test.

agents.each do |agent|
  shell('cat /etc/puppet/puppet.conf') do |result|
    assert_match result.stdout, /server = #{master}/, 'WTF Mate'
  end
end

Parameters:

  • command (String, Command)

    The command to execute on host.

  • block (Proc)

    Additional actions or assertions.

  • opts (Hash{Symbol=>String}) (defaults to: {})

    Options to alter execution.

Options Hash (opts):

  • :silent (Boolean) — default: false

    Do not produce log output

  • :acceptable_exit_codes (Array<Fixnum>) — default: [0]

    An array (or range) of integer exit codes that should be considered acceptable. An error will be thrown if the exit code does not match one of the values in this list.

  • :environment (Hash{String=>String}) — default: {}

    These will be treated as extra environment variables that should be set before running the command.

Returns:

  • (Result)

    An object representing the outcome of command.

Raises:

  • (FailTest)

    Raises an exception if command obviously fails.



133
134
135
# File 'lib/beaker/dsl/helpers.rb', line 133

def shell(command, opts = {}, &block)
  on(default, command, opts, &block)
end

#sign_certificateObject

prompt the master to sign certs then check to confirm the cert for the default host is signed



1256
1257
1258
# File 'lib/beaker/dsl/helpers.rb', line 1256

def sign_certificate
  sign_certificate_for(default)
end

#sign_certificate_for(host) ⇒ Object

Ensure the host has requested a cert, then sign it

Parameters:

  • host (Host, Array<Host>, String, Symbol)

    One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.

Returns:

  • nil

Raises:



1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
# File 'lib/beaker/dsl/helpers.rb', line 1228

def sign_certificate_for(host)
  block_on host do | host |
    if [master, dashboard, database].include? host

      on host, puppet( 'agent -t' ), :acceptable_exit_codes => [0,1,2]
      on master, puppet( "cert --allow-dns-alt-names sign #{host}" ), :acceptable_exit_codes => [0,24]

    else

      hostname = Regexp.escape host.node_name

      last_sleep = 0
      next_sleep = 1
      (0..10).each do |i|
        fail_test("Failed to sign cert for #{hostname}") if i == 10

        on master, puppet("cert --sign --all"), :acceptable_exit_codes => [0,24]
        break if on(master, puppet("cert --list --all")).stdout =~ /\+ "?#{hostname}"?/
        sleep next_sleep
        (last_sleep, next_sleep) = next_sleep, last_sleep+next_sleep
      end

    end
  end
end

#sleep_until_puppetdb_started(host) ⇒ Object



1108
1109
1110
1111
1112
# File 'lib/beaker/dsl/helpers.rb', line 1108

def sleep_until_puppetdb_started(host)
  curl_with_retries("start puppetdb", host, "http://localhost:8080", 0, 120)
  curl_with_retries("start puppetdb (ssl)",
                    host, "https://#{host.node_name}:8081", [35, 60])
end

#split_author_modulename(author_module_attr) ⇒ Hash<Symbol,String>?

Split the Author-Name into a hash

Parameters:

  • author_module_attr (String)

Returns:

  • (Hash<Symbol,String>, nil)

    :author and :module symbols will be returned



1398
1399
1400
1401
1402
1403
1404
1405
# File 'lib/beaker/dsl/helpers.rb', line 1398

def split_author_modulename(author_module_attr)
  result = /(\w+)-(\w+)/.match(author_module_attr)
  if result
    {:author => result[1], :module => result[2]}
  else
    nil
  end
end

#stderrObject

Deprecated.

An proxy for the last Result#stderr returned by a method that makes remote calls. Use the Result object returned by the method directly instead. For Usage see Result.



152
153
154
155
# File 'lib/beaker/dsl/helpers.rb', line 152

def stderr
  return nil if @result.nil?
  @result.stderr
end

#stdoutObject

Deprecated.

An proxy for the last Result#stdout returned by a method that makes remote calls. Use the Result object returned by the method directly instead. For Usage see Result.



142
143
144
145
# File 'lib/beaker/dsl/helpers.rb', line 142

def stdout
  return nil if @result.nil?
  @result.stdout
end

#stop_agentObject

stops the puppet agent running on the default host

See Also:



1210
1211
1212
# File 'lib/beaker/dsl/helpers.rb', line 1210

def stop_agent
  stop_agent_on(default)
end

#stop_agent_on(agent) ⇒ Object

stops the puppet agent running on the host

Parameters:

  • agent (Host, Array<Host>, String, Symbol)

    One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.



1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
# File 'lib/beaker/dsl/helpers.rb', line 1177

def stop_agent_on(agent)
  block_on agent do | host |
    vardir = agent.puppet['vardir']
    agent_running = true
    while agent_running
      result = on host, "[ -e '#{vardir}/state/agent_catalog_run.lock' ]", :acceptable_exit_codes => [0,1]
      agent_running = (result.exit_code == 0)
      sleep 2 unless agent_running
    end

    # The agent service is `pe-puppet` everywhere EXCEPT certain linux distros on PE 2.8
    # In all the case that it is different, this init script will exist. So we can assume
    # that if the script doesn't exist, we should just use `pe-puppet`
    result = on agent, "[ -e /etc/init.d/pe-puppet-agent ]", :acceptable_exit_codes => [0,1]
    agent_service = (result.exit_code == 0) ? 'pe-puppet-agent' : 'pe-puppet'

    # Under a number of stupid circumstances, we can't stop the
    # agent using puppet.  This is usually because of issues with
    # the init script or system on that particular configuration.
    avoid_puppet_at_all_costs = false
    avoid_puppet_at_all_costs ||= agent['platform'] =~ /el-4/
    avoid_puppet_at_all_costs ||= agent['pe_ver'] && version_is_less(agent['pe_ver'], '3.2') && agent['platform'] =~ /sles/

    if avoid_puppet_at_all_costs
      on agent, "/etc/init.d/#{agent_service} stop"
    else
      on agent, puppet_resource('service', agent_service, 'ensure=stopped')
    end
  end
end

#stub_forge(forge_host = nil) ⇒ Object

This wraps the method ‘stub_hosts` and makes the stub specific to the forge alias.

See Also:



1102
1103
1104
1105
1106
# File 'lib/beaker/dsl/helpers.rb', line 1102

def stub_forge(forge_host = nil)
  #use global options hash
  forge_host ||= options[:forge_host]
  stub_forge_on(default, forge_host)
end

#stub_forge_on(machine, forge_host = nil) ⇒ Object

This wraps the method ‘stub_hosts_on` and makes the stub specific to the forge alias.

forge api v1 canonical source is forge.puppetlabs.com forge api v3 canonical source is forgeapi.puppetlabs.com

Parameters:

  • machine (String)

    the host to perform the stub on

  • forge_host (String) (defaults to: nil)

    The URL to use as the forge alias, will default to using :forge_host in the global options hash



1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'lib/beaker/dsl/helpers.rb', line 1088

def stub_forge_on(machine, forge_host = nil)
  #use global options hash
  forge_host ||= options[:forge_host]
  @forge_ip ||= Resolv.getaddress(forge_host)
  block_on machine do | host |
    stub_hosts_on(host, 'forge.puppetlabs.com' => @forge_ip)
    stub_hosts_on(host, 'forgeapi.puppetlabs.com' => @forge_ip)
  end
end

#stub_hosts(ip_spec) ⇒ Object

This method accepts a block and using the puppet resource ‘host’ will setup host aliases before and after that block on the default host

Examples:

Stub puppetlabs.com on the default host to 127.0.0.1

stub_hosts('puppetlabs.com' => '127.0.0.1')

See Also:



1075
1076
1077
# File 'lib/beaker/dsl/helpers.rb', line 1075

def stub_hosts(ip_spec)
  stub_hosts_on(default, ip_spec)
end

#stub_hosts_on(machine, ip_spec) ⇒ Object

This method accepts a block and using the puppet resource ‘host’ will setup host aliases before and after that block.

A teardown step is also added to make sure unstubbing of the host is removed always.

Examples:

Stub puppetlabs.com on the master to 127.0.0.1

stub_hosts_on(master, 'puppetlabs.com' => '127.0.0.1')

Parameters:

  • machine (Host, Array<Host>, String, Symbol)

    One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts.

  • ip_spec (Hash{String=>String})

    a hash containing the host to ip mappings



1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
# File 'lib/beaker/dsl/helpers.rb', line 1053

def stub_hosts_on(machine, ip_spec)
  block_on machine do | host |
    ip_spec.each do |address, ip|
      logger.notify("Stubbing address #{address} to IP #{ip} on machine #{host}")
      on( host, puppet('resource', 'host', address, 'ensure=present', "ip=#{ip}") )
    end

    teardown do
      ip_spec.each do |address, ip|
        logger.notify("Unstubbing address #{address} to IP #{ip} on machine #{host}")
        on( host, puppet('resource', 'host', address, 'ensure=absent') )
      end
    end
  end
end

#upgrade_package(host, package_name) ⇒ Result

Upgrade a package on a host. The package must already be installed

Parameters:

  • host (Host)

    A host object

  • package_name (String)

    Name of the package to install

Returns:

  • (Result)

    An object representing the outcome of *upgrade command*.



235
236
237
# File 'lib/beaker/dsl/helpers.rb', line 235

def upgrade_package host, package_name
  host.upgrade_package package_name
end

#version_is_less(a, b) ⇒ Boolean

Note:

3.0.0-160-gac44cfb is greater than 3.0.0, and 2.8.2

Note:

-rc being less than final builds is not yet implemented.

Is semver-ish version a less than semver-ish version b

Parameters:

  • a (String)

    A version of the from ‘d.d.d.*’

  • b (String)

    A version of the form ‘d.d.d.*’

Returns:

  • (Boolean)

    true if a is less than b, otherwise return false



1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
# File 'lib/beaker/dsl/helpers.rb', line 1147

def version_is_less a, b
  a_nums = a.split('-')[0].split('.')
  b_nums = b.split('-')[0].split('.')
  (0...a_nums.length).each do |i|
    if i < b_nums.length
      if a_nums[i] < b_nums[i]
        return true
      elsif a_nums[i] > b_nums[i]
        return false
      end
    else
      return false
    end
  end
  #checks all dots, they are equal so examine the rest
  a_rest = a.split('-', 2)[1]
  b_rest = b.split('-', 2)[1]
  if a_rest and b_rest and a_rest < b_rest
    return false
  elsif a_rest and not b_rest
    return false
  elsif not a_rest and b_rest
    return true
  end
  return false
end

#wait_for_host_in_dashboard(host) ⇒ Object

wait for a given host to appear in the dashboard



1216
1217
1218
1219
# File 'lib/beaker/dsl/helpers.rb', line 1216

def wait_for_host_in_dashboard(host)
  hostname = host.node_name
  retry_command("Wait for #{hostname} to be in the console", dashboard, "! curl --sslv3 -k -I https://#{dashboard}/nodes/#{hostname} | grep '404 Not Found'")
end

#with_puppet_running(conf_opts, testdir = host.tmpdir(File.basename(@path)), &block) ⇒ Object

Test Puppet running in a certain run mode with specific options, on the default host



654
655
656
# File 'lib/beaker/dsl/helpers.rb', line 654

def with_puppet_running conf_opts, testdir = host.tmpdir(File.basename(@path)), &block
  with_puppet_running_on(default, conf_opts, testdir, &block)
end

#with_puppet_running_on(host, conf_opts, testdir = host.tmpdir(File.basename(@path)), &block) ⇒ Object

Test Puppet running in a certain run mode with specific options. This ensures the following steps are performed:

  1. The pre-test Puppet configuration is backed up

  2. A new Puppet configuraton file is layed down

  3. Puppet is started or restarted in the specified run mode

  4. Ensure Puppet has started correctly

  5. Further tests are yielded to

  6. Revert Puppet to the pre-test state

  7. Testing artifacts are saved in a folder named for the test

Examples:

A simple use case to ensure a master is running

with_puppet_running_on( master ) do
    ...tests that require a master...
end

Fully utilizing the possiblities of config options

with_puppet_running_on( master,
                        :main => {:logdest => '/var/blah'},
                        :master => {:masterlog => '/elswhere'},
                        :agent => {:server => 'localhost'} ) do

  ...tests to be ran...
end

Parameters:

  • host (Host)

    One object that act like Host

  • conf_opts (Hash{Symbol=>String})

    Represents puppet settings. Sections of the puppet.conf may be specified, if no section is specified the a puppet.conf file will be written with the options put in a section named after [mode]

  • testdir (File) (defaults to: host.tmpdir(File.basename(@path)))

    The temporary directory which will hold backup configuration, and other test artifacts.

  • block (Block)

    The point of this method, yields so tests may be ran. After the block is finished puppet will revert to a previous state.

Options Hash (conf_opts):

  • :__commandline_args__ (String)

    A special setting for command_line arguments such as –debug or –logdest, which cannot be set in puppet.conf. For example:

    :commandline_args => ‘–logdest /tmp/a.log’

    These will only be applied when starting a FOSS master, as a pe master is just bounced.

  • :__service_args__ (Hash)

    A special setting of options for controlling how the puppet master service is handled. The only setting currently is :bypass_service_script, which if set true will force stopping and starting a webrick master using the start_puppet_from_source_* methods, even if it seems the host has passenger. This is needed in FOSS tests to initialize SSL.

Raises:

  • (ArgumentError)


565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/beaker/dsl/helpers.rb', line 565

def with_puppet_running_on host, conf_opts, testdir = host.tmpdir(File.basename(@path)), &block
  raise(ArgumentError, "with_puppet_running_on's conf_opts must be a Hash. You provided a #{conf_opts.class}: '#{conf_opts}'") if !conf_opts.kind_of?(Hash)
  cmdline_args = conf_opts[:__commandline_args__]
  service_args = conf_opts[:__service_args__] || {}
  conf_opts = conf_opts.reject { |k,v| [:__commandline_args__, :__service_args__].include?(k) }

  curl_retries = host['master-start-curl-retries'] || options['master-start-curl-retries']
  logger.debug "Setting curl retries to #{curl_retries}"

  if options[:is_jvm_puppet]
    confdir = host.puppet('master')['confdir']
    vardir = host.puppet('master')['vardir']

    if cmdline_args
      split_args = cmdline_args.split()

      split_args.each do |arg|
        case arg
        when /--confdir=(.*)/
          confdir = $1
        when /--vardir=(.*)/
          vardir = $1
        end
      end
    end

    jvm_puppet_opts = { "jruby-puppet" => {
      "master-conf-dir" => confdir,
      "master-var-dir" => vardir,
    }}

    jvm_puppet_conf = File.join("#{host['jvm-puppet-confdir']}", "jvm-puppet.conf")
    modify_tk_config(host, jvm_puppet_conf, jvm_puppet_opts)
  end

  begin
    backup_file = backup_the_file(host, host['puppetpath'], testdir, 'puppet.conf')
    lay_down_new_puppet_conf host, conf_opts, testdir

    if host.use_service_scripts? && !service_args[:bypass_service_script]
      bounce_service( host, host['puppetservice'], curl_retries )
    else
      puppet_master_started = start_puppet_from_source_on!( host, cmdline_args )
    end

    yield self if block_given?

  rescue Exception => early_exception
    original_exception = RuntimeError.new("PuppetAcceptance::DSL::Helpers.with_puppet_running_on failed (check backtrace for location) because: #{early_exception}\n#{early_exception.backtrace.join("\n")}\n")
    raise(original_exception)

  ensure
    begin

      if host.use_service_scripts? && !service_args[:bypass_service_script]
        restore_puppet_conf_from_backup( host, backup_file )
        bounce_service( host, host['puppetservice'], curl_retries )
      else
        if puppet_master_started
          stop_puppet_from_source_on( host )
        else
          dump_puppet_log(host)
        end
        restore_puppet_conf_from_backup( host, backup_file )
      end

    rescue Exception => teardown_exception
      begin
        if !host.is_pe?
          dump_puppet_log(host)
        end
      rescue Exception => dumping_exception
        logger.error("Raised during attempt to dump puppet logs: #{dumping_exception}")
      end

      if original_exception
        logger.error("Raised during attempt to teardown with_puppet_running_on: #{teardown_exception}\n---\n")
        raise original_exception
      else
        raise teardown_exception
      end
    end
  end
end