Skip to content

Commit cdb5e0c

Browse files
committed
[API] Skip all tests in a file if there's a skip setup in top level
1 parent a81ab86 commit cdb5e0c

File tree

5 files changed

+57
-36
lines changed

5 files changed

+57
-36
lines changed

api-spec-testing/test_file.rb

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121
require 'logger'
2222

2323
module Elasticsearch
24-
2524
module RestAPIYAMLTests
25+
# Custom exception to raise when a test file needs to be skipped. This is
26+
# captured as soon as possible so the test runners can move on to the next test.
27+
class SkipTestsException < StandardError
28+
end
2629

2730
# Class representing a single test file, containing a setup, teardown, and multiple tests.
2831
#
2932
# @since 6.2.0
3033
class TestFile
31-
32-
attr_reader :features_to_skip
33-
attr_reader :name
34+
attr_reader :features_to_skip, :name, :client
3435

3536
# Initialize a single test file.
3637
#
@@ -41,8 +42,9 @@ class TestFile
4142
# @param [ Array<Symbol> ] skip_features The names of features to skip.
4243
#
4344
# @since 6.1.0
44-
def initialize(file_name, features_to_skip = [])
45+
def initialize(file_name, client, features_to_skip = [])
4546
@name = file_name
47+
@client = client
4648
begin
4749
documents = YAML.load_stream(File.new(file_name))
4850
rescue StandardError => e
@@ -52,10 +54,40 @@ def initialize(file_name, features_to_skip = [])
5254
end
5355
@test_definitions = documents.reject { |doc| doc['setup'] || doc['teardown'] }
5456
@setup = documents.find { |doc| doc['setup'] }
57+
skip_entire_test_file? if @setup
5558
@teardown = documents.find { |doc| doc['teardown'] }
5659
@features_to_skip = REST_API_YAML_SKIP_FEATURES + features_to_skip
5760
end
5861

62+
def skip_entire_test_file?
63+
@skip = @setup['setup']&.select { |a| a['skip'] }
64+
return false if @skip.empty?
65+
66+
raise SkipTestsException if skip_version?(@client, @skip.first['skip'])
67+
end
68+
69+
def skip_version?(client, skip_definition)
70+
return true if skip_definition['version'] == 'all'
71+
72+
range_partition = /\s*-\s*/
73+
return unless (versions = skip_definition['version']) && skip_definition['version'].partition(range_partition)
74+
75+
low, high = __parse_versions(versions)
76+
range = low..high
77+
begin
78+
server_version = client.info['version']['number']
79+
rescue
80+
warn('Could not determine Elasticsearch version when checking if test should be skipped.')
81+
end
82+
range.cover?(server_version)
83+
end
84+
85+
def __parse_versions(versions)
86+
low = (['', nil].include? versions[0]) ? '0' : versions[0]
87+
high = (['', nil].include? versions[2]) ? '9999' : versions[2]
88+
[low, high]
89+
end
90+
5991
# Get a list of tests in the test file.
6092
#
6193
# @example Get the list of tests

api-spec-testing/test_file/action.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def execute(client, test = nil)
5656
@definition.each.inject(client) do |client, (method_chain, args)|
5757
chain = method_chain.split('.')
5858

59+
# If we have a method nested in a namespace, client becomes the
60+
# client/namespace. Eg for `indices.resolve_index`, `client =
61+
# client.indices` and then we call `resolve_index` on `client`.
5962
if chain.size > 1
6063
client = chain[0...-1].inject(client) do |_client, _method|
6164
_client.send(_method)

api-spec-testing/test_file/test.rb

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ def split_and_parse_key(key)
6464
end
6565
end
6666

67-
attr_reader :description
68-
attr_reader :test_file
69-
attr_reader :cached_values
70-
attr_reader :file_basename
67+
attr_reader :description, :test_file, :cached_values, :file_basename
7168

