Module: Beaker::DSL::Helpers::HoconHelpers

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

Overview

Note:

For usage guides for these methods, check these sources:

  • Beaker docs.

  • Beaker acceptance tests in acceptance/tests/base/dsl/helpers/hocon_helpers_test.rb

Convenience methods for modifying and reading Hocon configs

Instance Method Summary collapse

Instance Method Details

#hocon_file_edit_in_place_on(hosts, filename, &block) ⇒ Object

Note:

that a the crucial difference between this & #hocon_file_edit_on is that your Proc will need to return the Hocon::ConfigValueFactory doc you want saved for the in-place save to work correctly.

Note:

for info about parameters, please checkout #hocon_file_edit_on.

Grabs the given hocon file from a SUT, allowing you to edit the file and have those edits saved in-place of the file on the SUT.

Examples:

setting an attribute & saving

hocon_file_edit_in_place_on(hosts, hocon_filename) do |host, doc|
  doc.set_value('c.d', '[6, 2, 73, 4, 45]')
end


83
84
85
86
87
88
# File 'lib/beaker/dsl/helpers/hocon_helpers.rb', line 83

def hocon_file_edit_in_place_on(hosts, filename, &block)
  hocon_file_edit_on(hosts, filename) do |host, doc|
    content_doc = yield host, doc
    create_remote_file(host, filename, content_doc.render)
  end
end

#hocon_file_edit_on(hosts, filename, &block) {|Host| ... } ⇒ Object

Note:

This method does not save the hocon file after editing. Our recommended workflow for that is included in our example. If you'd rather just save a file in-place on a SUT, then #hocon_file_edit_in_place_on is a better method to use.

Grabs the given hocon file from a SUT, allowing you to edit the file just like you would a local one in the passed block.

Examples:

Editing a value & saving as a new file

hocon_file_edit_on(hosts, 'hocon.conf') do |host, doc|
  doc2 = doc.set_value('a.b', '[1, 2, 3, 4, 5]')
  create_remote_file(host, 'hocon_latest.conf', doc2.render)
end

Parameters:

  • hosts (Host, Array<Host>)

    Host (or an array of hosts) to edit the hocon file on.

  • filename (String)

    Name of the file to edit.

  • block (Proc)

    Code to edit the hocon file.

Yields:

  • (Host)

    Currently executing host.

  • (Hocon::ConfigValueFactory)

    Doc to edit. Refer to Hocon's basic usage doc for info on how to use this object.

Returns:

  • nil

Raises:

  • ArgumentError if arguments are missing or incorrect.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/beaker/dsl/helpers/hocon_helpers.rb', line 56

def hocon_file_edit_on(hosts, filename, &block)
  if not block_given?
    msg = 'DSL method `hocon_file_edit_on` provides a given block'
    msg << ' a hocon file to edit. No block was provided.'
    raise ArgumentError, msg
  end
  block_on hosts, {} do | host |
    doc = hocon_file_read_on(host, filename)
    yield host, doc
  end
end

#hocon_file_read_on(host, filename) ⇒ Hocon::ConfigValueFactory

Reads the given hocon file from a SUT

Parameters:

  • host (Host)

    Host to get hocon file from.

  • filename (String)

    Name of the hocon file to get

Returns:

  • (Hocon::ConfigValueFactory)

    parsed hocon file

Raises:

  • ArgumentError if arguments are missing or incorrect



22
23
24
25
26
27
28
# File 'lib/beaker/dsl/helpers/hocon_helpers.rb', line 22

def hocon_file_read_on(host, filename)
  if filename.nil? || filename.empty?
    raise ArgumentError, '#hocon_file_edit_on requires a filename'
  end
  file_contents = on(host, "cat #{filename}").stdout
  Hocon::Parser::ConfigDocumentFactory.parse_string(file_contents)
end