150 lines
4.2 KiB
Rust
150 lines
4.2 KiB
Rust
|
|
//! Comprehensive integration tests for the audit module
|
||
|
|
|
||
|
|
use audit::{AuditConfig, AuditCoordinator, AuditRunner, Sbom, SbomFormat};
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_audit_coordinator_creation() {
|
||
|
|
let config = AuditConfig::default();
|
||
|
|
let coordinator = AuditCoordinator::new(config);
|
||
|
|
assert!(coordinator.is_ok());
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_audit_runner_creation() {
|
||
|
|
let config = AuditConfig::default();
|
||
|
|
let coordinator = AuditCoordinator::new(config).expect("Failed to create coordinator");
|
||
|
|
let _runner = AuditRunner::new(coordinator);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[tokio::test]
|
||
|
|
async fn test_audit_runner_sbom_generation() {
|
||
|
|
let config = AuditConfig::default();
|
||
|
|
let coordinator = AuditCoordinator::new(config).expect("Failed to create coordinator");
|
||
|
|
let runner = AuditRunner::new(coordinator);
|
||
|
|
|
||
|
|
let sbom = runner.run_sbom().await;
|
||
|
|
assert!(sbom.is_ok());
|
||
|
|
|
||
|
|
let sbom_data = sbom.unwrap();
|
||
|
|
assert_eq!(sbom_data.name, "audit-sbom");
|
||
|
|
assert_eq!(sbom_data.format, SbomFormat::CycloneDx);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_sbom_creation() {
|
||
|
|
let sbom = Sbom::new("test-sbom", SbomFormat::CycloneDx, "test-app", "1.0.0");
|
||
|
|
|
||
|
|
assert_eq!(sbom.name, "test-sbom");
|
||
|
|
assert!(sbom.components.is_empty());
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_config_from_defaults() {
|
||
|
|
let config = AuditConfig::default();
|
||
|
|
assert_eq!(config.name, "audit");
|
||
|
|
assert!(config.enabled);
|
||
|
|
assert_eq!(config.sbom.format, "cyclonedx");
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(feature = "chaos")]
|
||
|
|
#[test]
|
||
|
|
fn test_chaos_scenario_enum() {
|
||
|
|
use audit::chaos::{ChaosCluster, ChaosScenario};
|
||
|
|
|
||
|
|
assert_eq!(ChaosScenario::PodDelete.as_str(), "pod_delete");
|
||
|
|
assert_eq!(ChaosScenario::NetworkLatency.as_str(), "network_latency");
|
||
|
|
|
||
|
|
assert_eq!(ChaosCluster::Mesh.as_str(), "chaos-mesh");
|
||
|
|
assert_eq!(ChaosCluster::Litmus.as_str(), "litmus");
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(feature = "chaos")]
|
||
|
|
#[test]
|
||
|
|
fn test_chaos_experiment_creation() {
|
||
|
|
use audit::chaos::{ChaosExperiment, ChaosScenario};
|
||
|
|
|
||
|
|
let experiment = ChaosExperiment::new(
|
||
|
|
"test-experiment",
|
||
|
|
ChaosScenario::PodDelete,
|
||
|
|
"default",
|
||
|
|
"test-app",
|
||
|
|
);
|
||
|
|
|
||
|
|
assert_eq!(experiment.name, "test-experiment");
|
||
|
|
assert_eq!(experiment.namespace, "default");
|
||
|
|
assert_eq!(experiment.workload, "test-app");
|
||
|
|
assert_eq!(experiment.duration_seconds, 300);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(feature = "benchmark")]
|
||
|
|
#[test]
|
||
|
|
fn test_benchmark_result_creation() {
|
||
|
|
use audit::benchmark::{BenchmarkResult, BenchmarkSuite};
|
||
|
|
|
||
|
|
let result = BenchmarkResult::new("test-operation", 100.0, 10);
|
||
|
|
assert_eq!(result.operation, "test-operation");
|
||
|
|
assert_eq!(result.mean_ms, 10.0);
|
||
|
|
assert_eq!(result.throughput, 100.0);
|
||
|
|
|
||
|
|
let mut suite = BenchmarkSuite::new("test-suite");
|
||
|
|
suite.add_result(result);
|
||
|
|
assert_eq!(suite.total_duration_ms(), 100.0);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(feature = "knowledge-docs")]
|
||
|
|
#[test]
|
||
|
|
fn test_knowledge_base_creation_and_search() {
|
||
|
|
use audit::knowledge::{KnowledgeBase, KnowledgeDocument};
|
||
|
|
|
||
|
|
let mut kb = KnowledgeBase::new("test-kb", "1.0.0");
|
||
|
|
|
||
|
|
let doc = KnowledgeDocument::new("CVE Guide", "How to handle CVEs", "Guide")
|
||
|
|
.with_keyword("cve")
|
||
|
|
.with_category("security");
|
||
|
|
|
||
|
|
kb.add_document(doc);
|
||
|
|
|
||
|
|
let results = kb.search("cve");
|
||
|
|
assert_eq!(results.len(), 1);
|
||
|
|
assert_eq!(results[0].title, "CVE Guide");
|
||
|
|
}
|
||
|
|
|
||
|
|
#[tokio::test]
|
||
|
|
async fn test_audit_workflow_integration() {
|
||
|
|
// Test the complete audit workflow
|
||
|
|
let config = AuditConfig::default();
|
||
|
|
|
||
|
|
assert!(config.validate().is_ok());
|
||
|
|
let coordinator = AuditCoordinator::new(config);
|
||
|
|
assert!(coordinator.is_ok());
|
||
|
|
|
||
|
|
let coordinator = coordinator.unwrap();
|
||
|
|
let runner = AuditRunner::new(coordinator);
|
||
|
|
|
||
|
|
// Test SBOM generation
|
||
|
|
let sbom = runner.run_sbom().await;
|
||
|
|
assert!(sbom.is_ok());
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_concurrent_audit_operations() {
|
||
|
|
use std::sync::Arc;
|
||
|
|
use std::thread;
|
||
|
|
|
||
|
|
let config = Arc::new(AuditConfig::default());
|
||
|
|
let mut handles = vec![];
|
||
|
|
|
||
|
|
for i in 0..3 {
|
||
|
|
let config_clone = Arc::clone(&config);
|
||
|
|
let handle = thread::spawn(move || {
|
||
|
|
let coordinator = AuditCoordinator::new((*config_clone).clone());
|
||
|
|
assert!(coordinator.is_ok(), "Thread {} failed", i);
|
||
|
|
});
|
||
|
|
handles.push(handle);
|
||
|
|
}
|
||
|
|
|
||
|
|
for handle in handles {
|
||
|
|
assert!(handle.join().is_ok());
|
||
|
|
}
|
||
|
|
}
|