@@ -60,30 +60,42 @@ def client_read(path):
6060 return TestServiceDiscovery .mock_tpls .get (image )[0 ][config_parts .index (config_part )]
6161
6262
63+ def issue_read (identifier ):
64+ return TestServiceDiscovery .mock_tpls .get (identifier )
65+
66+
6367class TestServiceDiscovery (unittest .TestCase ):
6468 docker_container_inspect = {
6569 u'Id' : u'69ff25598b2314d1cdb7752cc3a659fb1c1352b32546af4f1454321550e842c0' ,
66- u'Image' : u'6ffc02088cb870652eca9ccd4c4fb582f75b29af2879792ed09bb46fd1c898ef ' ,
70+ u'Image' : u'nginx ' ,
6771 u'Name' : u'/nginx' ,
6872 u'NetworkSettings' : {u'IPAddress' : u'172.17.0.21' , u'Ports' : {u'443/tcp' : None , u'80/tcp' : None }}
6973 }
74+ docker_container_inspect_with_label = {
75+ u'Id' : u'69ff25598b2314d1cdb7752cc3a659fb1c1352b32546af4f1454321550e842c0' ,
76+ u'Image' : u'nginx' ,
77+ u'Name' : u'/nginx' ,
78+ u'NetworkSettings' : {u'IPAddress' : u'172.17.0.21' , u'Ports' : {u'443/tcp' : None , u'80/tcp' : None }},
79+ u'Labels' : {'com.datadoghq.sd.check.id' : 'custom-nginx' }
80+ }
7081 kubernetes_container_inspect = {
7182 u'Id' : u'389dc8a4361f3d6c866e9e9a7b6972b26a31c589c4e2f097375d55656a070bc9' ,
72- u'Image' : u'de309495e6c7b2071bc60c0b7e4405b0d65e33e3a4b732ad77615d90452dd827 ' ,
83+ u'Image' : u'foo ' ,
7384 u'Name' : u'/k8s_sentinel.38057ab9_redis-master_default_27b84e1e-a81c-11e5-8347-42010af00002_f70875a1' ,
7485 u'Config' : {u'ExposedPorts' : {u'6379/tcp' : {}}},
7586 u'NetworkSettings' : {u'IPAddress' : u'' , u'Ports' : None }
7687 }
7788 malformed_container_inspect = {
7889 u'Id' : u'69ff25598b2314d1cdb7752cc3a659fb1c1352b32546af4f1454321550e842c0' ,
79- u'Image' : u'6ffc02088cb870652eca9ccd4c4fb582f75b29af2879792ed09bb46fd1c898ef ' ,
90+ u'Image' : u'foo ' ,
8091 u'Name' : u'/nginx'
8192 }
8293 container_inspects = [
83- # (inspect_dict, expected_ip, expected_port)
84- (docker_container_inspect , '172.17.0.21' , 'port' , '443' ),
85- (kubernetes_container_inspect , None , 'port' , '6379' ), # arbitrarily defined in the mocked pod_list
86- (malformed_container_inspect , None , 'port' , KeyError )
94+ # (inspect_dict, expected_ip, expected_port, expected_ident)
95+ (docker_container_inspect , '172.17.0.21' , ['80' , '443' ], 'nginx' ),
96+ (docker_container_inspect_with_label , '172.17.0.21' , ['80' , '443' ], 'custom-nginx' ),
97+ (kubernetes_container_inspect , None , ['6379' ], 'foo' ), # arbitrarily defined in the mocked pod_list
98+ (malformed_container_inspect , None , KeyError , 'foo' )
8799 ]
88100
89101 # templates with variables already extracted
@@ -114,7 +126,12 @@ class TestServiceDiscovery(unittest.TestCase):
114126 [('check_2' , {}, {"host" : "%%host%%" , "port" : "%%port%%" })]),
115127 'bad_image_0' : ((['invalid template' ]), []),
116128 'bad_image_1' : (('invalid template' ), []),
117- 'bad_image_2' : (None , [])
129+ 'bad_image_2' : (None , []),
130+ 'nginx' : ('["nginx"]' , '[{}]' , '[{"host": "localhost"}]' ),
131+ 'nginx:latest' : ('["nginx"]' , '[{}]' , '[{"host": "localhost", "tags": ["foo"]}]' ),
132+ 'custom-nginx' : ('["nginx"]' , '[{}]' , '[{"host": "localhost"}]' ),
133+ 'repo/custom-nginx' : ('["nginx"]' , '[{}]' , '[{"host": "localhost", "tags": ["bar"]}]' ),
134+ 'repo/dir:5000/custom-nginx:latest' : ('["nginx"]' , '[{}]' , '[{"host": "local", "tags": ["foobar"]}]' )
118135 }
119136
120137 bad_mock_templates = {
@@ -207,22 +224,22 @@ def test_get_host_address(self, mock_check_yaml, mock_get):
207224 mock_check_yaml .return_value = kubernetes_config
208225 mock_get .return_value = Response (pod_list )
209226
210- for c_ins , tpl_var , expected_ip in ip_address_inspects :
227+ for c_ins , expected_ip , _ , _ in self . container_inspects :
211228 with mock .patch .object (AbstractConfigStore , '__init__' , return_value = None ):
212229 with mock .patch ('utils.dockerutil.DockerUtil.client' , return_value = None ):
213230 with mock .patch ('utils.kubeutil.get_conf_path' , return_value = None ):
214231 sd_backend = get_sd_backend (agentConfig = self .auto_conf_agentConfig )
215- self .assertEquals (sd_backend ._get_host_address (c_ins , tpl_var ), expected_ip )
232+ self .assertEqual (sd_backend ._get_host (c_ins ), expected_ip )
216233 clear_singletons (self .auto_conf_agentConfig )
217234
218235 def test_get_port (self ):
219236 with mock .patch ('utils.dockerutil.DockerUtil.client' , return_value = None ):
220- for c_ins , _ , var_tpl , expected_ports in self .container_inspects :
237+ for c_ins , _ , expected_ports , _ in self .container_inspects :
221238 sd_backend = get_sd_backend (agentConfig = self .auto_conf_agentConfig )
222- if isinstance (expected_ports , str ):
223- self .assertEquals (sd_backend ._get_port (c_ins , var_tpl ), expected_ports )
239+ if isinstance (expected_ports , list ):
240+ self .assertEqual (sd_backend ._get_ports (c_ins ), expected_ports )
224241 else :
225- self .assertRaises (expected_ports , sd_backend ._get_port , c_ins , var_tpl )
242+ self .assertRaises (expected_ports , sd_backend ._get_ports , c_ins )
226243 clear_singletons (self .auto_conf_agentConfig )
227244
228245 @mock .patch ('docker.Client.inspect_container' , side_effect = _get_container_inspect )
@@ -498,3 +515,29 @@ def test_get_check_tpls(self, mock_client_read):
498515 for image in invalid_config :
499516 tpl = self .mock_tpls .get (image )[1 ]
500517 self .assertEquals (tpl , config_store .get_check_tpls (image ))
518+
519+ def test_get_config_id (self ):
520+ """Test get_config_id"""
521+ with mock .patch ('utils.dockerutil.DockerUtil.client' , return_value = None ):
522+ for c_ins , _ , _ , expected_ident in self .container_inspects :
523+ sd_backend = get_sd_backend (agentConfig = self .auto_conf_agentConfig )
524+ self .assertEqual (
525+ sd_backend .get_config_id (c_ins .get ('Image' ), c_ins .get ('Labels' , {})),
526+ expected_ident )
527+ clear_singletons (self .auto_conf_agentConfig )
528+
529+ @mock .patch .object (AbstractConfigStore , '_issue_read' , side_effect = issue_read )
530+ def test_read_config_from_store (self , issue_read ):
531+ """Test read_config_from_store"""
532+ valid_idents = [('nginx' , 'nginx' ), ('nginx:latest' , 'nginx:latest' ),
533+ ('custom-nginx' , 'custom-nginx' ), ('custom-nginx:latest' , 'custom-nginx' ),
534+ ('repo/custom-nginx:latest' , 'custom-nginx' ),
535+ ('repo/dir:5000/custom-nginx:latest' , 'repo/dir:5000/custom-nginx:latest' )]
536+ invalid_idents = ['foo' ]
537+ config_store = get_config_store (self .auto_conf_agentConfig )
538+ for ident , expected_key in valid_idents :
539+ tpl = config_store .read_config_from_store (ident )
540+ # source is added after reading from the store
541+ self .assertEquals (tpl , ('template' ,) + self .mock_tpls .get (expected_key ))
542+ for ident in invalid_idents :
543+ self .assertEquals (config_store .read_config_from_store (ident ), [])
0 commit comments