-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
1307 lines (1175 loc) · 46.3 KB
/
Copy pathmod.rs
File metadata and controls
1307 lines (1175 loc) · 46.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Environment Domain Module
//!
//! This module contains all environment-related domain entities and types.
//!
//! ## Architecture: Context + State Design
//!
//! The `Environment` entity uses a two-part design to separate immutable identity
//! from mutable lifecycle state:
//!
//! ### `EnvironmentContext` - Three Semantic Categories
//!
//! The context is organized into three distinct semantic types, each with a clear purpose:
//!
//! #### 1. **User Inputs** (`UserInputs`)
//! - **Purpose**: Configuration provided when creating an environment
//! - **Characteristics**: Immutable throughout environment lifecycle
//! - **Fields**: `name`, `instance_name`, `profile_name`, `ssh_credentials`, `ssh_port`
//! - **When to add**: User needs to configure something at creation time
//!
//! #### 2. **Internal Config** (`InternalConfig`)
//! - **Purpose**: Derived configuration for internal use
//! - **Characteristics**: Calculated from user inputs
//! - **Fields**: `build_dir`, `data_dir`
//! - **When to add**: Need internal paths or derived configuration
//!
//! #### 3. **Runtime Outputs** (`RuntimeOutputs`)
//! - **Purpose**: Data generated during deployment operations
//! - **Characteristics**: Mutable as operations progress
//! - **Fields**: `instance_ip` (more fields expected as deployment evolves)
//! - **When to add**: Operations produce new data about deployed infrastructure
//!
//! ### `state: S` - Mutable Lifecycle State
//!
//! Tracks the current phase in the deployment lifecycle using the type-state pattern:
//! - **Success states**: `Created`, `Provisioning`, `Provisioned`, `Configuring`, etc.
//! - **Error states**: `ProvisionFailed`, `ConfigureFailed`, etc.
//!
//! ### Benefits of This Design
//!
//! - **Compile-time safety**: Invalid state transitions caught at compile time
//! - **Reduced pattern matching**: Access common fields without matching on state (83% reduction)
//! - **Clear separation**: Identity vs. lifecycle are distinct concerns
//! - **Semantic clarity**: Types document the purpose of each field
//! - **Developer guidance**: Clear where to add new fields based on their purpose
//! - **Easy extension**: Adding fields or states is straightforward
//!
//! ## Submodules
//!
//! - `context` - Environment context composing the three semantic types
//! - `user_inputs` - User-provided configuration
//! - `internal_config` - Derived paths and internal settings
//! - `runtime_outputs` - Data generated during deployment
//! - `name` - Environment name validation and management
//! - `state` - State marker types and type erasure for environment state machine
//!
//! ## Main Entity
//!
//! The `Environment` entity encapsulates all environment-specific configuration for deployments.
//! Each environment represents an isolated deployment context with its own directories,
//! SSH keys, and instance naming.
//!
//! ## Purpose
//!
//! The Environment entity provides:
//! - Environment-specific directory structure (`data/{env_name}/`, `build/{env_name}/`)
//! - Instance naming with conflict avoidance (`torrust-tracker-vm-{env_name}`)
//! - SSH key pair management per environment
//! - JSON serialization for future state persistence
//!
//! ## Usage Example
//!
//! ```rust
//! use torrust_tracker_deployer_lib::domain::environment::{Environment, name::EnvironmentName};
//! use torrust_tracker_deployer_lib::shared::Username;
//! use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
//! use std::path::PathBuf;
//!
//! let env_name = EnvironmentName::new("e2e-config".to_string())?;
//! let ssh_username = Username::new("torrust".to_string())?;
//! let ssh_credentials = SshCredentials::new(
//! PathBuf::from("fixtures/testing_rsa"),
//! PathBuf::from("fixtures/testing_rsa.pub"),
//! ssh_username,
//! );
//! let environment = Environment::new(env_name, ssh_credentials, 22);
//!
//! // Environment automatically generates paths
//! assert_eq!(*environment.data_dir(), PathBuf::from("data/e2e-config"));
//! assert_eq!(*environment.build_dir(), PathBuf::from("build/e2e-config"));
//! assert_eq!(environment.templates_dir(), PathBuf::from("data/e2e-config/templates"));
//!
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
pub mod context;
pub mod internal_config;
pub mod name;
pub mod repository;
pub mod runtime_outputs;
pub mod state;
mod trace_id;
pub mod user_inputs;
// Test utilities (only available in test configuration)
#[cfg(test)]
pub mod testing;
// Re-export TraceId for use by state module
pub use trace_id::TraceId;
// Re-export commonly used types for convenience
pub use context::EnvironmentContext;
pub use internal_config::InternalConfig;
pub use name::{EnvironmentName, EnvironmentNameError};
pub use runtime_outputs::RuntimeOutputs;
pub use state::{
AnyEnvironmentState, ConfigureFailed, Configured, Configuring, Created, DestroyFailed,
Destroyed, Destroying, ProvisionFailed, Provisioned, Provisioning, ReleaseFailed, Released,
Releasing, RunFailed, Running,
};
pub use user_inputs::UserInputs;
use crate::adapters::ssh::SshCredentials;
use crate::domain::{InstanceName, ProfileName};
use crate::shared::Username;
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use std::path::PathBuf;
/// Directory name for trace files within an environment's data directory
pub const TRACES_DIR_NAME: &str = "traces";
/// Directory name for template files within an environment's data directory
pub const TEMPLATES_DIR_NAME: &str = "templates";
/// Directory name for Ansible-related files
pub const ANSIBLE_DIR_NAME: &str = "ansible";
/// Directory name for OpenTofu-related files
pub const TOFU_DIR_NAME: &str = "tofu";
/// Provider name for LXD infrastructure
pub const LXD_PROVIDER_NAME: &str = "lxd";
/// Environment configuration encapsulating all environment-specific settings
///
/// This entity represents a complete environment configuration including naming,
/// directory structure, SSH keys, and derived paths. It follows the principle of
/// environment isolation where each environment has its own separate resources.
///
/// # Architecture: Context + State Design
///
/// The `Environment<S>` is composed of two distinct parts:
///
/// ## `context: EnvironmentContext` - Immutable Identity
///
/// Contains all state-independent data that remains constant throughout the
/// environment's lifecycle. This includes identity (`name`, `instance_name`),
/// configuration (SSH credentials, port), and paths (`build_dir`, `data_dir`).
///
/// Accessing context data is efficient and requires no pattern matching on state.
///
/// ## `state: S` - Mutable Lifecycle State
///
/// Represents the current phase in the deployment lifecycle using type parameters.
/// The type-state pattern ensures that state transitions are validated at compile-time.
///
/// # Type-State Pattern
///
/// The Environment uses the type-state pattern to enforce valid state transitions
/// at compile-time. Each state is represented by a distinct type parameter `S`,
/// ensuring that operations are only callable on appropriate states.
///
/// # Design Principles
///
/// - **Isolation**: Each environment is completely isolated from others
/// - **Compile-time Safety**: Invalid state transitions caught during compilation
/// - **Separation of Concerns**: Context (identity) vs. State (lifecycle) are distinct
/// - **Consistency**: All paths follow the same naming pattern
/// - **Predictability**: Paths are derived automatically from environment name
/// - **Traceability**: All artifacts are organized by environment for debugging
/// - **Type Safety**: Invalid state transitions are prevented at compile-time
///
/// # Directory Structure
///
/// ```text
/// data/{env_name}/
/// templates/ # Environment-specific templates
/// build/{env_name}/ # Environment-specific build artifacts
/// ```
///
/// # Instance Naming
///
/// Instance names follow the pattern: `torrust-tracker-vm-{env_name}`
/// This ensures multiple environments can run concurrently without conflicts.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Environment<S = Created> {
/// Core environment data shared across all states
context: EnvironmentContext,
/// Current state of the environment in the deployment lifecycle
state: S,
}
impl Environment {
/// Creates a new Environment with auto-generated paths and instance name
///
/// # Arguments
///
/// * `name` - The validated environment name
/// * `ssh_credentials` - SSH credentials for connecting to instances
/// * `ssh_port` - SSH port for connecting to instances
///
/// # Returns
///
/// A new Environment instance with all paths and instance name automatically
/// generated based on the environment name.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
///
/// let env_name = EnvironmentName::new("production".to_string())?;
/// let ssh_username = Username::new("torrust".to_string())?;
/// let ssh_credentials = SshCredentials::new(
/// PathBuf::from("keys/prod_rsa"),
/// PathBuf::from("keys/prod_rsa.pub"),
/// ssh_username,
/// );
/// let ssh_port = 22;
/// let environment = Environment::new(env_name, ssh_credentials, ssh_port);
///
/// assert_eq!(environment.instance_name().as_str(), "torrust-tracker-vm-production");
/// assert_eq!(*environment.data_dir(), PathBuf::from("data/production"));
/// assert_eq!(*environment.build_dir(), PathBuf::from("build/production"));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Panics
///
/// This function does not panic. All instance name generation is guaranteed
/// to succeed for valid environment names.
#[must_use]
#[allow(clippy::needless_pass_by_value)] // Public API takes ownership for ergonomics
pub fn new(
name: EnvironmentName,
ssh_credentials: SshCredentials,
ssh_port: u16,
) -> Environment<Created> {
let context = EnvironmentContext::new(&name, ssh_credentials, ssh_port);
Environment {
context,
state: Created,
}
}
}
// Common transitions available from any state
impl<S> Environment<S> {
/// Internal helper: Creates a new environment with a different state
///
/// This is a private helper method used by state transition methods to avoid
/// duplicating field copying code. It transfers all environment data while
/// changing only the state type parameter.
///
/// This method automatically logs all state transitions at info level with
/// structured fields for observability and audit trail purposes.
///
/// # Type Parameters
///
/// * `T` - The target state type
///
/// # Arguments
///
/// * `new_state` - The new state instance to transition to
///
/// # Returns
///
/// A new `Environment<T>` with all fields preserved except the state
fn with_state<T>(self, new_state: T) -> Environment<T> {
// Log state transition for observability and audit trail
tracing::info!(
environment_name = %self.context.user_inputs.name,
instance_name = %self.context.user_inputs.instance_name,
from_state = std::any::type_name::<S>(),
to_state = std::any::type_name::<T>(),
"Environment state transition"
);
Environment {
context: self.context,
state: new_state,
}
}
/// Transitions from any state to Destroying state
///
/// This method can be called from any state to begin the environment destruction process.
/// It indicates that the destroy command has started executing.
#[must_use]
pub fn start_destroying(self) -> Environment<Destroying> {
self.with_state(Destroying)
}
/// Transitions from any state to Destroyed state
///
/// This method can be called from any state to destroy the environment.
/// It indicates that all infrastructure resources have been released.
#[must_use]
pub fn destroy(self) -> Environment<Destroyed> {
self.with_state(Destroyed)
}
}
// Type Erasure: Typed → Runtime conversions (into_any)
// Generic implementations for all states
impl<S> Environment<S> {
/// Get a reference to the environment context
///
/// Provides access to all state-independent environment data.
#[must_use]
pub fn context(&self) -> &EnvironmentContext {
&self.context
}
/// Get a mutable reference to the environment context
///
/// Used for operations that need to modify context data, such as
/// setting the instance IP after provisioning.
fn context_mut(&mut self) -> &mut EnvironmentContext {
&mut self.context
}
/// Returns a reference to the current state
#[must_use]
pub fn state(&self) -> &S {
&self.state
}
/// Returns the environment name
#[must_use]
pub fn name(&self) -> &EnvironmentName {
&self.context.user_inputs.name
}
/// Returns the instance name for this environment
#[must_use]
pub fn instance_name(&self) -> &InstanceName {
&self.context.user_inputs.instance_name
}
/// Returns the profile name for this environment
///
/// Returns the unique LXD profile name based on the environment name
/// to ensure profile isolation between different test environments.
#[must_use]
pub fn profile_name(&self) -> &ProfileName {
&self.context.user_inputs.profile_name
}
/// Returns the SSH credentials for this environment
#[must_use]
pub fn ssh_credentials(&self) -> &SshCredentials {
&self.context.user_inputs.ssh_credentials
}
/// Returns the SSH port for this environment
#[must_use]
pub fn ssh_port(&self) -> u16 {
self.context.user_inputs.ssh_port
}
/// Returns the SSH username for this environment
#[must_use]
pub fn ssh_username(&self) -> &Username {
self.context.ssh_username()
}
/// Returns the SSH private key path for this environment
#[must_use]
pub fn ssh_private_key_path(&self) -> &PathBuf {
self.context.ssh_private_key_path()
}
/// Returns the SSH public key path for this environment
#[must_use]
pub fn ssh_public_key_path(&self) -> &PathBuf {
self.context.ssh_public_key_path()
}
/// Returns the build directory for this environment
#[must_use]
pub fn build_dir(&self) -> &PathBuf {
&self.context.internal_config.build_dir
}
/// Returns the data directory for this environment
#[must_use]
pub fn data_dir(&self) -> &PathBuf {
&self.context.internal_config.data_dir
}
/// Returns the instance IP address if available
///
/// The instance IP is populated after successful provisioning and is
/// `None` for environments that haven't been provisioned yet.
///
/// # Returns
///
/// - `Some(IpAddr)` if the environment has been provisioned
/// - `None` if the environment hasn't been provisioned yet
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
/// use std::net::{IpAddr, Ipv4Addr};
///
/// let env_name = EnvironmentName::new("test".to_string())?;
/// let ssh_username = Username::new("torrust".to_string())?;
/// let ssh_credentials = SshCredentials::new(
/// PathBuf::from("keys/test_rsa"),
/// PathBuf::from("keys/test_rsa.pub"),
/// ssh_username,
/// );
/// let environment = Environment::new(env_name, ssh_credentials, 22);
///
/// // Before provisioning
/// assert_eq!(environment.instance_ip(), None);
///
/// // After provisioning (simulated)
/// let ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100));
/// let environment = environment.with_instance_ip(ip);
/// assert_eq!(environment.instance_ip(), Some(ip));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[must_use]
pub fn instance_ip(&self) -> Option<IpAddr> {
self.context.runtime_outputs.instance_ip
}
/// Sets the instance IP address for this environment
///
/// This method is typically called by the `ProvisionCommandHandler` after successfully
/// provisioning the infrastructure and obtaining the instance's IP address.
///
/// # Arguments
///
/// * `ip` - The IP address of the provisioned instance
///
/// # Returns
///
/// A new Environment instance with the IP address set
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
/// use std::net::{IpAddr, Ipv4Addr};
///
/// let env_name = EnvironmentName::new("production".to_string())?;
/// let ssh_username = Username::new("torrust".to_string())?;
/// let ssh_credentials = SshCredentials::new(
/// PathBuf::from("keys/prod_rsa"),
/// PathBuf::from("keys/prod_rsa.pub"),
/// ssh_username,
/// );
/// let environment = Environment::new(env_name, ssh_credentials, 22);
///
/// // Set IP after provisioning
/// let ip = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 42));
/// let environment = environment.with_instance_ip(ip);
///
/// assert_eq!(environment.instance_ip(), Some(ip));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[must_use]
pub fn with_instance_ip(mut self, ip: IpAddr) -> Self {
self.context_mut().runtime_outputs.instance_ip = Some(ip);
self
}
/// Returns the templates directory for this environment
///
/// The templates directory is located at `data/{env_name}/templates/`
/// and contains environment-specific template files.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
///
/// let env_name = EnvironmentName::new("staging".to_string())?;
/// let ssh_username = Username::new("torrust".to_string())?;
/// let ssh_credentials = SshCredentials::new(
/// PathBuf::from("keys/staging_rsa"),
/// PathBuf::from("keys/staging_rsa.pub"),
/// ssh_username,
/// );
/// let environment = Environment::new(env_name, ssh_credentials, 22);
///
/// assert_eq!(
/// environment.templates_dir(),
/// PathBuf::from("data/staging/templates")
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[must_use]
pub fn templates_dir(&self) -> PathBuf {
self.context.templates_dir()
}
/// Returns the traces directory for this environment
///
/// The traces directory is located at `data/{env_name}/traces/`
/// and contains error trace files for failed operations.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
///
/// let env_name = EnvironmentName::new("production".to_string())?;
/// let ssh_username = Username::new("torrust".to_string())?;
/// let ssh_credentials = SshCredentials::new(
/// PathBuf::from("keys/prod_rsa"),
/// PathBuf::from("keys/prod_rsa.pub"),
/// ssh_username,
/// );
/// let environment = Environment::new(env_name, ssh_credentials, 22);
///
/// assert_eq!(
/// environment.traces_dir(),
/// PathBuf::from("data/production/traces")
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[must_use]
pub fn traces_dir(&self) -> PathBuf {
self.context.traces_dir()
}
/// Returns the ansible build directory for this environment
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
///
/// let env_name = EnvironmentName::new("dev".to_string())?;
/// let ssh_username = Username::new("torrust".to_string())?;
/// let ssh_credentials = SshCredentials::new(
/// PathBuf::from("keys/dev_rsa"),
/// PathBuf::from("keys/dev_rsa.pub"),
/// ssh_username,
/// );
/// let environment = Environment::new(env_name, ssh_credentials, 22);
///
/// assert_eq!(
/// environment.ansible_build_dir(),
/// PathBuf::from("build/dev/ansible")
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[must_use]
pub fn ansible_build_dir(&self) -> PathBuf {
self.context.ansible_build_dir()
}
/// Returns the tofu build directory for this environment
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
///
/// let env_name = EnvironmentName::new("test".to_string())?;
/// let ssh_username = Username::new("torrust".to_string())?;
/// let ssh_credentials = SshCredentials::new(
/// PathBuf::from("keys/test_rsa"),
/// PathBuf::from("keys/test_rsa.pub"),
/// ssh_username,
/// );
/// let environment = Environment::new(env_name, ssh_credentials, 22);
///
/// assert_eq!(
/// environment.tofu_build_dir(),
/// PathBuf::from("build/test/tofu/lxd")
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[must_use]
pub fn tofu_build_dir(&self) -> PathBuf {
self.context.tofu_build_dir()
}
/// Returns the ansible templates directory for this environment
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
///
/// let env_name = EnvironmentName::new("integration".to_string())?;
/// let ssh_username = Username::new("torrust".to_string())?;
/// let ssh_credentials = SshCredentials::new(
/// PathBuf::from("keys/integration_rsa"),
/// PathBuf::from("keys/integration_rsa.pub"),
/// ssh_username,
/// );
/// let environment = Environment::new(env_name, ssh_credentials, 22);
///
/// assert_eq!(
/// environment.ansible_templates_dir(),
/// PathBuf::from("data/integration/templates/ansible")
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[must_use]
pub fn ansible_templates_dir(&self) -> PathBuf {
self.context.ansible_templates_dir()
}
/// Returns the tofu templates directory for this environment
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
/// use torrust_tracker_deployer_lib::shared::Username;
/// use torrust_tracker_deployer_lib::adapters::ssh::SshCredentials;
/// use std::path::PathBuf;
///
/// let env_name = EnvironmentName::new("load-test".to_string())?;
/// let ssh_username = Username::new("torrust".to_string())?;
/// let ssh_credentials = SshCredentials::new(
/// PathBuf::from("keys/load-test-rsa"),
/// PathBuf::from("keys/load-test-rsa.pub"),
/// ssh_username,
/// );
/// let environment = Environment::new(env_name, ssh_credentials, 22);
///
/// assert_eq!(
/// environment.tofu_templates_dir(),
/// PathBuf::from("data/load-test/templates/tofu")
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[must_use]
pub fn tofu_templates_dir(&self) -> PathBuf {
self.context.tofu_templates_dir()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::adapters::ssh::SshCredentials;
use crate::domain::EnvironmentName;
use std::path::Path;
use tempfile::TempDir;
// ============================================================================
// Test Fixtures - Builder Pattern
// ============================================================================
/// Test builder for creating Environment instances with sensible defaults
///
/// This builder simplifies test setup by providing default values and allowing
/// customization through a fluent API. It automatically manages temporary
/// directories and creates all required value objects.
///
/// # Examples
///
/// ```rust
/// // Simple environment with defaults
/// let env = EnvironmentTestBuilder::new().build();
///
/// // Customized environment
/// let env = EnvironmentTestBuilder::new()
/// .with_name("staging")
/// .with_ssh_key_name("custom_key")
/// .build();
///
/// // Environment with access to temp directory
/// let (env, temp_dir) = EnvironmentTestBuilder::new()
/// .with_name("test-env")
/// .build_with_temp_dir();
/// ```
pub struct EnvironmentTestBuilder {
env_name: String,
ssh_key_name: String,
ssh_username: String,
temp_dir: TempDir,
}
impl EnvironmentTestBuilder {
/// Creates a new builder with sensible defaults
pub fn new() -> Self {
Self {
env_name: "test-env".to_string(),
ssh_key_name: "test_key".to_string(),
ssh_username: "torrust".to_string(),
temp_dir: TempDir::new().expect("Failed to create temp directory"),
}
}
/// Sets the environment name
pub fn with_name(mut self, name: &str) -> Self {
self.env_name = name.to_string();
self
}
/// Sets the SSH key name (without .pub extension)
pub fn with_ssh_key_name(mut self, key_name: &str) -> Self {
self.ssh_key_name = key_name.to_string();
self
}
/// Sets the SSH username
#[allow(dead_code)]
pub fn with_ssh_username(mut self, username: &str) -> Self {
self.ssh_username = username.to_string();
self
}
/// Builds an Environment in Created state
///
/// This is the most common use case - creates an environment with
/// auto-generated paths based on the environment name.
pub fn build(self) -> Environment<Created> {
let env_name = EnvironmentName::new(self.env_name).unwrap();
let ssh_username = Username::new(self.ssh_username).unwrap();
let temp_path = self.temp_dir.path();
let ssh_credentials = SshCredentials::new(
temp_path.join(&self.ssh_key_name),
temp_path.join(format!("{}.pub", &self.ssh_key_name)),
ssh_username,
);
let ssh_port = 22;
Environment::new(env_name, ssh_credentials, ssh_port)
}
/// Builds an Environment and returns the `TempDir`
///
/// Use this when you need access to the temp directory in your test,
/// for example to verify paths or create additional test files.
#[allow(dead_code)]
pub fn build_with_temp_dir(self) -> (Environment<Created>, TempDir) {
let env_name = EnvironmentName::new(self.env_name).unwrap();
let ssh_username = Username::new(self.ssh_username).unwrap();
let temp_path = self.temp_dir.path();
let ssh_credentials = SshCredentials::new(
temp_path.join(&self.ssh_key_name),
temp_path.join(format!("{}.pub", &self.ssh_key_name)),
ssh_username,
);
let ssh_port = 22;
let environment = Environment::new(env_name, ssh_credentials, ssh_port);
(environment, self.temp_dir)
}
/// Builds an Environment with custom paths
///
/// Use this when you need full control over the data and build directories.
/// Returns the environment, `data_dir`, `build_dir`, and `temp_dir`.
pub fn build_with_custom_paths(self) -> (Environment<Created>, PathBuf, PathBuf, TempDir) {
let temp_path = self.temp_dir.path();
let data_dir = temp_path.join("data").join(&self.env_name);
let build_dir = temp_path.join("build").join(&self.env_name);
let env_name = EnvironmentName::new(self.env_name).unwrap();
let ssh_username = Username::new(self.ssh_username).unwrap();
let ssh_credentials = SshCredentials::new(
temp_path.join(&self.ssh_key_name),
temp_path.join(format!("{}.pub", &self.ssh_key_name)),
ssh_username,
);
let instance_name =
InstanceName::new(format!("torrust-tracker-vm-{}", env_name.as_str())).unwrap();
let profile_name = ProfileName::new(format!("lxd-{}", env_name.as_str())).unwrap();
let context = EnvironmentContext {
user_inputs: UserInputs {
name: env_name,
instance_name,
profile_name,
ssh_credentials,
ssh_port: 22,
},
internal_config: InternalConfig {
data_dir: data_dir.clone(),
build_dir: build_dir.clone(),
},
runtime_outputs: RuntimeOutputs { instance_ip: None },
};
let environment = Environment {
context,
state: Created,
};
(environment, data_dir, build_dir, self.temp_dir)
}
/// Returns a reference to the temp directory path
#[allow(dead_code)]
pub fn temp_path(&self) -> &Path {
self.temp_dir.path()
}
}
impl Default for EnvironmentTestBuilder {
fn default() -> Self {
Self::new()
}
}
// ============================================================================
// Custom Assertion Helpers
// ============================================================================
/// Asserts that the environment's paths are within the temp directory
#[allow(dead_code)]
fn assert_paths_in_temp_dir(env: &Environment<impl Clone>, temp_path: &Path, env_name: &str) {
assert!(
env.data_dir().starts_with(temp_path),
"data_dir should be in temp: {:?} not in {:?}",
env.data_dir(),
temp_path
);
assert!(
env.build_dir().starts_with(temp_path),
"build_dir should be in temp: {:?} not in {:?}",
env.build_dir(),
temp_path
);
assert!(
env.data_dir().to_string_lossy().contains(env_name),
"data_dir should contain env name: {:?}",
env.data_dir()
);
assert!(
env.build_dir().to_string_lossy().contains(env_name),
"build_dir should contain env name: {:?}",
env.build_dir()
);
}
/// Asserts that SSH credentials match expected paths
fn assert_ssh_credentials(
env: &Environment<impl Clone>,
expected_private: &Path,
expected_public: &Path,
) {
assert_eq!(
env.ssh_private_key_path(),
expected_private,
"SSH private key path mismatch"
);
assert_eq!(
env.ssh_public_key_path(),
expected_public,
"SSH public key path mismatch"
);
}
/// Asserts that the instance name matches the expected format
fn assert_instance_name_format(env: &Environment<impl Clone>, env_name: &str) {
let expected = format!("torrust-tracker-vm-{env_name}");
assert_eq!(
env.instance_name().as_str(),
expected,
"Instance name should match expected format"
);
}
/// Asserts that a path ends with the expected suffix
#[allow(dead_code)]
fn assert_path_ends_with(actual: &Path, expected_suffix: &str) {
assert!(
actual.to_string_lossy().ends_with(expected_suffix),
"Path {actual:?} should end with {expected_suffix:?}"
);
}
// ============================================================================
// Tests
// ============================================================================
#[test]
fn it_should_create_environment_with_auto_generated_paths() {
// Arrange
let (environment, data_dir, build_dir, temp_dir) = EnvironmentTestBuilder::new()
.with_name("test-env")
.with_ssh_key_name("testing_rsa")
.build_with_custom_paths();
let temp_path = temp_dir.path();
// Act & Assert: Check basic fields
assert_eq!(environment.name().as_str(), "test-env");
assert_eq!(environment.ssh_username().as_str(), "torrust");
// Assert: Check SSH credentials
assert_ssh_credentials(
&environment,
&temp_path.join("testing_rsa"),
&temp_path.join("testing_rsa.pub"),
);
// Assert: Check paths are in temp directory
assert_eq!(*environment.data_dir(), data_dir);
assert_eq!(*environment.build_dir(), build_dir);
// Assert: Check instance name format
assert_instance_name_format(&environment, "test-env");
}
#[test]
fn it_should_generate_correct_template_directories() {
// Arrange
let (environment, data_dir, _build_dir, _temp_dir) = EnvironmentTestBuilder::new()
.with_name("test-production")
.with_ssh_key_name("prod_rsa")
.build_with_custom_paths();
// Act
let templates_dir = environment.templates_dir();
let ansible_dir = environment.ansible_templates_dir();
let tofu_dir = environment.tofu_templates_dir();
// Assert
assert_eq!(templates_dir, data_dir.join("templates"));
assert_eq!(ansible_dir, data_dir.join("templates").join("ansible"));
assert_eq!(tofu_dir, data_dir.join("templates").join("tofu"));
}
#[test]
fn it_should_generate_correct_build_directories() {
// Arrange
let (environment, _data_dir, build_dir, _temp_dir) = EnvironmentTestBuilder::new()
.with_name("test-staging")
.with_ssh_key_name("staging_rsa")
.build_with_custom_paths();
// Act
let ansible_dir = environment.ansible_build_dir();
let tofu_dir = environment.tofu_build_dir();
// Assert
assert_eq!(ansible_dir, build_dir.join("ansible"));
assert_eq!(tofu_dir, build_dir.join("tofu").join("lxd"));
}
#[test]
fn it_should_handle_different_environment_names() {
// Arrange: Test cases with environment names and expected instance names
let test_cases = vec![
("test-dev", "torrust-tracker-vm-test-dev"),