1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::time::Duration;
use crate::error::ErrorInner;
use crate::util::{KillOnDrop, TempDir};
use crate::Error;
use std::process::Command;
/// A temporary MongoDB instance.
///
/// All state of the MongoDB instance is stored in a temporary directory.
/// Unless disabled, the temporary directory is deleted when this object is dropped.
pub struct TempMongo {
tempdir: TempDir,
socket_path: PathBuf,
log_path: PathBuf,
client: mongodb::Client,
server: KillOnDrop,
}
impl TempMongo {
/// Spawn a new MongoDB instance with a temporary state directory.
pub async fn new() -> Result<Self, Error> {
Self::from_builder(&TempMongoBuilder::new()).await
}
/// Create a builder to customize your [`TempMongo`].
///
/// After configuring the desirec options, run [`TempMongoBuilder::spawn()`].
pub fn builder() -> TempMongoBuilder {
TempMongoBuilder::new()
}
/// Get the PID of the MongoDB process.
pub fn process_id(&self) -> u32 {
self.server.id()
}
/// Get the path of the temporary state directory.
pub fn directory(&self) -> &Path {
self.tempdir.path()
}
/// Get the path of the listening socket of the MongoDB instance.
pub fn socket_path(&self) -> &Path {
&self.socket_path
}
/// Get the path of the log file of the MongoDB instance.
pub fn log_path(&self) -> &Path {
&self.log_path
}
/// Get a client for the MongDB instance.
///
/// This returns a client by reference,
/// but it can be cloned and sent to other threads or tasks if needed.
pub fn client(&self) -> &mongodb::Client {
&self.client
}
/// Enable or disable clean-up of the temporary directory when this object is dropped.
pub fn set_clean_on_drop(&mut self, clean_on_drop: bool) {
self.tempdir.set_clean_on_drop(clean_on_drop);
}
/// Kill the server and remove the temporary state directory on the filesystem.
///
/// Note that the server will also be killed when this object is dropped,
/// and unless disabled, the temporary state directory will be removed by the [`Drop`] implementation too.
///
/// This function ignores the value of `clean_on_drop`.
/// It also allows for better error handling compared to just dropping the object.
pub async fn kill_and_clean(mut self) -> Result<(), Error> {
self.client.shutdown_immediate().await;
self.server.kill()
.map_err(ErrorInner::KillServer)?;
let path = self.tempdir.path().to_owned();
self.tempdir.close()
.map_err(|e| ErrorInner::CleanDir(path, e))?;
Ok(())
}
/// Kill the server, but leave the temporary state directory on the filesystem.
///
/// Note that the server will also be killed when this object is dropped.
///
/// This function ignores the value of `clean_on_drop`.
/// It also allows for better error handling compared to just dropping the object.
pub async fn kill_no_clean(mut self) -> Result<(), Error> {
let _path = self.tempdir.into_path();
self.client.shutdown_immediate().await;
self.server.kill()
.map_err(ErrorInner::KillServer)?;
Ok(())
}
/// Create the temporary directory and spawn a server based on the configuration of the given builder object.
async fn from_builder(builder: &TempMongoBuilder) -> Result<Self, Error> {
let tempdir = builder.make_temp_dir().map_err(ErrorInner::MakeTempDir)?;
let db_dir = tempdir.path().join("db");
let socket_path = tempdir.path().join("mongod.sock");
let log_path = tempdir.path().join("mongod.log");
std::fs::create_dir(&db_dir)
.map_err(|e| ErrorInner::MakeDbDir(db_dir.clone(), e))?;
let server = Command::new(builder.get_command())
.arg("--bind_ip")
.arg(&socket_path)
.arg("--dbpath")
.arg(db_dir)
.arg("--logpath")
.arg(&log_path)
.arg("--nounixsocket")
.arg("--noauth")
.spawn()
.map_err(|e| ErrorInner::SpawnServer(builder.get_command_string(), e))?;
let server = KillOnDrop::new(server);
let client_options = mongodb::options::ClientOptions::builder()
.hosts(vec![mongodb::options::ServerAddress::Unix { path: socket_path.clone() }])
.connect_timeout(Duration::from_millis(10))
.build();
let client = mongodb::Client::with_options(client_options)
.map_err(|e| ErrorInner::Connect(socket_path.display().to_string(), e))?;
client.list_databases(None, None).await
.map_err(|e| ErrorInner::Connect(socket_path.display().to_string(), e))?;
Ok(Self {
tempdir,
socket_path,
log_path,
server,
client,
})
}
}
/// Builder for customizing your [`TempMongo`] object.
///
/// After configuring the desirec options, run [`TempMongoBuilder::spawn()`].
pub struct TempMongoBuilder {
/// The parent directory for the temporary directory.
///
/// Use the system default if set to `None`.
parent_directory: Option<PathBuf>,
/// Clean up the temprorary directory when the [`TempMongo`] object is dropped.
clean_on_drop: bool,
/// The mongdb command to execute.
command: Option<OsString>,
}
impl TempMongoBuilder {
/// Create a new builder.
pub fn new() -> Self {
Self {
parent_directory: None,
command: None,
clean_on_drop: true,
}
}
/// Spawn the MongoDB server and connect to it.
pub async fn spawn(&self) -> Result<TempMongo, Error> {
TempMongo::from_builder(self).await
}
/// Enable or disable cleaning of the temporary state directory when the [`TempMongo`] object is dropped.
///
/// This can also be changed after creation with [`TempMongo::set_clean_on_drop()`].
pub fn clean_on_drop(mut self, clean_on_drop: bool) -> Self {
self.clean_on_drop = clean_on_drop;
self
}
/// Overwrite the `mongod` command to run.
///
/// Can be used to run a `mongod` binary from an alternative location.
pub fn mongod_command(mut self, command: impl Into<OsString>) -> Self {
self.command = Some(command.into());
self
}
/// Get the command to execute to run MongoDB.
fn get_command(&self) -> &OsStr {
self.command
.as_deref()
.unwrap_or("mongod".as_ref())
}
/// Get the command to execute to run MongDB as a string, for diagnostic purposes.
fn get_command_string(&self) -> String {
self.get_command().to_string_lossy().into()
}
/// Create a temporary directory according to the configuration of the builder.
fn make_temp_dir(&self) -> std::io::Result<TempDir> {
match &self.parent_directory {
Some(dir) => TempDir::new_in(dir, self.clean_on_drop),
None => TempDir::new(self.clean_on_drop),
}
}
}
impl Default for TempMongoBuilder {
fn default() -> Self {
Self::new()
}
}