Skip to content

Commit 8e57da4

Browse files
committed
Integration test improvements
Add more tests to check correct creation of write alias and templates Tests for default settings
1 parent 9251097 commit 8e57da4

File tree

3 files changed

+109
-21
lines changed

3 files changed

+109
-21
lines changed

spec/es_spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'manticore'
33
require 'elasticsearch'
44
require_relative "support/elasticsearch/api/actions/delete_ilm_policy"
5+
require_relative "support/elasticsearch/api/actions/get_alias"
56
require_relative "support/elasticsearch/api/actions/get_ilm_policy"
67
require_relative "support/elasticsearch/api/actions/put_ilm_policy"
78

spec/integration/outputs/ilm_spec.rb

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@
120120
res[index_written] += 1
121121
end
122122
expect(indexes_written.count).to eq(3)
123-
expect(indexes_written["#{ilm_write_alias}-000001"]).to eq(3)
124-
expect(indexes_written["#{ilm_write_alias}-000002"]).to eq(3)
125-
expect(indexes_written["#{ilm_write_alias}-000003"]).to eq(3)
123+
expect(indexes_written["#{expected_index}-000001"]).to eq(3)
124+
expect(indexes_written["#{expected_index}-000002"]).to eq(3)
125+
expect(indexes_written["#{expected_index}-000003"]).to eq(3)
126126
end
127127
end
128128

@@ -161,7 +161,7 @@
161161
res[index_written] += 1
162162
end
163163
expect(indexes_written.count).to eq(1)
164-
expect(indexes_written["#{ilm_write_alias}-000001"]).to eq(6)
164+
expect(indexes_written["#{expected_index}-000001"]).to eq(6)
165165
end
166166
end
167167
end
@@ -182,18 +182,11 @@
182182
DEFAULT_INTERVAL = '600s'
183183

184184
require "logstash/outputs/elasticsearch"
185-
let (:ilm_write_alias) { "the_write_alias" }
186-
let (:index) { ilm_write_alias }
187185
let (:ilm_enabled) { true }
188186

189187
let (:settings) {
190188
{
191-
"index" => index,
192189
"ilm_enabled" => ilm_enabled,
193-
"ilm_write_alias" => ilm_write_alias,
194-
"manage_template" => true,
195-
"template_name" => ilm_write_alias,
196-
"template_overwrite" => true,
197190
"hosts" => "#{get_host_port()}"
198191
}
199192
}
@@ -260,15 +253,57 @@
260253
context 'with ilm enabled' do
261254
let (:ilm_enabled) { true }
262255

256+
263257
context 'when using the default policy' do
264258
it 'should install it if it is not present' do
265259
expect{get_policy(@es, LogStash::Outputs::ElasticSearch::DEFAULT_POLICY)}.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound)
266260
subject.register
267261
sleep(1)
268262
expect{get_policy(@es, LogStash::Outputs::ElasticSearch::DEFAULT_POLICY)}.not_to raise_error
269263
end
270-
end
271264

265+
it 'should create the default write alias' do
266+
267+
expect(@es.indices.exists_alias(index: "logstash")).to be_falsey
268+
subject.register
269+
sleep(1)
270+
expect(@es.indices.exists_alias(index: "logstash")).to be_truthy
271+
expect(@es.get_alias(name: "logstash")).to include("logstash-000001")
272+
end
273+
274+
it 'should ingest into a single index' do
275+
subject.register
276+
277+
subject.multi_receive([
278+
LogStash::Event.new("message" => "sample message here"),
279+
LogStash::Event.new("somemessage" => { "message" => "sample nested message here" }),
280+
LogStash::Event.new("somevalue" => 100),
281+
])
282+
283+
sleep(6)
284+
285+
subject.multi_receive([
286+
LogStash::Event.new("country" => "us"),
287+
LogStash::Event.new("country" => "at"),
288+
LogStash::Event.new("geoip" => { "location" => [ 0.0, 0.0 ] })
289+
])
290+
291+
@es.indices.refresh
292+
293+
# Wait or fail until everything's indexed.
294+
Stud::try(20.times) do
295+
r = @es.search
296+
expect(r["hits"]["total"]).to eq(6)
297+
end
298+
indexes_written = @es.search['hits']['hits'].each_with_object(Hash.new(0)) do |x, res|
299+
index_written = x['_index']
300+
res[index_written] += 1
301+
end
302+
303+
expect(indexes_written.count).to eq(1)
304+
expect(indexes_written["logstash-000001"]).to eq(6)
305+
end
306+
end
272307

273308
context 'when not using the default policy' do
274309
let (:ilm_policy_name) {"new_one"}
@@ -299,12 +334,22 @@
299334
end
300335

301336
context 'with the default template' do
337+
let(:expected_index) { "logstash" }
302338

