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
# 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
end

Instance Method Details

#build_and_run_tests(test_path) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/elasticsearch/tests/test_runner.rb', line 74

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



103
104
105
106
107
# File 'lib/elasticsearch/tests/test_runner.rb', line 103

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



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/elasticsearch/tests/test_runner.rb', line 37

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/elasticsearch/tests/test_runner.rb', line 51

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



92
93
94
95
96
97
98
99
100
101
# File 'lib/elasticsearch/tests/test_runner.rb', line 92

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
  Dir.glob(tests_path)
end

#test_filename(file) ⇒ Object



87
88
89
90
# File 'lib/elasticsearch/tests/test_runner.rb', line 87

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