|
| 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