Skip to content

Commit 272847e

Browse files
committed
feat: add validate command with comprehensive E2E tests and documentation
Implements the 'validate' command to check environment configuration files without creating deployments. This command follows the outside-in development approach and serves as a real-world validation of the add-new-command skill guide. Implementation: - Presentation layer: CLI command with --env-file flag, progress reporting - Application layer: JSON parsing, domain validation (SSH keys, constraints) - E2E tests: 5 scenarios covering success and all error paths - Documentation: Comprehensive user guide with examples and troubleshooting This completes roadmap task 9.1.
1 parent a9a2f2e commit 272847e

File tree

19 files changed

+2331
-18
lines changed

19 files changed

+2331
-18
lines changed

.github/skills/add-new-command/skill.md

Lines changed: 954 additions & 0 deletions
Large diffs are not rendered by default.

docs/user-guide/commands/README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ This directory contains detailed guides for all Torrust Tracker Deployer command
1010
- `create template` - Generate environment configuration template
1111
- `create environment` - Create deployment environment from configuration
1212

13+
### Configuration Validation
14+
15+
- **[validate](validate.md)** - Validate environment configuration files without deployment
16+
1317
### Environment Information
1418

1519
- **[show](show.md)** - Display environment information with state-aware details
@@ -38,15 +42,16 @@ The typical command sequence for a complete deployment:
3842
```text
3943
1. create template → Generate configuration template
4044
2. (edit template) → Customize your settings
41-
3. create environment → Create environment from config
42-
4. show → View environment details
43-
5. provision → Provision VM infrastructure
44-
6. configure → Install Docker, Docker Compose, configure firewall
45-
7. test → Verify infrastructure readiness
46-
8. release → Deploy application configuration and files
47-
9. run → Start Torrust Tracker services
48-
10. destroy → Tear down infrastructure
49-
11. purge → Remove local data (optional, for cleanup)
45+
3. validate → Verify configuration is correct (optional but recommended)
46+
4. create environment → Create environment from config
47+
5. show → View environment details
48+
6. provision → Provision VM infrastructure
49+
7. configure → Install Docker, Docker Compose, configure firewall
50+
8. test → Verify infrastructure readiness
51+
9. release → Deploy application configuration and files
52+
10. run → Start Torrust Tracker services
53+
11. destroy → Tear down infrastructure
54+
12. purge → Remove local data (optional, for cleanup)
5055
```
5156

