Skip to content

Commit a6a99b7

Browse files
committed
fix: add SSH key fixtures and improve E2E test SSH connectivity
- Add testing SSH key pair in fixtures/ directory for CI/CD compatibility - Update cloud-init.yml to use the new testing public key - Modify e2e_tests.rs to copy SSH keys to temporary directory during tests - Use temporary SSH keys instead of relying on local ~/.ssh/testing_rsa - Add tempfile dependency for secure temporary directory management - Suppress dead_code warning for temp_dir field (needed for directory lifetime) - Add verbose logging for SSH key and temp directory paths This resolves the SSH connectivity issue in GitHub Actions by providing self-contained SSH keys that don't depend on local machine configuration.
1 parent 1788523 commit a6a99b7

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ anyhow = "1.0"
1919
clap = { version = "4.0", features = ["derive"] }
2020
regex = "1.0"
2121
dirs = "5.0"
22+
tempfile = "3.0"

config/tofu/lxd/cloud-init.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ users:
1616
shell: /bin/bash
1717
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
1818
ssh_authorized_keys:
19-
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDVvNSEw9lRv0wIHXHle7jNpGBgQfJ5fsT4WlntGqzVW0smjSAy9GgsERUrkX8N4jz5NherZVJpzMGn6V69CiClyptXeUUuz+BmWAtBH8pIJf5quku4Xmmxg9+hlal5JtdqH5ZwSCrF0EFzb2fS/hTk9npwZknR5Av4Nk+iREM+EvB/rbOfnXU7xNezIJ6sZNt2zIH3bROsmthmffEegosSQ2KO4YGHv6mW0SV/O+X22RHelP/3tAMu/2mM1ecdMOib32IlG38PjHnlhB6gmUoQ1USsPILxhaZLdU7pKs7Jx3FRZHuPQHNqj8ZVMzYZ2ONZ+uYUzg+kocJGiqV20PiJBrzTvUzoknvzU7l8cI5smmrXdZ3EKtykdbLLe21T8hrL4KndYYKTeXRTM63kHjluvX/ZVr4jmnz3GHTwf1HFnTmrq7jSaF5n4Y82qEu1ps9KFSCx4fXHxUeSNIzIqF+AOOS0t1nVWgc+q22p9khgnKsakVrTK+WEbvGSqjEWkzh2J2MjCwuc0IJClBrpqNFyOW/+eZutbrDjX7NAxBYCJZd64tCC2/BAefvbCDWIR2t2XGY8JcaPH5MvRnXR391kq/savEe8527AIkp3ZaV99js1psUIv67434bggpPz1LwpQW2bBA9mxW1XFXZsDQEdefXUujmcb4EbuYcow+SAEw== testing-key-for-torrust-deploy
19+
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCw16sai+XVnawp/P/Q23kcXKekygZ6ALmQAyslREo6kbG8s5RScsmbQqOQEcIwnV2Vo88eeWVzX0N0H1dIczRa/ezijBEsGefthzmz9Ix/vM4lodzTPQFtW8c2eYw7ESy12/2x5//UQQ3mxawEWsz5Ri8XuyBEy/Xh7xH/KpoektaocIOt2/WdCe8CvZdMLd7AviGcTdHFWRiOVrmHM1Pd8znqeA3/1KQP/M4Ae5q21oPjchGjVfPkGh/e62Wt+Wo/2lT30AyMO7JHA1tB1W4xANRQkOd1Kb/TrDLXfg0PaHQ+Irmycjp/H4KkcdB06nzYawXMN5csd/5TWKwkb9/vofp6GQNP731U8+JR4cxRfD107KoHroDSJpG2Fanb2PVBkSXAiJl29YrtoP9vUtSIemQCD/aXFtTcpSv7Y16bdp7v+0adCEHwBmodm9GzLL808FpI2ZCzCi+Ae98P3z+yPCxbrnVAahU8AM2NSbrfyH1w2eb4hJ22oPjdd//tBYtkE1TZBw+i3n0vRn04s5BfPRwwj5GISxacTOZm/YWvoE4UU9axtFXOtMUniVKL3ycA+LEfK7C4velOKbluyL8fYYu4pUxHnYOOkYYeRoi2jf3oagbABOpznloPd93wYP3NoUpIdtMZW+iCF0NnZkVLC9lm1FbTcnmrfNzFtGVKCQ== testing@torrust-testing-infra
2020

2121
runcmd:
2222
- echo "VM provisioned successfully" > /tmp/provision_complete

