Skip to content

Commit 3bc54d4

Browse files
authored
Fix autodiscover flaky tests (elastic#21242) (elastic#21287)
Use a tagged image instead of doing an untagged pull, this was pulling all busybox tags, busybox:latest image used now is pre-cached. Better control when the containers are started and stopped, run the container detached. Use container name instead of container image for conditions and templates, better ensuring that we are testing against the proper container. Move common code to helper function. (cherry picked from commit 0c8f82b)
1 parent 9549dac commit 3bc54d4

File tree

1 file changed

+52
-61
lines changed

1 file changed

+52
-61
lines changed
+52-61
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import os
1+
import docker
22
import filebeat
3+
import os
34
import unittest
45

56
from beat.beat import INTEGRATION_TESTS
7+
from contextlib import contextmanager
68

79

810
class TestAutodiscover(filebeat.BaseTest):
@@ -16,89 +18,78 @@ def test_docker(self):
1618
"""
1719
Test docker autodiscover starts input
1820
"""
19-
import docker
20-
docker_client = docker.from_env()
21-
22-
self.render_config_template(
23-
inputs=False,
24-
autodiscover={
25-
'docker': {
26-
'cleanup_timeout': '0s',
27-
'templates': '''
28-
- condition:
29-
equals.docker.container.image: busybox
30-
config:
31-
- type: log
32-
paths:
33-
- %s/${data.docker.container.image}.log
34-
''' % self.working_dir,
21+
with self.container_running() as container:
22+
self.render_config_template(
23+
inputs=False,
24+
autodiscover={
25+
'docker': {
26+
'cleanup_timeout': '0s',
27+
'templates': f'''
28+
- condition:
29+
equals.docker.container.name: {container.name}
30+
config:
31+
- type: log
32+
paths:
33+
- %s/${{data.docker.container.name}}.log
34+
''' % self.working_dir,
35+
},
3536
},
36-
},
37-
)
37+
)
3838

39-
with open(os.path.join(self.working_dir, 'busybox.log'), 'wb') as f:
40-
f.write(b'Busybox output 1\n')
41-
42-
proc = self.start_beat()
43-
docker_client.images.pull('busybox')
44-
docker_client.containers.run('busybox', 'sleep 1')
39+
proc = self.start_beat()
40+
self._test(container)
4541

46-
self.wait_until(lambda: self.log_contains('Starting runner: input'))
4742
self.wait_until(lambda: self.log_contains('Stopping runner: input'))
48-
49-
output = self.read_output_json()
5043
proc.check_kill_and_wait()
5144

52-
# Check metadata is added
53-
assert output[0]['message'] == 'Busybox output 1'
54-
assert output[0]['container']['image']['name'] == 'busybox'
55-
assert output[0]['docker']['container']['labels'] == {}
56-
assert 'name' in output[0]['container']
57-
58-
self.assert_fields_are_documented(output[0])
59-
6045
@unittest.skipIf(not INTEGRATION_TESTS or
6146
os.getenv("TESTING_ENVIRONMENT") == "2x",
6247
"integration test not available on 2.x")
6348
def test_default_settings(self):
6449
"""
6550
Test docker autodiscover default config settings
6651
"""
67-
import docker
68-
docker_client = docker.from_env()
69-
70-
self.render_config_template(
71-
inputs=False,
72-
autodiscover={
73-
'docker': {
74-
'cleanup_timeout': '0s',
75-
'hints.enabled': 'true',
76-
'hints.default_config': '''
77-
type: log
78-
paths:
79-
- %s/${data.container.image}.log
80-
''' % self.working_dir,
52+
with self.container_running() as container:
53+
self.render_config_template(
54+
inputs=False,
55+
autodiscover={
56+
'docker': {
57+
'cleanup_timeout': '0s',
58+
'hints.enabled': 'true',
59+
'hints.default_config': '''
60+
type: log
61+
paths:
62+
- %s/${data.container.name}.log
63+
''' % self.working_dir,
64+
},
8165
},
82-
},
83-
)
66+
)
67+
proc = self.start_beat()
68+
self._test(container)
8469

85-
with open(os.path.join(self.working_dir, 'busybox.log'), 'wb') as f:
86-
f.write(b'Busybox output 1\n')
70+
self.wait_until(lambda: self.log_contains('Stopping runner: input'))
71+
proc.check_kill_and_wait()
8772

88-
proc = self.start_beat()
89-
docker_client.images.pull('busybox')
90-
docker_client.containers.run('busybox', 'sleep 1')
73+
def _test(self, container):
74+
with open(os.path.join(self.working_dir, f'{container.name}.log'), 'wb') as f:
75+
f.write(b'Busybox output 1\n')
9176

9277
self.wait_until(lambda: self.log_contains('Starting runner: input'))
93-
self.wait_until(lambda: self.log_contains('Stopping runner: input'))
78+
self.wait_until(lambda: self.output_has(lines=1))
9479

9580
output = self.read_output_json()
96-
proc.check_kill_and_wait()
9781

9882
# Check metadata is added
9983
assert output[0]['message'] == 'Busybox output 1'
100-
assert output[0]['container']['image']['name'] == 'busybox'
101-
assert output[0]['docker']['container']['labels'] == {}
84+
assert output[0]['container']['name'] == container.name
85+
assert output[0]['docker']['container']['labels'] == container.labels
10286
assert 'name' in output[0]['container']
10387

10488
self.assert_fields_are_documented(output[0])
89+
90+
@contextmanager
91+
def container_running(self, image_name='busybox:latest'):
92+
docker_client = docker.from_env()
93+
container = docker_client.containers.run(image_name, 'sleep 60', detach=True, remove=True)
94+
yield container
95+
container.remove(force=True)

0 commit comments

Comments
 (0)