5257
## Command Categories
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
# Validate Command
2+
3+
Validates environment configuration files without creating deployments. Use this command to check configuration correctness before running the actual deployment workflow.
4+
5+
## Command Syntax
6+
7+
```bash
8+
torrust-tracker-deployer validate --env-file <CONFIG_FILE>
9+
```
10+
11+
### Options
12+
13+
- `--env-file, -f <FILE>` - Path to the environment configuration file to validate (required)
14+
15+
## Usage Examples
16+
17+
### Basic Usage
18+
19+
```bash
20+
torrust-tracker-deployer validate --env-file envs/my-environment.json
21+
```
22+
23+
### Validate with Short Flag
24+
25+
```bash
26+
torrust-tracker-deployer validate -f config/production.json
27+
```
28+
29+
## What This Command Does
30+
31+
The validate command performs comprehensive validation of environment configuration files:
32+
33+
1. **File Validation** - Verifies the configuration file exists and is readable
34+
2. **JSON Schema Validation** - Checks JSON syntax and structure
35+
3. **Domain Validation** - Validates field constraints and business rules:
36+
- SSH key files must exist at specified paths
37+
- Environment names must follow naming rules (lowercase with dashes)
38+
- Port numbers must be valid
39+
- IP addresses must be well-formed
40+
- Domain names must follow DNS conventions
41+
- All required fields must be present
42+
43+
## When to Use
44+
45+
- **Before Creating Environments** - Catch configuration errors early before starting infrastructure provisioning
46+
- **CI/CD Pipelines** - Validate configurations as part of automated testing
47+
- **Configuration Review** - Verify configuration correctness during code review
48+
- **Troubleshooting** - Diagnose why environment creation failed
49+
- **Learning** - Understand configuration structure and requirements
50+
51+
## When NOT to Use
52+
53+
- **After Environment Creation** - Use `show` command to view existing environment details instead
54+
- **Creating Templates** - Use `create template` to generate valid configuration files
55+
- **Deployment** - Validate does not deploy - use full workflow commands for actual deployment
56+
57+
## Output Details
58+
59+
### Success Output
60+
61+
```text
62+
⏳ [1/3] Loading configuration file...
63+
⏳ ✓ Configuration file loaded (took 0ms)
64+
⏳ [2/3] Validating JSON schema...
65+
⏳ ✓ Schema validation passed (took 0ms)
66+
⏳ [3/3] Validating configuration fields...
67+
⏳ ✓ Field validation passed (took 0ms)
68+
69+
✅ Configuration file 'envs/my-environment.json' is valid
70+
71+
Environment Details:
72+
• Name: my-environment
73+
• Provider: lxd
74+
• Prometheus: Enabled
75+
• Grafana: Enabled
76+
• HTTPS: Disabled
77+
• Backups: Enabled
78+
```
79+
80+
### Error Output Examples
81+
82+
**File Not Found**:
83+
84+
```text
85+
❌ Validate command failed: Configuration file not found: /tmp/nonexistent.json
86+
87+
For detailed troubleshooting:
88+
Verify the file path is correct: /tmp/nonexistent.json
89+
Use 'create template' to generate a valid configuration file.
90+
```
91+
92+
**Invalid JSON**:
93+
94+
```text
95+
❌ Validate command failed: Validation failed for configuration file: config.json
96+
97+
For detailed troubleshooting:
98+
JSON parsing failed for file 'config.json'.
99+
100+
Error details:
101+
key must be a string at line 1 column 3
102+
103+
Common issues:
104+
- Missing or extra commas
105+
- Unmatched braces or brackets
106+
- Invalid escape sequences
107+
- Comments (not allowed in JSON)
108+
109+
Tips:
110+
- Use a JSON validator or editor with syntax highlighting
111+
- Compare with a template: 'create template --provider lxd'
112+
- Check the JSON schema in schemas/environment-config.json
113+
```
114+
115+
**Missing SSH Keys**:
116+
117+
```text
118+
❌ Validate command failed: Validation failed for configuration file: config.json
119+
120+
For detailed troubleshooting:
121+
Configuration validation failed.
122+
123+
Error: SSH private key file not found: /tmp/nonexistent-key
124+
125+
This means the configuration file has valid JSON syntax but violates
126+
domain constraints or business rules.
127+
128+
Common issues:
129+
- SSH key files don't exist at specified paths
130+
- Invalid environment name (must be lowercase with dashes)
131+
- Invalid port numbers or IP addresses
132+
- Missing required fields
133+
- HTTPS configured but no services have TLS enabled
134+
```
135+
136+
## Common Scenarios
137+
138+
### Scenario 1: Pre-Deployment Validation
139+
140+
Before starting a deployment, validate your configuration:
141+
142+
```bash
143+
# Create configuration from template
144+
torrust-tracker-deployer create template -p lxd > envs/my-env.json
145+
146+
# Edit configuration...
147+
# vim envs/my-env.json
148+
149+
# Validate before deploying
150+
torrust-tracker-deployer validate -f envs/my-env.json
151+
152+
# If valid, proceed with deployment
153+
torrust-tracker-deployer create environment -f envs/my-env.json
154+
```
155+
156+
### Scenario 2: CI/CD Pipeline Integration
157+
158+
Add validation as a CI test:
159+
160+
```bash
161+
#!/bin/bash
162+
# .github/workflows/validate-configs.sh
163+
164+
for config in envs/*.json; do
165+
echo "Validating $config..."
166+
torrust-tracker-deployer validate -f "$config"
167+
168+
if [ $? -ne 0 ]; then
169+
echo "❌ Validation failed for $config"
170+
exit 1
171+
fi
172+
done
173+
174+
echo "✅ All configurations valid"
175+
```
176+
177+
### Scenario 3: Troubleshooting Configuration Issues
178+
179+
When environment creation fails, use validate to diagnose:
180+
181+
```bash
182+
# Creation fails
183+
torrust-tracker-deployer create environment -f envs/broken.json
184+
# Error: SSH key not found...
185+
186+
# Use validate for detailed error messages
187+
torrust-tracker-deployer validate -f envs/broken.json
188+
# Shows: SSH private key file not found: /path/to/key
189+
# With troubleshooting tips...
190+
191+
# Fix issue and re-validate
192+
vim envs/broken.json
193+
torrust-tracker-deployer validate -f envs/broken.json
194+
# ✅ Configuration valid
195+
196+
# Retry creation
197+
torrust-tracker-deployer create environment -f envs/broken.json
198+
```
199+
200+
## Troubleshooting
201+
202+
### Error: Configuration file not found
203+
204+
**Cause**: The file path is incorrect or the file doesn't exist.
205+
206+
**Solution**: Verify the file path and ensure the file exists:
207+
208+
```bash
209+
# Check if file exists
210+
ls -l envs/my-config.json
211+
212+
# Use absolute path if relative path fails
213+
torrust-tracker-deployer validate -f /full/path/to/envs/my-config.json
214+
215+
# Generate a template if starting fresh
216+
torrust-tracker-deployer create template -p lxd > envs/new-config.json
217+
```
218+
219+
### Error: JSON parsing failed
220+
221+
**Cause**: The JSON syntax is invalid (missing commas, unmatched braces, etc.).
222+
223+
**Solution**: Use a JSON validator or IDE with JSON support:
224+
225+
```bash
226+
# Use jq to validate and format
227+
jq . envs/my-config.json
228+
229+
# Use Python's JSON tool
230+
python -m json.tool < envs/my-config.json
231+
232+
# Compare with template
233+
torrust-tracker-deployer create template -p lxd | jq . > valid-template.json
234+
diff envs/my-config.json valid-template.json
235+
```
236+
237+
### Error: SSH key file not found
238+
239+
**Cause**: The SSH key paths in the configuration don't exist.
240+
241+
**Solution**: Generate SSH keys or update paths to existing keys:
242+
243+
```bash
244+
# Option 1: Generate new SSH keys
245+
ssh-keygen -t rsa -b 4096 -f ~/.ssh/torrust_deployer -N ""
246+
247+
# Option 2: Update configuration with correct paths
248+
vim envs/my-config.json
249+
# Update "private_key_path" and "public_key_path"
250+
251+
# Validate
252+
torrust-tracker-deployer validate -f envs/my-config.json
253+
```
254+
255+
### Error: Invalid environment name
256+
257+
**Cause**: Environment name doesn't follow naming rules.
258+
259+
**Solution**: Use valid naming format (lowercase letters, numbers, dashes):
260+
261+
```bash
262+
# Invalid names:
263+
# - "MyEnv" (uppercase)
264+
# - "my_env" (underscores)
265+
# - "123-env" (starts with number)
266+
# - "my-env-" (ends with dash)
267+
268+
# Valid names:
269+
# - "my-env"
270+
# - "production"
271+
# - "dev-01"
272+
# - "staging-2025"
273+
274+
# Fix in configuration
275+
vim envs/my-config.json
276+
# Change "name": "MyEnv" to "name": "my-env"
277+
278+
torrust-tracker-deployer validate -f envs/my-config.json
279+
```
280+
281+
## Related Commands
282+
283+
- [`create template`](./create-template.md) - Generate valid configuration templates
284+
- [`create environment`](./create-environment.md) - Create deployment environments (runs validation internally)
285+
- [`show`](./show.md) - Display existing environment details
286+
287+
## See Also
288+
289+
- [Environment Creation Configuration](../../user-guide/environment-creation-config.md)
290+
- [JSON Schema](../../../schemas/environment-config.json)
291+
- [SSH Key Setup](../../user-guide/ssh-key-setup.md)
292+
- [Troubleshooting Guide](../../user-guide/troubleshooting.md)

src/application/command_handlers/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
//! - `run` - Stack execution on target instances
2121
//! - `show` - Display environment information and status (read-only)
2222
//! - `test` - Deployment testing and validation
23+
//! - `validate` - Validate environment configuration files (read-only)
2324
//!
2425
//! Each command handler encapsulates a complete business workflow, handling orchestration,
2526
//! error management, and coordination across multiple infrastructure services.
@@ -36,6 +37,7 @@ pub mod release;
3637
pub mod run;
3738
pub mod show;
3839
pub mod test;
40+
pub mod validate;
3941

4042
pub use configure::ConfigureCommandHandler;
4143
pub use create::CreateCommandHandler;
@@ -48,3 +50,4 @@ pub use release::ReleaseCommandHandler;
4850
pub use run::RunCommandHandler;
4951
pub use show::ShowCommandHandler;
5052
pub use test::TestCommandHandler;
53+
pub use validate::ValidateCommandHandler;

0 commit comments

Comments
 (0)