Skip to content

Commit

Permalink
Fixes missing tmp_dir for watchtower-plugin
Browse files Browse the repository at this point in the history
Some of the tmp paths in the `watchtower-plugin` tests were not using
`TempDir` and still manually removing the directories.
  • Loading branch information
sr-gi committed Aug 16, 2022
1 parent cfaa7aa commit cca0dcf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 62 deletions.
79 changes: 35 additions & 44 deletions watchtower-plugin/src/retrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ mod tests {

use httpmock::prelude::*;
use serde_json::json;
use tokio::fs;
use tempdir::TempDir;
use tokio::sync::mpsc::unbounded_channel;

use teos_common::errors;
Expand All @@ -202,9 +202,11 @@ mod tests {
// TODO: It'll be nice to toggle the mock on and off instead of having it always on. Not sure MockServer allows that though:
// https://github.com/alexliesenfeld/httpmock/issues/67
async fn test_manage_retry_reachable() {
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let (tx, rx) = unbounded_channel();
let wt_client = Arc::new(Mutex::new(WTClient::new(tmp_path.into(), tx.clone()).await));
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.path().to_path_buf(), tx.clone()).await,
));
let server = MockServer::start();

// Add a tower with pending appointments
Expand Down Expand Up @@ -272,14 +274,15 @@ mod tests {
api_mock.assert();

task.abort();
fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_manage_retry_unreachable() {
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let (tx, rx) = unbounded_channel();
let wt_client = Arc::new(Mutex::new(WTClient::new(tmp_path.into(), tx.clone()).await));
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.path().to_path_buf(), tx.clone()).await,
));

// Add a tower with pending appointments
let (_, tower_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -336,14 +339,15 @@ mod tests {
);

task.abort();
fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_manage_retry_rejected() {
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let (tx, rx) = unbounded_channel();
let wt_client = Arc::new(Mutex::new(WTClient::new(tmp_path.into(), tx.clone()).await));
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.path().to_path_buf(), tx.clone()).await,
));
let server = MockServer::start();

// Add a tower with pending appointments
Expand Down Expand Up @@ -414,14 +418,15 @@ mod tests {
api_mock.assert();

task.abort();
fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_manage_retry_misbehaving() {
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let (tx, rx) = unbounded_channel();
let wt_client = Arc::new(Mutex::new(WTClient::new(tmp_path.into(), tx.clone()).await));
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.path().to_path_buf(), tx.clone()).await,
));
let server = MockServer::start();

// Add a tower with pending appointments
Expand Down Expand Up @@ -479,14 +484,15 @@ mod tests {
api_mock.assert();

task.abort();
fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_manage_retry_abandoned() {
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let (tx, rx) = unbounded_channel();
let wt_client = Arc::new(Mutex::new(WTClient::new(tmp_path.into(), tx.clone()).await));
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.path().to_path_buf(), tx.clone()).await,
));
let server = MockServer::start();

// Add a tower with pending appointments
Expand Down Expand Up @@ -516,16 +522,15 @@ mod tests {
assert!(!wt_client.lock().unwrap().towers.contains_key(&tower_id));

task.abort();
fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_add_appointment() {
let (tower_sk, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.into(), unbounded_channel().0).await,
WTClient::new(tmp_path.path().to_path_buf(), unbounded_channel().0).await,
));
let server = MockServer::start();

Expand Down Expand Up @@ -562,17 +567,15 @@ mod tests {
let r = Retrier::dummy(wt_client).add_appointment(tower_id).await;
assert_eq!(r, Ok(()));
api_mock.assert();

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_add_appointment_no_pending() {
let (_, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.into(), unbounded_channel().0).await,
WTClient::new(tmp_path.path().to_path_buf(), unbounded_channel().0).await,
));
let server = MockServer::start();

Expand All @@ -588,17 +591,15 @@ mod tests {
let r = Retrier::dummy(wt_client).add_appointment(tower_id).await;

assert_eq!(r, Ok(()));

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_add_appointment_misbehaving() {
let (_, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.into(), unbounded_channel().0).await,
WTClient::new(tmp_path.path().to_path_buf(), unbounded_channel().0).await,
));
let server = MockServer::start();

