forked from logstash-plugins/logstash-output-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes_spec_helper.rb
217 lines (189 loc) · 5.13 KB
/
es_spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require_relative './spec_helper'
require 'elasticsearch'
require_relative "support/elasticsearch/api/actions/delete_ilm_policy"
require_relative "support/elasticsearch/api/actions/get_alias"
require_relative "support/elasticsearch/api/actions/put_alias"
require_relative "support/elasticsearch/api/actions/get_ilm_policy"
require_relative "support/elasticsearch/api/actions/put_ilm_policy"
require 'json'
require 'cabin'
module ESHelper
def get_host_port
if ENV["INTEGRATION"] == "true"
"elasticsearch:9200"
else
"localhost:9200"
end
end
def get_client
Elasticsearch::Client.new(:hosts => [get_host_port]).tap do |client|
allow(client).to receive(:verify_elasticsearch).and_return(true) # bypass client side version checking
end
end
def doc_type
if ESHelper.es_version_satisfies?(">=8")
nil
elsif ESHelper.es_version_satisfies?(">=7")
"_doc"
else
"doc"
end
end
def self.action_for_version(action)
action_params = action[1]
if ESHelper.es_version_satisfies?(">=8")
action_params.delete(:_type)
end
action[1] = action_params
action
end
def todays_date
Time.now.strftime("%Y.%m.%d")
end
def field_properties_from_template(template_name, field)
template = get_template(@es, template_name)
mappings = get_template_mappings(template)
mappings["properties"][field]["properties"]
end
def routing_field_name
:routing
end
def self.es_version
[
nilify(RSpec.configuration.filter[:es_version]),
nilify(ENV['ES_VERSION']),
nilify(ENV['ELASTIC_STACK_VERSION']),
].compact.first
end
RSpec::Matchers.define :have_hits do |expected|
match do |actual|
if ESHelper.es_version_satisfies?(">=7")
expected == actual['hits']['total']['value']
else
expected == actual['hits']['total']
end
end
end
RSpec::Matchers.define :have_index_pattern do |expected|
match do |actual|
test_against = Array(actual['index_patterns'].nil? ? actual['template'] : actual['index_patterns'])
test_against.include?(expected)
end
end
def self.es_version_satisfies?(*requirement)
es_version = nilify(RSpec.configuration.filter[:es_version]) || nilify(ENV['ES_VERSION']) || nilify(ENV['ELASTIC_STACK_VERSION'])
if es_version.nil?
puts "Info: ES_VERSION, ELASTIC_STACK_VERSION or 'es_version' tag wasn't set. Returning false to all `es_version_satisfies?` call."
return false
end
es_release_version = Gem::Version.new(es_version).release
Gem::Requirement.new(requirement).satisfied_by?(es_release_version)
end
private
def self.nilify(str)
if str.nil?
return str
end
str.empty? ? nil : str
end
public
def clean(client)
client.indices.delete_template(:name => "*")
client.indices.delete_index_template(:name => "logstash*") rescue nil
# This can fail if there are no indexes, ignore failure.
client.indices.delete(:index => "*") rescue nil
clean_ilm(client) if supports_ilm?(client)
end
def set_cluster_settings(client, cluster_settings)
client.cluster.put_settings(body: cluster_settings)
get_cluster_settings(client)
end
def get_cluster_settings(client)
client.cluster.get_settings
end
def get_policy(client, policy_name)
client.get_ilm_policy(name: policy_name)
end
def put_policy(client, policy_name, policy)
client.put_ilm_policy({:name => policy_name, :body=> policy})
end
def put_alias(client, the_alias, index)
body = {
"aliases" => {
index => {
"is_write_index"=> true
}
}
}
client.put_alias({name: the_alias, body: body})
end
def clean_ilm(client)
client.get_ilm_policy.each_key { |key| client.delete_ilm_policy(name: key) if key =~ /logstash-policy/ }
end
def supports_ilm?(client)
begin
client.get_ilm_policy
true
rescue
false
end
end
def max_docs_policy(max_docs)
{
"policy" => {
"phases"=> {
"hot" => {
"actions" => {
"rollover" => {
"max_docs" => max_docs
}
}
}
}
}
}
end
def max_age_policy(max_age)
{
"policy" => {
"phases"=> {
"hot" => {
"actions" => {
"rollover" => {
"max_age" => max_age
}
}
}
}
}
}
end
def get_template(client, name)
if ESHelper.es_version_satisfies?(">=8")
t = client.indices.get_index_template(name: name)
t['index_templates'][0]['index_template']
else
t = client.indices.get_template(name: name)
t[name]
end
end
def get_template_settings(template)
if ESHelper.es_version_satisfies?(">=8")
template['template']['settings']
else
template['settings']
end
end
def get_template_mappings(template)
if ESHelper.es_version_satisfies?(">=8")
template['template']['mappings']
elsif ESHelper.es_version_satisfies?(">=7")
template['mappings']
else
template['mappings']["_default_"]
end
end
end
RSpec.configure do |config|
config.include ESHelper
end