Skip to content

Commit ec8bd3f

Browse files
Update rest tests 7.x (#1265)
Updates rest tests to use artifacts - Backports #1262 * [CI] Updates run-elasticsearch script * [API] Test Runner: Run tests from downloaded artifacts * [API] Test Runner: Exit and show instruction if tests files not present * [DOCS] Updates README files in elasticsearch and elasticsearch-api * [CI] Test Runner: Moves download artifacts task to elasticsearch * [CI] Test Runner: Run x-pack tests from artifacts * [CI] Test Runner: Add download artifacts task when running tests in Jenkins * [CI] Test Runner - Instantiate admin_client early to reuse in subsequent tasks * [CI] Test Runner: Refactor * [DOCS] Update docs
1 parent d16bcd2 commit ec8bd3f

File tree

11 files changed

+194
-157
lines changed

11 files changed

+194
-157
lines changed

.ci/run-elasticsearch.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
# Export the TEST_SUITE variable, eg. 'free' or 'platinum' defaults to 'free'.
88
# Export the NUMBER_OF_NODES variable to start more than 1 node
99

10-
# Version 1.2.0
10+
# Version 1.3.0
1111
# - Initial version of the run-elasticsearch.sh script
1212
# - Deleting the volume should not dependent on the container still running
1313
# - Fixed `ES_JAVA_OPTS` config
1414
# - Moved to STACK_VERSION and TEST_VERSION
1515
# - Refactored into functions and imports
1616
# - Support NUMBER_OF_NODES
1717
# - Added 5 retries on docker pull for fixing transient network errors
18+
# - Added flags to make local CCR configurations work
19+
# - Added action.destructive_requires_name=false as the default will be true in v8
1820

1921
script_path=$(dirname $(realpath -s $0))
2022
source $script_path/functions/imports.sh
@@ -37,6 +39,7 @@ environment=($(cat <<-END
3739
--env node.attr.testattr=test
3840
--env path.repo=/tmp
3941
--env repositories.url.allowed_urls=http://snapshot.test*
42+
--env action.destructive_requires_name=false
4043
END
4144
))
4245
if [[ "$TEST_SUITE" == "platinum" ]]; then
@@ -50,6 +53,7 @@ if [[ "$TEST_SUITE" == "platinum" ]]; then
5053
--env xpack.security.http.ssl.certificate=certs/testnode.crt
5154
--env xpack.security.http.ssl.certificate_authorities=certs/ca.crt
5255
--env xpack.security.transport.ssl.enabled=true
56+
--env xpack.security.transport.ssl.verification_mode=certificate
5357
--env xpack.security.transport.ssl.key=certs/testnode.key
5458
--env xpack.security.transport.ssl.certificate=certs/testnode.crt
5559
--env xpack.security.transport.ssl.certificate_authorities=certs/ca.crt
@@ -109,7 +113,7 @@ END
109113
docker run \
110114
--name "$node_name" \
111115
--network "$network_name" \
112-
--env "ES_JAVA_OPTS=-Xms1g -Xmx1g" \
116+
--env "ES_JAVA_OPTS=-Xms1g -Xmx1g -da:org.elasticsearch.xpack.ccr.index.engine.FollowingEngineAssertions" \
113117
"${environment[@]}" \
114118
"${volumes[@]}" \
115119
--publish "$http_port":9200 \
@@ -127,5 +131,4 @@ END
127131
if wait_for_container "$es_node_name" "$network_name"; then
128132
echo -e "\033[32;1mSUCCESS:\033[0m Running on: $node_url\033[0m"
129133
fi
130-
131134
done

.ci/run-repository.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ if [[ $TEST_SUITE != "platinum" ]]; then
4949
--name elasticsearch-ruby \
5050
--rm \
5151
elastic/elasticsearch-ruby \
52-
bundle exec rake test:rest_api
52+
bundle exec rake test:download_artifacts test:rest_api
5353
else
5454
docker run \
5555
--network="${network_name}" \
@@ -62,5 +62,5 @@ else
6262
--name elasticsearch-ruby \
6363
--rm \
6464
elastic/elasticsearch-ruby \
65-
bundle exec rake test:security
65+
bundle exec rake test:download_artifacts test:security
6666
fi

README.md

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -99,37 +99,8 @@ This will clone the Elasticsearch repository into the project, and run `bundle i
9999

100100
To run tests, you need to start a testing cluster on port 9250, or provide a different one in the `TEST_CLUSTER_PORT` environment variable.
101101

102-
There's a Rake task to start the testing cluster. By default this is going to try and use the `elasticsearch` command on your system:
102+
There's a Rake task to start a testing cluster in a Docker container:
103103

104-
```
105-
rake test:cluster:start
106-
```
107-
108-
You can also configure where the elasticsearch startup script is found with the `TEST_CLUSTER_COMMAND` environment variable, e.g.:
109-
```
110-
TEST_CLUSTER_COMMAND=~/elasticsearch/bin/elasticsearch rake test:cluster:stop
111-
```
112-
113-
You also can configure the port, number of nodes, and other settings with environment variables:
114-
115-
```
116-
TEST_CLUSTER_COMMAND=./tmp/builds/elasticsearch-7.10.0-SNAPSHOT/bin/elasticsearch \
117-
TEST_CLUSTER_PORT=9250 \
118-
TEST_CLUSTER_NODES=2 \
119-
TEST_CLUSTER_NAME=my_cluster \
120-
ES_JAVA_OPTS='-Xms500m -Xmx500m' \
121-
TEST_CLUSTER_TIMEOUT=120 \
122-
rake test:cluster:start
123-
```
124-
125-
You can stop the cluster with a rake task, passing in the `TEST_CLUSTER_COMMAND` variable:
126-
127-
```
128-
TEST_CLUSTER_COMMAND=./tmp/builds/elasticsearch-7.10.0-SNAPSHOT/bin/elasticsearch \
129-
rake test:cluster:stop
130-
```
131-
132-
There's also a rake task for starting up Elasticsearch in a Docker container:
133104
`rake docker:start[version]` - E.g.: `rake docker:start[7.x-SNAPSHOT]`. To start the container with X-Pack, pass it in as a parameter: `rake docker:start[7.x-SNAPSHOT,xpack]`.
134105

135106
To run tests against unreleased Elasticsearch versions, you can use the `rake elasticsearch:build` Rake task to build Elasticsearch from the cloned source (use `rake elasticsearch:update` to update the repository):

Rakefile

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,38 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
# Admin client is used by tests and other rake tasks to communicate with a running cluster.
19+
def admin_client
20+
$admin_client ||= begin
21+
transport_options = {}
22+
test_suite = ENV['TEST_SUITE'].freeze
23+
24+
if (hosts = ENV['TEST_ES_SERVER'] || ENV['ELASTICSEARCH_HOSTS'])
25+
split_hosts = hosts.split(',').map do |host|
26+
/(http\:\/\/)?\S+/.match(host)
27+
end
28+
uri = URI.parse(split_hosts.first[0])
29+
end
30+
31+
if test_suite == 'platinum'
32+
transport_options.merge!(
33+
ssl: {
34+
verify: false,
35+
ca_path: CERT_DIR
36+
}
37+
)
38+
39+
password = ENV['ELASTIC_PASSWORD']
40+
user = ENV['ELASTIC_USER'] || 'elastic'
41+
url = "https://#{user}:#{password}@#{uri.host}:#{uri.port}"
42+
else
43+
url = "http://#{uri&.host || 'localhost'}:#{uri&.port || 9200}"
44+
end
45+
puts "Elasticsearch Client url: #{url}"
46+
Elasticsearch::Client.new(host: url, transport_options: transport_options)
47+
end
48+
end
49+
1850
import 'rake_tasks/elasticsearch_tasks.rake'
1951
import 'rake_tasks/test_tasks.rake'
2052
import 'rake_tasks/docker_tasks.rake'
@@ -44,37 +76,6 @@ CERT_DIR = ENV['CERT_DIR'] || '.ci/certs'
4476
# Import build task after setting constants:
4577
import 'rake_tasks/unified_release_tasks.rake'
4678

47-
def admin_client
48-
$admin_client ||= begin
49-
transport_options = {}
50-
test_suite = ENV['TEST_SUITE'].freeze
51-
52-
if hosts = ENV['TEST_ES_SERVER'] || ENV['ELASTICSEARCH_HOSTS']
53-
split_hosts = hosts.split(',').map do |host|
54-
/(http\:\/\/)?\S+/.match(host)
55-
end
56-
uri = URI.parse(split_hosts.first[0])
57-
end
58-
59-
if test_suite == 'platinum'
60-
transport_options.merge!(
61-
ssl: {
62-
verify: false,
63-
ca_path: CERT_DIR
64-
}
65-
)
66-
67-
password = ENV['ELASTIC_PASSWORD']
68-
user = ENV['ELASTIC_USER'] || 'elastic'
69-
url = "https://#{user}:#{password}@#{uri.host}:#{uri.port}"
70-
else
71-
url = "http://#{uri&.host || 'localhost'}:#{uri&.port || 9200}"
72-
end
73-
puts "Elasticsearch Client url: #{url}"
74-
Elasticsearch::Client.new(host: url, transport_options: transport_options)
75-
end
76-
end
77-
7879
# TODO: Figure out "bundle exec or not"
7980
# subprojects.each { |project| $LOAD_PATH.unshift CURRENT_PATH.join(project, "lib").to_s }
8081

elasticsearch-api/README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ It does not provide an Elasticsearch client; see the
1313

1414
The library is compatible with Ruby 1.9 and higher.
1515

16-
It is compatible with Elasticsearch's API versions from 0.90 till current,
17-
just use a release matching major version of Elasticsearch.
16+
It is compatible with Elasticsearch's API versions from 0.90 till current, just use a release matching major version of Elasticsearch.
1817

1918
| Ruby | | Elasticsearch |
2019
|:-------------:|:-:| :-----------: |
@@ -190,18 +189,18 @@ Elasticsearch::API.serializer.dump({foo: 'bar'})
190189

191190
## Development
192191

193-
To work on the code, clone and bootstrap the main repository first --
194-
please see instructions in the main [README](../README.md#development).
192+
To work on the code, clone and bootstrap the main repository first -- please see instructions in the main [README](../README.md#development).
195193

196-
To run tests, launch a testing cluster -- again, see instructions
197-
in the main [README](../README.md#development) -- and use the Rake tasks:
194+
To run tests, launch a testing cluster -- again, see instructions in the main [README](../README.md#development) -- and use the Rake tasks:
198195

199196
```
200197
time rake test:unit
201198
time rake test:integration
202199
```
203200

204-
You can read more about how we test the API using the Elasticsearch spec in [the test runner README](https://github.com/elastic/elasticsearch-ruby/tree/master/api-spec-testing#rest-api-yaml-test-runner).
201+
We run the test suite for Elasticsearch's Rest API tests. You can read more about this in [the test runner README](https://github.com/elastic/elasticsearch-ruby/tree/master/api-spec-testing#rest-api-yaml-test-runner).
202+
203+
The `rest_api` needs the test files from Elasticsearch. You can run the rake task to download the test artifacts in the root folder of the project. This task needs a running cluster to determine which version and build hash of Elasticsearch to use and test against. `TEST_ES_SERVER=http://localhost:9200 rake test:download_artifacts`. This will download the necessary files used for the integration tests to `./tmp`.
205204

206205
## License
207206

elasticsearch-api/Rakefile

Lines changed: 46 additions & 62 deletions
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'
@@ -46,73 +39,28 @@ namespace :test do
4639
t.exclude_pattern = 'spec/**{,/*/**}/rest_api_yaml_spec.rb'
4740
end
4841

49-
# Rest API Spec tests - rake test:rest_api
42+
desc "Run Rest API Spec tests"
5043
RSpec::Core::RakeTask.new(:rest_api) do |t|
51-
t.pattern = 'spec/**{,/*/**}/rest_api_yaml_spec.rb'
52-
end
53-
54-
desc 'Update the repository with YAML tests'
55-
task :update do
56-
git_specs 'fetch origin', verbose: true
57-
end
58-
59-
desc "Run integration tests"
60-
task integration: :update do
6144
require 'elasticsearch'
62-
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-
7745
# Check if a test cluster is running
7846
begin
7947
url = ENV['TEST_CLUSTER_URL'] || ENV['TEST_ES_SERVER']
80-
url = "http://localhost:#{ENV['TEST_CLUSTER_PORT'] || 9250}" unless url
48+
url = "http://localhost:#{ENV['TEST_CLUSTER_PORT'] || 9200}" unless url
8149
client = Elasticsearch::Client.new :url => url
8250
es_version_info = client.info['version']
51+
version_number = es_version_info['number']
8352
build_hash = es_version_info['build_hash']
84-
cluster_running = true
8553
rescue Faraday::ConnectionFailed
8654
STDERR.puts "[!] Test cluster not running?"
87-
cluster_running = false
55+
exit 1
8856
end
8957

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
58+
t.pattern = 'spec/**{,/*/**}/rest_api_yaml_spec.rb'
59+
end
9360

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
61+
desc "Run integration tests"
62+
task :integration do
63+
Rake::Task['test:rest_api'].invoke
11664
end
11765

11866
desc 'Run unit and integration tests'
@@ -121,6 +69,42 @@ namespace :test do
12169
Rake::Task['test:integration'].invoke
12270
end
12371

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

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
require 'rest_yaml_tests_helper'
77

88
describe 'Rest API YAML tests' do
9+
if REST_API_YAML_FILES.empty?
10+
logger = Logger.new($stdout)
11+
logger.error 'No test files found!'
12+
logger.info 'Use rake rake test:download_artifacts in the root directory of the project to download the test artifacts.'
13+
exit 1
14+
end
15+
916
# Traverse YAML files and create TestFile object:
1017
REST_API_YAML_FILES.each do |file|
1118
begin

0 commit comments

Comments
 (0)