Module: BeakerPuppetHelpers::DSL

Defined in:
lib/beaker_puppet_helpers/dsl.rb

Overview

The DSL methods for beaker. These are included in Beaker.

Instance Method Summary collapse

Instance Method Details

#apply_manifest(manifest, opts = {}, &block) ⇒ Array<Result>, ...

Runs ‘puppet apply’ on default host

Returns:

  • (Array<Result>, Result, nil)

    An array of results, a result object, or nil. Check Beaker::Shared::HostManager#run_block_on for more details on this.

See Also:



192
193
194
# File 'lib/beaker_puppet_helpers/dsl.rb', line 192

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

#apply_manifest_on(hosts, manifest, opts = {}, &block) ⇒ Array<Result>, ...

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

Parameters:

  • hosts (Beaker::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.

  • :accept_all_exit_codes (Boolean) — default: false

    Consider all exit codes as passing.

  • :dry_run (Boolean) — default: false

    Do not actually execute any commands on the SUT

  • :stdin (String) — default: nil

    Input to be provided during command execution on the SUT.

  • :pty (Boolean) — default: false

    Execute this command in a pseudoterminal.

  • :expect_connection_failure (Boolean) — default: false

    Expect this command to result in a connection failure, reconnect and continue execution.

  • :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 ‘;’.)

  • :hiera_config (String)

    The path of the hiera.yaml configuration.

  • :debug (Boolean) — default: false

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

  • :run_in_parallel (Boolean)

    Whether to run on each host in parallel.

  • :show_diff (Boolean) — default: false

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

Returns:

  • (Array<Result>, Result, nil)

    An array of results, a result object, or nil. Check Beaker::Shared::HostManager#run_block_on for more details on this.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/beaker_puppet_helpers/dsl.rb', line 109

def apply_manifest_on(hosts, manifest, opts = {}, &block)
  block_on hosts, opts do |host|
    on_options = {}
    on_options[:acceptable_exit_codes] = Array(opts[:acceptable_exit_codes])

    puppet_apply_opts = {}
    if opts[:debug]
      puppet_apply_opts[:debug] = nil
    else
      puppet_apply_opts[:verbose] = nil
    end
    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[:hiera_config] = opts[:hiera_config] if opts[:hiera_config]
    puppet_apply_opts[:noop] = nil if opts[:noop]
    puppet_apply_opts[:show_diff] = nil if opts[:show_diff]

    # 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)
    puppet_apply_opts['ENV'] = opts[:environment] if opts.key?(:environment)

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

    file_path = host.tmpfile(%(apply_manifest_#{Time.now.strftime('%H%M%S%L')}.pp))
    begin
      create_remote_file(host, file_path, "#{manifest}\n")

      on(host, Beaker::PuppetCommand.new('apply', file_path, puppet_apply_opts), **on_options, &block)
    ensure
      host.rm_rf(file_path)
    end
  end
end

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

Get a facter fact from the default host

See Also:



224
225
226
# File 'lib/beaker_puppet_helpers/dsl.rb', line 224

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 (Beaker::Host, Array<Beaker::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.

  • :accept_all_exit_codes (Boolean) — default: false

    Consider all exit codes as passing.

  • :dry_run (Boolean) — default: false

    Do not actually execute any commands on the SUT

  • :stdin (String) — default: nil

    Input to be provided during command execution on the SUT.

  • :pty (Boolean) — default: false

    Execute this command in a pseudoterminal.

  • :expect_connection_failure (Boolean) — default: false

    Expect this command to result in a connection failure, reconnect and continue execution.

  • :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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/beaker_puppet_helpers/dsl.rb', line 205

def fact_on(host, name, opts = {})
  raise(ArgumentError, "fact_on's `name` option must be a String. You provided a #{name.class}: '#{name}'") unless name.is_a?(String)

  if opts.is_a?(Hash)
    opts['json'] = nil
  else
    opts << ' --json'
  end

  result = on host, Beaker::Command.new('facter', [name], opts)
  if result.is_a?(Array)
    result.map { |res| JSON.parse(res.stdout)[name] }
  else
    JSON.parse(result.stdout)[name]
  end
end