1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
module Elasticsearch
module RestAPIYAMLTests
class TestFile
class Test
# Representation of a block of actions consisting of some 'do' actions and their verifications.
#
# For example, this is a task group:
#
# - do:
# xpack.security.get_role:
# name: "admin_role"
# - match: { admin_role.cluster.0: "all" }
# - match: { admin_role.metadata.key1: "val1" }
# - match: { admin_role.metadata.key2: "val2" }
#
# @since 6.2.0
class TaskGroup
attr_reader :exception, :response, :test
# Initialize a TaskGroup object.
#
# @example Create a TaskGroup
# TaskGroup.new(test)
#
# @param [ Test ] test The test this task group is part of.
#
# @since 6.2.0
def initialize(test)
@actions = []
@exception = nil
@response = nil
@variables = {}
@test = test
end
# Add an action to the task group definition.
#
# @example Add an action
# task_group.add_action(action)
#
# @param [ Hash ] action The hash representation of an action.
#
# @return [ self ]
#
# @since 6.2.0
def add_action(action)
@actions << action if ACTIONS.any? { |a| action[a] }
self
end
# Run the actions in the task group.
#
# @example Run the actions
# task_group.run(client)
#
# @param [ Elasticsearch::Client ] client The client to use to run the actions.
#
# @return [ self ]
#
# @since 6.2.0
def run(client)
# Allow the actions to be execute only once.
return if @executed
@executed = true
do_actions.inject(client) do |_client, action|
action.execute(_client, test)
# Cache the result of the action, if a set action is defined.
set_variable(action)
transform_and_set_variable(action)
_client
end
self
rescue => ex
raise ex unless catch_exception?
# Cache the exception raised as a result of the operation, if the task group has a 'catch' defined.
@exception = ex
end
# Consider the response of interest the one resulting from the last action.
#
# @return [ Hash ] The response from the last action.
#
# @since 6.2.0
def response
@response ||= begin
if do_actions.any? { |a| a.yaml_response? }
YAML.load(do_actions[-1].response)
else
do_actions[-1].response
end
end
end
# Does this task group expect to raise an exception for an action?
#
# @return [ true, false ] If the task group contains an action expected to raise an exception.
#
# @since 6.2.0
def catch_exception?
!!expected_exception_message
end
# Does this task group have match clauses.
#
# @return [ true, false ] If the task group has match clauses.
#
# @since 6.2.0
def has_match_clauses?
!!match_clauses
end
# Does this task group have match clauses on field value length.
#
# @return [ true, false ] If the task group has match clauses on field value length.
#
# @since 6.2.0
def has_length_match_clauses?
!!length_match_clauses
end
# Does this task group have match clauses on a field value being true.
#
# @return [ true, false ] If the task group has match clauses on a field value being true.
#
# @since 6.2.0
def has_true_clauses?
!!true_clauses
end
# Does this task group have match clauses on a field value being false.
#
# @return [ true, false ] If the task group has match clauses on a field value being false.
#
# @since 6.2.0
def has_false_clauses?
!!false_clauses
end
# Does this task group have clauses on a field value being gte.
#
# @return [ true, false ] If the task group has clauses on a field value being gte.
#
# @since 6.2.0
def has_gte_clauses?
!!gte_clauses
end
# Does this task group have clauses on a field value being gt.
#
# @return [ true, false ] If the task group has clauses on a field value being gt.
#
# @since 6.2.0
def has_gt_clauses?
!!gt_clauses
end
# Does this task group have clauses on a field value being lte.
#
# @return [ true, false ] If the task group has clauses on a field value being lte.
#
# @since 6.2.0
def has_lte_clauses?
!!lte_clauses
end
# Does this task group have clauses on a field value being lt.
#
# @return [ true, false ] If the task group has clauses on a field value being lt.
#
# @since 6.2.0
def has_lt_clauses?
!!lt_clauses
end
# The expected exception message.
#
# @return [ String ] The expected exception message.
#
# @since 6.2.0
def expected_exception_message
@expected_exception_message ||= begin
if do_definitions = @actions.group_by { |a| a.keys.first }['do']
if catch_exception = do_definitions.find { |a| a['do']['catch'] }
catch_exception['do']['catch']
end
end
end
end
# The match clauses.
#
# @return [ Array<Hash> ] The match clauses.
#
# @since 6.2.0
def match_clauses
@match_actions ||= @actions.group_by { |a| a.keys.first }['match']
end
# The true match clauses.
#
# @return [ Array<Hash> ] The true match clauses.
#
# @since 6.2.0
def true_clauses
@true_clauses ||= @actions.group_by { |a| a.keys.first }['is_true']
end
# The false match clauses.
#
# @return [ Array<Hash> ] The false match clauses.
#
# @since 6.2.0
def false_clauses
@false_clauses ||= @actions.group_by { |a| a.keys.first }['is_false']
end
# The gte clauses.
#
# @return [ Array<Hash> ] The gte clauses.
#
# @since 6.2.0
def gte_clauses
@gte_clauses ||= @actions.group_by { |a| a.keys.first }['gte']
end
# The gt clauses.
#
# @return [ Array<Hash> ] The gt clauses.
#
# @since 6.2.0
def gt_clauses
@gt_clauses ||= @actions.group_by { |a| a.keys.first }['gt']
end
# The lte clauses.
#
# @return [ Array<Hash> ] The lte clauses.
#
# @since 6.2.0
def lte_clauses
@lte_clauses ||= @actions.group_by { |a| a.keys.first }['lte']
end
# The lt clauses.
#
# @return [ Array<Hash> ] The lt clauses.
#
# @since 6.2.0
def lt_clauses
@lt_clauses ||= @actions.group_by { |a| a.keys.first }['lt']
end
# The field length match clauses.
#
# @return [ Array<Hash> ] The field length match clauses.
#
# @since 6.2.0
def length_match_clauses
@match_length ||= @actions.group_by { |a| a.keys.first }['length']
end
private
ACTIONS = (Test::GROUP_TERMINATORS + ['do']).freeze
def do_actions
@do_actions ||= @actions.group_by { |a| a.keys.first }['do'].map { |definition| Action.new(definition['do']) }
end
def variables_to_set
@variables_to_set ||= (@actions.group_by { |a| a.keys.first }['set'] || [])
end
def variables_to_transform_and_set
@variables_to_transform_and_set ||= (@actions.group_by { |a| a.keys.first }['transform_and_set'] || [])
end
def transform_and_set_variable(action)
variables_to_transform_and_set.each do |set_definition|
set_definition['transform_and_set'].each do |response_key, transform_description|
match_base_64_transform = /(\#base64EncodeCredentials\()(\S*)\)/
matches = match_base_64_transform.match(transform_description)
fields = matches[2].split(',') if matches.length > 0
values_to_encode = action.response.select do |key|
fields.include?(key)
end.values if fields
to_set = Base64.strict_encode64(values_to_encode.join(':'))
@test.cache_value(response_key, to_set)
end
end
end
def set_variable(action)
variables_to_set.each do |set_definition|
set_definition['set'].each do |response_key, variable_name|
nested_key_chain = response_key.split('.').map do |key|
# If there's a variable in the set key, get the value:
key.gsub!(key, @test.cached_values[key.gsub('$', '')]) if key.match?(/\$.+/)
(key =~ /\A[-+]?[0-9]+\z/) ? key.to_i: key
end
if to_set = find_value(nested_key_chain, action.response)
@test.cache_value(variable_name, to_set)
end
end
end
end
def find_value(chain, document)
# Return the first key if an 'arbitrary key' should be returned
return document.keys[0] if chain[0] == '_arbitrary_key_'
return document[chain[0]] unless chain.size > 1
find_value(chain[1..-1], document[chain[0]]) if document[chain[0]]
end
end
end
end
end
end
|