Skip to content

Commit 3d359de

Browse files
committed
[API] Test Runner: Run tests from downloaded artifacts
1 parent 6003502 commit 3d359de

File tree

4 files changed

+56
-70
lines changed

4 files changed

+56
-70
lines changed

.ci/run-repository.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ if [[ $TEST_SUITE != "platinum" ]]; then
5050
--name elasticsearch-ruby \
5151
--rm \
5252
elastic/elasticsearch-ruby \
53-
bundle exec rake test:rest_api
53+
bundle exec rake test:rest_api[true]
5454
else
5555
docker run \
5656
--network="${network_name}" \

elasticsearch-api/Rakefile

+49-60
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@
1616
# under the License.
1717

1818
require 'bundler/gem_tasks'
19-
20-
def __current__
21-
Pathname( File.expand_path('..', __FILE__) )
22-
end
23-
24-
def git_specs(command, options={})
25-
sh "git --git-dir=#{__current__.join('../tmp/elasticsearch/.git')} --work-tree=#{__current__.join('../tmp/elasticsearch')} #{command}", options
26-
end
19+
require 'json'
2720

2821
task(:default) { system 'rake --tasks' }
2922
task test: 'test:unit'
@@ -47,79 +40,75 @@ namespace :test do
4740
t.exclude_pattern = 'spec/**{,/*/**}/rest_api_yaml_spec.rb'
4841
end
4942

50-
# Rest API Spec tests - rake test:rest_api
51-
RSpec::Core::RakeTask.new(:rest_api) do |t|
52-
t.pattern = 'spec/**{,/*/**}/rest_api_yaml_spec.rb'
53-
end
54-
55-
desc 'Update the repository with YAML tests'
56-
task :update do
57-
git_specs 'fetch origin', verbose: true
58-
end
59-
60-
desc "Run integration tests"
61-
task integration: :update do
43+
desc "Run Rest API Spec tests, use `rake test:rest_api[true]` to update artifacts"
44+
RSpec::Core::RakeTask.new(:rest_api, :refresh) do |t, args|
6245
require 'elasticsearch'
63-
branches = `git --git-dir=#{__current__.join('../tmp/elasticsearch/.git')} --work-tree=#{__current__.join('../tmp/elasticsearch')} branch --no-color`
64-
65-
current_branch = branches.
66-
split("\n").
67-
select { |b| b =~ /^\*/ }.
68-
reject { |b| b =~ /no branch|detached/ }.
69-
map { |b| b.gsub(/^\*\s*/, '') }.
70-
first
71-
72-
unless current_branch
73-
STDERR.puts "[!] Unable to determine current branch, defaulting to 'master'"
74-
current_branch = 'master'
75-
end
76-
7746
# Check if a test cluster is running
7847
begin
7948
url = ENV['TEST_CLUSTER_URL'] || ENV['TEST_ES_SERVER']
80-
url = "http://localhost:#{ENV['TEST_CLUSTER_PORT'] || 9250}" unless url
49+
url = "http://localhost:#{ENV['TEST_CLUSTER_PORT'] || 9200}" unless url
8150
client = Elasticsearch::Client.new :url => url
8251
es_version_info = client.info['version']
52+
version_number = es_version_info['number']
8353
build_hash = es_version_info['build_hash']
8454
cluster_running = true
8555
rescue Faraday::ConnectionFailed
8656
STDERR.puts "[!] Test cluster not running?"
8757
cluster_running = false
58+
exit 1
8859
end
8960

90-
checkout_specs_version = ENV['TEST_NO_CHECKOUT'].nil? ? true : false
91-
checkout_build_hash = ENV['TEST_BUILD_REF'] || build_hash
92-
ENV['TEST_BUILD_REF'] = checkout_build_hash
61+
refresh_artifacts(build_hash, version_number) if args[:refresh]
9362

94-
begin
95-
unless checkout_specs_version
96-
STDERR.puts '-'*80, "YAML tests: Not switching, TEST_NO_CHECKOUT=y", '-'*80
97-
end
98-
99-
if checkout_specs_version && !checkout_build_hash
100-
STDERR.puts "[!] Cannot determine checkout build hash -- server not running or TEST_BUILD_REF not specified"
101-
exit(1)
102-
end
103-
104-
if checkout_specs_version && checkout_build_hash
105-
# Checkout the commit corresponding to the running server build, or passed TEST_BUILD_REF
106-
name = ENV['CI'] ? checkout_build_hash : "[\e[1m#{checkout_build_hash}\e[0m]"
107-
STDERR.puts '-'*80, "YAML tests: Switching to #{name} from #{current_branch}", '-'*80
108-
git_specs "checkout #{checkout_build_hash} --force --quiet"
109-
end
110-
111-
Rake::Task['test:rest_api'].invoke
112-
113-
ensure
114-
git_specs "checkout #{current_branch} --force --quiet" if checkout_specs_version && current_branch
115-
end
63+
t.pattern = 'spec/**{,/*/**}/rest_api_yaml_spec.rb'
64+
end
65+
66+
desc "Run integration tests"
67+
task :integration do
68+
Rake::Task['test:rest_api'].invoke
11669
end
11770

