voa-core 0.4.1

File Hierarchy for the Verification of OS Artifacts (VOA)
Documentation
//! Containerized integration tests

#![cfg(feature = "_containerized-integration-test")]
use std::{
    collections::BTreeMap,
    fs::{File, create_dir_all},
    io::Write,
    os::unix::fs::symlink,
    path::{Path, PathBuf},
};

use log::debug;
use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
use voa_core::Verifier;

mod system;
mod user;

/// Init logger
pub fn init_logger() {
    if TermLogger::init(
        LevelFilter::Debug,
        Config::default(),
        TerminalMode::Stderr,
        ColorChoice::Auto,
    )
    .is_err()
    {
        debug!("Not initializing another logger, as one is initialized already.");
    }
}

/// Objects to create for a VOA test setup
#[derive(Debug)]
pub enum TestObject {
    /// A filesystem path
    Path(&'static str),

    /// An empty file
    File(&'static str),

    /// A file with specific content
    FileWithContent(&'static str, &'static [u8]),

    /// A symlink (from the second path to the first path)
    SymLink(&'static str, &'static str),
}

/// Set up a test environment
///
/// Note that the list of `objects` is processed in order, so the ordering of entries is important!
/// (E.g.: a directory must be created first, before creating a file inside that directory).
pub fn setup(objects: &[TestObject]) -> std::io::Result<()> {
    for object in objects {
        match object {
            TestObject::Path(p) => create_dir_all(p)?,
            TestObject::File(f) => {
                File::create(f)?;
            }
            TestObject::FileWithContent(f, data) => {
                let mut f = File::create(f)?;
                f.write_all(data)?;
            }
            TestObject::SymLink(orig, link) => symlink(orig, link)?,
        }
    }

    Ok(())
}

/// Check if the canonical paths in "verifiers" match the paths in "expected"
fn compare_expected(verifiers: &BTreeMap<PathBuf, Vec<Verifier>>, expected: &[&str]) {
    let paths: Vec<_> = verifiers.keys().collect();
    let expected: Vec<_> = expected.iter().map(Path::new).collect();
    assert_eq!(paths, expected);
}