Skip to content

Commit

Permalink
move from regular to structured testing
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Jan 3, 2025
1 parent 4551923 commit 3d38723
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 82 deletions.
10 changes: 6 additions & 4 deletions server/src/db/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Event {
// insert into db
let mut tx = pool.begin().await?;
if let Err(e) = Event::delete(&mut tx, id).await {
error!("could not delete existing events because {e:?}");
error!(error = ?e, "could not delete existing events");
tx.rollback().await?;
return Err(e.into());
}
Expand All @@ -119,12 +119,14 @@ impl Event {
}
if let Some((cnt, e)) = failed {
warn!(
"{cnt}/{total} events could not be inserted because of {e:?}",
total = events.len()
error = ?e,
cnt,
total = events.len(),
"events could not be inserted because",
);
}
tx.commit().await?;
debug!("finished inserting into the db for {id}");
debug!(?id, "finished inserting into the db");
Ok(())
}
#[tracing::instrument(skip(tx))]
Expand Down
2 changes: 1 addition & 1 deletion server/src/external/connectum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl OauthAccessToken {
async fn get_possibly_refreshed_token(&self) -> String {
let mut token = self.try_refresh_token().await;
while let Err(e) = token {
error!("retrying to get oauth token because {e:?}");
error!(error = ?e, "retrying to get oauth token");
sleep(Duration::from_secs(10)).await;
token = self.try_refresh_token().await;
}
Expand Down
13 changes: 9 additions & 4 deletions server/src/external/download_map_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ impl MapImageDownloadTask {
Ok(bytes) => match image::load_from_memory(&bytes.0) {
Ok(img) => Some((self.index, img)),
Err(e) => {
error!("Error while parsing image: {e:?} for {self:?}");
error!(?self, error = ?e, "Error while parsing image");
None
}
},
Err(e) => {
error!("could not fulfill {self:?} because {e}");
error!(?self, error = ?e, "could not fulfill task");
None
}
}
Expand All @@ -105,7 +105,7 @@ async fn download_map_image(location: TileLocation) -> anyhow::Result<LimitedVec
let response = reqwest::get(&url).await?;
let status = response.status();
if status.as_u16() == 400 {
error!("could not find {location:?} at {url} with {status:?}");
error!(url, ?status, "could not find {location:?}");
return Err(io::Error::other("could not find requested tile").into());
}
let bytes = response.bytes().await?;
Expand All @@ -116,7 +116,12 @@ async fn download_map_image(location: TileLocation) -> anyhow::Result<LimitedVec
}
let wait_time_ms = 1.5_f32.powi(i).round() as u64;
let wait_time = Duration::from_millis(wait_time_ms);
warn!("retrying {url} in {wait_time:?} because response({status:?}) is only {size}B");
warn!(
url,
?status,
retrying_in= ?wait_time,
"retrying because response is only {size}B"
);
tokio::time::sleep(wait_time).await;
}
Err(anyhow::anyhow!("Got only short Responses from {url}"))
Expand Down
10 changes: 5 additions & 5 deletions server/src/external/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Default for GitHub {
Octocrab::builder()
.personal_token(personal_token)
.build()
.map_err(|e| error!("Could not create Octocrab instance: {e:?}"))
.map_err(|e| error!(error = ?e, "Could not create Octocrab instance"))
.ok()
} else {
None
Expand Down Expand Up @@ -56,7 +56,7 @@ impl GitHub {
.content_type("text/plain")
.body(issue.html_url.to_string()),
Err(e) => {
error!("Error creating issue: {e:?}");
error!(error = ?e, "Error creating issue");
HttpResponse::InternalServerError()
.content_type("text/plain")
.body("Failed to create issue, please try again later")
Expand Down Expand Up @@ -89,7 +89,7 @@ impl GitHub {
{
Ok(pr) => pr.number,
Err(e) => {
error!("Error creating pull request: {e:?}");
error!(error = ?e, "Error creating pull request");
return HttpResponse::InternalServerError()
.content_type("text/plain")
.body("Failed to create a pull request, please try again later");
Expand All @@ -110,7 +110,7 @@ impl GitHub {
.content_type("text/plain")
.body(issue.html_url.to_string()),
Err(e) => {
error!("Error updating PR: {e:?}");
error!(error = ?e, "Error updating PR");
HttpResponse::InternalServerError()
.content_type("text/plain")
.body("Failed to create a pull request, please try again later")
Expand Down Expand Up @@ -138,7 +138,7 @@ fn github_token() -> Option<String> {
match std::env::var("GITHUB_TOKEN") {
Ok(token) => Some(token.trim().to_string()),
Err(e) => {
error!("GITHUB_TOKEN has to be set for feedback: {e:?}");
error!(error = ?e, "GITHUB_TOKEN has to be set for feedback");
None
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async fn health_status_handler(data: web::Data<AppData>) -> HttpResponse {
.content_type("text/plain")
.body(format!("healthy\nsource_code: {github_link}")),
Err(e) => {
error!("database error: {e:?}",);
error!(error = ?e, "database error");
HttpResponse::ServiceUnavailable()
.content_type("text/plain")
.body(format!("unhealthy\nsource_code: {github_link}"))
Expand Down Expand Up @@ -157,7 +157,7 @@ fn main() -> anyhow::Result<()> {
actix_web::rt::System::new().block_on(async { run().await })?;
Ok(())
}
#[tracing::instrument(skip(pool))]
#[tracing::instrument(skip(pool, meilisearch_initialised, initialisation_started))]
async fn run_maintenance_work(
pool: Pool<Postgres>,
meilisearch_initialised: Arc<RwLock<()>>,
Expand Down
5 changes: 4 additions & 1 deletion server/src/overlays/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ impl OverlayMapTask {
"building" | "joined_building" => 16,
"virtual_room" | "room" | "poi" => 17,
entry => {
warn!("map generation encountered an type for {entry:?}. Assuming it to be a building");
warn!(
?entry,
"map generation encountered an type. Assuming it to be a building",
);
16
}
};
Expand Down
25 changes: 16 additions & 9 deletions server/src/refresh/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ fn can_never_succeed() -> bool {
Ok(s) => s.trim().is_empty(),
};
if client_id_invalid {
error!("cannot get environment variable CONNECTUM_OAUTH_CLIENT_ID, nessesary to refresh all calendars");
error!("cannot get environment variable CONNECTUM_OAUTH_CLIENT_ID, necessary to refresh all calendars");
return true;
}
let client_secret_invalid = match env::var("CONNECTUM_OAUTH_CLIENT_SECRET") {
Err(_) => true,
Ok(s) => s.trim().is_empty(),
};
if client_secret_invalid {
error!("cannot get environment variable CONNECTUM_OAUTH_CLIENT_SECRET, nessesary to refresh all calendars");
error!("cannot get environment variable CONNECTUM_OAUTH_CLIENT_SECRET, necessary to refresh all calendars");
return true;
}
false
Expand All @@ -80,7 +80,10 @@ pub async fn all_entries(pool: &PgPool) {
let ids = match entries_which_need_scraping(pool).await {
Ok(ids) => ids,
Err(e) => {
error!("Could not download get LocationKeys from the database because {e:?}");
error!(
error = ?e,
"Could not download get LocationKeys from the database",
);
continue;
}
};
Expand All @@ -95,7 +98,7 @@ pub async fn all_entries(pool: &PgPool) {

#[tracing::instrument(skip(api, pool))]
async fn refresh_events(pool: &PgPool, api: &APIRequestor, mut ids: LimitedVec<LocationKey>) {
debug!("Downloading {len} room-calendars", len = ids.len());
debug!(requested_ids_cnt = ids.len(), "downloading room-calendars");
// we want to scrape all ~2k rooms once per hour
// 1 thread is 15..20 per minute => we need at least 2 threads
// this uses a FuturesUnordered which refills itsself to be able to work effectively with lagging tasks
Expand All @@ -117,24 +120,28 @@ async fn refresh_events(pool: &PgPool, api: &APIRequestor, mut ids: LimitedVec<L
async fn refresh_single(pool: &PgPool, mut api: APIRequestor, id: String) -> anyhow::Result<()> {
let sync_start = chrono::Utc::now();
if let Err(e) = Event::update_last_calendar_scrape_at(pool, &id, &sync_start).await {
error!("could not update last_calendar_scrape_at because {e:?}");
error!(error = ?e, "could not update last_calendar_scrape_at");
return Err(e.into());
}

let events = match api.list_events(&id).await {
Ok(events) => {
debug!(
"finished fetching for {cnt} calendar events of {id}",
cnt = events.len(),
id,
fetched_events_cnt = events.len(),
"finished fetching for calendar events",
);
events
}
Err(e) => {
// TODO: this measure is to temporarily make the log usefully again until CO accepts my fix
if e.to_string() == *"error decoding response body" {
debug!("Cannot download calendar because of https://gitlab.campusonline.community/tum/connectum/-/issues/118")
debug!(
error = "https://gitlab.campusonline.community/tum/connectum/-/issues/118",
"Cannot download calendar"
)
} else {
error!("Could not download calendar because {e:?}");
error!(error = ?e, "Could not download calendar");
}
return Err(e);
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/routes/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub async fn calendar_handler(
let locations = match CalendarLocation::get_locations(&data.pool, &ids).await {
Ok(l) => l.0,
Err(e) => {
error!("could not refetch due to {e:?}");
error!(error = ?e, "could not refetch");
return HttpResponse::InternalServerError()
.content_type("text/plain")
.body("could not get calendar entries, please try again later");
Expand All @@ -97,7 +97,7 @@ pub async fn calendar_handler(
{
Ok(events) => events.0,
Err(e) => {
error!("could not get entries from the db for {ids:?} because {e:?}");
error!(error = ?e,ids = ?ids,"could not get entries from the db");
return HttpResponse::InternalServerError()
.content_type("text/plain")
.body("could not get calendar entries, please try again later");
Expand Down
7 changes: 3 additions & 4 deletions server/src/routes/feedback/proposed_edits/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,15 @@ impl AppliableEdit for Image {
match (content_result, metadata_result) {
(Ok(()), Ok(())) => success,
(Err(e), Ok(())) => {
error!("Error saving image: {e:?} for {self:?}");
error!(?self, error = ?e, "Error saving image");
"Error saving image".to_string()
}
(Ok(()), Err(e)) => {
error!("Error saving metadata: {e:?} for {self:?}");
error!(?self, error = ?e, "Error saving metadata");
"Error saving metadata".to_string()
}
(Err(content), Err(meta)) => {
error!("Error saving metadata: {meta:?} for {self:?}");
error!("Error saving content: {content:?} for {self:?}");
error!(?meta, ?content, ?self, "Error saving metadata and content");
"Error saving metadata+content".to_string()
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/routes/feedback/proposed_edits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ pub async fn propose_edits(
)
.await
}
Err(e) => {
error!("Error while applying changes: {e}", e = e);
Err(error) => {
error!(?error, "could not apply changes");
HttpResponse::InternalServerError()
.content_type("text/plain")
.body("Could apply changes, please try again later")
Expand Down
14 changes: 7 additions & 7 deletions server/src/routes/feedback/proposed_edits/tmp_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl TempRepo {
pub async fn clone_and_checkout(url: &'static str, branch_name: &str) -> anyhow::Result<Self> {
let dir = tempfile::tempdir()?;

info!("Cloning {url} into {dir:?}");
info!(url, target_dir= ?dir,"Cloning repository");
let out = Command::new("git")
.current_dir(&dir)
.arg("clone")
Expand All @@ -23,7 +23,7 @@ impl TempRepo {
.arg(dir.path())
.output()
.await?;
debug!("commit output: {out:?}");
debug!(output=?out,"git clone output");
if out.status.code() != Some(0) {
anyhow::bail!("git status failed with output: {out:?}");
}
Expand All @@ -37,7 +37,7 @@ impl TempRepo {
.arg("main")
.output()
.await?;
debug!("checkout output: {out:?}");
debug!(output=?out,"git checkout output");
match out.status.code() {
Some(0) => Ok(Self {
dir,
Expand Down Expand Up @@ -68,7 +68,7 @@ impl TempRepo {
.arg(".")
.output()
.await?;
debug!("git-add output: {out:?}");
debug!(output=?out,"git add output");
let out = Command::new("git")
.current_dir(&self.dir)
.arg("commit")
Expand All @@ -77,7 +77,7 @@ impl TempRepo {
.arg(title)
.output()
.await?;
debug!("commit output: {out:?}");
debug!(output=?out,"git commit output");
match out.status.code() {
Some(0) => Ok(()),
_ => anyhow::bail!("git commit failed with output: {out:?}"),
Expand All @@ -90,7 +90,7 @@ impl TempRepo {
.arg("status")
.output()
.await?;
debug!("git status: {out:?}");
debug!(output=?out,"git status output");
if out.status.code() != Some(0) {
anyhow::bail!("git status failed with output: {out:?}");
}
Expand All @@ -102,7 +102,7 @@ impl TempRepo {
.arg(&self.branch_name)
.output()
.await?;
debug!("git push: {out:?}");
debug!(output=?out,"git push output");
match out.status.code() {
Some(0) => Ok(()),
_ => anyhow::bail!("git push failed with output: {out:?}"),
Expand Down
4 changes: 2 additions & 2 deletions server/src/routes/feedback/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl RecordedTokens {
let kid = match jwt_token {
Ok(token) => token.claims.kid,
Err(e) => {
error!("Failed to decode token: {:?}", e.kind());
error!(kind=?e.kind(),"Failed to decode token");
return Some(HttpResponse::Forbidden().content_type("text/plain").body(
match e.kind() {
jsonwebtoken::errors::ErrorKind::ImmatureSignature => {
Expand Down Expand Up @@ -165,7 +165,7 @@ pub async fn get_token() -> HttpResponse {
HttpResponse::Created().json(Token { created_at, token })
}
Err(e) => {
error!("Failed to generate token: {e:?}");
error!(error = ?e, "Failed to generate token");
HttpResponse::InternalServerError()
.content_type("text/plain")
.body("Failed to generate token, please try again later")
Expand Down
9 changes: 5 additions & 4 deletions server/src/routes/indoor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ pub async fn get_indoor_map(
CacheDirective::Public,
]))
.json(geometry),
Err(err) => {
Err(e) => {
error!(
"Failed to fetch indoor map {id} because {err:?}",
id = params.id
error=?e,
id = params.id,
"Failed to fetch indoor map"
);
HttpResponse::InternalServerError()
.content_type("text/plain")
Expand Down Expand Up @@ -162,7 +163,7 @@ pub async fn list_indoor_maps(
let maps = match maps {
Ok(m) => m,
Err(e) => {
error!("could not list maps because {e:?}");
error!(error = ?e,"could not list maps");
return HttpResponse::InternalServerError()
.content_type("text/plain")
.body("could not get indoor maps, please try again later");
Expand Down
Loading

0 comments on commit 3d38723

Please sign in to comment.