1
- import os
1
+ import docker
2
2
import filebeat
3
+ import os
3
4
import unittest
4
5
5
6
from beat .beat import INTEGRATION_TESTS
7
+ from contextlib import contextmanager
6
8
7
9
8
10
class TestAutodiscover (filebeat .BaseTest ):
@@ -16,89 +18,78 @@ def test_docker(self):
16
18
"""
17
19
Test docker autodiscover starts input
18
20
"""
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
+ },
35
36
},
36
- },
37
- )
37
+ )
38
38
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 )
45
41
46
- self .wait_until (lambda : self .log_contains ('Starting runner: input' ))
47
42
self .wait_until (lambda : self .log_contains ('Stopping runner: input' ))
48
-
49
- output = self .read_output_json ()
50
43
proc .check_kill_and_wait ()
51
44
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
-
60
45
@unittest .skipIf (not INTEGRATION_TESTS or
61
46
os .getenv ("TESTING_ENVIRONMENT" ) == "2x" ,
62
47
"integration test not available on 2.x" )
63
48
def test_default_settings (self ):
64
49
"""
65
50
Test docker autodiscover default config settings
66
51
"""
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
+ },
81
65
},
82
- },
83
- )
66
+ )
67
+ proc = self .start_beat ()
68
+ self ._test (container )
84
69
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 ( )
87
72
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 ' )
91
76
92
77
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 ))
94
79
95
80
output = self .read_output_json ()
96
- proc .check_kill_and_wait ()
97
81
98
82
# Check metadata is added
99
83
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
102
86
assert 'name' in output [0 ]['container' ]
103
87
104
88
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