Skip to content

Commit

Permalink
Create isotoenv library
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteOtter committed Aug 14, 2024
1 parent e2c08ae commit 50b3f89
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 0 deletions.
21 changes: 21 additions & 0 deletions isotoenv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# Added by cargo
/target

# Editor Configs
.idea/
.vscode/
18 changes: 18 additions & 0 deletions isotoenv/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "isotoenv"
version = "0.1.0"
edition = "2021"
description = "A library for creating an openQA test environment on a designated target"
readme = "README.md"
repository = "https://github.com/os-autoinst/isotest-ng/tree/main/isotoenv"
license = "GPL-2.0"

[dependencies]
log = "0.4.22"
env_logger = { version = "0.11.5", optional = true }

[dev-dependencies]

[features]
# Feature to enable default logging configuration
default-logging = ["env_logger"]
339 changes: 339 additions & 0 deletions isotoenv/LICENSE

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions isotoenv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# isotoenv

Set up a [openQA](https://open.qa) test environment on a remote worker.
3 changes: 3 additions & 0 deletions isotoenv/src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! This module defines custom error types to be returned by `isototest`.
//! These types are thematically split into submodules.
pub mod util_errors;
28 changes: 28 additions & 0 deletions isotoenv/src/errors/util_errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! This module defines and implements error type which refer to the supporting util of this library.
//! This means functionality like logging and similar supporting functionality, which do not necessarily
//! impact the core functionality of this library itself.
use std::fmt;

#[derive(Debug)]
#[cfg(feature = "default-logging")]
pub enum LoggingError {
LoggingInitError(String),
InvalidLogLevelError(String),
}

#[cfg(feature = "default-logging")]
impl fmt::Display for LoggingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LoggingError::LoggingInitError(msg) => {
write!(f, "[error] Logging initialization failed: '{}'", msg)
}
LoggingError::InvalidLogLevelError(msg) => {
write!(f, "[error] Invalid log level: '{}'", msg)
}
}
}
}

#[cfg(feature = "default-logging")]
impl std::error::Error for LoggingError {}
23 changes: 23 additions & 0 deletions isotoenv/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Prepare a test environment for [openQA](https://open.qa).
//!
//! This crate is responsible for preparing an `openQA` worker machine or VM for executing the test suite.
//!
//! ## Optional Features
//!
//! * `default-logging` - Provides you with a sensible logger configuration using the `env_logger` crate.
// Organize library structure
pub mod logging;
pub mod errors;

// Provide code on the root level of the library
#[cfg(feature = "default-logging")]
use crate::logging::init_default_logging;
#[cfg(feature = "default-logging")]
use crate::errors::util_errors::LoggingError;

#[cfg(feature = "default-logging")]
pub fn init_logging(level: Option<&str>) -> Result<(), LoggingError> {
init_default_logging(level)?;
Ok(())
}
56 changes: 56 additions & 0 deletions isotoenv/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! This module provides a sensible default configuration of a logging system.
#[cfg(feature = "default-logging")]
use env_logger::Builder;
#[cfg(feature = "default-logging")]
use std::io::Write;
#[cfg(feature = "default-logging")]
use crate::errors::util_errors::LoggingError;

pub const LOG_TARGET: &str = "[isototest]";

#[cfg(feature = "default-logging")]
/// Initialize default logging configuration with given log level.
///
/// Log Level can be:
/// * "info" - Default log level also adapted if level is `none`. Logs events relevant for general usage.
/// * "debug" - Debug level for extended logging. Should only be used for development purposes.
/// * "trace" - Extensive log evel logging every event. Should only be used for development purposes.
///
pub(crate) fn init_default_logging(level: Option<&str>) -> Result<(), LoggingError> {
match level {
Some("info") | None => {
log_builder(log::LevelFilter::Info);
Ok(())
},
Some("debug") => {
log_builder(log::LevelFilter::Debug);
Ok(())
},
Some("trace") => {
log_builder(log::LevelFilter::Trace);
Ok(())
},
Some(invalid) => {
Err(LoggingError::InvalidLogLevelError(format!("Invalid log level '{}'!", invalid)))
}
}
}

#[cfg(feature = "default-logging")]
fn log_builder(level: log::LevelFilter) {
Builder::new()
.filter_level(level)
.format(|buf, record| {
writeln!(
buf,
"{} [{}] {}: {}",
buf.timestamp(),
record.level(),
record.target(),
record.args()
)
})
.init();
}

0 comments on commit 50b3f89

Please sign in to comment.