7269
# Actions that if followed by a 'do' action, indicate that they complete their task group.
7370
# For example, consider this sequence of actions:
@@ -117,7 +114,6 @@ def initialize(test_file, test_definition)
117114
@definition = test_definition[description].select { |doc| !doc.key?('skip') }
118115
@definition.delete_if { |doc| doc['skip'] }
119116
@cached_values = {}
120-
121117
skip_definitions = test_definition[description].select { |doc| doc['skip'] }.compact
122118
@skip = skip_definitions unless skip_definitions.empty?
123119
end
@@ -133,7 +129,6 @@ def initialize(test_file, test_definition)
133129
def task_groups
134130
@task_groups ||= begin
135131
@definition.each_with_index.inject([]) do |task_groups, (action, i)|
136-
137132
# the action has a catch, it's a singular task group
138133
if action['do'] && action['do']['catch']
139134
task_groups << TaskGroup.new(self)
@@ -160,7 +155,7 @@ def task_groups
160155
#
161156
# @since 6.2.0
162157
def cache_value(cache_key, value)
163-
@cached_values["#{cache_key}"] = value
158+
@cached_values[cache_key] = value
164159
@cached_values
165160
end
166161

@@ -225,7 +220,7 @@ def skip_test?(client, features_to_skip = test_file.features_to_skip)
225220

226221
if @skip
227222
@skip.collect { |s| s['skip'] }.any? do |skip|
228-
contains_features_to_skip?(features_to_skip, skip) || skip_version?(client, skip)
223+
contains_features_to_skip?(features_to_skip, skip) || test_file.skip_version?(client, skip)
229224
end
230225
end
231226
end
@@ -259,34 +254,13 @@ def pre_defined_skip?
259254
end
260255
end
261256

262-
def skip_version?(client, skip_definition)
263-
return true if skip_definition['version'] == 'all'
264-
range_partition = /\s*-\s*/
265-
if versions = skip_definition['version'] && skip_definition['version'].partition(range_partition)
266-
low, high = __parse_versions(versions)
267-
range = low..high
268-
begin
269-
server_version = client.info['version']['number']
270-
rescue
271-
warn('Could not determine Elasticsearch version when checking if test should be skipped.')
272-
end
273-
range.cover?(server_version)
274-
end
275-
end
276-
277257
def is_a_validation?(action)
278258
GROUP_TERMINATORS.any? { |validation| action[validation] } || expects_exception?(action)
279259
end
280260

281261
def expects_exception?(action)
282262
action['do'] && action['do']['catch']
283263
end
284-
285-
def __parse_versions(versions)
286-
low = (['', nil].include? versions[0]) ? '0' : versions[0]
287-
high = (['', nil].include? versions[2]) ? '9999' : versions[2]
288-
[low, high]
289-
end
290264
end
291265
end
292266
end

elasticsearch-api/spec/elasticsearch/api/rest_api_yaml_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
describe 'Rest API YAML tests' do
99
# Traverse YAML files and create TestFile object:
1010
REST_API_YAML_FILES.each do |file|
11-
test_file = Elasticsearch::RestAPIYAMLTests::TestFile.new(file, REST_API_YAML_SKIP_FEATURES)
11+
begin
12+
test_file = Elasticsearch::RestAPIYAMLTests::TestFile.new(file, REST_API_YAML_SKIP_FEATURES)
13+
rescue SkipTestsException => _e
14+
# If the test file has a `skip` at the top level that applies to this
15+
# version of Elasticsearch, continue with the next text.
16+
next
17+
end
1218

1319
context "#{file.gsub("#{YAML_FILES_DIRECTORY}/", '')}" do
1420
test_file.tests.each do |test|

elasticsearch-xpack/spec/xpack/rest_api_yaml_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020

2121
describe 'XPack Rest API YAML tests' do
2222
REST_API_YAML_FILES.each do |file|
23-
test_file = Elasticsearch::RestAPIYAMLTests::TestFile.new(file, REST_API_YAML_SKIP_FEATURES)
23+
begin
24+
test_file = Elasticsearch::RestAPIYAMLTests::TestFile.new(file, DEFAULT_CLIENT, REST_API_YAML_SKIP_FEATURES)
25+
rescue SkipTestsException => _e
26+
# If the test file has a `skip` at the top level that applies to this
27+
# version of Elasticsearch, continue with the next text.
28+
next
29+
end
2430

2531
context "#{file.gsub("#{YAML_FILES_DIRECTORY}/", '')}" do
2632
before(:all) do

0 commit comments

Comments
 (0)