3
3
4
4
#![ cfg( unix) ]
5
5
6
+ use std:: collections:: HashMap ;
7
+ use std:: io:: { Read , Write } ;
6
8
use std:: path:: Path ;
7
9
use std:: process;
8
10
use std:: { fs, path:: PathBuf } ;
@@ -23,32 +25,15 @@ fn test_crash_tracking_bin_release() {
23
25
}
24
26
25
27
fn test_crash_tracking_bin ( crash_tracking_receiver_profile : BuildProfile ) {
26
- let crashtracker_bin = ArtifactsBuild {
27
- name : "crashtracker_bin_test" . to_owned ( ) ,
28
- build_profile : crash_tracking_receiver_profile,
29
- artifact_type : ArtifactType :: Bin ,
30
- triple_target : None ,
31
- } ;
32
- let crashtracker_receiver = ArtifactsBuild {
33
- name : "crashtracker_receiver" . to_owned ( ) ,
34
- build_profile : crash_tracking_receiver_profile,
35
- artifact_type : ArtifactType :: Bin ,
36
- triple_target : None ,
37
- } ;
38
- let artifacts = build_artifacts ( & [ & crashtracker_receiver, & crashtracker_bin] ) . unwrap ( ) ;
39
-
40
- let tmpdir = tempfile:: TempDir :: new ( ) . unwrap ( ) ;
41
-
42
- let crash_profile_path = extend_path ( tmpdir. path ( ) , "crash" ) ;
43
- let crash_telemetry_path = extend_path ( tmpdir. path ( ) , "crash.telemetry" ) ;
44
- let stdout_path = extend_path ( tmpdir. path ( ) , "out.stdout" ) ;
45
- let stderr_path = extend_path ( tmpdir. path ( ) , "out.stderr" ) ;
28
+ let ( crashtracker_bin, crashtracker_receiver) =
29
+ setup_crashtracking_crates ( crash_tracking_receiver_profile) ;
30
+ let fixtures = setup_test_fixtures ( & [ & crashtracker_receiver, & crashtracker_bin] ) ;
46
31
47
- let mut p = process:: Command :: new ( & artifacts[ & crashtracker_bin] )
48
- . arg ( & crash_profile_path)
49
- . arg ( artifacts[ & crashtracker_receiver] . as_os_str ( ) )
50
- . arg ( & stderr_path)
51
- . arg ( & stdout_path)
32
+ let mut p = process:: Command :: new ( & fixtures . artifacts [ & crashtracker_bin] )
33
+ . arg ( format ! ( "file://{}" , fixtures . crash_profile_path. display ( ) ) )
34
+ . arg ( fixtures . artifacts [ & crashtracker_receiver] . as_os_str ( ) )
35
+ . arg ( & fixtures . stderr_path )
36
+ . arg ( & fixtures . stdout_path )
52
37
. spawn ( )
53
38
. unwrap ( ) ;
54
39
let exit_status = bin_tests:: timeit!( "exit after signal" , {
@@ -61,10 +46,10 @@ fn test_crash_tracking_bin(crash_tracking_receiver_profile: BuildProfile) {
61
46
// running before the receiver has a chance to send the report.
62
47
std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
63
48
64
- let stderr = fs:: read ( stderr_path)
49
+ let stderr = fs:: read ( fixtures . stderr_path )
65
50
. context ( "reading crashtracker stderr" )
66
51
. unwrap ( ) ;
67
- let stdout = fs:: read ( stdout_path)
52
+ let stdout = fs:: read ( fixtures . stdout_path )
68
53
. context ( "reading crashtracker stdout" )
69
54
. unwrap ( ) ;
70
55
assert ! ( matches!(
@@ -74,7 +59,7 @@ fn test_crash_tracking_bin(crash_tracking_receiver_profile: BuildProfile) {
74
59
assert_eq ! ( Ok ( "" ) , String :: from_utf8( stdout) . as_deref( ) ) ;
75
60
76
61
// Check the crash data
77
- let crash_profile = fs:: read ( crash_profile_path)
62
+ let crash_profile = fs:: read ( fixtures . crash_profile_path )
78
63
. context ( "reading crashtracker profiling payload" )
79
64
. unwrap ( ) ;
80
65
let crash_payload = serde_json:: from_slice :: < serde_json:: Value > ( & crash_profile)
@@ -97,12 +82,17 @@ fn test_crash_tracking_bin(crash_tracking_receiver_profile: BuildProfile) {
97
82
crash_payload[ "siginfo" ]
98
83
) ;
99
84
100
- let crash_telemetry = fs:: read ( crash_telemetry_path)
85
+ let crash_telemetry = fs:: read ( fixtures . crash_telemetry_path )
101
86
. context ( "reading crashtracker telemetry payload" )
102
87
. unwrap ( ) ;
103
- let telemetry_payload = serde_json:: from_slice :: < serde_json:: Value > ( & crash_telemetry)
104
- . context ( "deserializing crashtracker telemetry payload to json" )
105
- . unwrap ( ) ;
88
+ assert_telemetry_message ( & crash_telemetry) ;
89
+ }
90
+
91
+ fn assert_telemetry_message ( crash_telemetry : & [ u8 ] ) {
92
+ let telemetry_payload: serde_json:: Value =
93
+ serde_json:: from_slice :: < serde_json:: Value > ( crash_telemetry)
94
+ . context ( "deserializing crashtracker telemetry payload to json" )
95
+ . unwrap ( ) ;
106
96
assert_eq ! ( telemetry_payload[ "request_type" ] , "logs" ) ;
107
97
assert_eq ! (
108
98
serde_json:: json!( {
@@ -136,6 +126,87 @@ fn test_crash_tracking_bin(crash_tracking_receiver_profile: BuildProfile) {
136
126
assert_eq ! ( telemetry_payload[ "payload" ] [ 0 ] [ "is_sensitive" ] , true ) ;
137
127
}
138
128
129
+ #[ test]
130
+ #[ cfg_attr( miri, ignore) ]
131
+ #[ cfg( unix) ]
132
+ fn crash_tracking_empty_endpoint ( ) {
133
+ use std:: os:: unix:: net:: UnixListener ;
134
+
135
+ let ( crashtracker_bin, crashtracker_receiver) = setup_crashtracking_crates ( BuildProfile :: Debug ) ;
136
+ let fixtures = setup_test_fixtures ( & [ & crashtracker_receiver, & crashtracker_bin] ) ;
137
+
138
+ let socket_path = extend_path ( fixtures. tmpdir . path ( ) , "trace_agent.socket" ) ;
139
+ let listener = UnixListener :: bind ( & socket_path) . unwrap ( ) ;
140
+
141
+ process:: Command :: new ( & fixtures. artifacts [ & crashtracker_bin] )
142
+ // empty url, endpoint will be set to none
143
+ . arg ( "" )
144
+ . arg ( fixtures. artifacts [ & crashtracker_receiver] . as_os_str ( ) )
145
+ . arg ( & fixtures. stderr_path )
146
+ . arg ( & fixtures. stdout_path )
147
+ . env (
148
+ "DD_TRACE_AGENT_URL" ,
149
+ format ! ( "unix://{}" , socket_path. display( ) ) ,
150
+ )
151
+ . spawn ( )
152
+ . unwrap ( ) ;
153
+
154
+ let ( mut stream, _) = listener. accept ( ) . unwrap ( ) ;
155
+ let mut out = vec ! [ 0 ; 65536 ] ;
156
+ let read = stream. read ( & mut out) . unwrap ( ) ;
157
+
158
+ stream
159
+ . write_all ( b"HTTP/1.1 404\r \n Content-Length: 0\r \n \r \n " )
160
+ . unwrap ( ) ;
161
+ let resp = String :: from_utf8_lossy ( & out[ ..read] ) ;
162
+ let pos = resp. find ( "\r \n \r \n " ) . unwrap ( ) ;
163
+ let body = & resp[ pos + 4 ..] ;
164
+ assert_telemetry_message ( body. as_bytes ( ) ) ;
165
+ }
166
+
167
+ struct TestFixtures < ' a > {
168
+ tmpdir : tempfile:: TempDir ,
169
+ crash_profile_path : PathBuf ,
170
+ crash_telemetry_path : PathBuf ,
171
+ stdout_path : PathBuf ,
172
+ stderr_path : PathBuf ,
173
+
174
+ artifacts : HashMap < & ' a ArtifactsBuild , PathBuf > ,
175
+ }
176
+
177
+ fn setup_test_fixtures < ' a > ( crates : & [ & ' a ArtifactsBuild ] ) -> TestFixtures < ' a > {
178
+ let artifacts = build_artifacts ( crates) . unwrap ( ) ;
179
+
180
+ let tmpdir = tempfile:: TempDir :: new ( ) . unwrap ( ) ;
181
+ TestFixtures {
182
+ crash_profile_path : extend_path ( tmpdir. path ( ) , "crash" ) ,
183
+ crash_telemetry_path : extend_path ( tmpdir. path ( ) , "crash.telemetry" ) ,
184
+ stdout_path : extend_path ( tmpdir. path ( ) , "out.stdout" ) ,
185
+ stderr_path : extend_path ( tmpdir. path ( ) , "out.stderr" ) ,
186
+
187
+ artifacts,
188
+ tmpdir,
189
+ }
190
+ }
191
+
192
+ fn setup_crashtracking_crates (
193
+ crash_tracking_receiver_profile : BuildProfile ,
194
+ ) -> ( ArtifactsBuild , ArtifactsBuild ) {
195
+ let crashtracker_bin = ArtifactsBuild {
196
+ name : "crashtracker_bin_test" . to_owned ( ) ,
197
+ build_profile : crash_tracking_receiver_profile,
198
+ artifact_type : ArtifactType :: Bin ,
199
+ triple_target : None ,
200
+ } ;
201
+ let crashtracker_receiver = ArtifactsBuild {
202
+ name : "crashtracker_receiver" . to_owned ( ) ,
203
+ build_profile : crash_tracking_receiver_profile,
204
+ artifact_type : ArtifactType :: Bin ,
205
+ triple_target : None ,
206
+ } ;
207
+ ( crashtracker_bin, crashtracker_receiver)
208
+ }
209
+
139
210
fn extend_path < T : AsRef < Path > > ( parent : & Path , path : T ) -> PathBuf {
140
211
let mut parent = parent. to_path_buf ( ) ;
141
212
parent. push ( path) ;
0 commit comments