Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better handling of non existent versions #57

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions src/partial_quicksync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,34 @@
let client = Client::new();
let conn = Connection::open(target_db_path)?;
let user_version = get_user_version(&conn)?;
let remote_metadata = client
let response = client
.get(format!(
"{}/{}/metadata.csv?version={}",
base_url,
user_version,
env!("CARGO_PKG_VERSION")
))
.send()?
.text()?;
.send()
.with_context(|| {
format!(

Check warning on line 172 in src/partial_quicksync.rs

View check run for this annotation

Codecov / codecov/patch

src/partial_quicksync.rs#L171-L172

Added lines #L171 - L172 were not covered by tests
"Failed to fetch remote metadata.csv for user_version={}",
user_version
)
})?;

if response.status() == reqwest::StatusCode::NOT_FOUND {
anyhow::bail!(
"Remote server returned 404 for metadata.csv. User version {} might not exist.",
user_version
);
}

let remote_metadata = response.text().with_context(|| {
format!(

Check warning on line 186 in src/partial_quicksync.rs

View check run for this annotation

Codecov / codecov/patch

src/partial_quicksync.rs#L186

Added line #L186 was not covered by tests
"Failed to read remote metadata.csv for user_version={}",
user_version
)
})?;

let latest_layer = get_latest_from_db(&conn)?;
let layer_from = (latest_layer + 1).saturating_sub(untrusted_layers);
Expand Down Expand Up @@ -636,4 +655,31 @@
.contains("No suitable restore points found, seems that state.sql is too old"));
mock_metadata.assert();
}

#[test]
fn non_existing_user_version() {
let dir = tempdir().unwrap();
let db_path = dir.path().join("state.db");
{
let conn = create_test_db(Some(&db_path));
insert_layer(&conn, 80, 100, &[0xFF, 0xFF]);
}
let mut server = mockito::Server::new();

let mock_metadata = server
.mock("GET", "/0/metadata.csv")
.match_query(Matcher::UrlEncoded(
"version".into(),
env!("CARGO_PKG_VERSION").into(),
))
.with_status(404)
.with_body("Not Found")
.create();
let err = super::partial_restore(&server.url(), &db_path, dir.path(), 0, 0).unwrap_err();
println!("{}", err);
assert!(err
.to_string()
.contains("Remote server returned 404 for metadata.csv. User version 0 might not exist."));
mock_metadata.assert();
}
}