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
|
# 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.
require_relative 'test_file/action'
require_relative 'test_file/test'
require_relative 'test_file/task_group'
require 'logger'
module Elasticsearch
module RestAPIYAMLTests
# Custom exception to raise when a test file needs to be skipped. This is
# captured as soon as possible so the test runners can move on to the next test.
class SkipTestsException < StandardError
end
# Class representing a single test file, containing a setup, teardown, and multiple tests.
#
# @since 6.2.0
class TestFile
attr_reader :features_to_skip, :name, :client
LOGGER = Logger.new($stdout)
# Initialize a single test file.
#
# @example Create a test file object.
# TestFile.new(file_name)
#
# @param [ String ] file_name The name of the test file.
# @param [ Client] An instance of the client
# @param [ Array<Symbol> ] skip_features The names of features to skip.
#
# @since 6.1.0
def initialize(file_name, client, features_to_skip = [])
@name = file_name
@client = client
begin
documents = YAML.load_stream(File.new(file_name))
rescue StandardError => e
LOGGER.error e
LOGGER.error "Filename : #{@name}"
end
@test_definitions = documents.reject { |doc| doc['setup'] || doc['teardown'] }
@setup = documents.find { |doc| doc['setup'] }
skip_entire_test_file? if @setup
@teardown = documents.find { |doc| doc['teardown'] }
@features_to_skip = REST_API_YAML_SKIP_FEATURES + features_to_skip
end
def skip_entire_test_file?
@skip = @setup['setup']&.select { |a| a['skip'] }
return false if @skip.empty?
raise SkipTestsException if skip_version?(@client, @skip.first['skip'])
end
def skip_version?(client, skip_definition)
return true if skip_definition['version'] == 'all'
return unless (versions = skip_definition['version'])
begin
server_version = client.info['version']['number']
rescue
warn('Could not determine Elasticsearch version when checking if test should be skipped.')
end
range_partition = /\s*-\s*/
if versions.include?(',')
# == " - 7.17.3, 8.0.0 - 8.2.99"
versions.split(',').each do |version_range|
low, high = __parse_versions(version_range.partition(range_partition))
range = low..high
return true if range.cover?(Gem::Version.new(server_version))
end
else
low, high = __parse_versions(versions.partition(range_partition))
range = low..high
range.cover?(Gem::Version.new(server_version))
end
end
def __parse_versions(versions)
versions = versions.split('-') if versions.is_a? String
low = (['', nil].include? versions[0]) ? '0' : versions[0]
high = (['', nil].include? versions[2]) ? '9999' : versions[2]
[Gem::Version.new(low), Gem::Version.new(high)]
end
# Get a list of tests in the test file.
#
# @example Get the list of tests
# test_file.tests
#
# @return [ Array<Test> ] A list of Test objects.
#
# @since 6.2.0
def tests
@test_definitions.collect do |test_definition|
Test.new(self, test_definition)
end
end
# Run the setup tasks defined for a single test file.
#
# @example Run the setup tasks.
# test_file.setup
#
# @param [ Elasticsearch::Client ] client The client to use to perform the setup tasks.
#
# @return [ self ]
#
# @since 6.2.0
def setup
return unless @setup
actions = @setup['setup'].select { |action| action['do'] }.map { |action| Action.new(action['do']) }
count = 0
loop do
actions.delete_if do |action|
begin
action.execute(client)
true
rescue Elasticsearch::Transport::Transport::Errors::ServiceUnavailable => e
# The action sometimes gets the cluster in a recovering state, so we
# retry a few times and then raise an exception if it's still
# happening
count += 1
raise e if count > 9
false
end
end
break if actions.empty?
end
self
end
# Run the teardown tasks defined for a single test file.
#
# @example Run the teardown tasks.
# test_file.teardown
#
# @param [ Elasticsearch::Client ] client The client to use to perform the teardown tasks.
#
# @return [ self ]
#
# @since 6.2.0
def teardown
return unless @teardown
actions = @teardown['teardown'].select { |action| action['do'] }.map { |action| Action.new(action['do']) }
actions.each { |action| action.execute(client) }
self
end
class << self
end
end
end
end
|