Expand Down Expand Up @@ -634,17 +635,15 @@ mod tests {
let r = Retrier::dummy(wt_client).add_appointment(tower_id).await;
assert_eq!(r, Err(Error::permanent("Tower misbehaved")));
api_mock.assert();

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_add_appointment_unreachable() {
let (_, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.into(), unbounded_channel().0).await,
WTClient::new(tmp_path.path().to_path_buf(), unbounded_channel().0).await,
));

// The tower we'd like to retry sending appointments to has to exist within the plugin
Expand All @@ -663,17 +662,15 @@ mod tests {
.add_pending_appointment(tower_id, &appointment);
let r = Retrier::dummy(wt_client).add_appointment(tower_id).await;
assert_eq!(r, Err(Error::transient("Tower cannot be reached")));

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_add_appointment_subscription_error() {
let (_, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.into(), unbounded_channel().0).await,
WTClient::new(tmp_path.path().to_path_buf(), unbounded_channel().0).await,
));
let server = MockServer::start();

Expand Down Expand Up @@ -705,17 +702,15 @@ mod tests {

assert_eq!(r, Err(Error::transient("Subscription error")));
api_mock.assert();

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_add_appointment_rejected() {
let (_, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.into(), unbounded_channel().0).await,
WTClient::new(tmp_path.path().to_path_buf(), unbounded_channel().0).await,
));
let server = MockServer::start();

Expand Down Expand Up @@ -757,17 +752,15 @@ mod tests {
.unwrap()
.invalid_appointments
.contains(&appointment.locator));

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_add_appointment_abandoned() {
let (_, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.into(), unbounded_channel().0).await,
WTClient::new(tmp_path.path().to_path_buf(), unbounded_channel().0).await,
));
let server = MockServer::start();

Expand All @@ -789,7 +782,5 @@ mod tests {
r,
Err(Error::permanent("Tower was flagged to be abandoned"))
);

fs::remove_dir_all(tmp_path).await.unwrap();
}
}
27 changes: 9 additions & 18 deletions watchtower-plugin/src/wt_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,6 @@ mod tests {
.lock()
.unwrap()
.appointment_exists(appointment.locator));

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -608,8 +606,6 @@ mod tests {
.lock()
.unwrap()
.appointment_exists(appointment.locator));

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -701,8 +697,6 @@ mod tests {
.lock()
.unwrap()
.appointment_exists(appointment.locator));

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -743,8 +737,9 @@ mod tests {

#[tokio::test]
async fn test_remove_tower() {
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let mut wt_client = WTClient::new(tmp_path.into(), unbounded_channel().0).await;
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let mut wt_client =
WTClient::new(tmp_path.path().to_path_buf(), unbounded_channel().0).await;

let receipt = get_random_registration_receipt();
let (tower_sk, tower_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -808,17 +803,16 @@ mod tests {
.lock()
.unwrap()
.appointment_receipt_exists(locator, tower_id));

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_remove_tower_shared_appointment() {
// Lets test removing a tower that has associated data shared with another tower.
// For instance, having an appointment that was sent to two towers, and then deleting one of them
// should only remove the link between the tower and the appointment, but not delete the data.
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let mut wt_client = WTClient::new(tmp_path.into(), unbounded_channel().0).await;
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let mut wt_client =
WTClient::new(tmp_path.path().to_path_buf(), unbounded_channel().0).await;

let receipt = get_random_registration_receipt();
let (tower1_sk, tower1_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -886,20 +880,17 @@ mod tests {
.lock()
.unwrap()
.appointment_receipt_exists(locator, tower2_id));

fs::remove_dir_all(tmp_path).await.unwrap();
}

#[tokio::test]
async fn test_remove_inexistent_tower() {
let tmp_path = &format!(".watchtower_{}/", get_random_user_id());
let mut wt_client = WTClient::new(tmp_path.into(), unbounded_channel().0).await;
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let mut wt_client =
WTClient::new(tmp_path.path().to_path_buf(), unbounded_channel().0).await;

assert!(matches!(
wt_client.remove_tower(get_random_user_id()),
Err(DBError::NotFound)
));

fs::remove_dir_all(tmp_path).await.unwrap();
}
}

0 comments on commit cca0dcf

Please sign in to comment.