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
-
#add_role_def(role) ⇒ Object
Create a new role method for a given arbitrary role name.
-
#agents ⇒ Array<Host>
The hosts for which [‘roles’] include ‘agent’.
-
#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.
-
#dashboard ⇒ Array<Host>
The host for which [‘roles’] include ‘dashboard’.
-
#database ⇒ Array<Host>
The host for which [‘roles’] include ‘database’.
-
#default ⇒ Array<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.
-
#find_only_one(role) ⇒ Host
Returns the host, if one and only one is found.
-
#hosts_as(desired_role = nil) ⇒ Array<Host>
Select hosts that include a desired role from #hosts.
-
#master ⇒ Array<Host>
The host for which [‘roles’] include ‘master’.
Instance Method Details
#add_role_def(role) ⇒ Object
Create a new role method for a given arbitrary role name. Makes it possible to be able to run commands without having to refer to role by String or Symbol. Will not add a new method definition if the name is already in use. Symbol or an Array of Strings or Symbols.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/beaker/dsl/roles.rb', line 99 def add_role_def role if role.kind_of?(Array) role.each do |r| add_role_def r end else if not respond_to? role if role !~ /\A[[:alpha:]]+[a-zA-Z0-9_]*[!?=]?\Z/ raise "Role name format error for '#{role}'. Allowed characters are: \na-Z\n0-9 (as long as not at the beginning of name)\n'_'\n'?', '!' and '=' (only as individual last character at end of name)" end self.class.send :define_method, role.to_s do hosts_with_role = hosts_as role.to_sym if hosts_with_role.length == 1 hosts_with_role = hosts_with_role.pop end hosts_with_role end end end end |
#agents ⇒ Array<Host>
The hosts for which [‘roles’] include ‘agent’
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
129 130 131 |
# File 'lib/beaker/dsl/roles.rb', line 129 def any_hosts_as?(role) hosts_as(role).length > 0 end |
#dashboard ⇒ Array<Host>
The host for which [‘roles’] include ‘dashboard’
70 71 72 |
# File 'lib/beaker/dsl/roles.rb', line 70 def dashboard find_only_one :dashboard end |
#database ⇒ Array<Host>
The host for which [‘roles’] include ‘database’
57 58 59 |
# File 'lib/beaker/dsl/roles.rb', line 57 def database find_only_one :database end |
#default ⇒ Array<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
87 88 89 |
# File 'lib/beaker/dsl/roles.rb', line 87 def default find_only_one :default end |
#find_only_one(role) ⇒ Host
Returns the host, if one and only one is found
154 155 156 157 158 |
# File 'lib/beaker/dsl/roles.rb', line 154 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
146 147 148 |
# File 'lib/beaker/dsl/roles.rb', line 146 def hosts_as(desired_role = nil) hosts_with_role(hosts, desired_role) end |
#master ⇒ Array<Host>
The host for which [‘roles’] include ‘master’. If no host has the ‘master’ role, then use the host defined as ‘default’. If no host is defined as a ‘master’ and there is no ‘default’ host defined then raise an error.
44 45 46 |
# File 'lib/beaker/dsl/roles.rb', line 44 def master find_only_one :master end |