Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autodiscover flaky tests #21242

Merged
merged 4 commits into from
Sep 24, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 47 additions & 61 deletions filebeat/tests/system/test_autodiscover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest

from beat.beat import INTEGRATION_TESTS
from contextlib import contextmanager


class TestAutodiscover(filebeat.BaseTest):
Expand All @@ -16,46 +17,25 @@ def test_docker(self):
"""
Test docker autodiscover starts input
"""
import docker
docker_client = docker.from_env()

self.render_config_template(
inputs=False,
autodiscover={
'docker': {
'cleanup_timeout': '0s',
'templates': '''
- condition:
equals.docker.container.image: busybox
config:
- type: log
paths:
- %s/${data.docker.container.image}.log
''' % self.working_dir,
with self.container_running() as container:
self.render_config_template(
inputs=False,
autodiscover={
'docker': {
'cleanup_timeout': '0s',
'templates': f'''
- condition:
equals.docker.container.name: {container.name}
config:
- type: log
paths:
- %s/${{data.docker.container.name}}.log
''' % self.working_dir,
},
},
},
)
)

with open(os.path.join(self.working_dir, 'busybox.log'), 'wb') as f:
f.write(b'Busybox output 1\n')

proc = self.start_beat()
docker_client.images.pull('busybox')
docker_client.containers.run('busybox', 'sleep 1')

self.wait_until(lambda: self.log_contains('Starting runner: input'))
self.wait_until(lambda: self.log_contains('Stopping runner: input'))

output = self.read_output_json()
proc.check_kill_and_wait()

# Check metadata is added
assert output[0]['message'] == 'Busybox output 1'
assert output[0]['container']['image']['name'] == 'busybox'
assert output[0]['docker']['container']['labels'] == {}
assert 'name' in output[0]['container']

self.assert_fields_are_documented(output[0])
self._test(container)

@unittest.skipIf(not INTEGRATION_TESTS or
os.getenv("TESTING_ENVIRONMENT") == "2x",
Expand All @@ -64,41 +44,47 @@ def test_default_settings(self):
"""
Test docker autodiscover default config settings
"""
import docker
docker_client = docker.from_env()

self.render_config_template(
inputs=False,
autodiscover={
'docker': {
'cleanup_timeout': '0s',
'hints.enabled': 'true',
'hints.default_config': '''
type: log
paths:
- %s/${data.container.image}.log
''' % self.working_dir,
with self.container_running() as container:
self.render_config_template(
inputs=False,
autodiscover={
'docker': {
'cleanup_timeout': '0s',
'hints.enabled': 'true',
'hints.default_config': '''
type: log
paths:
- %s/${data.container.name}.log
''' % self.working_dir,
},
},
},
)
)

self._test(container)

with open(os.path.join(self.working_dir, 'busybox.log'), 'wb') as f:
def _test(self, container):
with open(os.path.join(self.working_dir, f'{container.name}.log'), 'wb') as f:
f.write(b'Busybox output 1\n')

proc = self.start_beat()
docker_client.images.pull('busybox')
docker_client.containers.run('busybox', 'sleep 1')

self.wait_until(lambda: self.log_contains('Starting runner: input'))
self.wait_until(lambda: self.log_contains('Stopping runner: input'))
self.wait_until(lambda: self.output_has(lines=1))
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved

output = self.read_output_json()
proc.check_kill_and_wait()

# Check metadata is added
assert output[0]['message'] == 'Busybox output 1'
assert output[0]['container']['image']['name'] == 'busybox'
assert output[0]['docker']['container']['labels'] == {}
assert output[0]['container']['name'] == container.name
assert output[0]['docker']['container']['labels'] == container.labels
assert 'name' in output[0]['container']

self.assert_fields_are_documented(output[0])

@contextmanager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

def container_running(self, image_name='busybox:latest'):
import docker
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved
docker_client = docker.from_env()
container = docker_client.containers.run(image_name, 'sleep 60', detach=True, remove=True)
yield container
container.remove(force=True)