Method: Beaker::Shared::HostManager#only_host_with_role

Defined in:
lib/beaker/shared/host_manager.rb

#only_host_with_role(hosts, role) ⇒ Host

Find a single host with the role provided. Raise an error if more than one host is found to have the provided role.

Parameters:

  • hosts (Array<Host>)

    The hosts to examine

  • role (String)

    The host returned will have this role in its role list

Returns:

  • (Host)

    The single host with the desired role in its roles list

Raises:

  • (ArgumentError)

    Raised if more than one host has the given role defined, or if no host has the role defined.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/beaker/shared/host_manager.rb', line 38

def only_host_with_role(hosts, role)
  a_host = hosts_with_role(hosts, role)
  case
    when a_host.length == 0
      raise ArgumentError, "There should be one host with #{role} defined!"
    when a_host.length > 1
      host_string = ( a_host.map { |host| host.name } ).join( ', ')
      raise ArgumentError, "There should be only one host with #{role} defined, but I found #{a_host.length} (#{host_string})"
  end
  a_host.first
end