test(downtimes): route tests use row ids no other test writes (#17078)

This commit is contained in:
Ashish Kolhe authored and GitHub committed 2026-11-21 08:29:06 +05:30
1 parent fd465c661e
commit 5b8285e4e8
4 files changed
+48 -36

No files matched your search

@@ -311,9 +311,11 @@ mod tests {
let hour = 3_600_000_000;
let group = |key: &str| serde_json::json!({ "alert_id": alert_id, "group_key": key });
let conn = infra::db::get_orm_client_rw().await;
let put = |id: &str, groups: serde_json::Value, version: i64| {
// Rows are keyed by id alone, so a fixed id would move another test's row into this org.
let id_of = |name: &str| format!("{org}_{name}");
let put = |name: &str, groups: serde_json::Value, version: i64| {
let row: Downtime = serde_json::from_value(serde_json::json!({
"id": id, "org": org, "name": "Quick mute",
"id": id_of(name), "org": org, "name": "Quick mute",
"targets": [{ "module": "alerts", "folders": { "kind": "all" }, "ids": [alert_id] }],
"alert_groups": groups,
"schedule": { "repeat": "none", "starts_at": now - hour, "ends_at": now + hour,
@@ -324,19 +326,19 @@ mod tests {
.unwrap();
async move { infra::table::downtimes::put_with(conn, &row).await.unwrap() }
};
let unmute = |id: &'static str, version: Option<i64>| {
let unmute = |name: &'static str, version: Option<i64>| {
cancel_alert_mute(
Path((org.clone(), alert_id.clone(), id.to_string())),
Path((org.clone(), alert_id.clone(), id_of(name))),
Query(CancelDowntimeQuery { version }),
Headers(UserEmail {
user_id: root.to_string(),
}),
)
};
let cancelled = |id: &'static str| {
let org = org.clone();
let cancelled = |name: &'static str| {
let (org, id) = (org.clone(), id_of(name));
async move {
let stored = infra::table::downtimes::get_rw(&org, id)
let stored = infra::table::downtimes::get_rw(&org, &id)
.await
.unwrap()
.unwrap();
+12 -9
View File
@@ -697,9 +697,12 @@ mod tests {
let hour = 3_600_000_000;
let conn = infra::db::get_orm_client_rw().await;
let mut versions = std::collections::HashMap::new();
for id in ["stale", "current", "absent"] {
// Rows are keyed by id alone, so a fixed id would move another test's row into this org.
let id_of = |name: &str| format!("{org}_{name}");
for name in ["stale", "current", "absent"] {
let id = id_of(name);
let row: Downtime = serde_json::from_value(serde_json::json!({
"id": id, "org": org, "name": id,
"id": id, "org": org, "name": name,
"targets": [{ "module": "alerts", "folders": { "kind": "all" } }],
"schedule": { "repeat": "none", "starts_at": now - hour, "ends_at": now + hour,
"timezone": "UTC", "duration_secs": 7200 },
@@ -707,25 +710,25 @@ mod tests {
}))
.unwrap();
infra::table::downtimes::put_with(conn, &row).await.unwrap();
let stored = infra::table::downtimes::get_rw(&org, id)
let stored = infra::table::downtimes::get_rw(&org, &id)
.await
.unwrap()
.unwrap();
versions.insert(id, stored.version);
versions.insert(name, stored.version);
}
let cancel = |id: &'static str, version: Option<i64>| {
let cancel = |name: &'static str, version: Option<i64>| {
cancel_downtime(
Path((org.clone(), id.to_string())),
Path((org.clone(), id_of(name))),
Query(CancelDowntimeQuery { version }),
Headers(UserEmail {
user_id: root.to_string(),
}),
)
};
let cancelled = |id: &'static str| {
let org = org.clone();
let cancelled = |name: &'static str| {
let (org, id) = (org.clone(), id_of(name));
async move {
let stored = infra::table::downtimes::get_rw(&org, id)
let stored = infra::table::downtimes::get_rw(&org, &id)
.await
.unwrap()
.unwrap();
+14 -10
View File
@@ -672,9 +672,12 @@ mod tests {
let now = now_micros();
let conn = infra::db::get_orm_client_rw().await;
let mut versions = BTreeMap::new();
for id in ["stale", "current", "absent"] {
// Rows are keyed by id alone, so a fixed id would move another test's row into this org.
let id_of = |name: &str| format!("{org}_{name}");
for name in ["stale", "current", "absent"] {
let id = id_of(name);
let mut mute = row(None, None);
mute.id = id.to_string();
mute.id = id.clone();
mute.org = org.clone();
mute.schedule.starts_at = now - HOUR;
mute.schedule.ends_at = Some(now + HOUR);
@@ -682,16 +685,16 @@ mod tests {
infra::table::downtimes::put_with(conn, &mute)
.await
.unwrap();
let stored = infra::table::downtimes::get_rw(&org, id)
let stored = infra::table::downtimes::get_rw(&org, &id)
.await
.unwrap()
.unwrap();
versions.insert(id, stored.version);
versions.insert(name, stored.version);
}
let live = |id: &'static str| {
let org = org.clone();
let live = |name: &'static str| {
let (org, id) = (org.clone(), id_of(name));
async move {
let stored = infra::table::downtimes::get_rw(&org, id)
let stored = infra::table::downtimes::get_rw(&org, &id)
.await
.unwrap()
.unwrap();
@@ -699,7 +702,8 @@ mod tests {
}
};
let refused = end_seen_quick_mute(&on, "stale", Some(versions["stale"] - 1), "ops").await;
let refused =
end_seen_quick_mute(&on, &id_of("stale"), Some(versions["stale"] - 1), "ops").await;
assert!(
matches!(
refused,
@@ -711,13 +715,13 @@ mod tests {
);
assert!(live("stale").await, "a refused unmute leaves the mute live");
let ended = end_seen_quick_mute(&on, "current", Some(versions["current"]), "ops")
let ended = end_seen_quick_mute(&on, &id_of("current"), Some(versions["current"]), "ops")
.await
.unwrap();
assert!(ended.cancelled_at.is_some());
assert!(!live("current").await);
end_seen_quick_mute(&on, "absent", None, "ops")
end_seen_quick_mute(&on, &id_of("absent"), None, "ops")
.await
.unwrap();
assert!(
+13 -10
View File
@@ -4106,27 +4106,30 @@ mod tests {
let now = now_micros();
let conn = infra::db::get_orm_client_rw().await;
let mut versions = HashMap::new();
for id in ["stale", "current", "absent"] {
// Rows are keyed by id alone, so a fixed id would move another test's row into this org.
let id_of = |name: &str| format!("{org}_{name}");
for name in ["stale", "current", "absent"] {
let id = id_of(name);
let mut mute = row(
vec![TargetModule::AnomalyDetections],
now - HOUR,
now + 10 * HOUR,
);
mute.id = id.to_string();
mute.id = id.clone();
mute.org = org.clone();
infra::table::downtimes::put_with(conn, &mute)
.await
.unwrap();
let stored = infra::table::downtimes::get_rw(&org, id)
let stored = infra::table::downtimes::get_rw(&org, &id)
.await
.unwrap()
.unwrap();
versions.insert(id, stored.version);
versions.insert(name, stored.version);
}
let live = |id: &'static str| {
let org = org.clone();
let live = |name: &'static str| {
let (org, id) = (org.clone(), id_of(name));
async move {
let stored = infra::table::downtimes::get_rw(&org, id)
let stored = infra::table::downtimes::get_rw(&org, &id)
.await
.unwrap()
.unwrap();
@@ -4134,7 +4137,7 @@ mod tests {
}
};
let refused = cancel(&org, root, "stale", Some(versions["stale"] - 1)).await;
let refused = cancel(&org, root, &id_of("stale"), Some(versions["stale"] - 1)).await;
assert!(
matches!(refused, Err(DowntimeError::Conflict(_))),
"{refused:?}"
@@ -4144,13 +4147,13 @@ mod tests {
"a refused cancel leaves the downtime live"
);
let ended = cancel(&org, root, "current", Some(versions["current"]))
let ended = cancel(&org, root, &id_of("current"), Some(versions["current"]))
.await
.unwrap();
assert!(ended.cancelled_at.is_some());
assert!(!live("current").await);
cancel(&org, root, "absent", None).await.unwrap();
cancel(&org, root, &id_of("absent"), None).await.unwrap();
assert!(
!live("absent").await,
"a caller with no version still cancels"