src/bin/e2e_tests.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use serde_json::Value;
55
use std::path::{Path, PathBuf};
66
use std::process::{Command, Stdio};
77
use std::time::{Duration, Instant};
8+
use tempfile::TempDir;
89
use tokio::time::sleep;
910

1011
#[derive(Parser)]
@@ -33,17 +34,46 @@ struct TestEnvironment {
3334
project_root: PathBuf,
3435
keep_env: bool,
3536
verbose: bool,
37+
ssh_key_path: PathBuf,
38+
#[allow(dead_code)] // Kept to maintain temp directory lifetime
39+
temp_dir: Option<tempfile::TempDir>,
3640
}
3741

3842
impl TestEnvironment {
3943
fn new(keep_env: bool, verbose: bool) -> Result<Self> {
4044
// Get project root (current directory when running from root)
4145
let project_root = std::env::current_dir()?;
4246

47+
// Create temporary directory for SSH keys
48+
let temp_dir = TempDir::new().context("Failed to create temporary directory")?;
49+
50+
// Copy SSH private key from fixtures to temp directory
51+
let fixtures_ssh_key = project_root.join("fixtures/testing_rsa");
52+
let temp_ssh_key = temp_dir.path().join("testing_rsa");
53+
54+
std::fs::copy(&fixtures_ssh_key, &temp_ssh_key)
55+
.context("Failed to copy SSH private key to temporary directory")?;
56+
57+
// Set proper permissions on the SSH key (600)
58+
#[cfg(unix)]
59+
{
60+
use std::os::unix::fs::PermissionsExt;
61+
let mut perms = std::fs::metadata(&temp_ssh_key)?.permissions();
62+
perms.set_mode(0o600);
63+
std::fs::set_permissions(&temp_ssh_key, perms)?;
64+
}
65+
66+
if verbose {
67+
println!("🔑 SSH key copied to temporary location: {}", temp_ssh_key.display());
68+
println!("📁 Temporary directory: {}", temp_dir.path().display());
69+
}
70+
4371
Ok(Self {
4472
project_root,
4573
keep_env,
4674
verbose,
75+
ssh_key_path: temp_ssh_key,
76+
temp_dir: Some(temp_dir),
4777
})
4878
}
4979

@@ -140,17 +170,13 @@ impl TestEnvironment {
140170
async fn wait_for_ssh_connectivity(&self, ip: &str) -> Result<()> {
141171
println!("🔌 Waiting for SSH connectivity...");
142172

143-
let ssh_key = dirs::home_dir()
144-
.ok_or_else(|| anyhow!("Could not determine home directory"))?
145-
.join(".ssh/testing_rsa");
146-
147173
let max_attempts = 30;
148174
let mut attempt = 0;
149175

150176
while attempt < max_attempts {
151177
let result = Command::new("ssh")
152178
.args(&[
153-
"-i", ssh_key.to_str().unwrap(),
179+
"-i", self.ssh_key_path.to_str().unwrap(),
154180
"-o", "StrictHostKeyChecking=no",
155181
"-o", "UserKnownHostsFile=/dev/null",
156182
"-o", "ConnectTimeout=5",
@@ -223,14 +249,10 @@ impl TestEnvironment {
223249
async fn validate_cloud_init_completion(&self, container_ip: &str) -> Result<()> {
224250
println!("🔍 Validating cloud-init completion...");
225251

226-
let ssh_key = dirs::home_dir()
227-
.ok_or_else(|| anyhow!("Could not determine home directory"))?
228-
.join(".ssh/testing_rsa");
229-
230252
// Check cloud-init status
231253
let output = Command::new("ssh")
232254
.args(&[
233-
"-i", ssh_key.to_str().unwrap(),
255+
"-i", self.ssh_key_path.to_str().unwrap(),
234256
"-o", "StrictHostKeyChecking=no",
235257
"-o", "UserKnownHostsFile=/dev/null",
236258
&format!("torrust@{}", container_ip),
@@ -251,7 +273,7 @@ impl TestEnvironment {
251273
// Check for completion marker file
252274
let marker_check = Command::new("ssh")
253275
.args(&[
254-
"-i", ssh_key.to_str().unwrap(),
276+
"-i", self.ssh_key_path.to_str().unwrap(),
255277
"-o", "StrictHostKeyChecking=no",
256278
"-o", "UserKnownHostsFile=/dev/null",
257279
&format!("torrust@{}", container_ip),

0 commit comments

Comments
 (0)