Module: Beaker::DSL::Helpers::FacterHelpers

Included in:
BeakerPuppet
Defined in:
lib/beaker-puppet/helpers/facter_helpers.rb

Overview

Methods that help you interact with your facter installation, facter must be installed for these methods to execute correctly

Instance Method Summary collapse

Instance Method Details

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

Get a facter fact from the default host

See Also:



60
61
62
# File 'lib/beaker-puppet/helpers/facter_helpers.rb', line 60

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.

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/beaker-puppet/helpers/facter_helpers.rb', line 38

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

  if opts.is_a?(Hash)
    opts.merge!({ json: nil })
  else
    opts << ' --json'
  end

  result = on host, facter("\"#{name}\"", opts)
  if result.is_a?(Array)
    result.map { |res| JSON.parse(res.stdout)[name] }
  else
    JSON.parse(result.stdout)[name]
  end
end