11import mock
22import pytest
33
4- from ddtrace .contrib .grpc .utils import _parse_target_from_args
5- from ddtrace .contrib .grpc .utils import parse_method_path
4+ from ddtrace .contrib .grpc import utils
65
76
87def test_parse_method_path_with_package ():
98 method_path = "/package.service/method"
10- parsed = parse_method_path (method_path )
9+ parsed = utils . parse_method_path (method_path )
1110 assert parsed == ("package" , "service" , "method" )
1211
1312
1413def test_parse_method_path_without_package ():
1514 method_path = "/service/method"
16- parsed = parse_method_path (method_path )
15+ parsed = utils . parse_method_path (method_path )
1716 assert parsed == (None , "service" , "method" )
1817
1918
@@ -23,19 +22,72 @@ def test_parse_method_path_without_package():
2322 [
2423 (("localhost:1234" ,), dict (), ("localhost" , 1234 ), None ),
2524 (("localhost" ,), dict (), ("localhost" , None ), None ),
25+ ((":1234" ,), dict (), (None , 1234 ), None ),
2626 (("[::]:1234" ,), dict (), ("::" , 1234 ), None ),
2727 (("[::]" ,), dict (), ("::" , None ), None ),
2828 (None , dict (target = "localhost:1234" ), ("localhost" , 1234 ), None ),
2929 (None , dict (target = "localhost" ), ("localhost" , None ), None ),
30+ (None , dict (target = ":1234" ), (None , 1234 ), None ),
3031 (None , dict (target = "[::]:1234" ), ("::" , 1234 ), None ),
3132 (None , dict (target = "[::]" ), ("::" , None ), None ),
3233 (("localhost:foo" ,), dict (), ("localhost" , None ), ("Non-integer port in target '%s'" , "localhost:foo" )),
3334 (("" ,), dict (), (None , None ), None ),
3435 ],
3536)
3637def test_parse_target_from_args (mock_log , args , kwargs , result , log_warning_call ):
37- assert _parse_target_from_args (args , kwargs ) == result
38+ assert utils . _parse_target_from_args (args , kwargs ) == result
3839 if log_warning_call :
3940 mock_log .warning .assert_called_once_with (* log_warning_call )
4041 else :
4142 mock_log .warning .assert_not_called ()
43+
44+
45+ @pytest .mark .parametrize (
46+ "host, port, calls" ,
47+ [
48+ (
49+ "localhost" ,
50+ 1234 ,
51+ [mock .call ("grpc.host" , "localhost" ), mock .call ("grpc.port" , "1234" ), mock .call ("span.kind" , "client" )],
52+ ),
53+ ("localhost" , None , [mock .call ("grpc.host" , "localhost" ), mock .call ("span.kind" , "client" )]),
54+ (None , 1234 , [mock .call ("grpc.port" , "1234" ), mock .call ("span.kind" , "client" )]),
55+ (None , None , [mock .call ("span.kind" , "client" )]),
56+ ],
57+ )
58+ def test_set_grpc_client_meta (host , port , calls ):
59+ span = mock .MagicMock ()
60+ utils .set_grpc_client_meta (span , host , port )
61+ span ._set_str_tag .assert_has_calls (calls )
62+
63+
64+ @pytest .mark .parametrize (
65+ "method, method_kind, calls" ,
66+ [
67+ (
68+ "/package.service/method" ,
69+ "unary" ,
70+ [
71+ mock .call ("grpc.method.path" , "/package.service/method" ),
72+ mock .call ("grpc.method.package" , "package" ),
73+ mock .call ("grpc.method.service" , "service" ),
74+ mock .call ("grpc.method.name" , "method" ),
75+ mock .call ("grpc.method.kind" , "unary" ),
76+ ],
77+ ),
78+ (
79+ "/service/method" ,
80+ "unary" ,
81+ [
82+ mock .call ("grpc.method.path" , "/service/method" ),
83+ mock .call ("grpc.method.service" , "service" ),
84+ mock .call ("grpc.method.name" , "method" ),
85+ mock .call ("grpc.method.kind" , "unary" ),
86+ ],
87+ ),
88+ ],
89+ )
90+ def test_set_grpc_method_meta (method , method_kind , calls ):
91+ span = mock .MagicMock ()
92+ utils .set_grpc_method_meta (span , method , method_kind )
93+ span ._set_str_tag .assert_has_calls (calls )
0 commit comments