1use fidl::endpoints::ClientEnd;
6use fidl_fuchsia_test_manager as ftest_manager;
7use ftest_manager::{
8 CaseStatus, DebugDataIteratorMarker, Event as FidlEvent, EventDetails as FidlEventDetails,
9 RunEvent as FidlRunEvent, RunEventPayload as FidlRunEventPayload, SuiteEvent as FidlSuiteEvent,
10 SuiteEventPayload as FidlSuiteEventPayload, SuiteResult, SuiteStatus, TestCaseResult,
11};
12
13pub(crate) enum RunEventPayload {
14 DebugData(ClientEnd<DebugDataIteratorMarker>),
15}
16
17pub(crate) struct RunEvent {
18 timestamp: i64,
19 payload: RunEventPayload,
20}
21
22impl Into<FidlRunEvent> for RunEvent {
23 fn into(self) -> FidlRunEvent {
24 match self.payload {
25 RunEventPayload::DebugData(client) => FidlRunEvent {
26 timestamp: Some(self.timestamp),
27 payload: Some(FidlRunEventPayload::Artifact(ftest_manager::Artifact::DebugData(
28 client,
29 ))),
30 ..Default::default()
31 },
32 }
33 }
34}
35
36impl RunEvent {
37 pub fn debug_data(client: ClientEnd<DebugDataIteratorMarker>) -> Self {
38 Self {
39 timestamp: zx::MonotonicInstant::get().into_nanos(),
40 payload: RunEventPayload::DebugData(client),
41 }
42 }
43
44 #[cfg(test)]
45 pub fn into_payload(self) -> RunEventPayload {
46 self.payload
47 }
48}
49
50pub(crate) enum SuiteEventPayload {
51 CaseFound(String, u32),
52 CaseStarted(u32),
53 CaseStopped(u32, CaseStatus),
54 CaseFinished(u32),
55 CaseStdout(u32, zx::Socket),
56 CaseStderr(u32, zx::Socket),
57 CustomArtifact(ftest_manager::CustomArtifact),
58 SuiteSyslog(ftest_manager::Syslog),
59 SuiteStderr(zx::Socket),
60 SuiteStarted,
61 SuiteStopped(SuiteStatus),
62 DebugData(ClientEnd<DebugDataIteratorMarker>),
63}
64
65pub struct SuiteEvents {
66 pub timestamp: i64,
67 pub payload: SuiteEventPayload,
68}
69
70impl Into<FidlSuiteEvent> for SuiteEvents {
71 fn into(self) -> FidlSuiteEvent {
72 match self.payload {
73 SuiteEventPayload::CaseFound(name, identifier) => FidlSuiteEvent {
74 timestamp: Some(self.timestamp),
75 payload: Some(FidlSuiteEventPayload::CaseFound(ftest_manager::CaseFound {
76 test_case_name: name,
77 identifier,
78 })),
79 ..Default::default()
80 },
81 SuiteEventPayload::CaseStarted(identifier) => FidlSuiteEvent {
82 timestamp: Some(self.timestamp),
83 payload: Some(FidlSuiteEventPayload::CaseStarted(ftest_manager::CaseStarted {
84 identifier,
85 })),
86 ..Default::default()
87 },
88 SuiteEventPayload::CaseStopped(identifier, status) => FidlSuiteEvent {
89 timestamp: Some(self.timestamp),
90 payload: Some(FidlSuiteEventPayload::CaseStopped(ftest_manager::CaseStopped {
91 identifier,
92 status,
93 })),
94 ..Default::default()
95 },
96 SuiteEventPayload::CaseFinished(identifier) => FidlSuiteEvent {
97 timestamp: Some(self.timestamp),
98 payload: Some(FidlSuiteEventPayload::CaseFinished(ftest_manager::CaseFinished {
99 identifier,
100 })),
101 ..Default::default()
102 },
103 SuiteEventPayload::CaseStdout(identifier, socket) => FidlSuiteEvent {
104 timestamp: Some(self.timestamp),
105 payload: Some(FidlSuiteEventPayload::CaseArtifact(ftest_manager::CaseArtifact {
106 identifier,
107 artifact: ftest_manager::Artifact::Stdout(socket),
108 })),
109 ..Default::default()
110 },
111 SuiteEventPayload::CaseStderr(identifier, socket) => FidlSuiteEvent {
112 timestamp: Some(self.timestamp),
113 payload: Some(FidlSuiteEventPayload::CaseArtifact(ftest_manager::CaseArtifact {
114 identifier,
115 artifact: ftest_manager::Artifact::Stderr(socket),
116 })),
117 ..Default::default()
118 },
119 SuiteEventPayload::CustomArtifact(custom) => FidlSuiteEvent {
120 timestamp: Some(self.timestamp),
121 payload: Some(FidlSuiteEventPayload::SuiteArtifact(ftest_manager::SuiteArtifact {
122 artifact: ftest_manager::Artifact::Custom(custom),
123 })),
124 ..Default::default()
125 },
126 SuiteEventPayload::SuiteSyslog(syslog) => FidlSuiteEvent {
127 timestamp: Some(self.timestamp),
128 payload: Some(FidlSuiteEventPayload::SuiteArtifact(ftest_manager::SuiteArtifact {
129 artifact: ftest_manager::Artifact::Log(syslog),
130 })),
131 ..Default::default()
132 },
133 SuiteEventPayload::SuiteStderr(socket) => FidlSuiteEvent {
134 timestamp: Some(self.timestamp),
135 payload: Some(FidlSuiteEventPayload::SuiteArtifact(ftest_manager::SuiteArtifact {
136 artifact: ftest_manager::Artifact::Stderr(socket),
137 })),
138 ..Default::default()
139 },
140 SuiteEventPayload::SuiteStarted => FidlSuiteEvent {
141 timestamp: Some(self.timestamp),
142 payload: Some(FidlSuiteEventPayload::SuiteStarted(ftest_manager::SuiteStarted {})),
143 ..Default::default()
144 },
145 SuiteEventPayload::SuiteStopped(status) => FidlSuiteEvent {
146 timestamp: Some(self.timestamp),
147 payload: Some(FidlSuiteEventPayload::SuiteStopped(ftest_manager::SuiteStopped {
148 status,
149 })),
150 ..Default::default()
151 },
152 SuiteEventPayload::DebugData(client) => FidlSuiteEvent {
153 timestamp: Some(self.timestamp),
154 payload: Some(FidlSuiteEventPayload::SuiteArtifact(ftest_manager::SuiteArtifact {
155 artifact: ftest_manager::Artifact::DebugData(client),
156 })),
157 ..Default::default()
158 },
159 }
160 }
161}
162
163fn to_case_result(status: CaseStatus) -> TestCaseResult {
164 match status {
165 CaseStatus::Passed => TestCaseResult::Passed,
166 CaseStatus::Failed => TestCaseResult::Failed,
167 CaseStatus::TimedOut => TestCaseResult::TimedOut,
168 CaseStatus::Skipped => TestCaseResult::Skipped,
169 CaseStatus::Error => TestCaseResult::Error,
170 _ => TestCaseResult::Error,
171 }
172}
173
174fn to_suite_result(status: SuiteStatus) -> SuiteResult {
175 match status {
176 SuiteStatus::Passed => SuiteResult::Finished,
177 SuiteStatus::Failed => SuiteResult::Failed,
178 SuiteStatus::DidNotFinish => SuiteResult::DidNotFinish,
179 SuiteStatus::TimedOut => SuiteResult::TimedOut,
180 SuiteStatus::Stopped => SuiteResult::Stopped,
181 SuiteStatus::InternalError => SuiteResult::InternalError,
182 _ => SuiteResult::InternalError,
183 }
184}
185
186impl Into<FidlEvent> for SuiteEvents {
187 fn into(self) -> FidlEvent {
188 match self.payload {
189 SuiteEventPayload::SuiteStarted => FidlEvent {
190 timestamp: Some(self.timestamp),
191 details: Some(FidlEventDetails::SuiteStarted(
192 ftest_manager::SuiteStartedEventDetails { ..Default::default() },
193 )),
194 ..Default::default()
195 },
196 SuiteEventPayload::CaseFound(name, identifier) => FidlEvent {
197 timestamp: Some(self.timestamp),
198 details: Some(FidlEventDetails::TestCaseFound(
199 ftest_manager::TestCaseFoundEventDetails {
200 test_case_name: Some(name),
201 test_case_id: Some(identifier),
202 ..Default::default()
203 },
204 )),
205 ..Default::default()
206 },
207 SuiteEventPayload::CaseStarted(identifier) => FidlEvent {
208 timestamp: Some(self.timestamp),
209 details: Some(FidlEventDetails::TestCaseStarted(
210 ftest_manager::TestCaseStartedEventDetails {
211 test_case_id: Some(identifier),
212 ..Default::default()
213 },
214 )),
215 ..Default::default()
216 },
217 SuiteEventPayload::CaseStopped(identifier, status) => FidlEvent {
218 timestamp: Some(self.timestamp),
219 details: Some(FidlEventDetails::TestCaseStopped(
220 ftest_manager::TestCaseStoppedEventDetails {
221 test_case_id: Some(identifier),
222 result: Some(to_case_result(status)),
223 ..Default::default()
224 },
225 )),
226 ..Default::default()
227 },
228 SuiteEventPayload::CaseFinished(identifier) => FidlEvent {
229 timestamp: Some(self.timestamp),
230 details: Some(FidlEventDetails::TestCaseFinished(
231 ftest_manager::TestCaseFinishedEventDetails {
232 test_case_id: Some(identifier),
233 ..Default::default()
234 },
235 )),
236 ..Default::default()
237 },
238 SuiteEventPayload::CaseStdout(identifier, socket) => FidlEvent {
239 timestamp: Some(self.timestamp),
240 details: Some(FidlEventDetails::TestCaseArtifactGenerated(
241 ftest_manager::TestCaseArtifactGeneratedEventDetails {
242 test_case_id: Some(identifier),
243 artifact: Some(ftest_manager::Artifact::Stdout(socket)),
244 ..Default::default()
245 },
246 )),
247 ..Default::default()
248 },
249 SuiteEventPayload::CaseStderr(identifier, socket) => FidlEvent {
250 timestamp: Some(self.timestamp),
251 details: Some(FidlEventDetails::TestCaseArtifactGenerated(
252 ftest_manager::TestCaseArtifactGeneratedEventDetails {
253 test_case_id: Some(identifier),
254 artifact: Some(ftest_manager::Artifact::Stderr(socket)),
255 ..Default::default()
256 },
257 )),
258 ..Default::default()
259 },
260 SuiteEventPayload::CustomArtifact(custom) => FidlEvent {
261 timestamp: Some(self.timestamp),
262 details: Some(FidlEventDetails::SuiteArtifactGenerated(
263 ftest_manager::SuiteArtifactGeneratedEventDetails {
264 artifact: Some(ftest_manager::Artifact::Custom(custom)),
265 ..Default::default()
266 },
267 )),
268 ..Default::default()
269 },
270 SuiteEventPayload::SuiteSyslog(syslog) => FidlEvent {
271 timestamp: Some(self.timestamp),
272 details: Some(FidlEventDetails::SuiteArtifactGenerated(
273 ftest_manager::SuiteArtifactGeneratedEventDetails {
274 artifact: Some(ftest_manager::Artifact::Log(syslog)),
275 ..Default::default()
276 },
277 )),
278 ..Default::default()
279 },
280 SuiteEventPayload::SuiteStderr(socket) => FidlEvent {
281 timestamp: Some(self.timestamp),
282 details: Some(FidlEventDetails::SuiteArtifactGenerated(
283 ftest_manager::SuiteArtifactGeneratedEventDetails {
284 artifact: Some(ftest_manager::Artifact::Stderr(socket)),
285 ..Default::default()
286 },
287 )),
288 ..Default::default()
289 },
290 SuiteEventPayload::SuiteStopped(status) => FidlEvent {
291 timestamp: Some(self.timestamp),
292 details: Some(FidlEventDetails::SuiteStopped(
293 ftest_manager::SuiteStoppedEventDetails {
294 result: Some(to_suite_result(status)),
295 ..Default::default()
296 },
297 )),
298 ..Default::default()
299 },
300 SuiteEventPayload::DebugData(client) => FidlEvent {
301 timestamp: Some(self.timestamp),
302 details: Some(FidlEventDetails::SuiteArtifactGenerated(
303 ftest_manager::SuiteArtifactGeneratedEventDetails {
304 artifact: Some(ftest_manager::Artifact::DebugData(client)),
305 ..Default::default()
306 },
307 )),
308 ..Default::default()
309 },
310 }
311 }
312}
313
314impl SuiteEvents {
315 pub fn case_found(identifier: u32, name: String) -> Self {
316 Self {
317 timestamp: zx::MonotonicInstant::get().into_nanos(),
318 payload: SuiteEventPayload::CaseFound(name, identifier),
319 }
320 }
321
322 pub fn case_started(identifier: u32) -> Self {
323 Self {
324 timestamp: zx::MonotonicInstant::get().into_nanos(),
325 payload: SuiteEventPayload::CaseStarted(identifier),
326 }
327 }
328
329 pub fn case_stopped(identifier: u32, status: CaseStatus) -> Self {
330 Self {
331 timestamp: zx::MonotonicInstant::get().into_nanos(),
332 payload: SuiteEventPayload::CaseStopped(identifier, status),
333 }
334 }
335
336 pub fn case_finished(identifier: u32) -> Self {
337 Self {
338 timestamp: zx::MonotonicInstant::get().into_nanos(),
339 payload: SuiteEventPayload::CaseFinished(identifier),
340 }
341 }
342
343 pub fn case_stdout(identifier: u32, socket: zx::Socket) -> Self {
344 Self {
345 timestamp: zx::MonotonicInstant::get().into_nanos(),
346 payload: SuiteEventPayload::CaseStdout(identifier, socket),
347 }
348 }
349
350 pub fn case_stderr(identifier: u32, socket: zx::Socket) -> Self {
351 Self {
352 timestamp: zx::MonotonicInstant::get().into_nanos(),
353 payload: SuiteEventPayload::CaseStderr(identifier, socket),
354 }
355 }
356
357 pub fn suite_syslog(syslog: ftest_manager::Syslog) -> Self {
358 Self {
359 timestamp: zx::MonotonicInstant::get().into_nanos(),
360 payload: SuiteEventPayload::SuiteSyslog(syslog),
361 }
362 }
363
364 pub fn suite_stderr(socket: zx::Socket) -> Self {
365 Self {
366 timestamp: zx::MonotonicInstant::get().into_nanos(),
367 payload: SuiteEventPayload::SuiteStderr(socket),
368 }
369 }
370
371 pub fn suite_custom_artifact(custom: ftest_manager::CustomArtifact) -> Self {
372 Self {
373 timestamp: zx::MonotonicInstant::get().into_nanos(),
374 payload: SuiteEventPayload::CustomArtifact(custom),
375 }
376 }
377
378 pub fn suite_started() -> Self {
379 Self {
380 timestamp: zx::MonotonicInstant::get().into_nanos(),
381 payload: SuiteEventPayload::SuiteStarted,
382 }
383 }
384
385 pub fn suite_stopped(status: SuiteStatus) -> Self {
386 Self {
387 timestamp: zx::MonotonicInstant::get().into_nanos(),
388 payload: SuiteEventPayload::SuiteStopped(status),
389 }
390 }
391
392 pub fn debug_data(client: ClientEnd<DebugDataIteratorMarker>) -> Self {
393 Self {
394 timestamp: zx::MonotonicInstant::get().into_nanos(),
395 payload: SuiteEventPayload::DebugData(client),
396 }
397 }
398
399 #[cfg(test)]
400 pub fn into_suite_run_event(self) -> FidlSuiteEvent {
401 self.into()
402 }
403
404 #[cfg(test)]
405 pub fn into_event(self) -> FidlEvent {
406 self.into()
407 }
408
409 #[cfg(test)]
410 pub fn into_payload(self) -> SuiteEventPayload {
411 self.payload
412 }
413}
414
415#[cfg(test)]
416mod tests {
417 use super::*;
418 use assert_matches::assert_matches;
419
420 #[test]
421 fn suite_events() {
422 let event = SuiteEvents::case_found(1, "case1".to_string()).into_suite_run_event();
423 assert_matches!(event.timestamp, Some(_));
424 assert_eq!(
425 event.payload,
426 Some(FidlSuiteEventPayload::CaseFound(ftest_manager::CaseFound {
427 test_case_name: "case1".into(),
428 identifier: 1
429 }))
430 );
431
432 let event = SuiteEvents::case_started(2).into_suite_run_event();
433 assert_matches!(event.timestamp, Some(_));
434 assert_eq!(
435 event.payload,
436 Some(FidlSuiteEventPayload::CaseStarted(ftest_manager::CaseStarted { identifier: 2 }))
437 );
438
439 let event = SuiteEvents::case_stopped(2, CaseStatus::Failed).into_suite_run_event();
440 assert_matches!(event.timestamp, Some(_));
441 assert_eq!(
442 event.payload,
443 Some(FidlSuiteEventPayload::CaseStopped(ftest_manager::CaseStopped {
444 identifier: 2,
445 status: CaseStatus::Failed
446 }))
447 );
448
449 let event = SuiteEvents::case_finished(2).into_suite_run_event();
450 assert_matches!(event.timestamp, Some(_));
451 assert_eq!(
452 event.payload,
453 Some(FidlSuiteEventPayload::CaseFinished(ftest_manager::CaseFinished {
454 identifier: 2
455 }))
456 );
457
458 let (sock1, _sock2) = zx::Socket::create_stream();
459 let event = SuiteEvents::case_stdout(2, sock1).into_suite_run_event();
460 assert_matches!(event.timestamp, Some(_));
461 assert_matches!(
462 event.payload,
463 Some(FidlSuiteEventPayload::CaseArtifact(ftest_manager::CaseArtifact {
464 identifier: 2,
465 artifact: ftest_manager::Artifact::Stdout(_)
466 }))
467 );
468
469 let (sock1, _sock2) = zx::Socket::create_stream();
470 let event = SuiteEvents::case_stderr(2, sock1).into_suite_run_event();
471 assert_matches!(event.timestamp, Some(_));
472 assert_matches!(
473 event.payload,
474 Some(FidlSuiteEventPayload::CaseArtifact(ftest_manager::CaseArtifact {
475 identifier: 2,
476 artifact: ftest_manager::Artifact::Stderr(_)
477 }))
478 );
479
480 let event = SuiteEvents::suite_stopped(SuiteStatus::Failed).into_suite_run_event();
481 assert_matches!(event.timestamp, Some(_));
482 assert_eq!(
483 event.payload,
484 Some(FidlSuiteEventPayload::SuiteStopped(ftest_manager::SuiteStopped {
485 status: SuiteStatus::Failed,
486 }))
487 );
488
489 let (client_end, _server_end) = zx::Socket::create_stream();
490 let event = SuiteEvents::suite_syslog(ftest_manager::Syslog::Stream(client_end))
491 .into_suite_run_event();
492 assert_matches!(event.timestamp, Some(_));
493 assert_matches!(
494 event.payload,
495 Some(FidlSuiteEventPayload::SuiteArtifact(ftest_manager::SuiteArtifact {
496 artifact: ftest_manager::Artifact::Log(ftest_manager::Syslog::Stream(_)),
497 }))
498 );
499
500 let (client_end, _server_end) = fidl::endpoints::create_endpoints();
501 let event = SuiteEvents::suite_syslog(ftest_manager::Syslog::Batch(client_end))
502 .into_suite_run_event();
503 assert_matches!(event.timestamp, Some(_));
504 assert_matches!(
505 event.payload,
506 Some(FidlSuiteEventPayload::SuiteArtifact(ftest_manager::SuiteArtifact {
507 artifact: ftest_manager::Artifact::Log(ftest_manager::Syslog::Batch(_)),
508 }))
509 );
510
511 let event = SuiteEvents::suite_started().into_event();
514 assert_matches!(event.timestamp, Some(_));
515 assert_eq!(
516 event.details,
517 Some(FidlEventDetails::SuiteStarted(ftest_manager::SuiteStartedEventDetails {
518 ..Default::default()
519 }))
520 );
521
522 let event = SuiteEvents::suite_stopped(SuiteStatus::Failed).into_event();
523 assert_matches!(event.timestamp, Some(_));
524 assert_eq!(
525 event.details,
526 Some(FidlEventDetails::SuiteStopped(ftest_manager::SuiteStoppedEventDetails {
527 result: Some(SuiteResult::Failed),
528 ..Default::default()
529 }))
530 );
531
532 let event = SuiteEvents::case_found(1, "case1".to_string()).into_event();
533 assert_matches!(event.timestamp, Some(_));
534 assert_eq!(
535 event.details,
536 Some(FidlEventDetails::TestCaseFound(ftest_manager::TestCaseFoundEventDetails {
537 test_case_name: Some("case1".into()),
538 test_case_id: Some(1),
539 ..Default::default()
540 }))
541 );
542
543 let event = SuiteEvents::case_started(2).into_event();
544 assert_matches!(event.timestamp, Some(_));
545 assert_eq!(
546 event.details,
547 Some(FidlEventDetails::TestCaseStarted(ftest_manager::TestCaseStartedEventDetails {
548 test_case_id: Some(2),
549 ..Default::default()
550 }))
551 );
552
553 let (sock1, _sock2) = zx::Socket::create_stream();
554 let event = SuiteEvents::case_stdout(2, sock1).into_event();
555 assert_matches!(event.timestamp, Some(_));
556 assert_matches!(
557 event.details,
558 Some(FidlEventDetails::TestCaseArtifactGenerated(
559 ftest_manager::TestCaseArtifactGeneratedEventDetails {
560 test_case_id: Some(2),
561 artifact: Some(ftest_manager::Artifact::Stdout(_)),
562 ..
563 }
564 ))
565 );
566
567 let (sock1, _sock2) = zx::Socket::create_stream();
568 let event = SuiteEvents::case_stderr(2, sock1).into_event();
569 assert_matches!(event.timestamp, Some(_));
570 assert_matches!(
571 event.details,
572 Some(FidlEventDetails::TestCaseArtifactGenerated(
573 ftest_manager::TestCaseArtifactGeneratedEventDetails {
574 test_case_id: Some(2),
575 artifact: Some(ftest_manager::Artifact::Stderr(_)),
576 ..
577 }
578 ))
579 );
580
581 let event = SuiteEvents::case_stopped(2, CaseStatus::Failed).into_event();
582 assert_matches!(event.timestamp, Some(_));
583 assert_eq!(
584 event.details,
585 Some(FidlEventDetails::TestCaseStopped(ftest_manager::TestCaseStoppedEventDetails {
586 test_case_id: Some(2),
587 result: Some(TestCaseResult::Failed),
588 ..Default::default()
589 }))
590 );
591
592 let event = SuiteEvents::case_finished(2).into_event();
593 assert_matches!(event.timestamp, Some(_));
594 assert_eq!(
595 event.details,
596 Some(FidlEventDetails::TestCaseFinished(ftest_manager::TestCaseFinishedEventDetails {
597 test_case_id: Some(2),
598 ..Default::default()
599 }))
600 );
601
602 let (client_end, _server_end) = zx::Socket::create_stream();
603 let event =
604 SuiteEvents::suite_syslog(ftest_manager::Syslog::Stream(client_end)).into_event();
605 assert_matches!(event.timestamp, Some(_));
606 assert_matches!(
607 event.details,
608 Some(FidlEventDetails::SuiteArtifactGenerated(
609 ftest_manager::SuiteArtifactGeneratedEventDetails {
610 artifact: Some(ftest_manager::Artifact::Log(ftest_manager::Syslog::Stream(_))),
611 ..
612 }
613 ))
614 );
615
616 let (client_end, _server_end) = fidl::endpoints::create_endpoints();
617 let event =
618 SuiteEvents::suite_syslog(ftest_manager::Syslog::Batch(client_end)).into_event();
619 assert_matches!(event.timestamp, Some(_));
620 assert_matches!(
621 event.details,
622 Some(FidlEventDetails::SuiteArtifactGenerated(
623 ftest_manager::SuiteArtifactGeneratedEventDetails {
624 artifact: Some(ftest_manager::Artifact::Log(ftest_manager::Syslog::Batch(_))),
625 ..
626 }
627 ))
628 );
629
630 let event = SuiteEvents::suite_stopped(SuiteStatus::Failed).into_event();
631 assert_matches!(event.timestamp, Some(_));
632 assert_eq!(
633 event.details,
634 Some(FidlEventDetails::SuiteStopped(ftest_manager::SuiteStoppedEventDetails {
635 result: Some(SuiteResult::Failed),
636 ..Default::default()
637 }))
638 );
639 }
640}