303-
it 'should write the write alias' do
304-
expect(@es.indices.exists_alias(index: ilm_write_alias)).to be_falsey
339+
it 'should create the write alias' do
340+
expect(@es.indices.exists_alias(index: expected_index)).to be_falsey
305341
subject.register
306342
sleep(1)
307-
expect(@es.indices.exists_alias(index: ilm_write_alias)).to be_truthy
343+
expect(@es.indices.exists_alias(index: expected_index)).to be_truthy
344+
expect(@es.get_alias(name: expected_index)).to include("#{expected_index}-000001")
345+
end
346+
347+
it 'should write the ILM settings into the template' do
348+
subject.register
349+
sleep(1)
350+
expect(@es.indices.get_template(name: "logstash")["logstash"]["index_patterns"]).to eq(["logstash-*"])
351+
expect(@es.indices.get_template(name: "logstash")["logstash"]["settings"]['index']['lifecycle']['name']).to eq("logstash-policy")
352+
expect(@es.indices.get_template(name: "logstash")["logstash"]["settings"]['index']['lifecycle']['rollover_alias']).to eq("logstash")
308353
end
309354

310355
it_behaves_like 'an ILM enabled Logstash'
@@ -313,24 +358,39 @@
313358
context 'with a custom template' do
314359
let (:ilm_write_alias) { "custom" }
315360
let (:index) { ilm_write_alias }
361+
let(:expected_index) { index }
362+
let (:settings) { super.merge("ilm_policy" => ilm_policy_name,
363+
"template" => template,
364+
"template_name" => template_name,
365+
"ilm_write_alias" => ilm_write_alias)}
316366
let (:template_name) { "custom" }
367+
317368
if ESHelper.es_version_satisfies?(">= 7.0")
318369
let (:template) { "spec/fixtures/template-with-policy-es7x.json" }
319370
else
320371
let (:template) { "spec/fixtures/template-with-policy-es6x.json" }
321372
end
322373
let (:ilm_enabled) { true }
323374
let (:ilm_policy_name) { "custom-policy" }
324-
let (:settings) { super.merge("ilm_policy" => ilm_policy_name, "template" => template)}
375+
325376

326377
before :each do
327378
put_policy(@es,ilm_policy_name, policy)
328379
end
329-
it 'should write the write alias' do
380+
it 'should create the write alias' do
330381
expect(@es.indices.exists_alias(index: ilm_write_alias)).to be_falsey
331382
subject.register
332383
sleep(1)
333384
expect(@es.indices.exists_alias(index: ilm_write_alias)).to be_truthy
385+
expect(@es.get_alias(name: ilm_write_alias)).to include("#{ilm_write_alias}-000001")
386+
end
387+
388+
it 'should write the ILM settings into the template' do
389+
subject.register
390+
sleep(1)
391+
expect(@es.indices.get_template(name: template_name)[template_name]["index_patterns"]).to eq(["#{ilm_write_alias}-*"])
392+
expect(@es.indices.get_template(name: template_name)[template_name]["settings"]['index']['lifecycle']['name']).to eq(ilm_policy_name)
393+
expect(@es.indices.get_template(name: template_name)[template_name]["settings"]['index']['lifecycle']['rollover_alias']).to eq(ilm_write_alias)
334394
end
335395

336396
it_behaves_like 'an ILM enabled Logstash'
@@ -340,11 +400,11 @@
340400
context 'with ilm disabled' do
341401
let (:ilm_enabled) { false }
342402

343-
it 'should not write the write alias' do
344-
expect(@es.indices.exists_alias(index: ilm_write_alias)).to be_falsey
403+
it 'should not create a write alias' do
404+
expect(@es.get_alias).to be_empty
345405
subject.register
346406
sleep(1)
347-
expect(@es.indices.exists_alias(index: ilm_write_alias)).to be_falsey
407+
expect(@es.get_alias).to be_empty
348408
end
349409

350410
it 'should not install the default policy' do
@@ -353,6 +413,15 @@
353413
expect{get_policy(@es, LogStash::Outputs::ElasticSearch::DEFAULT_POLICY)}.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound)
354414
end
355415

416+
it 'should write the ILM settings into the template' do
417+
subject.register
418+
sleep(1)
419+
expect(@es.indices.get_template(name: "logstash")["logstash"]["index_patterns"]).to eq(["logstash-*"])
420+
expect(@es.indices.get_template(name: "logstash")["logstash"]["settings"]['index']['lifecycle']).to be_nil
421+
422+
end
423+
424+
356425
context 'with an existing policy that will roll over' do
357426
let (:policy) { small_max_doc_policy }
358427
let (:ilm_policy_name) { "3_docs"}
@@ -387,7 +456,7 @@
387456
res[index_written] += 1
388457
end
389458
expect(indexes_written.count).to eq(1)
390-
expect(indexes_written["#{index}"]).to eq(6)
459+
expect(indexes_written.values.first).to eq(6)
391460
end
392461
end
393462
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
# or more contributor license agreements. Licensed under the Elastic License;
3+
# you may not use this file except in compliance with the Elastic License.
4+
5+
module Elasticsearch
6+
module API
7+
module Actions
8+
9+
# Retrieve the list of index lifecycle management policies
10+
def get_alias(arguments={})
11+
method = HTTP_GET
12+
path = Utils.__pathify '_alias', Utils.__escape(arguments[:name])
13+
params = {}
14+
perform_request(method, path, params, nil).body
15+
end
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)