Add redaction system

This commit is contained in:
Mubelotix
2025-08-26 13:39:08 +02:00
parent 8e3a0c339f
commit 39e796ee03
5 changed files with 125 additions and 4 deletions

View File

@ -43,3 +43,4 @@ tracing-subscriber = "0.3.19"
tracing-trace = { version = "0.1.0", path = "../tracing-trace" }
uuid = { version = "1.17.0", features = ["v7", "serde"] }
similar-asserts = "1.7.0"
chrono = "0.4"

View File

@ -158,6 +158,46 @@ async fn wait_for_tasks(client: &Client) -> anyhow::Result<()> {
Ok(())
}
fn json_eq_ignore(reference: &Value, value: &Value) -> bool {
match reference {
Value::Null | Value::Bool(_) | Value::Number(_) => reference == value,
Value::String(s) => (s.starts_with('[') && s.ends_with(']')) || reference == value,
Value::Array(values) => match value {
Value::Array(other_values) => {
if values.len() != other_values.len() {
return false;
}
for (value, other_value) in values.iter().zip(other_values.iter()) {
if !json_eq_ignore(value, other_value) {
return false;
}
}
true
}
_ => false,
},
Value::Object(map) => match value {
Value::Object(other_map) => {
if map.len() != other_map.len() {
return false;
}
for (key, value) in map.iter() {
match other_map.get(key) {
Some(other_value) => {
if !json_eq_ignore(value, other_value) {
return false;
}
}
None => return false,
}
}
true
}
_ => false,
},
}
}
#[tracing::instrument(skip(client, command, assets, asset_folder), fields(command = %command))]
pub async fn run(
client: &Client,
@ -222,7 +262,7 @@ pub async fn run(
if return_value {
return Ok(Some((response, code)));
}
if &response != expected_response {
if !json_eq_ignore(expected_response, &response) {
let expected_pretty = serde_json::to_string_pretty(expected_response)
.context("serializing expected response as pretty JSON")?;
let response_pretty = serde_json::to_string_pretty(&response)

View File

@ -1,6 +1,8 @@
use anyhow::Context;
use cargo_metadata::semver::Version;
use chrono::DateTime;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{collections::BTreeMap, sync::Arc};
use crate::{
@ -29,6 +31,29 @@ enum CommandOrUpgradeVec<'a> {
Upgrade(VersionOrLatest),
}
fn produce_reference_value(value: &mut Value) {
match value {
Value::Null | Value::Bool(_) | Value::Number(_) => (),
Value::String(string) => {
if DateTime::parse_from_rfc3339(string.as_str()).is_ok() {
*string = String::from("[timestamp]");
} else if uuid::Uuid::parse_str(string).is_ok() {
*string = String::from("[uuid]");
}
}
Value::Array(values) => {
for value in values {
produce_reference_value(value);
}
}
Value::Object(map) => {
for (_key, value) in map.iter_mut() {
produce_reference_value(value);
}
},
}
}
/// A test workload.
/// Not to be confused with [a bench workload](crate::bench::workload::Workload).
#[derive(Serialize, Deserialize)]
@ -104,11 +129,12 @@ impl TestWorkload {
.await?;
if return_responses {
assert_eq!(responses.len(), cloned.len());
for (command, (response, status)) in commands.into_iter().zip(responses) {
for (command, (mut response, status)) in commands.into_iter().zip(responses) {
if args.update_responses
|| (dbg!(args.add_missing_responses)
&& dbg!(command.expected_response.is_none()))
{
produce_reference_value(&mut response);
command.expected_response = Some(response);
command.expected_status = Some(status.as_u16());
}