Skip to content

Commit

Permalink
Fix remote temp Windows path being UNIX-like
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Dec 13, 2024
1 parent a3a6156 commit 0ae60df
Showing 1 changed file with 53 additions and 41 deletions.
94 changes: 53 additions & 41 deletions test/test-manager/src/vm/provision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,43 +59,50 @@ async fn provision_ssh(
let user = user.to_owned();
let password = password.to_owned();

let remote_dir = match os_type {
OsType::Windows => r"C:\testing",
OsType::Macos | OsType::Linux => r"/opt/testing",
};

let local_runner_dir = local_runner_dir.to_owned();
let local_app_manifest = local_app_manifest.to_owned();

tokio::task::spawn_blocking(move || {
let remote_dir = tokio::task::spawn_blocking(move || {
blocking_ssh(
user,
password,
guest_ip,
os_type,
&local_runner_dir,
local_app_manifest,
remote_dir,
)
})
.await
.context("Failed to join SSH task")??;

Ok(remote_dir.to_string())
Ok(remote_dir)
}

/// Returns the remote runner directory
fn blocking_ssh(
user: String,
password: String,
guest_ip: IpAddr,
os_type: OsType,
local_runner_dir: &Path,
local_app_manifest: package::Manifest,
remote_dir: &str,
) -> Result<()> {
// Directory that receives the payload. Any directory that the SSH user has access to.
const REMOTE_TEMP_DIR: &str = "/tmp/";
) -> Result<String> {
let remote_dir = match os_type {
// FIXME: There is a problem with the `ssh2` crate (both with scp and sftp) that
// we can not create new directories, so instead we have to rely on pre-existing
// directories if we want to create / upload files to the Windows guest. As a
// workaround, use `C:` as a temporary directory.
OsType::Windows => "c:",
OsType::Macos | OsType::Linux => "/opt/testing",
};

let temp_dir = Path::new(REMOTE_TEMP_DIR);
// Directory that receives the payload. Any directory that the SSH user has access to.
let remote_temp_dir = match os_type {
OsType::Windows => r"c:\temp",
OsType::Macos | OsType::Linux => r"/tmp/",
};

std::thread::sleep(std::time::Duration::from_secs(10));
let stream = TcpStream::connect(SocketAddr::new(guest_ip, 22)).context("TCP connect failed")?;

let mut session = Session::new().context("Failed to connect to SSH server")?;
Expand All @@ -106,6 +113,7 @@ fn blocking_ssh(
.userauth_password(&user, &password)
.context("SSH auth failed")?;

let temp_dir = Path::new(remote_temp_dir);
// Transfer a test runner
let source = local_runner_dir.join("test-runner");
ssh_send_file(&session, &source, temp_dir)
Expand Down Expand Up @@ -136,34 +144,38 @@ fn blocking_ssh(

// Transfer setup script
// TODO: Move this name to a constant somewhere?
let bootstrap_script_dest = temp_dir.join("ssh-setup.sh");
ssh_write(&session, &bootstrap_script_dest, BOOTSTRAP_SCRIPT)
.context("failed to send bootstrap script to remote")?;

// Run setup script
let app_package_path = local_app_manifest
.app_package_path
.file_name()
.unwrap()
.to_string_lossy();
let app_package_to_upgrade_from_path = local_app_manifest
.app_package_to_upgrade_from_path
.map(|path| path.file_name().unwrap().to_string_lossy().into_owned())
.unwrap_or_default();
let gui_package_path = local_app_manifest
.gui_package_path
.map(|path| path.file_name().unwrap().to_string_lossy().into_owned())
.unwrap_or_default();

// Run the setup script in the test runner
let cmd = format!(
r#"sudo {} {remote_dir} "{app_package_path}" "{app_package_to_upgrade_from_path}" "{gui_package_path}" "{UNPRIVILEGED_USER}""#,
bootstrap_script_dest.display(),
);
log::debug!("Running setup script on remote, cmd: {cmd}");
ssh_exec(&session, &cmd)
.map(drop)
.context("Failed to run setup script")
if matches!(os_type, OsType::Linux | OsType::Macos) {
let bootstrap_script_dest = temp_dir.join("ssh-setup.sh");
ssh_write(&session, &bootstrap_script_dest, BOOTSTRAP_SCRIPT)
.context("failed to send bootstrap script to remote")?;

// Run setup script
let app_package_path = local_app_manifest
.app_package_path
.file_name()
.unwrap()
.to_string_lossy();
let app_package_to_upgrade_from_path = local_app_manifest
.app_package_to_upgrade_from_path
.map(|path| path.file_name().unwrap().to_string_lossy().into_owned())
.unwrap_or_default();
let gui_package_path = local_app_manifest
.gui_package_path
.map(|path| path.file_name().unwrap().to_string_lossy().into_owned())
.unwrap_or_default();

// Run the setup script in the test runner
let cmd = format!(
r#"sudo {} {remote_dir} "{app_package_path}" "{app_package_to_upgrade_from_path}" "{gui_package_path}" "{UNPRIVILEGED_USER}""#,
bootstrap_script_dest.display(),
);
log::debug!("Running setup script on remote, cmd: {cmd}");
ssh_exec(&session, &cmd)
.map(drop)
.context("Failed to run setup script")?;
}

Ok(remote_dir.to_string())
}

/// Copy a `source` file to `dest_dir` in the test runner.
Expand Down

0 comments on commit 0ae60df

Please sign in to comment.