11871
desc 'Run unit and integration tests'
11972
task :all do
12073
Rake::Task['test:unit'].invoke
12174
Rake::Task['test:integration'].invoke
12275
end
76+
77+
def refresh_artifacts(build_hash, version_number)
78+
unless build_hash
79+
STDERR.puts "[!] Cannot determine checkout build hash -- server not running"
80+
exit(1)
81+
end
82+
83+
puts 'Downloading artifacts file...'
84+
filename = 'tmp/artifacts.json'
85+
`curl -s https://artifacts-api.elastic.co/v1/versions/#{version_number} -o #{filename}`
86+
87+
unless File.exists?("./#{filename}")
88+
STDERR.puts '[!] Couldn\'t download artifacts file'
89+
exit 1
90+
end
91+
92+
artifacts = JSON.parse(File.read('./tmp/artifacts.json'))
93+
94+
build_hash_artifact = artifacts['version']['builds'].select do |a|
95+
a.dig('projects', 'elasticsearch', 'commit_hash') == build_hash
96+
end.first
97+
# Dig into the elasticsearch packages, search for the rest-resources-zip package and catch the URL:
98+
zip_url = build_hash_artifact.dig('projects', 'elasticsearch', 'packages').select { |k,v| k =~ /rest-resources-zip/ }.map { | _, v| v['url'] }.first
99+
100+
filename = zip_url.split('/').last
101+
puts 'Downloading zip file:'
102+
`curl -s #{zip_url} -o tmp/#{filename}`
103+
104+
unless File.exists?("./tmp/#{filename}")
105+
STDERR.puts '[!] Couldn\'t download artifact'
106+
exit 1
107+
end
108+
109+
puts "Unzipping file #{filename}"
110+
`unzip -o tmp/#{filename} -d tmp/`
111+
end
123112
end
124113

125114
# ----- Documentation tasks ---------------------------------------------------

elasticsearch-api/spec/rest_yaml_tests_helper.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
include Elasticsearch::RestAPIYAMLTests
2121

2222
TRANSPORT_OPTIONS = {}
23-
PROJECT_PATH = File.join(File.dirname(__FILE__), '..', '..')
23+
PROJECT_PATH = File.join(File.dirname(__FILE__), '..')
2424

2525
if (hosts = ELASTICSEARCH_URL)
2626
split_hosts = hosts.split(',').map do |host|
@@ -47,18 +47,17 @@
4747
end
4848
end
4949

50-
YAML_FILES_DIRECTORY = "#{File.expand_path(File.dirname('..'), '..')}" +
51-
'/tmp/elasticsearch/rest-api-spec/src/main/resources/rest-api-spec/test'
50+
YAML_FILES_DIRECTORY = "#{PROJECT_PATH}/tmp/rest-api-spec/test/free"
5251

5352
SINGLE_TEST = if ENV['SINGLE_TEST'] && !ENV['SINGLE_TEST'].empty?
5453
test_target = ENV['SINGLE_TEST']
5554
path = File.expand_path(File.dirname('..'), '..')
5655

5756
if test_target.match?(/\.yml$/)
58-
["#{path}/tmp/elasticsearch/rest-api-spec/src/main/resources/rest-api-spec/test/#{test_target}"]
57+
["#{PROJECT_PATH}/tmp/rest-api-spec/test/free/#{test_target}"]
5958
else
6059
Dir.glob(
61-
["#{path}/tmp/elasticsearch/rest-api-spec/src/main/resources/rest-api-spec/test/#{test_target}/*.yml"]
60+
["#{PROJECT_PATH}/tmp/rest-api-spec/test/free/#{test_target}/*.yml"]
6261
)
6362
end
6463
end

rake_tasks/test_tasks.rake

+2-4
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ namespace :test do
4949
end
5050
end
5151

52-
# Note: Start Elasticsearch with the following command if Docker is not used.
53-
# bin/elasticsearch -Erepositories.url.allowed_urls=http://* -Epath.repo=/tmp -Enode.attr.testattr=test
5452
desc 'Run rest api tests'
55-
task rest_api: ['elasticsearch:update', 'elasticsearch:wait_for_green'] do
53+
task rest_api: ['elasticsearch:wait_for_green'] do
5654
puts '-' * 80
57-
sh "cd #{CURRENT_PATH.join('elasticsearch-api')} && unset BUNDLE_GEMFILE && bundle exec rake test:integration"
55+
sh "cd #{CURRENT_PATH.join('elasticsearch-api')} && unset BUNDLE_GEMFILE && bundle exec rake test:rest_api[true]"
5856
puts "\n"
5957
end
6058

0 commit comments

Comments
 (0)