-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathsynthetics.rs
More file actions
1070 lines (994 loc) · 37.1 KB
/
Copy pathsynthetics.rs
File metadata and controls
1070 lines (994 loc) · 37.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use anyhow::Result;
use datadog_api_client::datadogV1::api_synthetics::{
ListTestsOptionalParams, SearchTestsOptionalParams, SyntheticsAPI,
};
use datadog_api_client::datadogV2::api_synthetics::{
GetSyntheticsBrowserTestResultOptionalParams, GetSyntheticsTestResultOptionalParams,
GetSyntheticsTestVersionOptionalParams, ListSyntheticsBrowserTestLatestResultsOptionalParams,
ListSyntheticsDowntimesOptionalParams, ListSyntheticsTestLatestResultsOptionalParams,
ListSyntheticsTestVersionsOptionalParams, SearchSuitesOptionalParams,
SyntheticsAPI as SyntheticsV2API,
};
use datadog_api_client::datadogV2::model::{
DeletedSuitesRequestDelete, DeletedSuitesRequestDeleteAttributes,
DeletedSuitesRequestDeleteRequest, SuiteCreateEditRequest, SyntheticsDowntimeRequest,
SyntheticsTestResultRunType, SyntheticsTestResultStatus,
};
use crate::config::Config;
use crate::formatter;
fn synthetics_intake_base_url(cfg: &Config) -> String {
if cfg.site == "datadoghq.com" || cfg.site == "datad0g.com" {
format!("https://intake.synthetics.{}/api/v1", cfg.site)
} else {
format!("{}/api/v1", cfg.api_base_url())
}
}
#[cfg(not(target_arch = "wasm32"))]
fn build_auth_headers(cfg: &Config) -> anyhow::Result<reqwest::header::HeaderMap> {
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
let mut headers = HeaderMap::new();
// Prefer OAuth2 bearer token, falling back to API + app keys.
if let Some(token) = cfg.access_token.as_ref() {
headers.insert(
reqwest::header::AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {token}"))?,
);
} else if let (Some(api_key), Some(app_key)) = (cfg.api_key.as_ref(), cfg.app_key.as_ref()) {
headers.insert(
HeaderName::from_static("dd-api-key"),
HeaderValue::from_str(api_key)?,
);
headers.insert(
HeaderName::from_static("dd-application-key"),
HeaderValue::from_str(app_key)?,
);
} else {
anyhow::bail!(
"'synthetics tests run' requires authentication: run 'pup auth login' or set DD_API_KEY and DD_APP_KEY"
);
}
headers.insert(
reqwest::header::USER_AGENT,
HeaderValue::from_str(&crate::useragent::get())?,
);
Ok(headers)
}
const POLL_INTERVAL_SECS: u64 = 5;
const TRIGGER_APP: &str = "pup_cli";
#[cfg(not(target_arch = "wasm32"))]
pub async fn tests_run(
cfg: &Config,
public_ids: Vec<String>,
use_tunnel: bool,
timeout_secs: u64,
) -> Result<()> {
if public_ids.is_empty() {
anyhow::bail!("at least one public ID is required");
}
let auth_headers = build_auth_headers(cfg)?;
let intake_url = synthetics_intake_base_url(cfg);
let client = reqwest::Client::new();
let active_tunnel = if use_tunnel {
eprintln!(
"Fetching tunnel presigned URL for {} test(s)...",
public_ids.len()
);
let query: Vec<(&str, &str)> = public_ids
.iter()
.map(|id| ("test_id", id.as_str()))
.collect();
let tunnel_resp = client
.get(format!("{intake_url}/synthetics/ci/tunnel"))
.query(&query)
.headers(auth_headers.clone())
.send()
.await?;
if !tunnel_resp.status().is_success() {
let status = tunnel_resp.status();
let body = tunnel_resp.text().await.unwrap_or_default();
anyhow::bail!("failed to get tunnel URL (HTTP {status}): {body}");
}
let tunnel_json: serde_json::Value = tunnel_resp.json().await?;
let presigned_url = tunnel_json["url"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("missing 'url' in tunnel response"))?
.to_string();
eprintln!("Starting tunnel...");
let (tunnel_info, tunnel) =
crate::tunnel::Tunnel::start(&presigned_url, public_ids.clone()).await?;
eprintln!("Tunnel connected (id: {})", tunnel_info.id);
Some((tunnel_info, tunnel))
} else {
None
};
let tests_payload: Vec<serde_json::Value> = public_ids
.iter()
.map(|id| {
let mut test = serde_json::json!({ "public_id": id });
if let Some((ref info, _)) = active_tunnel {
test["tunnel"] = serde_json::json!({
"id": info.id,
"host": info.host,
"privateKey": info.private_key,
});
}
test
})
.collect();
let trigger_payload = serde_json::json!({ "tests": tests_payload });
eprintln!("Triggering {} test(s)...", public_ids.len());
let trigger_resp = client
.post(format!("{intake_url}/synthetics/tests/trigger/ci"))
.headers(auth_headers.clone())
.header("X-Trigger-App", TRIGGER_APP)
.json(&trigger_payload)
.send()
.await?;
if !trigger_resp.status().is_success() {
let status = trigger_resp.status();
let body = trigger_resp.text().await.unwrap_or_default();
if let Some((_, tunnel)) = active_tunnel {
tunnel.stop();
}
anyhow::bail!("failed to trigger tests (HTTP {status}): {body}");
}
let trigger_json: serde_json::Value = trigger_resp.json().await?;
let batch_id = trigger_json["batch_id"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("missing 'batch_id' in trigger response"))?
.to_string();
eprintln!("Batch ID: {batch_id}");
let poll_url = format!(
"{}/api/v1/synthetics/ci/batch/{batch_id}",
cfg.api_base_url()
);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(timeout_secs);
let final_result = loop {
tokio::time::sleep(std::time::Duration::from_secs(POLL_INTERVAL_SECS)).await;
let batch_resp = client
.get(&poll_url)
.headers(auth_headers.clone())
.send()
.await?;
if !batch_resp.status().is_success() {
let status = batch_resp.status();
let body = batch_resp.text().await.unwrap_or_default();
if let Some((_, tunnel)) = active_tunnel {
tunnel.stop();
}
anyhow::bail!("failed to poll batch (HTTP {status}): {body}");
}
let batch_json: serde_json::Value = batch_resp.json().await?;
let status = batch_json["data"]["status"].as_str().unwrap_or("unknown");
eprintln!("Batch status: {status}");
if status != "in_progress" {
break batch_json;
}
if std::time::Instant::now() >= deadline {
if let Some((_, tunnel)) = active_tunnel {
tunnel.stop();
}
anyhow::bail!("timeout after {timeout_secs}s waiting for test results");
}
};
if let Some((_, tunnel)) = active_tunnel {
tunnel.stop();
}
let results = &final_result["data"]["results"];
if cfg.output_format == crate::config::OutputFormat::Table {
let table_rows: Vec<serde_json::Value> = results
.as_array()
.map(|arr| {
arr.iter()
.map(|r| {
serde_json::json!({
"status": r["status"],
"test_name": r["test_name"],
"test_public_id": r["test_public_id"],
"location": r["location"],
"duration_ms": r["duration"],
"test_type": r["test_type"],
"execution_rule": r["execution_rule"],
})
})
.collect()
})
.unwrap_or_default();
formatter::output(cfg, &table_rows)
} else {
formatter::output(cfg, results)
}
}
pub async fn tests_list(cfg: &Config, page_size: i64, page_number: i64) -> Result<()> {
let api = crate::make_api!(SyntheticsAPI, cfg);
let resp = api
.list_tests(
ListTestsOptionalParams::default()
.page_size(page_size)
.page_number(page_number),
)
.await
.map_err(|e| anyhow::anyhow!("failed to list tests: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn tests_get(cfg: &Config, public_id: &str) -> Result<()> {
let api = crate::make_api!(SyntheticsAPI, cfg);
let resp = api
.get_test(public_id.to_string())
.await
.map_err(|e| anyhow::anyhow!("failed to get test: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn tests_search(
cfg: &Config,
text: Option<String>,
facets_only: bool,
include_full_config: bool,
count: i64,
start: i64,
sort: Option<String>,
) -> Result<()> {
let api = crate::make_api!(SyntheticsAPI, cfg);
let mut params = SearchTestsOptionalParams::default();
if let Some(t) = text {
params = params.text(t);
}
if facets_only {
params = params.facets_only(true);
}
if include_full_config {
params = params.include_full_config(true);
}
if count != 50 {
params = params.count(count);
}
if start != 0 {
params = params.start(start);
}
if let Some(s) = sort {
params = params.sort(s);
}
let resp = api
.search_tests(params)
.await
.map_err(|e| anyhow::anyhow!("failed to search tests: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn locations_list(cfg: &Config) -> Result<()> {
let api = crate::make_api!(SyntheticsAPI, cfg);
let resp = api
.list_locations()
.await
.map_err(|e| anyhow::anyhow!("failed to list locations: {e:?}"))?;
formatter::output(cfg, &resp)
}
// ---- Suites (V2 API) ----
pub async fn suites_list(cfg: &Config, query: Option<String>) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let mut params = SearchSuitesOptionalParams::default();
if let Some(q) = query {
params = params.query(q);
}
let resp = api
.search_suites(params)
.await
.map_err(|e| anyhow::anyhow!("failed to list synthetic suites: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn suites_get(cfg: &Config, suite_id: &str) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let resp = api
.get_synthetics_suite(suite_id.to_string())
.await
.map_err(|e| anyhow::anyhow!("failed to get synthetic suite: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn suites_create(cfg: &Config, file: &str) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let body: SuiteCreateEditRequest = crate::util::read_json_file(file)?;
let resp = api
.create_synthetics_suite(body)
.await
.map_err(|e| anyhow::anyhow!("failed to create synthetic suite: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn suites_update(cfg: &Config, suite_id: &str, file: &str) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let body: SuiteCreateEditRequest = crate::util::read_json_file(file)?;
let resp = api
.edit_synthetics_suite(suite_id.to_string(), body)
.await
.map_err(|e| anyhow::anyhow!("failed to update synthetic suite: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn suites_delete(cfg: &Config, suite_ids: Vec<String>) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let attrs = DeletedSuitesRequestDeleteAttributes::new(suite_ids);
let data = DeletedSuitesRequestDelete::new(attrs);
let body = DeletedSuitesRequestDeleteRequest::new(data);
let resp = api
.delete_synthetics_suites(body)
.await
.map_err(|e| anyhow::anyhow!("failed to delete synthetic suites: {e:?}"))?;
formatter::output(cfg, &resp)
}
// ---- Tests (V2 API) ----
pub async fn tests_get_fast_result(cfg: &Config, result_id: &str) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let resp = api
.get_synthetics_fast_test_result(result_id.to_string())
.await
.map_err(|e| anyhow::anyhow!("failed to get fast test result: {e:?}"))?;
formatter::output(cfg, &resp)
}
fn parse_result_status(s: &str) -> Result<SyntheticsTestResultStatus> {
Ok(match s {
"passed" => SyntheticsTestResultStatus::PASSED,
"failed" => SyntheticsTestResultStatus::FAILED,
"no_data" => SyntheticsTestResultStatus::NO_DATA,
_ => anyhow::bail!("invalid status '{s}' — use one of: passed, failed, no_data"),
})
}
fn parse_result_run_type(s: &str) -> Result<SyntheticsTestResultRunType> {
Ok(match s {
"scheduled" => SyntheticsTestResultRunType::SCHEDULED,
"fast" => SyntheticsTestResultRunType::FAST,
"ci" => SyntheticsTestResultRunType::CI,
"triggered" => SyntheticsTestResultRunType::TRIGGERED,
_ => anyhow::bail!("invalid run-type '{s}' — use one of: scheduled, fast, ci, triggered"),
})
}
pub async fn tests_get_result(
cfg: &Config,
public_id: &str,
result_id: &str,
event_id: Option<String>,
timestamp: Option<i64>,
) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let mut params = GetSyntheticsTestResultOptionalParams::default();
if let Some(e) = event_id {
params.event_id = Some(e);
}
if let Some(t) = timestamp {
params.timestamp = Some(t);
}
let resp = api
.get_synthetics_test_result(public_id.to_string(), result_id.to_string(), params)
.await
.map_err(|e| anyhow::anyhow!("failed to get test result: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn tests_get_browser_result(
cfg: &Config,
public_id: &str,
result_id: &str,
event_id: Option<String>,
timestamp: Option<i64>,
) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let mut params = GetSyntheticsBrowserTestResultOptionalParams::default();
if let Some(e) = event_id {
params.event_id = Some(e);
}
if let Some(t) = timestamp {
params.timestamp = Some(t);
}
let resp = api
.get_synthetics_browser_test_result(public_id.to_string(), result_id.to_string(), params)
.await
.map_err(|e| anyhow::anyhow!("failed to get browser test result: {e:?}"))?;
formatter::output(cfg, &resp)
}
#[allow(clippy::too_many_arguments)]
pub async fn tests_list_latest_results(
cfg: &Config,
public_id: &str,
from_ts: Option<i64>,
to_ts: Option<i64>,
status: Option<String>,
run_type: Option<String>,
probe_dc: Option<Vec<String>>,
device_id: Option<Vec<String>>,
) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let mut params = ListSyntheticsTestLatestResultsOptionalParams::default();
if let Some(t) = from_ts {
params.from_ts = Some(t);
}
if let Some(t) = to_ts {
params.to_ts = Some(t);
}
if let Some(s) = status {
params.status = Some(parse_result_status(&s)?);
}
if let Some(r) = run_type {
params.run_type = Some(parse_result_run_type(&r)?);
}
if let Some(p) = probe_dc {
params.probe_dc = Some(p);
}
if let Some(d) = device_id {
params.device_id = Some(d);
}
let resp = api
.list_synthetics_test_latest_results(public_id.to_string(), params)
.await
.map_err(|e| anyhow::anyhow!("failed to list latest test results: {e:?}"))?;
formatter::output(cfg, &resp)
}
#[allow(clippy::too_many_arguments)]
pub async fn tests_list_latest_browser_results(
cfg: &Config,
public_id: &str,
from_ts: Option<i64>,
to_ts: Option<i64>,
status: Option<String>,
run_type: Option<String>,
probe_dc: Option<Vec<String>>,
device_id: Option<Vec<String>>,
) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let mut params = ListSyntheticsBrowserTestLatestResultsOptionalParams::default();
if let Some(t) = from_ts {
params.from_ts = Some(t);
}
if let Some(t) = to_ts {
params.to_ts = Some(t);
}
if let Some(s) = status {
params.status = Some(parse_result_status(&s)?);
}
if let Some(r) = run_type {
params.run_type = Some(parse_result_run_type(&r)?);
}
if let Some(p) = probe_dc {
params.probe_dc = Some(p);
}
if let Some(d) = device_id {
params.device_id = Some(d);
}
let resp = api
.list_synthetics_browser_test_latest_results(public_id.to_string(), params)
.await
.map_err(|e| anyhow::anyhow!("failed to list latest browser test results: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn tests_poll_results(cfg: &Config, result_ids: Vec<String>) -> Result<()> {
if result_ids.is_empty() {
anyhow::bail!("at least one result-id is required");
}
// The endpoint takes a JSON-encoded array as a query string parameter.
let encoded = serde_json::to_string(&result_ids)
.map_err(|e| anyhow::anyhow!("failed to encode result IDs: {e}"))?;
let api = crate::make_api!(SyntheticsV2API, cfg);
let resp = api
.poll_synthetics_test_results(encoded)
.await
.map_err(|e| anyhow::anyhow!("failed to poll test results: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn tests_get_version(
cfg: &Config,
public_id: &str,
version: i64,
include_change_metadata: bool,
) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let mut params = GetSyntheticsTestVersionOptionalParams::default();
if include_change_metadata {
params = params.include_change_metadata(true);
}
let resp = api
.get_synthetics_test_version(public_id.to_string(), version, params)
.await
.map_err(|e| anyhow::anyhow!("failed to get test version: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn tests_list_versions(
cfg: &Config,
public_id: &str,
limit: Option<i64>,
last_version_number: Option<i64>,
) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let mut params = ListSyntheticsTestVersionsOptionalParams::default();
if let Some(l) = limit {
params = params.limit(l);
}
if let Some(v) = last_version_number {
params = params.last_version_number(v);
}
let resp = api
.list_synthetics_test_versions(public_id.to_string(), params)
.await
.map_err(|e| anyhow::anyhow!("failed to list test versions: {e:?}"))?;
formatter::output(cfg, &resp)
}
// ---- Downtimes (V2 API) ----
pub async fn downtime_list(
cfg: &Config,
filter_test_ids: Option<String>,
filter_active: Option<String>,
) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let mut params = ListSyntheticsDowntimesOptionalParams::default();
if let Some(ids) = filter_test_ids.clone() {
params = params.filter_test_ids(ids);
}
if let Some(active) = filter_active.clone() {
params = params.filter_active(active);
}
match api.list_synthetics_downtimes(params).await {
Ok(resp) => formatter::output(cfg, &resp),
// Issue #722: the typed V2 model rejects downtime payloads that contain
// `null` where it expects an array ("invalid type: null, expected a
// sequence"). This deserialization failure only happens on a successful
// (2xx) response, so fall back to emitting the raw JSON:API body, which
// tolerates nulls. Genuine HTTP errors surface through the arm below.
Err(datadog_api_client::datadog::Error::Serde(_)) => {
let mut query: Vec<(&str, &str)> = Vec::new();
if let Some(ids) = filter_test_ids.as_deref() {
query.push(("filter[test_ids]", ids));
}
if let Some(active) = filter_active.as_deref() {
query.push(("filter[active]", active));
}
let resp = crate::raw_client::raw_get(cfg, "/api/v2/synthetics/downtimes", &query)
.await
.map_err(|e| anyhow::anyhow!("failed to list synthetics downtimes: {e}"))?;
formatter::output(cfg, &resp)
}
Err(e) => Err(anyhow::anyhow!(
"failed to list synthetics downtimes: {e:?}"
)),
}
}
pub async fn downtime_create(cfg: &Config, file: &str) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let body: SyntheticsDowntimeRequest = crate::util::read_json_file(file)?;
let resp = api
.create_synthetics_downtime(body)
.await
.map_err(|e| anyhow::anyhow!("failed to create synthetics downtime: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn downtime_delete(cfg: &Config, downtime_id: &str) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
api.delete_synthetics_downtime(downtime_id.to_string())
.await
.map_err(|e| anyhow::anyhow!("failed to delete synthetics downtime: {e:?}"))?;
println!("Synthetics downtime {downtime_id} deleted.");
Ok(())
}
// ---- Multistep (V2 API) ----
pub async fn multistep_get_subtests(cfg: &Config, public_id: &str) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let resp = api
.get_api_multistep_subtests(public_id.to_string())
.await
.map_err(|e| anyhow::anyhow!("failed to get multistep subtests: {e:?}"))?;
formatter::output(cfg, &resp)
}
pub async fn multistep_get_subtest_parents(cfg: &Config, public_id: &str) -> Result<()> {
let api = crate::make_api!(SyntheticsV2API, cfg);
let resp = api
.get_api_multistep_subtest_parents(public_id.to_string())
.await
.map_err(|e| anyhow::anyhow!("failed to get multistep subtest parents: {e:?}"))?;
formatter::output(cfg, &resp)
}
#[cfg(test)]
mod tests {
use crate::test_support::*;
#[tokio::test]
async fn test_synthetics_tests_list() {
let _lock = lock_env().await;
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
mock_all(&mut s, r#"{"tests": []}"#).await;
let _ = super::tests_list(&cfg, 10, 0).await;
cleanup_env();
}
#[tokio::test]
async fn test_synthetics_tests_get() {
let _lock = lock_env().await;
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
mock_all(&mut s, r#"{}"#).await;
let _ = super::tests_get(&cfg, "pub1").await;
cleanup_env();
}
#[tokio::test]
async fn test_synthetics_locations_list() {
let _lock = lock_env().await;
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
mock_all(&mut s, r#"{"locations": []}"#).await;
let _ = super::locations_list(&cfg).await;
cleanup_env();
}
#[tokio::test]
async fn test_synthetics_tests_get_result() {
let _lock = lock_env().await;
std::env::set_var("DD_TOKEN_STORAGE", "file");
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{}"#).await;
let result = super::tests_get_result(&cfg, "abc-def-ghi", "result-1", None, None).await;
assert!(
result.is_ok(),
"tests_get_result failed: {:?}",
result.err()
);
cleanup_env();
std::env::remove_var("DD_TOKEN_STORAGE");
}
#[tokio::test]
async fn test_synthetics_tests_get_result_with_params() {
let _lock = lock_env().await;
std::env::set_var("DD_TOKEN_STORAGE", "file");
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{}"#).await;
let result = super::tests_get_result(
&cfg,
"abc-def-ghi",
"result-1",
Some("evt-1".into()),
Some(1700000000),
)
.await;
assert!(
result.is_ok(),
"tests_get_result with params failed: {:?}",
result.err()
);
cleanup_env();
std::env::remove_var("DD_TOKEN_STORAGE");
}
#[tokio::test]
async fn test_synthetics_tests_get_result_404() {
let _lock = lock_env().await;
std::env::set_var("DD_TOKEN_STORAGE", "file");
let mut server = mockito::Server::new_async().await;
let cfg = test_config(&server.url());
let _mock = server
.mock("GET", mockito::Matcher::Any)
.with_status(404)
.with_header("content-type", "application/json")
.with_body(r#"{"errors":["not found"]}"#)
.create_async()
.await;
let result = super::tests_get_result(&cfg, "abc-def-ghi", "missing", None, None).await;
assert!(result.is_err(), "expected 404 error");
cleanup_env();
std::env::remove_var("DD_TOKEN_STORAGE");
}
#[tokio::test]
async fn test_synthetics_tests_get_browser_result() {
let _lock = lock_env().await;
std::env::set_var("DD_TOKEN_STORAGE", "file");
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{}"#).await;
let result =
super::tests_get_browser_result(&cfg, "abc-def-ghi", "result-1", None, None).await;
assert!(
result.is_ok(),
"tests_get_browser_result failed: {:?}",
result.err()
);
cleanup_env();
std::env::remove_var("DD_TOKEN_STORAGE");
}
#[tokio::test]
async fn test_synthetics_tests_list_latest_results() {
let _lock = lock_env().await;
std::env::set_var("DD_TOKEN_STORAGE", "file");
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{}"#).await;
let result = super::tests_list_latest_results(
&cfg,
"abc-def-ghi",
None,
None,
None,
None,
None,
None,
)
.await;
assert!(
result.is_ok(),
"tests_list_latest_results failed: {:?}",
result.err()
);
cleanup_env();
std::env::remove_var("DD_TOKEN_STORAGE");
}
#[tokio::test]
async fn test_synthetics_tests_list_latest_results_with_filters() {
let _lock = lock_env().await;
std::env::set_var("DD_TOKEN_STORAGE", "file");
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{}"#).await;
let result = super::tests_list_latest_results(
&cfg,
"abc-def-ghi",
Some(1700000000000),
Some(1700001000000),
Some("passed".into()),
Some("scheduled".into()),
Some(vec!["aws:us-east-1".into()]),
Some(vec!["chrome.laptop_large".into()]),
)
.await;
assert!(
result.is_ok(),
"tests_list_latest_results with filters failed: {:?}",
result.err()
);
cleanup_env();
std::env::remove_var("DD_TOKEN_STORAGE");
}
#[tokio::test]
async fn test_synthetics_tests_list_latest_results_bad_status() {
let _lock = lock_env().await;
let cfg = test_config("http://unused.local");
let result = super::tests_list_latest_results(
&cfg,
"abc-def-ghi",
None,
None,
Some("bogus".into()),
None,
None,
None,
)
.await;
assert!(result.is_err(), "expected status parse error");
assert!(result.unwrap_err().to_string().contains("invalid status"));
}
#[tokio::test]
async fn test_synthetics_tests_list_latest_results_bad_run_type() {
let _lock = lock_env().await;
let cfg = test_config("http://unused.local");
let result = super::tests_list_latest_results(
&cfg,
"abc-def-ghi",
None,
None,
None,
Some("bogus".into()),
None,
None,
)
.await;
assert!(result.is_err(), "expected run-type parse error");
assert!(result.unwrap_err().to_string().contains("invalid run-type"));
}
#[tokio::test]
async fn test_synthetics_tests_list_latest_browser_results() {
let _lock = lock_env().await;
std::env::set_var("DD_TOKEN_STORAGE", "file");
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{}"#).await;
let result = super::tests_list_latest_browser_results(
&cfg,
"abc-def-ghi",
None,
None,
None,
None,
None,
None,
)
.await;
assert!(
result.is_ok(),
"tests_list_latest_browser_results failed: {:?}",
result.err()
);
cleanup_env();
std::env::remove_var("DD_TOKEN_STORAGE");
}
#[tokio::test]
async fn test_synthetics_tests_poll_results() {
let _lock = lock_env().await;
std::env::set_var("DD_TOKEN_STORAGE", "file");
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{}"#).await;
let result = super::tests_poll_results(&cfg, vec!["r1".into(), "r2".into()]).await;
assert!(
result.is_ok(),
"tests_poll_results failed: {:?}",
result.err()
);
cleanup_env();
std::env::remove_var("DD_TOKEN_STORAGE");
}
#[tokio::test]
async fn test_synthetics_tests_poll_results_empty() {
let _lock = lock_env().await;
let cfg = test_config("http://unused.local");
let result = super::tests_poll_results(&cfg, vec![]).await;
assert!(result.is_err(), "expected empty result_ids error");
assert!(result
.unwrap_err()
.to_string()
.contains("at least one result-id"));
}
#[tokio::test]
async fn test_synthetics_downtime_list() {
let _lock = lock_env().await;
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{"data":[]}"#).await;
let result = super::downtime_list(&cfg, None, None).await;
assert!(result.is_ok(), "downtime_list failed: {:?}", result.err());
cleanup_env();
}
#[tokio::test]
async fn test_synthetics_downtime_list_with_filters() {
let _lock = lock_env().await;
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{"data":[]}"#).await;
let result = super::downtime_list(
&cfg,
Some("abc-def-ghi".to_string()),
Some("true".to_string()),
)
.await;
assert!(
result.is_ok(),
"downtime_list with filters failed: {:?}",
result.err()
);
cleanup_env();
}
#[tokio::test]
async fn test_synthetics_downtime_list_error() {
let _lock = lock_env().await;
let mut server = mockito::Server::new_async().await;
let cfg = test_config(&server.url());
let _mock = server
.mock("GET", mockito::Matcher::Any)
.with_status(403)
.with_header("content-type", "application/json")
.with_body(r#"{"errors":["Forbidden"]}"#)
.create_async()
.await;
let result = super::downtime_list(&cfg, None, None).await;
assert!(result.is_err(), "expected 403 error from downtime_list");
cleanup_env();
}
// Issue #722: a successful (2xx) response whose shape the typed V2 model
// cannot deserialize (here `data` is an object where a sequence is
// expected, mirroring the reported "invalid type: null, expected a
// sequence") must fall back to the raw JSON:API body instead of erroring.
#[tokio::test]
async fn test_synthetics_downtime_list_falls_back_on_deserialize_error() {
let _lock = lock_env().await;
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{"data":{}}"#).await;
let result = super::downtime_list(&cfg, None, None).await;
assert!(
result.is_ok(),
"downtime_list should fall back to raw output on deserialize error: {:?}",
result.err()
);
cleanup_env();
}
// The raw fallback must still forward the test-id and active filters.
#[tokio::test]
async fn test_synthetics_downtime_list_fallback_with_filters() {
let _lock = lock_env().await;
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(&mut s, "GET", r#"{"data":{}}"#).await;
let result = super::downtime_list(
&cfg,
Some("abc-def-ghi".to_string()),
Some("true".to_string()),
)
.await;
assert!(
result.is_ok(),
"downtime_list fallback with filters failed: {:?}",
result.err()
);
cleanup_env();
}
#[tokio::test]
async fn test_synthetics_downtime_create() {
let _lock = lock_env().await;
let mut s = mockito::Server::new_async().await;
let cfg = test_config(&s.url());
let _mock = mock_any(
&mut s,
"POST",
r#"{"data":{"id":"dt-123","type":"downtime","attributes":{"createdAt":"2024-01-01T00:00:00+00:00","createdBy":"u1","createdByName":"User One","description":"","isEnabled":true,"name":"test","tags":[],"testIds":[],"timeSlots":[],"updatedAt":"2024-01-01T00:00:00+00:00","updatedBy":"u1","updatedByName":"User One"}}}"#,
)
.await;
let tmp = write_temp_json(
"downtime_create.json",
r#"{"data":{"type":"downtime","attributes":{"name":"test","isEnabled":true,"testIds":[],"timeSlots":[]}}}"#,
);
let result = super::downtime_create(&cfg, tmp.to_str().unwrap()).await;
assert!(result.is_ok(), "downtime_create failed: {:?}", result.err());
cleanup_env();
}
#[tokio::test]
async fn test_synthetics_downtime_delete() {