This document summarizes the testing migration performed in the Merka Vault project.
We have successfully migrated the tests from the tests/integration.rs file to their appropriate module-specific locations. This migration follows best practices for Rust testing by placing tests close to the code they are testing.
-
Test Utilities
- Enhanced
src/vault/test_utils.rsto support container-based testing - Made it available to all module tests via
#[cfg(test)] pub mod test_utils;inmod.rs - Simplified container setup and improved error handling
- Enhanced
-
Module-Specific Tests
- Migrated PKI tests to
src/vault/pki.rs - Migrated transit tests to
src/vault/transit.rs - Migrated auth tests to
src/vault/auth.rs - Migrated init tests to
src/vault/init.rs - Migrated autounseal tests to
src/vault/autounseal.rs - Migrated seal tests to
src/vault/seal.rs - Migrated status tests to
src/vault/status.rs
- Migrated PKI tests to
-
Documentation Updates
- Updated
docs/testing.mdto reflect the new testing approach - Updated
tests/README.mdwith information about module-specific testing - Updated main
README.mdwith new test commands and recent changes - Created this summary document for future reference
- Updated
-
Cleanup
- Removed
tests/integration.rsas all tests have been migrated - Removed the need for the "testing" feature flag
- Updated all documentation references to integration.rs
- Removed
-
Improved Organization
- Tests are now located alongside the code they test
- Each module is responsible for its own test cases
- Tests are more focused and easier to maintain
-
Simpler Test Setup
- Tests can run without Docker for basic functionality
- Container-based tests are more reliable and focused
- Removed dependencies on external setup scripts
-
Better Error Handling
- Tests properly handle network errors when Vault isn't running
- Clear distinction between expected and unexpected errors
- Improved test reliability in various environments
-
Maintainability
- Tests will evolve alongside the code they test
- Easier to add new tests when adding new functionality
- Clearer ownership of test cases
-
Simplified CI/CD
- No need for special feature flags or test runners
- Standard
cargo testcommands can run all tests - Better test isolation for parallelization
-
Unit Testing
- Each module has its own unit tests in a
#[cfg(test)] mod testsblock - Tests focus on specific functionality within the module
- Error handling is tested thoroughly
- Each module has its own unit tests in a
-
Test Utilities
- Shared test utilities are available to all modules
- Support for different Vault modes (Dev, Regular, AutoUnseal)
- Helper functions for common test operations
-
Architectural Boundaries
- Tests respect the module boundaries of the codebase
- Public API is tested through integration tests
- Internal implementation details are tested through unit tests
Before (in integration.rs):
#[tokio::test]
async fn test_setup_pki() -> Result<(), Box<dyn std::error::Error>> {
init_logging();
let vault_container = setup_vault_container(common::VaultMode::Dev).await;
let host = vault_container.get_host().await.unwrap();
let host_port = vault_container.get_host_port_ipv4(8200).await.unwrap();
let vault_url = format!("http://{}:{}", host, host_port);
common::wait_for_vault_ready(&vault_url, 10, 1000).await?;
let domain = "example.com";
let ttl = "8760h";
let (cert, role_name) =
merka_vault::vault::pki::setup_pki(&vault_url, "root", domain, ttl, false, None, None)
.await?;
info!("CA Certificate:\n{}", cert);
info!("PKI role name: {}", role_name);
assert!(cert.contains("BEGIN CERTIFICATE"));
assert_eq!(role_name, domain.replace('.', "-"));
Ok(())
}After (in pki.rs):
#[cfg(test)]
mod tests {
use super::*;
use crate::init_logging;
use crate::vault::test_utils::{setup_vault_container, wait_for_vault_ready, VaultMode};
use tracing::info;
#[tokio::test]
async fn test_setup_pki() -> Result<(), Box<dyn std::error::Error>> {
init_logging();
let vault_container = setup_vault_container(VaultMode::Dev).await;
let host = vault_container.get_host().await.unwrap();
let host_port = vault_container.get_host_port_ipv4(8200).await.unwrap();
let vault_url = format!("http://{}:{}", host, host_port);
wait_for_vault_ready(&vault_url, 10, 1000)
.await
.map_err(|e| e.to_string())?;
let domain = "example.com";
let ttl = "8760h";
let (cert, role_name) =
setup_pki(&vault_url, "root", domain, ttl, false, None, None).await?;
info!("CA Certificate:\n{}", cert);
info!("PKI role name: {}", role_name);
assert!(cert.contains("BEGIN CERTIFICATE"));
assert_eq!(role_name, domain.replace('.', "-"));
Ok(())
}
// Other tests...
}The migration to module-specific tests has been successful, resulting in a more maintainable and organized codebase. The new testing approach follows Rust best practices and provides better isolation, improved error handling, and clearer ownership of test cases.
Going forward, all new tests should follow this module-specific approach, with integration tests using the actor-based API for cross-module functionality.