1010from utils .service_discovery .config_stores import get_config_store
1111from utils .service_discovery .consul_config_store import ConsulStore
1212from utils .service_discovery .etcd_config_store import EtcdStore
13- from utils .service_discovery .abstract_config_store import AbstractConfigStore
13+ from utils .service_discovery .abstract_config_store import AbstractConfigStore , CONFIG_FROM_KUBE
1414from utils .service_discovery .sd_backend import get_sd_backend
1515from utils .service_discovery .sd_docker_backend import SDDockerBackend
1616
@@ -40,14 +40,16 @@ def _get_container_inspect(c_id):
4040 return None
4141
4242
43- def _get_conf_tpls (image_name , trace_config = False , kube_annotations = None ):
43+ def _get_conf_tpls (image_name , kube_annotations = None , kube_pod_name = None ):
4444 """Return a mocked configuration template from self.mock_templates."""
45- return copy .deepcopy (TestServiceDiscovery .mock_templates .get (image_name )[0 ])
45+ return [(x , image_name + ':0' , y ) for x , y in
46+ copy .deepcopy (TestServiceDiscovery .mock_templates .get (image_name )[0 ])]
4647
4748
4849def _get_check_tpls (image_name , ** kwargs ):
4950 if image_name in TestServiceDiscovery .mock_templates :
50- return [copy .deepcopy (TestServiceDiscovery .mock_templates .get (image_name )[0 ][0 ][0 :3 ])]
51+ result = copy .deepcopy (TestServiceDiscovery .mock_templates .get (image_name )[0 ][0 ])
52+ return [(result [0 ], image_name + ':0' , result [1 ][0 :3 ])]
5153 elif image_name in TestServiceDiscovery .bad_mock_templates :
5254 try :
5355 return [copy .deepcopy (TestServiceDiscovery .bad_mock_templates .get (image_name ))]
@@ -107,30 +109,30 @@ class TestServiceDiscovery(unittest.TestCase):
107109
108110 # templates with variables already extracted
109111 mock_templates = {
110- # image_name: ([(check_name, init_tpl, instance_tpl, variables)], (expected_config_template))
112+ # image_name: ([(source, ( check_name, init_tpl, instance_tpl, variables) )], (expected_config_template))
111113 'image_0' : (
112- [('check_0' , {}, {'host' : '%%host%%' }, ['host' ])],
113- ('check_0' , {}, {'host' : '127.0.0.1' })),
114+ [('template' , ( ' check_0' , {}, {'host' : '%%host%%' }, ['host' ]) )],
115+ ('template' , 'image_0:0' , ( ' check_0' , {}, {'host' : '127.0.0.1' }) )),
114116 'image_1' : (
115- [('check_1' , {}, {'port' : '%%port%%' }, ['port' ])],
116- ('check_1' , {}, {'port' : '1337' })),
117+ [('template' , ( ' check_1' , {}, {'port' : '%%port%%' }, ['port' ]) )],
118+ ('template' , 'image_1:0' , ( ' check_1' , {}, {'port' : '1337' }) )),
117119 'image_2' : (
118- [('check_2' , {}, {'host' : '%%host%%' , 'port' : '%%port%%' }, ['host' , 'port' ])],
119- ('check_2' , {}, {'host' : '127.0.0.1' , 'port' : '1337' })),
120+ [('template' , ( ' check_2' , {}, {'host' : '%%host%%' , 'port' : '%%port%%' }, ['host' , 'port' ]) )],
121+ ('template' , 'image_2:0' , ( ' check_2' , {}, {'host' : '127.0.0.1' , 'port' : '1337' }) )),
120122 }
121123
122124 # raw templates coming straight from the config store
123125 mock_tpls = {
124126 # image_name: ('[check_name]', '[init_tpl]', '[instance_tpl]', expected_python_tpl_list)
125127 'image_0' : (
126128 ('["check_0"]' , '[{}]' , '[{"host": "%%host%%"}]' ),
127- [('check_0' , {}, {"host" : "%%host%%" })]),
129+ [('template' , 'image_0:0' , ( ' check_0' , {}, {"host" : "%%host%%" }) )]),
128130 'image_1' : (
129131 ('["check_1"]' , '[{}]' , '[{"port": "%%port%%"}]' ),
130- [('check_1' , {}, {"port" : "%%port%%" })]),
132+ [('template' , 'image_1:0' , ( ' check_1' , {}, {"port" : "%%port%%" }) )]),
131133 'image_2' : (
132134 ('["check_2"]' , '[{}]' , '[{"host": "%%host%%", "port": "%%port%%"}]' ),
133- [('check_2' , {}, {"host" : "%%host%%" , "port" : "%%port%%" })]),
135+ [('template' , 'image_2:0' , ( ' check_2' , {}, {"host" : "%%host%%" , "port" : "%%port%%" }) )]),
134136 'bad_image_0' : ((['invalid template' ]), []),
135137 'bad_image_1' : (('invalid template' ), []),
136138 'bad_image_2' : (None , []),
@@ -249,38 +251,39 @@ def test_get_port(self):
249251 self .assertRaises (expected_ports , sd_backend ._get_port , c_ins , var_tpl )
250252 clear_singletons (self .auto_conf_agentConfig )
251253
254+ @mock .patch ('utils.dockerutil.DockerUtil.client' , return_value = None )
255+ @mock .patch .object (SDDockerBackend , '_get_host_address' , return_value = '127.0.0.1' )
256+ @mock .patch .object (SDDockerBackend , '_get_port' , return_value = '1337' )
252257 @mock .patch ('docker.Client.inspect_container' , side_effect = _get_container_inspect )
253258 @mock .patch .object (SDDockerBackend , '_get_config_templates' , side_effect = _get_conf_tpls )
254- def test_get_check_configs (self , mock_inspect_container , mock_get_conf_tpls ):
259+ def test_get_check_configs (self , * args ):
255260 """Test get_check_config with mocked container inspect and config template"""
256- with mock .patch ('utils.dockerutil.DockerUtil.client' , return_value = None ):
257- with mock .patch .object (SDDockerBackend , '_get_host_address' , return_value = '127.0.0.1' ):
258- with mock .patch .object (SDDockerBackend , '_get_port' , return_value = '1337' ):
259- c_id = self .docker_container_inspect .get ('Id' )
260- for image in self .mock_templates .keys ():
261- sd_backend = get_sd_backend (agentConfig = self .auto_conf_agentConfig )
262- self .assertEquals (
263- sd_backend ._get_check_configs (c_id , image )[0 ],
264- self .mock_templates [image ][1 ])
265- clear_singletons (self .auto_conf_agentConfig )
261+ c_id = self .docker_container_inspect .get ('Id' )
262+ for image in self .mock_templates .keys ():
263+ sd_backend = get_sd_backend (agentConfig = self .auto_conf_agentConfig )
264+ self .assertEquals (
265+ sd_backend ._get_check_configs (c_id , image )[0 ],
266+ self .mock_templates [image ][1 ])
267+ clear_singletons (self .auto_conf_agentConfig )
266268
269+ @mock .patch ('utils.dockerutil.DockerUtil.client' , return_value = None )
270+ @mock .patch .object (ConsulStore , 'get_client' , return_value = None )
271+ @mock .patch .object (EtcdStore , 'get_client' , return_value = None )
267272 @mock .patch .object (AbstractConfigStore , 'get_check_tpls' , side_effect = _get_check_tpls )
268- def test_get_config_templates (self , mock_get_check_tpls ):
273+ def test_get_config_templates (self , * args ):
269274 """Test _get_config_templates with mocked get_check_tpls"""
270- with mock .patch ('utils.dockerutil.DockerUtil.client' , return_value = None ):
271- with mock .patch .object (EtcdStore , 'get_client' , return_value = None ):
272- with mock .patch .object (ConsulStore , 'get_client' , return_value = None ):
273- for agentConfig in self .agentConfigs :
274- sd_backend = get_sd_backend (agentConfig = agentConfig )
275- # normal cases
276- for image in self .mock_templates .keys ():
277- template = sd_backend ._get_config_templates (image )
278- expected_template = self .mock_templates .get (image )[0 ]
279- self .assertEquals (template , expected_template )
280- # error cases
281- for image in self .bad_mock_templates .keys ():
282- self .assertEquals (sd_backend ._get_config_templates (image ), None )
283- clear_singletons (agentConfig )
275+ for agentConfig in self .agentConfigs :
276+ sd_backend = get_sd_backend (agentConfig = agentConfig )
277+ # normal cases
278+ for image in self .mock_templates .keys ():
279+ template = sd_backend ._get_config_templates (image )
280+ expected_template = self .mock_templates .get (image )[0 ]
281+ expected_template = [(t [0 ], image + ':0' , t [1 ]) for t in expected_template ]
282+ self .assertEquals (template , expected_template )
283+ # error cases
284+ for image in self .bad_mock_templates .keys ():
285+ self .assertEquals (sd_backend ._get_config_templates (image ), None )
286+ clear_singletons (agentConfig )
284287
285288 def test_render_template (self ):
286289 """Test _render_template"""
@@ -526,12 +529,13 @@ def test_get_check_tpls(self, mock_client_read):
526529
527530 @mock .patch .object (AbstractConfigStore , 'client_read' , side_effect = client_read )
528531 def test_get_check_tpls_kube (self , mock_client_read ):
529- """Test get_check_tpls"""
532+ """Test get_check_tpls for kubernetes annotations """
530533 valid_config = ['image_0' , 'image_1' , 'image_2' ]
531534 invalid_config = ['bad_image_0' ]
532535 config_store = get_config_store (self .auto_conf_agentConfig )
533536 for image in valid_config + invalid_config :
534537 tpl = self .mock_tpls .get (image )[1 ]
538+ tpl = [(CONFIG_FROM_KUBE , t [1 ], t [2 ]) for t in tpl ]
535539 if tpl :
536540 self .assertNotEquals (
537541 tpl ,
@@ -540,6 +544,7 @@ def test_get_check_tpls_kube(self, mock_client_read):
540544 tpl ,
541545 config_store .get_check_tpls (
542546 'k8s-' + image , auto_conf = True ,
547+ kube_pod_name = image ,
543548 kube_annotations = dict (zip (
544549 ['com.datadoghq.sd/check_names' ,
545550 'com.datadoghq.sd/init_configs' ,
0 commit comments