Skip to content

Commit

Permalink
Add integration test for sqlite db
Browse files Browse the repository at this point in the history
  • Loading branch information
cholcombe973 committed Sep 20, 2017
1 parent 3acf901 commit f110629
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/host_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use self::block_utils::{RaidType, ScsiInfo};
use self::uname::uname;

/// All the host information we could gather
#[derive(Debug)]
pub struct Host {
pub hostname: String,
pub kernel: String,
Expand Down
46 changes: 36 additions & 10 deletions src/in_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@ use std::path::Path;
use self::time::Timespec;
use self::rusqlite::{Connection, Result};

#[cfg(test)]
mod tests {
extern crate mktemp;

use std::path::Path;

use self::mktemp::Temp;

#[test]
fn test_in_progress() {
let temp_dir = Temp::new_dir().expect("mktemp creation failed");
let mut db_file = temp_dir.to_path_buf();
db_file.push("test_db.sqlite3");

let conn = super::create_repair_database(&db_file).expect("sqlite3 creation failed");
super::record_new_repair_ticket(&conn, "001", &Path::new("/dev/sda"))
.expect("Create repair ticket failed");
let result = super::is_disk_in_progress(&conn, &Path::new("/dev/sda"))
.expect("failed to query disk in progress");
println!(
"Outstanding repair tickets: {:?}",
super::get_outstanding_repair_tickets(&conn)
);

assert!(result);
}
}

#[derive(Debug)]
pub struct DiskRepairTicket {
pub id: i32,
Expand Down Expand Up @@ -35,20 +63,18 @@ pub fn record_new_repair_ticket(
ticket_id: &str,
disk_path: &Path,
) -> Result<()> {
let ticket = DiskRepairTicket {
id: 0,
ticket_id: ticket_id.into(),
time_created: time::get_time(),
disk_path: disk_path.to_string_lossy().into_owned(),
};
debug!("Recording new repair ticket: {:?}", ticket);
debug!(
"Recording new repair ticket: id: {}, disk_path: {}",
ticket_id,
disk_path.display()
);
conn.execute(
"INSERT INTO repairs (ticket_id, time_created, disk_path)
VALUES (?1, ?2, ?3)",
&[
&ticket.ticket_id,
&ticket.time_created,
&ticket.disk_path,
&ticket_id.to_string(),
&time::get_time(),
&disk_path.to_string_lossy().into_owned(),
],
)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn check_for_failed_disks(config_dir: &str, simulate: bool) -> Result<(), String
let config_location = Path::new(&config.db_location);
//Host information to use in ticket creation
let host_info = Host::new().map_err(|e| e.to_string())?;
debug!("Gathered host info: {:?}", host_info);
let mut description = format!(
" disk on {} failed. Please investigate.
Details: Disk {} as failed. Please replace if necessary",
Expand Down Expand Up @@ -95,7 +96,6 @@ Details: Disk {} as failed. Please replace if necessary",
|e| e.to_string(),
)?;
if !simulate {
// TODO: Double check that this disk isn't already in progress
info!("Connecting to database to check if disk is in progress");
let conn = in_progress::create_repair_database(&config_location)
.map_err(|e| e.to_string())?;
Expand Down
6 changes: 2 additions & 4 deletions src/test_disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ extern crate libatasmart;
extern crate log;
extern crate mktemp;

use self::block_utils::{Device, get_block_devices, get_mount_device, get_mountpoint,
FilesystemType, is_mounted, MediaType, RaidType};
use self::fstab::{FsEntry, FsTab};
use self::block_utils::{Device, get_mountpoint, FilesystemType, MediaType};
use self::mktemp::Temp;

use std::fs::{File, OpenOptions};
use std::fs::OpenOptions;
use std::io::{Error, ErrorKind};
use std::io::{Result, Write};
use std::path::{Path, PathBuf};
Expand Down

0 comments on commit f110629

Please sign in to comment.