Skip to content

Commit afc127d

Browse files
committed
Add unit tests and include license notes.
1 parent 6ceafa8 commit afc127d

File tree

5 files changed

+180
-2
lines changed

5 files changed

+180
-2
lines changed

qa/support/logstash-integration-failure_injector/lib/logstash/filters/failure_injector.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# encoding: utf-8
22

3+
# Licensed to Elasticsearch B.V. under one or more contributor
4+
# license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright
6+
# ownership. Elasticsearch B.V. licenses this file to you under
7+
# the Apache License, Version 2.0 (the "License"); you may
8+
# not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
320
require 'logstash/inputs/base'
421
require 'logstash/namespace'
522

qa/support/logstash-integration-failure_injector/lib/logstash/outputs/failure_injector.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
118
# encoding: utf-8
219

320
require 'logstash/inputs/base'

qa/support/logstash-integration-failure_injector/lib/logstash/utils/failure_injector_util.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
118
# encoding: utf-8
219

320
class FailureInjectorUtils
@@ -15,7 +32,6 @@ def self.validate_config(type, params)
1532
end
1633

1734
crash_at = params['crash_at']
18-
puts "crash_at: #{crash_at}"
1935
raise "#{config_error_message} #{crash_at}" if crash_at && !accepted_configs.include?(crash_at)
2036
end
2137
end

qa/support/logstash-integration-failure_injector/logstash-integration-failure_injector.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Gem::Specification.new do |s|
1212
"logstash_plugin" => "true",
1313
"logstash_group" => "integration",
1414
"integration_plugins" => %w(
15-
logstash-input-failure_injector
1615
logstash-filter-failure_injector
1716
logstash-output-failure_injector
1817
).join(",")
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# encoding: utf-8
19+
20+
require "logstash/devutils/rspec/spec_helper"
21+
require "logstash/filters/failure_injector"
22+
require "logstash/outputs/failure_injector"
23+
24+
%w(filter output).each do | plugin_type |
25+
instance = plugin_type == 'filter' ? LogStash::Filters::FailureInjector : LogStash::Outputs::FailureInjector
26+
phase = plugin_type == 'filter' ? 'filter' : 'receive'
27+
describe instance do
28+
let(:params) { { 'degrade_at' => [], 'crash_at' => nil } }
29+
let(:event) { LogStash::Event.new }
30+
let(:plugin) { described_class.new(params) }
31+
32+
before do
33+
allow(plugin).to receive(:@logger).and_return(double('logger', :debug => nil, :trace => nil))
34+
end
35+
36+
describe 'plugin base' do
37+
subject { described_class }
38+
it { is_expected.to be_a_kind_of Class }
39+
it { is_expected.to be <= (plugin_type == 'filter' ? LogStash::Filters::Base : LogStash::Outputs::Base) }
40+
it { is_expected.to have_attributes(:config_name => "failure_injector") }
41+
end
42+
43+
shared_examples 'a phase that can degrade or crash' do |phase|
44+
context "when degrades at #{phase}" do
45+
let(:params) { { 'degrade_at' => [phase] } }
46+
47+
it 'calls the degrate method' do
48+
expect(plugin).to receive(:degrate).with(phase)
49+
case phase
50+
when 'filter'
51+
plugin.filter(event)
52+
when 'receive'
53+
plugin.multi_receive([event])
54+
else
55+
plugin.send(phase)
56+
end
57+
end
58+
end
59+
60+
context "when crashes at #{phase}" do
61+
let(:params) { { 'crash_at' => phase } }
62+
63+
it 'raises a crash error' do
64+
case phase
65+
when 'filter'
66+
expect { plugin.filter(event) }.to raise_error(RuntimeError, /crashing at #{phase}/)
67+
when 'receive'
68+
expect { plugin.multi_receive([event]) }.to raise_error(RuntimeError, /crashing at #{phase}/)
69+
else
70+
expect { plugin.send(phase) }.to raise_error(RuntimeError, /crashing at #{phase}/)
71+
end
72+
end
73+
end
74+
end
75+
76+
describe '#initialize' do
77+
context 'when valid params are passed' do
78+
let(:params) { { 'degrade_at' => [], 'crash_at' => nil } }
79+
80+
it 'does not raise any error' do
81+
expect { described_class.new(params) }.not_to raise_error
82+
end
83+
end
84+
85+
context 'when invalid params are passed' do
86+
it 'raises an error on invalid config' do
87+
configs = ["register", plugin_type == 'filter' ? "filter" : "receive", "close"]
88+
expect {
89+
described_class.new('degrade_at' => ['invalid'], 'crash_at' => 'invalid')
90+
}.to raise_error("failure_injector #{plugin_type} plugin accepts #{configs} configs but received invalid")
91+
end
92+
end
93+
end
94+
95+
describe '#register' do
96+
it_behaves_like 'a phase that can degrade or crash', 'register'
97+
end
98+
99+
if plugin_type == 'filter'
100+
describe '#filter' do
101+
it_behaves_like 'a phase that can degrade or crash', 'filter'
102+
end
103+
end
104+
105+
if plugin_type == 'output'
106+
describe '#receive' do
107+
it_behaves_like 'a phase that can degrade or crash', 'receive'
108+
end
109+
end
110+
111+
describe '#close' do
112+
it_behaves_like 'a phase that can degrade or crash', 'close'
113+
end
114+
115+
describe '#degrate' do
116+
it 'sleeps for a certain period of time' do
117+
expect(plugin).to receive(:sleep).at_least(:once)
118+
plugin.degrate('filter')
119+
end
120+
end
121+
122+
describe '#crash' do
123+
it 'raises an error with the phase' do
124+
expect { plugin.crash(phase) }.to raise_error(RuntimeError, /crashing at #{phase}/)
125+
end
126+
end
127+
end
128+
end
129+

0 commit comments

Comments
 (0)