Skip to content

Commit c31fcfd

Browse files
authored
Add helper method to wait for log message to be observed (#17589)
* 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.
1 parent 01962f6 commit c31fcfd

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

qa/docker/shared_examples/container_options.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@
8888
expect(output_plugins[0].dig('name')).to eql('elasticsearch')
8989

9090
# check if logs contain the ES request with the resolved ${USER}
91-
container_logs = @container.logs(stdout: true)
92-
expect(container_logs.include?('https://kimchy:xxxxxx@es:9200')).to be true
91+
wait_for_log_message(@container, 'https://kimchy:xxxxxx@es:9200')
9392
end
9493
end
9594
end

qa/docker/shared_examples/xpack.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,11 @@
4848
expect(settings['xpack.management.pipeline.id']).to eq("${XPACK_MANAGEMENT_PIPELINE_ID}")
4949
expect(settings['xpack.management.elasticsearch.hosts']).to eq("${XPACK_MANAGEMENT_ELASTICSEARCH_HOSTS}")
5050

51-
# get container logs
52-
container_logs = container.logs(stdout: true)
53-
5451
# check if logs contain node3 & node4 values actually resolved and used
55-
expect(container_logs.include?('pipeline_id=>["*"]')).to be true
52+
wait_for_log_message(container, 'pipeline_id=>["*"]', :stdout)
5653
# note that, we are not spinning up ES nodes, so values can be in errors or in pool update logs
57-
expect(container_logs.include?('http://node3:9200')).to be true
58-
expect(container_logs.include?('http://node4:9200')).to be true
54+
wait_for_log_message(container, 'http://node3:9200', :stdout)
55+
wait_for_log_message(container, 'http://node4:9200', :stdout)
5956
end
6057
end
6158
end

qa/docker/spec/spec_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ def wait_for_pipeline(container, pipeline = 'main')
4949
end
5050
end
5151

52+
def wait_for_log_message(container, search_string, stream = :stdout)
53+
Stud.try(40.times, [NoMethodError, Docker::Error::ConflictError, RSpec::Expectations::ExpectationNotMetError, TypeError]) do
54+
container_logs = container.logs(stream => true)
55+
expect(container_logs.include?(search_string)).to be true
56+
end
57+
end
58+
5259
def cleanup_container(container)
5360
unless container.nil?
5461
begin

0 commit comments

Comments
 (0)