16
16
# under the License.
17
17
18
18
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'
27
20
28
21
task ( :default ) { system 'rake --tasks' }
29
22
task test : 'test:unit'
@@ -47,79 +40,75 @@ namespace :test do
47
40
t . exclude_pattern = 'spec/**{,/*/**}/rest_api_yaml_spec.rb'
48
41
end
49
42
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 |
62
45
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
-
77
46
# Check if a test cluster is running
78
47
begin
79
48
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
81
50
client = Elasticsearch ::Client . new :url => url
82
51
es_version_info = client . info [ 'version' ]
52
+ version_number = es_version_info [ 'number' ]
83
53
build_hash = es_version_info [ 'build_hash' ]
84
54
cluster_running = true
85
55
rescue Faraday ::ConnectionFailed
86
56
STDERR . puts "[!] Test cluster not running?"
87
57
cluster_running = false
58
+ exit 1
88
59
end
89
60
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 ]
93
62
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
116
69
end
117
70
118
71
desc 'Run unit and integration tests'
119
72
task :all do
120
73
Rake ::Task [ 'test:unit' ] . invoke
121
74
Rake ::Task [ 'test:integration' ] . invoke
122
75
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
123
112
end
124
113
125
114
# ----- Documentation tasks ---------------------------------------------------
0 commit comments