pub struct TestContext {
pub config: Config,
pub services: Services,
pub environment: AnyEnvironmentState,
pub keep_env: bool,
/* private fields */
}Expand description
Main test context combining configuration and services
Fields§
§config: Config§services: Services§environment: AnyEnvironmentStateThe complete environment configuration containing instance name, SSH keys, and paths.
Stored as AnyEnvironmentState to track the actual current state throughout the
deployment lifecycle (Created → Provisioning → Provisioned → Configuring → Configured, etc.)
keep_env: boolWhether to keep the deployment environment after completion.
When false, the environment will be automatically cleaned up (destroyed)
after the test process completes. When true, the environment
will be left running for manual inspection or debugging.
Implementations§
Source§impl TestContext
impl TestContext
Sourcepub fn from_environment(
keep_env: bool,
environment: Environment,
context_type: TestContextType,
) -> Result<Self, TestContextError>
pub fn from_environment( keep_env: bool, environment: Environment, context_type: TestContextType, ) -> Result<Self, TestContextError>
Creates a new test environment from an Environment entity
This method provides a simplified interface that accepts an Environment entity containing all the necessary configuration, rather than individual parameters.
Important: This method does NOT initialize the environment. You must call
.init() on the returned TestContext to complete the setup.
§Arguments
keep_env- Whether to keep the environment after tests completeenvironment- The Environment entity containing instance name, SSH keys, and pathscontext_type- The type of test environment (Container orVirtualMachine)
§Returns
A TestContext that requires .init() to be called before use.
§Errors
Returns an error if:
- Input validation fails (empty or invalid templates directory)
- Current directory cannot be determined
- Temporary directory creation fails
- SSH key setup fails
§Examples
use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
use torrust_tracker_deployer_lib::domain::provider::{LxdConfig, ProviderConfig};
use torrust_tracker_deployer_lib::domain::ProfileName;
use torrust_tracker_deployer_lib::shared::Username;
use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
use torrust_tracker_deployer_lib::testing::e2e::context::{TestContext, TestContextType};
use std::path::PathBuf;
use tempfile::TempDir;
use chrono::{TimeZone, Utc};
// Use temporary directory to avoid creating real directories
let temp_dir = TempDir::new()?;
let temp_path = temp_dir.path();
let env_name = EnvironmentName::new("test-example".to_string())?;
let ssh_username = Username::new("torrust".to_string())?;
let ssh_credentials = SshCredentials::new(
temp_path.join("testing_rsa"),
temp_path.join("testing_rsa.pub"),
ssh_username,
);
let provider_config = ProviderConfig::Lxd(LxdConfig {
profile_name: ProfileName::new(format!("lxd-{}", env_name.as_str())).unwrap(),
});
let created_at = Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap();
let environment = Environment::new(env_name, provider_config, ssh_credentials, 22, created_at);
let test_context = TestContext::from_environment(
false,
environment,
TestContextType::Container,
)?.init()?;
Sourcepub fn init(self) -> Result<Self, TestContextError>
pub fn init(self) -> Result<Self, TestContextError>
Initializes the test environment by preparing templates and logging setup
This method performs the final environment setup with side effects.
It must be called explicitly after creating a TestContext to complete the setup.
§Errors
Returns an error if:
- Template preparation fails
- Environment persistence fails
Sourcepub fn temp_dir_path(&self) -> Option<&Path>
pub fn temp_dir_path(&self) -> Option<&Path>
Gets the temporary directory path for logging or debugging purposes
Sourcepub fn update_from_provisioned(
&mut self,
provisioned_env: Environment<Provisioned>,
)
pub fn update_from_provisioned( &mut self, provisioned_env: Environment<Provisioned>, )
Updates the test context environment from a provisioned environment
This method updates the internal environment state after provisioning
completes, ensuring the TestContext maintains the latest and accurate environment state.
§Arguments
provisioned_env- The provisioned environment returned byProvisionCommandHandler
§Examples
// After provisioning succeeds, update the test context
test_context.update_from_provisioned(provisioned_env);Sourcepub fn update_from_configured(
&mut self,
configured_env: Environment<Configured>,
)
pub fn update_from_configured( &mut self, configured_env: Environment<Configured>, )
Updates the test context environment from a configured environment
This method updates the internal environment state after configuration
completes, ensuring the TestContext maintains the latest and accurate environment state.
§Arguments
configured_env- The configured environment returned byConfigureCommandHandler
§Examples
// After configuration succeeds, update the test context
test_context.update_from_configured(configured_env);Sourcepub fn update_from_destroyed(&mut self, destroyed_env: Environment<Destroyed>)
pub fn update_from_destroyed(&mut self, destroyed_env: Environment<Destroyed>)
Updates the test context environment from a destroyed environment
This method updates the internal environment state after destruction
completes, ensuring the TestContext maintains the latest and accurate environment state.
§Arguments
destroyed_env- The destroyed environment returned byDestroyCommandHandler
§Examples
// After destruction succeeds, update the test context
test_context.update_from_destroyed(destroyed_env);Sourcepub fn create_repository(&self) -> Arc<dyn EnvironmentRepository> ⓘ
pub fn create_repository(&self) -> Arc<dyn EnvironmentRepository> ⓘ
Creates a repository for the current environment
This is a convenience method that creates an EnvironmentRepository
configured for this test context’s environment. The repository is
created using the repository factory with the environment’s data directory.
§Returns
An Arc<dyn EnvironmentRepository> that can be used to persist and load
environment state for this test context.
§Examples
let repository = test_context.create_repository();
// Use repository for state persistence...Trait Implementations§
Source§impl Debug for TestContext
impl Debug for TestContext
Source§impl Drop for TestContext
impl Drop for TestContext
Auto Trait Implementations§
impl !RefUnwindSafe for TestContext
impl !UnwindSafe for TestContext
impl Freeze for TestContext
impl Send for TestContext
impl Sync for TestContext
impl Unpin for TestContext
impl UnsafeUnpin for TestContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request