13
13
# limitations under the License.
14
14
15
15
import json
16
+
16
17
import pytest
18
+ from testing_support .fixtures import override_generic_settings
19
+ from testing_support .validators .validate_serverless_data import validate_serverless_data
20
+ from testing_support .validators .validate_serverless_metadata import (
21
+ validate_serverless_metadata ,
22
+ )
23
+ from testing_support .validators .validate_serverless_payload import (
24
+ validate_serverless_payload ,
25
+ )
17
26
18
27
from newrelic .api .application import application_instance
19
28
from newrelic .api .background_task import background_task
22
31
from newrelic .api .transaction import current_transaction
23
32
from newrelic .core .config import global_settings
24
33
25
- from testing_support .fixtures import override_generic_settings
26
- from testing_support .validators .validate_serverless_data import (
27
- validate_serverless_data )
28
- from testing_support .validators .validate_serverless_payload import (
29
- validate_serverless_payload )
30
- from testing_support .validators .validate_serverless_metadata import (
31
- validate_serverless_metadata )
32
34
33
-
34
- @pytest .fixture (scope = 'function' )
35
+ @pytest .fixture (scope = "function" )
35
36
def serverless_application (request ):
36
37
settings = global_settings ()
37
38
orig = settings .serverless_mode .enabled
38
39
settings .serverless_mode .enabled = True
39
40
40
- application_name = 'Python Agent Test (test_serverless_mode:%s)' % (
41
- request .node .name )
41
+ application_name = "Python Agent Test (test_serverless_mode:%s)" % (request .node .name )
42
42
application = application_instance (application_name )
43
43
application .activate ()
44
44
@@ -48,17 +48,18 @@ def serverless_application(request):
48
48
49
49
50
50
def test_serverless_payload (capsys , serverless_application ):
51
-
52
- @override_generic_settings (serverless_application .settings , {
53
- 'distributed_tracing.enabled' : True ,
54
- })
51
+ @override_generic_settings (
52
+ serverless_application .settings ,
53
+ {
54
+ "distributed_tracing.enabled" : True ,
55
+ },
56
+ )
55
57
@validate_serverless_data (
56
- expected_methods = ('metric_data' , 'analytic_event_data' ),
57
- forgone_methods = ('preconnect' , 'connect' , 'get_agent_commands' ))
58
+ expected_methods = ("metric_data" , "analytic_event_data" ),
59
+ forgone_methods = ("preconnect" , "connect" , "get_agent_commands" ),
60
+ )
58
61
@validate_serverless_payload ()
59
- @background_task (
60
- application = serverless_application ,
61
- name = 'test_serverless_payload' )
62
+ @background_task (application = serverless_application , name = "test_serverless_payload" )
62
63
def _test ():
63
64
transaction = current_transaction ()
64
65
assert transaction .settings .serverless_mode .enabled
@@ -75,17 +76,15 @@ def _test():
75
76
76
77
77
78
def test_no_cat_headers (serverless_application ):
78
- @background_task (
79
- application = serverless_application ,
80
- name = 'test_cat_headers' )
79
+ @background_task (application = serverless_application , name = "test_cat_headers" )
81
80
def _test_cat_headers ():
82
81
transaction = current_transaction ()
83
82
84
83
payload = ExternalTrace .generate_request_headers (transaction )
85
84
assert not payload
86
85
87
- trace = ExternalTrace (' testlib' , ' http://example.com' )
88
- response_headers = [(' X-NewRelic-App-Data' , ' Cookies' )]
86
+ trace = ExternalTrace (" testlib" , " http://example.com" )
87
+ response_headers = [(" X-NewRelic-App-Data" , " Cookies" )]
89
88
with trace :
90
89
trace .process_response_headers (response_headers )
91
90
@@ -94,61 +93,66 @@ def _test_cat_headers():
94
93
_test_cat_headers ()
95
94
96
95
97
- def test_dt_outbound (serverless_application ):
98
- @override_generic_settings (serverless_application .settings , {
99
- 'distributed_tracing.enabled' : True ,
100
- 'account_id' : '1' ,
101
- 'trusted_account_key' : '1' ,
102
- 'primary_application_id' : '1' ,
103
- })
104
- @background_task (
105
- application = serverless_application ,
106
- name = 'test_dt_outbound' )
107
- def _test_dt_outbound ():
96
+ @pytest .mark .parametrize ("trusted_account_key" , ("1" , None ), ids = ("tk_set" , "tk_unset" ))
97
+ def test_outbound_dt_payload_generation (serverless_application , trusted_account_key ):
98
+ @override_generic_settings (
99
+ serverless_application .settings ,
100
+ {
101
+ "distributed_tracing.enabled" : True ,
102
+ "account_id" : "1" ,
103
+ "trusted_account_key" : trusted_account_key ,
104
+ "primary_application_id" : "1" ,
105
+ },
106
+ )
107
+ @background_task (application = serverless_application , name = "test_outbound_dt_payload_generation" )
108
+ def _test_outbound_dt_payload_generation ():
108
109
transaction = current_transaction ()
109
110
payload = ExternalTrace .generate_request_headers (transaction )
110
111
assert payload
111
-
112
- _test_dt_outbound ()
113
-
114
-
115
- def test_dt_inbound (serverless_application ):
116
- @override_generic_settings (serverless_application .settings , {
117
- 'distributed_tracing.enabled' : True ,
118
- 'account_id' : '1' ,
119
- 'trusted_account_key' : '1' ,
120
- 'primary_application_id' : '1' ,
121
- })
122
- @background_task (
123
- application = serverless_application ,
124
- name = 'test_dt_inbound' )
125
- def _test_dt_inbound ():
112
+ # Ensure trusted account key or account ID present as vendor
113
+ assert dict (payload )["tracestate" ].startswith ("1@nr=" )
114
+
115
+ _test_outbound_dt_payload_generation ()
116
+
117
+
118
+ @pytest .mark .parametrize ("trusted_account_key" , ("1" , None ), ids = ("tk_set" , "tk_unset" ))
119
+ def test_inbound_dt_payload_acceptance (serverless_application , trusted_account_key ):
120
+ @override_generic_settings (
121
+ serverless_application .settings ,
122
+ {
123
+ "distributed_tracing.enabled" : True ,
124
+ "account_id" : "1" ,
125
+ "trusted_account_key" : trusted_account_key ,
126
+ "primary_application_id" : "1" ,
127
+ },
128
+ )
129
+ @background_task (application = serverless_application , name = "test_inbound_dt_payload_acceptance" )
130
+ def _test_inbound_dt_payload_acceptance ():
126
131
transaction = current_transaction ()
127
132
128
133
payload = {
129
- 'v' : [0 , 1 ],
130
- 'd' : {
131
- 'ty' : ' Mobile' ,
132
- 'ac' : '1' ,
133
- 'tk' : '1' ,
134
- 'ap' : ' 2827902' ,
135
- 'pa' : ' 5e5733a911cfbc73' ,
136
- 'id' : ' 7d3efb1b173fecfa' ,
137
- 'tr' : ' d6b4ba0c3a712ca' ,
138
- 'ti' : 1518469636035 ,
139
- 'tx' : ' 8703ff3d88eefe9d' ,
140
- }
134
+ "v" : [0 , 1 ],
135
+ "d" : {
136
+ "ty" : " Mobile" ,
137
+ "ac" : "1" ,
138
+ "tk" : "1" ,
139
+ "ap" : " 2827902" ,
140
+ "pa" : " 5e5733a911cfbc73" ,
141
+ "id" : " 7d3efb1b173fecfa" ,
142
+ "tr" : " d6b4ba0c3a712ca" ,
143
+ "ti" : 1518469636035 ,
144
+ "tx" : " 8703ff3d88eefe9d" ,
145
+ },
141
146
}
142
147
143
148
result = transaction .accept_distributed_trace_payload (payload )
144
149
assert result
145
150
146
- _test_dt_inbound ()
151
+ _test_inbound_dt_payload_acceptance ()
147
152
148
153
149
- @pytest .mark .parametrize (' arn_set' , (True , False ))
154
+ @pytest .mark .parametrize (" arn_set" , (True , False ))
150
155
def test_payload_metadata_arn (serverless_application , arn_set ):
151
-
152
156
# If the session object gathers the arn from the settings object before the
153
157
# lambda handler records it there, then this test will fail.
154
158
@@ -157,17 +161,17 @@ def test_payload_metadata_arn(serverless_application, arn_set):
157
161
158
162
arn = None
159
163
if arn_set :
160
- arn = ' arrrrrrrrrrRrrrrrrrn'
164
+ arn = " arrrrrrrrrrRrrrrrrrn"
161
165
162
- settings .aws_lambda_metadata .update ({' arn' : arn , ' function_version' : ' $LATEST' })
166
+ settings .aws_lambda_metadata .update ({" arn" : arn , " function_version" : " $LATEST" })
163
167
164
168
class Context (object ):
165
169
invoked_function_arn = arn
166
170
167
- @validate_serverless_metadata (exact_metadata = {' arn' : arn })
171
+ @validate_serverless_metadata (exact_metadata = {" arn" : arn })
168
172
@lambda_handler (application = serverless_application )
169
173
def handler (event , context ):
170
- assert settings .aws_lambda_metadata [' arn' ] == arn
174
+ assert settings .aws_lambda_metadata [" arn" ] == arn
171
175
return {}
172
176
173
177
try :
0 commit comments