Skip to content

Commit 2db9858

Browse files
committed
Moved from logger double to real log spy using log4j appender in SettingWithDeprecatedAlias spec test.
1 parent 35d3d93 commit 2db9858

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

logstash-core/spec/logstash/settings/setting_with_deprecated_alias_spec.rb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@
2525
let(:default_value) { "DeFaUlT" }
2626

2727
let(:settings) { LogStash::Settings.new }
28-
let(:canonical_setting) { LogStash::Setting::String.new(canonical_setting_name, default_value, true) }
28+
let(:canonical_setting) { LogStash::Setting::StringSetting.new(canonical_setting_name, default_value, true) }
29+
30+
let(:log_ctx) { setup_logger_spy }
31+
let(:log_spy) { retrieve_logger_spy(log_ctx) }
32+
33+
before(:each) do
34+
# Initialization of appender and logger use to spy, need to be done before executing any code that logs,
35+
# that's the reason wy to refer the spying logger context before any test.
36+
log_ctx
37+
end
2938

3039
before(:each) do
3140
allow(LogStash::Settings).to receive(:logger).and_return(double("SettingsLogger").as_null_object)
@@ -57,6 +66,8 @@
5766
it 'does not emit a deprecation warning' do
5867
expect(LogStash::Settings.deprecation_logger).to_not receive(:deprecated).with(a_string_including(deprecated_setting_name))
5968
settings.get_setting(deprecated_setting_name).observe_post_process
69+
log_spy = retrieve_logger_spy(log_ctx)
70+
expect(log_spy.messages).to be_empty
6071
end
6172
end
6273
end
@@ -66,22 +77,23 @@
6677

6778
before(:each) do
6879
settings.set(deprecated_setting_name, value)
80+
settings.get_setting(deprecated_setting_name).observe_post_process
6981
end
7082

7183
it 'resolves to the value provided for the deprecated alias' do
7284
expect(settings.get(canonical_setting_name)).to eq(value)
7385
end
7486

7587
it 'logs a deprecation warning' do
76-
expect(LogStash::Settings.deprecation_logger).to have_received(:deprecated).with(a_string_including(deprecated_setting_name))
88+
expect(log_spy.messages[0]).to include(deprecated_setting_name)
7789
end
7890

7991
include_examples '#validate_value success'
8092

8193
context "#observe_post_process" do
8294
it 're-emits the deprecation warning' do
83-
expect(LogStash::Settings.deprecation_logger).to receive(:deprecated).with(a_string_including(deprecated_setting_name))
8495
settings.get_setting(deprecated_setting_name).observe_post_process
96+
expect(log_spy.messages[0]).to include(deprecated_setting_name)
8597
end
8698
end
8799

@@ -149,15 +161,16 @@
149161
end
150162

151163
it 'does not produce a relevant deprecation warning' do
152-
expect(LogStash::Settings.deprecation_logger).to_not have_received(:deprecated).with(a_string_including(deprecated_setting_name))
164+
settings.get_setting(deprecated_setting_name).observe_post_process
165+
expect(log_spy.messages).to be_empty
153166
end
154167

155168
include_examples '#validate_value success'
156169

157170
context "#observe_post_process" do
158171
it 'does not emit a deprecation warning' do
159-
expect(LogStash::Settings.deprecation_logger).to_not receive(:deprecated).with(a_string_including(deprecated_setting_name))
160172
settings.get_setting(deprecated_setting_name).observe_post_process
173+
expect(log_spy.messages).to be_empty
161174
end
162175
end
163176
end
@@ -171,15 +184,15 @@
171184
context '#validate_value' do
172185
it "raises helpful exception" do
173186
expect { settings.get_setting(canonical_setting_name).validate_value }
174-
.to raise_exception(ArgumentError, a_string_including("Both `#{canonical_setting_name}` and its deprecated alias `#{deprecated_setting_name}` have been set. Please only set `#{canonical_setting_name}`"))
187+
.to raise_exception(java.lang.IllegalStateException, a_string_including("Both `#{canonical_setting_name}` and its deprecated alias `#{deprecated_setting_name}` have been set. Please only set `#{canonical_setting_name}`"))
175188
end
176189
end
177190
end
178191

179192
context 'Settings#get on deprecated alias' do
180193
it 'produces a WARN-level message to the logger' do
181-
expect(LogStash::Settings.logger).to receive(:warn).with(a_string_including "setting `#{canonical_setting_name}` has been queried by its deprecated alias `#{deprecated_setting_name}`")
182194
settings.get(deprecated_setting_name)
195+
expect(log_spy.messages[0]).to include("setting `#{canonical_setting_name}` has been queried by its deprecated alias `#{deprecated_setting_name}`")
183196
end
184197
end
185198
end

spec/spec_helper.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,40 @@ def puts(payload)
9090
def installed_plugins
9191
Gem::Specification.find_all.select { |spec| spec.metadata["logstash_plugin"] }.map { |plugin| plugin.name }
9292
end
93+
94+
95+
def setup_logger_spy
96+
java_import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
97+
java_import org.apache.logging.log4j.Level
98+
config_builder = ConfigurationBuilderFactory.newConfigurationBuilder
99+
configure_log_spy = config_builder
100+
.add(
101+
config_builder
102+
.newAppender("LOG_SPY", "List")
103+
.add(config_builder.newLayout("PatternLayout").addAttribute("pattern", "%-5p [%t]: %m%n"))
104+
)
105+
.add(
106+
config_builder
107+
.newRootLogger(Level::INFO)
108+
.add(config_builder.newAppenderRef("LOG_SPY")))
109+
.build(false)
110+
111+
java_import org.apache.logging.log4j.core.config.Configurator
112+
java_import org.apache.logging.log4j.core.config.Configuration
113+
114+
java_import org.apache.logging.log4j.LogManager
115+
java_import org.apache.logging.log4j.core.impl.Log4jContextFactory
116+
# This is done because the LoggerExt calls setFactory LogstashLoggerContextFactory implements
117+
# org.apache.logging.log4j.spi.LoggerContextFactory and doesn't extend org.apache.logging.log4j.core.impl.Log4jContextFactory
118+
# which is the class expected by the following Configurator to use the programmatic configuration.
119+
LogManager.setFactory(Log4jContextFactory.new)
120+
log_ctx = Configurator.java_send(:initialize, [Configuration], configure_log_spy)
121+
expect(log_ctx).not_to be nil
122+
log_ctx.reconfigure(configure_log_spy) # force the programmatic configuration, without this it's not used
123+
124+
return log_ctx
125+
end
126+
127+
def retrieve_logger_spy(log_ctx)
128+
log_ctx.getConfiguration().getAppender("LOG_SPY")
129+
end

0 commit comments

Comments
 (0)