Skip to content

Commit

Permalink
Merge branch 'main' into fix/ssh_config_path_check
Browse files Browse the repository at this point in the history
  • Loading branch information
empwilli committed Jun 12, 2024
2 parents b01330a + deb7db2 commit b3eb2e0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 78 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,10 @@ assert-json-diff = "2.0"
data-encoding = "2.5"
file_diff = "1.0"
httpmock = "0.6"
omnect-cli = { path = ".", features = ["mock"] }
ring = "0.17"
tar = "0.4.41"

# metadata for building with cargo-deb (https://crates.io/crates/cargo-deb)
[package.metadata.deb]
depends = "bmap-tools, e2tools, fdisk, keychain, libc6 (>= 2.34), libmagic1, libssl3 (>= 3.0.0), mtools"
revision = ""

[features]
default = []
mock = []
115 changes: 44 additions & 71 deletions src/docker.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use anyhow::{Context, Result};
use std::path::PathBuf;

use crate::file::compression::Compression;
use crate::image::Architecture;
use std::fs::File;
use std::os::fd::AsFd;
use std::process::{Command, Stdio};

impl From<Architecture> for &str {
fn from(arch: Architecture) -> &'static str {
Expand All @@ -13,80 +17,49 @@ impl From<Architecture> for &str {
}
}

#[cfg(not(feature = "mock"))]
mod inner {
use super::*;
const COMPRESSION_LEVEL: u32 = 9;

use crate::file::compression::Compression;
use std::fs::File;
use std::os::fd::AsFd;
use std::process::{Command, Stdio};
pub fn pull_image(name: impl AsRef<str>, arch: Architecture) -> Result<PathBuf> {
let cmd_out = Command::new("docker")
.args(["pull"])
.args(["--platform", arch.into()])
.arg(name.as_ref())
.output()
.context("pull_docker_image: could not run \"docker pull\" command")?;

const COMPRESSION_LEVEL: u32 = 9;

pub fn pull_image(name: impl AsRef<str>, arch: Architecture) -> Result<PathBuf> {
let cmd_out = Command::new("docker")
.args(["pull"])
.args(["--platform", arch.into()])
.arg(name.as_ref())
.output()
.context("pull_docker_image: could not run \"docker pull\" command")?;

if !cmd_out.status.success() {
let cmd_out = std::str::from_utf8(&cmd_out.stderr).unwrap();
anyhow::bail!("Could not pull docker image: {cmd_out}");
}

let mut child = Command::new("docker")
.args(["save"])
.arg(name.as_ref())
.stdout(Stdio::piped())
.spawn()
.context("pull_docker_image: could not run \"docker save\" command")?;

let stdout = child.stdout.take().unwrap();
let mut image_file = File::from(stdout.as_fd().try_clone_to_owned()?);

let out_path = std::path::PathBuf::from(format!("{}.tar.xz", name.as_ref()));
let mut out_file = std::fs::File::options()
.create_new(true)
.write(true)
.open(out_path.clone())
.context("pull_docker_image: could not create output file")?;

let xz = Compression::xz {
compression_level: COMPRESSION_LEVEL,
};
xz.compress(&mut image_file, &mut out_file)?;

let error_code = child.wait()?;

if !error_code.success() {
let cmd_out = std::str::from_utf8(&cmd_out.stderr).unwrap();
anyhow::bail!("Could not save docker image: {cmd_out}");
}

Ok(out_path)
if !cmd_out.status.success() {
let cmd_out = std::str::from_utf8(&cmd_out.stderr).unwrap();
anyhow::bail!("Could not pull docker image: {cmd_out}");
}
}

#[cfg(feature = "mock")]
mod inner {
use super::*;

use std::io::Write;

pub fn pull_image(name: impl AsRef<str>, _arch: Architecture) -> Result<PathBuf> {
let out_path = std::path::PathBuf::from(format!("{}.tar.gz", name.as_ref()));
let mut out_file = std::fs::File::options()
.create_new(true)
.write(true)
.open(out_path.clone())
.context("pull_docker_image: could not create output file")?;

out_file.write_all(b"some test data")?;
Ok(out_path)
let mut child = Command::new("docker")
.args(["save"])
.arg(name.as_ref())
.stdout(Stdio::piped())
.spawn()
.context("pull_docker_image: could not run \"docker save\" command")?;

let stdout = child.stdout.take().unwrap();
let mut image_file = File::from(stdout.as_fd().try_clone_to_owned()?);

let out_path = std::path::PathBuf::from(format!("{}.tar.xz", name.as_ref()));
let mut out_file = std::fs::File::options()
.create_new(true)
.write(true)
.open(out_path.clone())
.context("pull_docker_image: could not create output file")?;

let xz = Compression::xz {
compression_level: COMPRESSION_LEVEL,
};
xz.compress(&mut image_file, &mut out_file)?;

let error_code = child.wait()?;

if !error_code.success() {
let cmd_out = std::str::from_utf8(&cmd_out.stderr).unwrap();
anyhow::bail!("Could not save docker image: {cmd_out}");
}
}

pub use inner::pull_image;
Ok(out_path)
}
7 changes: 6 additions & 1 deletion tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,13 @@ Host test_device
assert_eq!(ssh_config, expected_config);
}

// currently disabled as we have no way to test this in our pipeline were we
// don't have docker installed
#[ignore]
#[test]
fn check_docker_inject_image_success() {
let image = std::env::var("TEST_DOCKER_IMAGE").unwrap_or("busybox".to_string());

let tr = Testrunner::new(function_name!().split("::").last().unwrap());

let image_path = tr.to_pathbuf("testfiles/image.wic");
Expand All @@ -943,7 +948,7 @@ fn check_docker_inject_image_success() {
let assert = docker_inject_image_config
.arg("docker")
.arg("inject")
.args(["--docker-image", "some-image"])
.args(["--docker-image", &image])
.args(["--image", &image_path.to_string_lossy()])
.args(["--partition", "factory"])
.args(["--dest", "/some/test/dir"])
Expand Down

0 comments on commit b3eb2e0

Please sign in to comment.