From 381c33a4192d30e0913dec565401f7eb216955a2 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 29 Apr 2025 12:26:43 -0700 Subject: [PATCH] Add helper method to wait for log message to be observed (#17589) (#17605) * Add helper method to wait for log message to be observed There appears to be a race condition in tests whereby log messages are not observed with a single interogation of the container logs. This commit adds a helper method to wait for log messages. This should make the tests more resilient when there is a delay in log messages being captured. * Use local scop container ref Due to a copy paste error, the wrong reference to container (instance var) was being used. When test file that did not define this uses the helper it failed. The helper should use the ref explicitly passed to the method. (cherry picked from commit c31fcfd9eec37bde5d242a37eba5d5fcf0fd98f2) Co-authored-by: Cas Donoghue --- qa/docker/shared_examples/container_options.rb | 3 +-- qa/docker/shared_examples/xpack.rb | 9 +++------ qa/docker/spec/spec_helper.rb | 7 +++++++ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/qa/docker/shared_examples/container_options.rb b/qa/docker/shared_examples/container_options.rb index 48c51d382..dce1e8486 100644 --- a/qa/docker/shared_examples/container_options.rb +++ b/qa/docker/shared_examples/container_options.rb @@ -88,8 +88,7 @@ shared_examples_for 'it applies settings correctly' do |flavor| expect(output_plugins[0].dig('name')).to eql('elasticsearch') # check if logs contain the ES request with the resolved ${USER} - container_logs = @container.logs(stdout: true) - expect(container_logs.include?('https://kimchy:xxxxxx@es:9200')).to be true + wait_for_log_message(@container, 'https://kimchy:xxxxxx@es:9200') end end end diff --git a/qa/docker/shared_examples/xpack.rb b/qa/docker/shared_examples/xpack.rb index 254e0354c..5b954b25f 100644 --- a/qa/docker/shared_examples/xpack.rb +++ b/qa/docker/shared_examples/xpack.rb @@ -48,14 +48,11 @@ shared_examples_for 'a container with xpack features' do |flavor| expect(settings['xpack.management.pipeline.id']).to eq("${XPACK_MANAGEMENT_PIPELINE_ID}") expect(settings['xpack.management.elasticsearch.hosts']).to eq("${XPACK_MANAGEMENT_ELASTICSEARCH_HOSTS}") - # get container logs - container_logs = container.logs(stdout: true) - # check if logs contain node3 & node4 values actually resolved and used - expect(container_logs.include?('pipeline_id=>["*"]')).to be true + wait_for_log_message(container, 'pipeline_id=>["*"]', :stdout) # note that, we are not spinning up ES nodes, so values can be in errors or in pool update logs - expect(container_logs.include?('http://node3:9200')).to be true - expect(container_logs.include?('http://node4:9200')).to be true + wait_for_log_message(container, 'http://node3:9200', :stdout) + wait_for_log_message(container, 'http://node4:9200', :stdout) end end end diff --git a/qa/docker/spec/spec_helper.rb b/qa/docker/spec/spec_helper.rb index f79514d5a..eb6622dc7 100644 --- a/qa/docker/spec/spec_helper.rb +++ b/qa/docker/spec/spec_helper.rb @@ -49,6 +49,13 @@ def wait_for_pipeline(container, pipeline = 'main') end end +def wait_for_log_message(container, search_string, stream = :stdout) + Stud.try(40.times, [NoMethodError, Docker::Error::ConflictError, RSpec::Expectations::ExpectationNotMetError, TypeError]) do + container_logs = container.logs(stream => true) + expect(container_logs.include?(search_string)).to be true + end +end + def cleanup_container(container) unless container.nil? begin