Module: Beaker::DSL::Roles

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

Overview

Identifying hosts.

This aids in reaching common subsets of hosts in a testing matrix.

It requires the class it is mixed into to provide the attribute ‘hosts` which contain the hosts to search, these should implement Host’s interface. They, at least, must have #[] and #to_s available and provide an array when #[](‘roles’) is called.

Also the constant Outcomes::FailTest needs to be defined it will be raised in error conditions

Instance Method Summary collapse

Instance Method Details

#agentsArray<Host>

The hosts for which [‘roles’] include ‘agent’

Examples:

Basic usage

agents.each do |agent|
  ...test each agent in turn...
end

Returns:

  • (Array<Host>)

    May be empty



28
29
30
# File 'lib/beaker/dsl/roles.rb', line 28

def agents
  hosts_as 'agent'
end

#any_hosts_as?(role) ⇒ Boolean

Determine if there is a host or hosts with the given role defined if any_hosts_as?(:master)

puts "master is defined"

end

Examples:

Usage

Returns:

  • (Boolean)

    True if there is a host with role, false otherwise



105
106
107
# File 'lib/beaker/dsl/roles.rb', line 105

def any_hosts_as?(role)
  hosts_as(role).length > 0
end

#dashboardArray<Host>

The host for which [‘roles’] include ‘dashboard’

Examples:

Basic usage

on, agent, "curl https://#{database}/nodes/#{agent}"

Returns:

Raises:



67
68
69
# File 'lib/beaker/dsl/roles.rb', line 67

def dashboard
  find_only_one :dashboard
end

#databaseArray<Host>

The host for which [‘roles’] include ‘database’

Examples:

Basic usage

on, agent, "curl -k http://#{database}:8080"

Returns:

Raises:



54
55
56
# File 'lib/beaker/dsl/roles.rb', line 54

def database
  find_only_one :database
end

#defaultArray<Host>

The default host

- if only one host defined, then that host
OR
- host with 'default' as a role
OR
- host with 'master' as a role

Examples:

Basic usage

on, default, "curl https://#{database}/nodes/#{agent}"

Returns:

Raises:



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/beaker/dsl/roles.rb', line 84

def default
  if hosts.length == 1
    return hosts[0]
  elsif any_hosts_as? :default
    return find_only_one :default
  elsif any_hosts_as? :master
    return find_only_one :master
  else
    raise DSL::Outcomes::FailTest, "no default host specified"
  end
end

#find_only_one(role) ⇒ Host

Returns the host, if one and only one is found

Parameters:

  • role (Symbol, String)

    The role to find a host for

Returns:

  • (Host)

    Returns the host, if one and only one is found

Raises:

  • Raises a failure exception if one and only one host that matches the specified role is NOT found.



130
131
132
133
134
# File 'lib/beaker/dsl/roles.rb', line 130

def find_only_one role
  only_host_with_role(hosts, role)
rescue ArgumentError => e
  raise DSL::Outcomes::FailTest, e.to_s
end

#hosts_as(desired_role = nil) ⇒ Array<Host>

Select hosts that include a desired role from #hosts

Examples:

Basic usage

hairy = hosts_as :yak
hairy.each do |yak|
  on yak, 'shave'
end

Parameters:

  • desired_role (String, Symbol) (defaults to: nil)

    The role to select for

Returns:

  • (Array<Host>)

    The hosts that match desired_role, may be empty



122
123
124
# File 'lib/beaker/dsl/roles.rb', line 122

def hosts_as(desired_role = nil)
  hosts_with_role(hosts, desired_role)
end

#masterArray<Host>

The host for which [‘roles’] include ‘master’

Examples:

Basic usage

on, master, 'cat /etc/puppet/puppet.conf'

Returns:

Raises:



41
42
43
# File 'lib/beaker/dsl/roles.rb', line 41

def master
  find_only_one :master
end