mirror of
https://github.com/imjasonh/krust
synced 2026-07-07 22:35:25 +00:00
Merge branch 'main' into repro
This commit is contained in:
commit
d56bae42de
5 changed files with 296 additions and 52 deletions
65
.github/workflows/ci.yml
vendored
65
.github/workflows/ci.yml
vendored
|
|
@ -24,6 +24,7 @@ jobs:
|
|||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
targets: x86_64-unknown-linux-musl,aarch64-unknown-linux-musl
|
||||
components: rustfmt,clippy
|
||||
- name: Install cross-compilation tools for both architectures
|
||||
run: |
|
||||
sudo apt-get update
|
||||
|
|
@ -44,6 +45,9 @@ jobs:
|
|||
path: target
|
||||
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
|
||||
|
||||
- run: make check-fmt
|
||||
- run: make lint
|
||||
|
||||
- name: Setup cargo config for CI cross-compilation
|
||||
run: |
|
||||
mkdir -p .cargo
|
||||
|
|
@ -58,59 +62,16 @@ jobs:
|
|||
- run: make verify-cross-compile
|
||||
- run: make build
|
||||
- run: make test
|
||||
- run: make test-e2e
|
||||
|
||||
- run: make run-built-image
|
||||
# Only run cross-compilation integration test on x86_64 runners
|
||||
- name: Run integration test (cross-compilation)
|
||||
if: matrix.os == 'ubuntu-latest'
|
||||
|
||||
fmt:
|
||||
name: Rustfmt
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
- name: Install Rust
|
||||
uses: dtolnay/rust-toolchain@6d653acede28d24f02e3cd41383119e8b1b35921 # stable
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt
|
||||
- name: Check formatting
|
||||
run: make check-fmt
|
||||
run: make run-built-image
|
||||
|
||||
clippy:
|
||||
name: Clippy
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
- name: Install Rust
|
||||
uses: dtolnay/rust-toolchain@6d653acede28d24f02e3cd41383119e8b1b35921 # stable
|
||||
with:
|
||||
toolchain: stable
|
||||
components: clippy
|
||||
- name: Run clippy
|
||||
run: make lint
|
||||
- run: cargo install cargo-audit
|
||||
- run: cargo audit
|
||||
|
||||
security-audit:
|
||||
name: Security Audit
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
- name: Install Rust
|
||||
uses: dtolnay/rust-toolchain@6d653acede28d24f02e3cd41383119e8b1b35921 # stable
|
||||
with:
|
||||
toolchain: stable
|
||||
- name: Install cargo-audit
|
||||
run: cargo install cargo-audit
|
||||
- name: Run security audit
|
||||
run: cargo audit
|
||||
|
||||
# Takes too long to run on CI, so it's commented out for now.
|
||||
# coverage:
|
||||
# name: Code coverage
|
||||
# runs-on: ubuntu-latest
|
||||
# steps:
|
||||
# - uses: actions/checkout@v4
|
||||
# - name: Install Rust
|
||||
# uses: dtolnay/rust-toolchain@stable
|
||||
# - name: Install cargo-tarpaulin
|
||||
# run: cargo install cargo-tarpaulin
|
||||
# - name: Generate code coverage
|
||||
# run: cargo tarpaulin --verbose --workspace
|
||||
# TODO: fails in CI for now.
|
||||
#- run: cargo install cargo-tarpaulin
|
||||
#- run: cargo tarpaulin --verbose --workspace
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::*;
|
||||
use std::fs;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn test_get_rust_target_triple() {
|
||||
|
|
@ -38,4 +40,72 @@ mod tests {
|
|||
);
|
||||
assert!(get_rust_target_triple("windows/amd64").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_binary_name_valid() {
|
||||
let dir = tempdir().unwrap();
|
||||
let cargo_toml = dir.path().join("Cargo.toml");
|
||||
fs::write(
|
||||
&cargo_toml,
|
||||
r#"
|
||||
[package]
|
||||
name = "test-binary"
|
||||
version = "0.1.0"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let builder = RustBuilder::new(dir.path(), "x86_64-unknown-linux-musl");
|
||||
let name = builder.get_binary_name().unwrap();
|
||||
assert_eq!(name, "test-binary");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_binary_name_missing_cargo_toml() {
|
||||
let dir = tempdir().unwrap();
|
||||
let builder = RustBuilder::new(dir.path(), "x86_64-unknown-linux-musl");
|
||||
let result = builder.get_binary_name();
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().to_string().contains("Cargo.toml"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_binary_name_invalid_toml() {
|
||||
let dir = tempdir().unwrap();
|
||||
let cargo_toml = dir.path().join("Cargo.toml");
|
||||
fs::write(&cargo_toml, "invalid toml [[[").unwrap();
|
||||
|
||||
let builder = RustBuilder::new(dir.path(), "x86_64-unknown-linux-musl");
|
||||
let result = builder.get_binary_name();
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().to_string().contains("parse"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_binary_name_missing_package_name() {
|
||||
let dir = tempdir().unwrap();
|
||||
let cargo_toml = dir.path().join("Cargo.toml");
|
||||
fs::write(
|
||||
&cargo_toml,
|
||||
r#"
|
||||
[package]
|
||||
version = "0.1.0"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let builder = RustBuilder::new(dir.path(), "x86_64-unknown-linux-musl");
|
||||
let result = builder.get_binary_name();
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().to_string().contains("package name"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rust_builder_with_cargo_args() {
|
||||
let dir = tempdir().unwrap();
|
||||
let builder = RustBuilder::new(dir.path(), "x86_64-unknown-linux-musl")
|
||||
.with_cargo_args(vec!["--features".to_string(), "foo".to_string()]);
|
||||
|
||||
assert_eq!(builder.cargo_args, vec!["--features", "foo"]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::*;
|
||||
use std::fs;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn test_default_config() {
|
||||
|
|
@ -17,4 +19,60 @@ mod tests {
|
|||
assert!(build_config.cargo_args.is_empty());
|
||||
assert!(build_config.target_dir.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_project_config_no_cargo_toml() {
|
||||
let dir = tempdir().unwrap();
|
||||
let config = Config::load_project_config(dir.path()).unwrap();
|
||||
assert!(config.base_image.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_project_config_with_metadata() {
|
||||
let dir = tempdir().unwrap();
|
||||
let cargo_toml = dir.path().join("Cargo.toml");
|
||||
fs::write(
|
||||
&cargo_toml,
|
||||
r#"
|
||||
[package]
|
||||
name = "test"
|
||||
version = "0.1.0"
|
||||
|
||||
[package.metadata.krust]
|
||||
base-image = "custom:latest"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let config = Config::load_project_config(dir.path()).unwrap();
|
||||
assert_eq!(config.base_image, Some("custom:latest".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_project_config_without_metadata() {
|
||||
let dir = tempdir().unwrap();
|
||||
let cargo_toml = dir.path().join("Cargo.toml");
|
||||
fs::write(
|
||||
&cargo_toml,
|
||||
r#"
|
||||
[package]
|
||||
name = "test"
|
||||
version = "0.1.0"
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let config = Config::load_project_config(dir.path()).unwrap();
|
||||
assert!(config.base_image.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_project_config_invalid_toml() {
|
||||
let dir = tempdir().unwrap();
|
||||
let cargo_toml = dir.path().join("Cargo.toml");
|
||||
fs::write(&cargo_toml, "invalid toml [[[").unwrap();
|
||||
|
||||
let result = Config::load_project_config(dir.path());
|
||||
assert!(result.is_err());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -475,5 +475,93 @@ mod tests {
|
|||
|
||||
// Clean up
|
||||
std::env::remove_var("SOURCE_DATE_EPOCH");
|
||||
fn test_parse_platform_invalid_format() {
|
||||
let builder = ImageBuilder::new(
|
||||
PathBuf::from("/tmp/test"),
|
||||
"test-base".to_string(),
|
||||
"linux-amd64".to_string(), // Wrong format
|
||||
);
|
||||
|
||||
let result = builder.parse_platform();
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().to_string().contains("Invalid platform"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_layer_with_valid_binary() {
|
||||
let mut temp_file = NamedTempFile::new().unwrap();
|
||||
temp_file.write_all(b"test binary content").unwrap();
|
||||
temp_file.flush().unwrap();
|
||||
|
||||
let builder = ImageBuilder::new(
|
||||
temp_file.path().to_path_buf(),
|
||||
"test-base".to_string(),
|
||||
"linux/amd64".to_string(),
|
||||
);
|
||||
|
||||
let result = builder.create_layer();
|
||||
assert!(result.is_ok());
|
||||
|
||||
let (compressed_data, diff_id) = result.unwrap();
|
||||
assert!(!compressed_data.is_empty());
|
||||
assert!(diff_id.starts_with("sha256:"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_layer_with_nonexistent_binary() {
|
||||
let builder = ImageBuilder::new(
|
||||
PathBuf::from("/nonexistent/binary"),
|
||||
"test-base".to_string(),
|
||||
"linux/amd64".to_string(),
|
||||
);
|
||||
|
||||
let result = builder.create_layer();
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_layered_config_adds_path_if_missing() {
|
||||
let binary_path = create_test_binary();
|
||||
let builder = ImageBuilder::new(
|
||||
binary_path,
|
||||
"test-base".to_string(),
|
||||
"linux/amd64".to_string(),
|
||||
);
|
||||
|
||||
let mut base_config = create_base_image_config();
|
||||
base_config.config.env = vec![]; // No PATH
|
||||
|
||||
let app_diff_id = "sha256:app_layer_diff_id";
|
||||
|
||||
let result = builder
|
||||
.create_layered_config(&base_config, app_diff_id)
|
||||
.unwrap();
|
||||
|
||||
// Should add PATH since it was missing
|
||||
assert!(result.config.env.iter().any(|env| env.starts_with("PATH=")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_layered_config_sets_cmd() {
|
||||
let binary_path = create_test_binary();
|
||||
let builder = ImageBuilder::new(
|
||||
binary_path.clone(),
|
||||
"test-base".to_string(),
|
||||
"linux/amd64".to_string(),
|
||||
);
|
||||
|
||||
let base_config = create_base_image_config();
|
||||
let app_diff_id = "sha256:app_layer_diff_id";
|
||||
|
||||
let result = builder
|
||||
.create_layered_config(&base_config, app_diff_id)
|
||||
.unwrap();
|
||||
|
||||
// Should set CMD to the binary
|
||||
let binary_name = binary_path.file_name().unwrap().to_str().unwrap();
|
||||
assert_eq!(
|
||||
result.config.cmd,
|
||||
Some(vec![format!("/app/{}", binary_name)])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -196,4 +196,71 @@ image: krust://./app2
|
|||
assert!(refs.contains("./app1"));
|
||||
assert!(refs.contains("./app2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_yaml_files_single_file() {
|
||||
use std::fs;
|
||||
use tempfile::tempdir;
|
||||
|
||||
let dir = tempdir().unwrap();
|
||||
let file_path = dir.path().join("test.yaml");
|
||||
fs::write(&file_path, "image: krust://./app").unwrap();
|
||||
|
||||
let files = read_yaml_files(&file_path).unwrap();
|
||||
assert_eq!(files.len(), 1);
|
||||
assert!(files[0].0.contains("test.yaml"));
|
||||
assert!(files[0].1.contains("krust://./app"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_yaml_files_directory() {
|
||||
use std::fs;
|
||||
use tempfile::tempdir;
|
||||
|
||||
let dir = tempdir().unwrap();
|
||||
fs::write(dir.path().join("test1.yaml"), "image: krust://./app1").unwrap();
|
||||
fs::write(dir.path().join("test2.yml"), "image: krust://./app2").unwrap();
|
||||
fs::write(dir.path().join("test.txt"), "not yaml").unwrap();
|
||||
|
||||
let files = read_yaml_files(dir.path()).unwrap();
|
||||
assert_eq!(files.len(), 2);
|
||||
assert!(files.iter().any(|(name, _)| name.contains("test1.yaml")));
|
||||
assert!(files.iter().any(|(name, _)| name.contains("test2.yml")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_yaml_files_empty_directory() {
|
||||
use tempfile::tempdir;
|
||||
|
||||
let dir = tempdir().unwrap();
|
||||
let result = read_yaml_files(dir.path());
|
||||
assert!(result.is_err());
|
||||
assert!(result
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("No YAML files found"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_yaml_files_nonexistent_path() {
|
||||
use std::path::PathBuf;
|
||||
|
||||
let path = PathBuf::from("/nonexistent/path/that/does/not/exist");
|
||||
let result = read_yaml_files(&path);
|
||||
assert!(result.is_err());
|
||||
assert!(result
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("Path does not exist"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_references_empty_replacements() {
|
||||
let yaml = r#"image: krust://./app"#;
|
||||
let replacements = HashMap::new();
|
||||
|
||||
let result = replace_krust_references(yaml, &replacements).unwrap();
|
||||
// Should keep original reference if no replacement found
|
||||
assert!(result.contains("krust://./app"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue