Class: Elasticsearch::Tests::TestRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/tests/test_runner.rb

Overview

Main YAML test runner

Constant Summary collapse

LOGGER =
Logger.new($stdout)

Instance Method Summary collapse

Constructor Details

#initialize(client, path = nil, logger = nil) ⇒ TestRunner

Returns a new instance of TestRunner.



30
31
32
33
34
35
36
# File 'lib/elasticsearch/tests/test_runner.rb', line 30

def initialize(client, path = nil, logger = nil)
  @client = client
  @serverless = defined?(::ElasticsearchServerless) && client.is_a?(::ElasticsearchServerless::Client)
  @path = path || File.expand_path('./tmp', __dir__)
  @logger = logger || LOGGER
  @tests_to_skip = []
end

Instance Method Details

#add_tests_to_skip(tests) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/elasticsearch/tests/test_runner.rb', line 38

def add_tests_to_skip(tests)
  if tests.is_a? String
    @tests_to_skip << tests
  else
    @tests_to_skip.merge!(tests)
  end
end

#build_and_run_tests(test_path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/elasticsearch/tests/test_runner.rb', line 83

def build_and_run_tests(test_path)
  yaml = YAML.load_stream(File.read(test_path))
  requires = extract_requires!(yaml).compact.first['requires']
  return unless (requires['serverless'] == true && @serverless) ||
                (requires['stack'] == true && !@serverless)

  test = Elasticsearch::Tests::Test.new(yaml, test_path, @client)
  test.execute
  @tests_count += test.count
rescue StandardError => e
  raise e
end

#extract_requires!(yaml) ⇒ Object



118
119
120
121
122
# File 'lib/elasticsearch/tests/test_runner.rb', line 118

def extract_requires!(yaml)
  yaml.map.with_index do |a, i|
    yaml.delete_at(i) if a.keys.first == 'requires'
  end.compact
end

#run(test_files = []) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/elasticsearch/tests/test_runner.rb', line 46

def run(test_files = [])
  raise 'Couldn\'t find test files. Run `Elasticsearch::Tests::Downloader::run(tests_path)` to download the tests' unless File.directory?(@path)

  @test_files = select_test_files(test_files)
  run_the_tests
  Elasticsearch::Tests::Printer::display_errors(@errors, @logger) unless @errors.empty?
  Elasticsearch::Tests::Printer::display_summary(@tests_count, @errors.count, @start_time, @logger)
  if @errors.empty?
    exit 0
  else
    exit 1
  end
end

#run_the_testsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/elasticsearch/tests/test_runner.rb', line 60

def run_the_tests
  @start_time = Time.now
  @tests_count = 0
  @errors = []

  @test_files.map do |test_path|
    test_file = test_filename(test_path)
    build_and_run_tests(test_path)
  rescue Psych::SyntaxError => e
    @errors << { error: e, file: test_file }
    @logger.warn("YAML error in #{test_file}")
    @logger.warn e
  rescue ActionError => e
    @errors << { error: e, file: test_file, action: e.action }
    @logger.debug e
  rescue StandardError => e
    @errors << { error: e, file: test_file }
    @logger.debug e
  rescue SystemExit, Interrupt
    exit
  end
end

#select_test_files(test_files) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/elasticsearch/tests/test_runner.rb', line 101

def select_test_files(test_files)
  tests_path = if test_files.empty?
                 "#{@path}/**/*.yml"
               elsif test_files.include?('yml')
                 return ["#{@path}/tests/#{test_files}"]
               else
                 "#{@path}/#{test_files}/*.yml"
               end
  tests = Dir.glob(tests_path)
  tests.each do |test|
    @tests_to_skip.each do |skip|
      tests.delete(test) if test.match?(skip)
    end
  end
  tests
end

#test_filename(file) ⇒ Object



96
97
98
99
# File 'lib/elasticsearch/tests/test_runner.rb', line 96

def test_filename(file)
  name = file.split('/')
  "#{name[-2]}/#{name[-1]}"
end