Class: Beaker::Options::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/options/validator.rb

Constant Summary collapse

VALID_FAIL_MODES =
/stop|fast|slow/
VALID_PRESERVE_HOSTS =
/always|onfail|onpass|never/
FRICTIONLESS_ROLE =
'frictionless'
FRICTIONLESS_ADDITIONAL_ROLES =
%w(master database dashboard console)

Instance Method Summary collapse

Instance Method Details

#check_yaml_file(f, msg = '') ⇒ Object

Determine is a given file exists and is a valid YAML file

Parameters:

  • f (String)

    The YAML file path to examine

  • msg (String) (defaults to: '')

    An options message to report in case of error

Raises:

  • (ArgumentError)

    Raise if file does not exist or is not valid YAML



14
15
16
17
18
19
20
21
22
# File 'lib/beaker/options/validator.rb', line 14

def check_yaml_file(f, msg = '')
  validator_error "#{f} does not exist (#{msg})" unless File.file?(f)

  begin
    YAML.load_file(f)
  rescue Beaker::Options::Parser::PARSE_ERROR => e
    validator_error "#{f} is not a valid YAML file (#{msg})\n\t#{e}"
  end
end

#default_set?(default) ⇒ true, false

Raises an ArgumentError if more than one default exists, otherwise returns true or false if default is set.

Parameters:

  • default (Array<String>)

    list of host names

Returns:

  • (true, false)


39
40
41
42
43
44
45
46
47
# File 'lib/beaker/options/validator.rb', line 39

def default_set?(default)
  if default.empty?
    return false
  elsif default.length > 1
    validator_error "Only one host may have the role 'default', default roles assigned to #{default}"
  end

  true
end

#validate_fail_mode(fail_mode) ⇒ nil

Raises an error if fail_mode is not a supported failure mode.

Parameters:

  • fail_mode (String)

    Failure mode setting

Returns:

  • (nil)

    Does not return anything



53
54
55
56
57
58
# File 'lib/beaker/options/validator.rb', line 53

def validate_fail_mode(fail_mode)
  #check for valid fail mode
  if fail_mode !~ VALID_FAIL_MODES
    validator_error "--fail-mode must be one of fast or slow, not '#{fail_mode}'"
  end
end

#validate_files(file_list, path) ⇒ Object

Raise an error if file_list is empty

Parameters:

  • file_list (Array<String>)

    list of files

  • path (String)

    file path to report in error

Raises:

  • (ArgumentError)

    Raises if file_list is empty



129
130
131
132
133
# File 'lib/beaker/options/validator.rb', line 129

def validate_files(file_list, path)
  if file_list.empty?
    validator_error("No files found for path: '#{path}'")
  end
end

#validate_frictionless_roles(role_array) ⇒ Object

Raises an error if role_array contains the frictionless role and conflicting roles.

Parameters:

  • role_array (Array<String>)

    List of roles

Raises:

  • (ArgumentError)

    Raises if role_array contains conflicting roles



107
108
109
110
111
# File 'lib/beaker/options/validator.rb', line 107

def validate_frictionless_roles(role_array)
  if role_array.include?(FRICTIONLESS_ROLE) and !(role_array & FRICTIONLESS_ADDITIONAL_ROLES).empty?
    validator_error "Only agent nodes may have the role 'frictionless'."
  end
end

#validate_master_count(count) ⇒ nil

Raise an error if the master count is incorrect.

Parameters:

  • count (Integer)

    Count of roles with ‘master’

Returns:

  • (nil)

    Nothing is returned

Raises:

  • (ArgumentError)

    Raises if master count is greater than 1



118
119
120
121
122
# File 'lib/beaker/options/validator.rb', line 118

def validate_master_count(count)
  if count > 1
    validator_error("Only one host/node may have the role 'master'.")
  end
end

#validate_path(path) ⇒ Object

Raise an error if path is not a valid file or directory

Parameters:

  • path (String)

    File path

Raises:

  • (ArgumentError)

    Raises if path is not a valid file or directory



139
140
141
142
143
# File 'lib/beaker/options/validator.rb', line 139

def validate_path(path)
  if !File.file?(path) && !File.directory?(path)
    validator_error("#{path} used as a file option but is not a file or directory!")
  end
end

#validate_platform(host, name) ⇒ nil

Raise an error if host does not have a platform defined.

Parameters:

Returns:

  • (nil)

    Does not return anything



76
77
78
79
80
# File 'lib/beaker/options/validator.rb', line 76

def validate_platform(host, name)
  if !host['platform'] || host['platform'].empty?
    validator_error "Host #{name} does not have a platform specified"
  end
end

#validate_preserve_hosts(hosts_setting) ⇒ nil

Raises an error if hosts_setting is not a supported preserve hosts value.

Parameters:

  • hosts_setting (String)

    Preserve hosts setting

Returns:

  • (nil)

    Does not return anything



64
65
66
67
68
69
# File 'lib/beaker/options/validator.rb', line 64

def validate_preserve_hosts(hosts_setting)
  #check for valid preserve_hosts option
  if hosts_setting !~ VALID_PRESERVE_HOSTS
    validator_error("--preserve_hosts must be one of always, onfail, onpass or never, not '#{hosts_setting}'")
  end
end

#validate_test_tags(tags_and, tags_or, tags_exclude) ⇒ nil

Note:

see test tagging logic at DSL::TestTagging module

Raise an error if an item exists in both the include and exclude lists.

Parameters:

  • tags_and (Array)

    included items

  • tags_exclude (Array)

    excluded items

Returns:

  • (nil)

    Does not return anything



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/beaker/options/validator.rb', line 89

def validate_test_tags(tags_and, tags_or, tags_exclude)
  if tags_and.length > 0 && tags_or.length > 0
    validator_error "cannot have values for both test tagging operands (AND and OR)"
  end

  tags_and.each do |included_tag|
    # select items from exclude set that match included_tag
    # no match is an empty list/array/[]
    if tags_exclude.select { |ex| ex == included_tag } != []
      validator_error "tag '#{included_tag}' cannot be in both the included and excluded tag sets"
    end
  end
end

#validator_error(msg = '') ⇒ Object Also known as: parser_error

Raises an ArgumentError with associated message

Parameters:

  • msg (String) (defaults to: '')

    The error message to be reported

Raises:

  • (ArgumentError)

    Takes the supplied message and raises it as an ArgumentError



27
28
29
# File 'lib/beaker/options/validator.rb', line 27

def validator_error(msg = '')
  raise ArgumentError, msg.to_s
end