Clean up
Some checks failed
Build and Publish Docker Image / test (push) Failing after 7s
Build and Publish Docker Image / build-and-push (push) Has been skipped
Build and Publish Docker Image / security-scan (push) Has been skipped

This commit is contained in:
Andras Schmelczer 2026-06-06 19:41:55 +01:00
parent 3f60b72c3b
commit b414c05400
18 changed files with 905 additions and 985 deletions

View file

@ -3,11 +3,11 @@ use docker_compose_updater::config::{Config, RegistryConfig, UpdateStrategy};
use docker_compose_updater::registry::{Client, ImageRef};
use std::collections::HashMap;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use tempfile::NamedTempFile;
#[tokio::test]
async fn test_end_to_end_compose_file_parsing_and_updating() {
async fn test_compose_file_parsing() {
let compose_content = r#"
version: '3.8'
services:
@ -15,12 +15,12 @@ services:
image: nginx:1.21.0 # Web server
ports:
- "80:80"
db:
image: postgres:13.7
environment:
POSTGRES_DB: myapp
redis:
image: redis:6.2.1-alpine
ports:
@ -30,9 +30,7 @@ services:
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(compose_content.as_bytes()).unwrap();
let config = create_test_config();
let updater = ComposeUpdater::new(config);
let updater = ComposeUpdater::new(create_test_config());
let result = updater
.parse_compose_file(temp_file.path().to_str().unwrap())
.unwrap();
@ -54,7 +52,7 @@ services:
}
#[tokio::test]
async fn test_comment_and_formatting_preservation_during_updates() {
async fn test_comment_and_formatting_preservation() {
let compose_content = r#"
version: '3.8'
services:
@ -62,7 +60,7 @@ services:
image: nginx:1.21.0 # This is a comment
ports:
- "80:80"
# This is another comment
db:
image: "postgres:13.7" # Database comment with quotes
@ -73,9 +71,7 @@ services:
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(compose_content.as_bytes()).unwrap();
let config = create_test_config();
let updater = ComposeUpdater::new(config);
let updater = ComposeUpdater::new(create_test_config());
let result = updater
.parse_compose_file(temp_file.path().to_str().unwrap())
.unwrap();
@ -87,34 +83,34 @@ services:
.original_line
.contains("# Database comment"));
// Replace unquoted image -> stays unquoted, preserves comment
let new_content = updater
.replace_image_in_content(&result.content, &result.services[0], "nginx:1.20.0")
.unwrap();
assert!(new_content.contains("# This is a comment"));
assert!(new_content.contains("nginx:1.20.0"));
assert!(new_content.contains("nginx:1.20.0 # This is a comment"));
assert!(!new_content.contains("nginx:1.21.0"));
// Replace quoted image -> stays quoted, preserves comment
let new_content = updater
.replace_image_in_content(&new_content, &result.services[1], "postgres:14.0")
.unwrap();
assert!(new_content.contains("# Database comment"));
assert!(new_content.contains("\"postgres:14.0\""));
assert!(new_content.contains("\"postgres:14.0\" # Database comment"));
assert!(!new_content.contains("postgres:13.7"));
}
#[tokio::test]
async fn test_empty_compose_paths_handling() {
async fn test_empty_compose_paths() {
let config = Config {
compose_paths: vec![],
..create_test_config()
};
let updater = ComposeUpdater::new(config);
let result = updater.update_all_compose_files().await;
assert!(result.is_ok());
assert!(result.unwrap().is_empty());
let result = updater.update_all_compose_files().await.unwrap();
assert_eq!(result.files_found, 0);
assert!(result.updated_files.is_empty());
}
#[test]
@ -140,6 +136,77 @@ fn test_config_defaults_and_image_filtering() {
assert!(!config_with_ignores.is_image_ignored("nginx:1.21.0"));
}
#[test]
fn test_env_var_substitution() {
std::env::set_var("TEST_TOKEN", "test_value_123");
let test_yaml = r#"
registries:
"test.registry":
url: "https://test.registry"
auth_token: "${TEST_TOKEN}"
"#;
let expanded = Config::expand_env_vars(test_yaml);
assert!(expanded.contains("test_value_123"));
assert!(!expanded.contains("${TEST_TOKEN}"));
std::env::remove_var("TEST_TOKEN");
}
#[test]
fn test_dollar_var_and_unset_var_expansion() {
std::env::set_var("TEST_DOLLAR_TOKEN", "dollar_value_456");
let expanded = Config::expand_env_vars("token: $TEST_DOLLAR_TOKEN");
assert!(expanded.contains("dollar_value_456"));
std::env::remove_var("TEST_DOLLAR_TOKEN");
let expanded = Config::expand_env_vars("token: ${DEFINITELY_UNSET_VAR}");
assert!(!expanded.contains("DEFINITELY_UNSET_VAR"));
assert!(expanded.contains("token: "));
}
#[tokio::test]
#[ignore = "Only run in CI with GITHUB_TOKEN set"]
async fn test_ghcr_authentication_e2e() {
let github_token = std::env::var("GITHUB_TOKEN").expect("GITHUB_TOKEN must be set");
let mut registries = HashMap::new();
registries.insert(
"docker.io".to_string(),
RegistryConfig {
url: "https://registry-1.docker.io".to_string(),
auth_token: None,
},
);
registries.insert(
"ghcr.io".to_string(),
RegistryConfig {
url: "https://ghcr.io".to_string(),
auth_token: Some(github_token),
},
);
let config = Config {
compose_paths: vec![PathBuf::from("./test")],
schedule: "0 0 2 * * *".to_string(),
registries,
update_strategy: UpdateStrategy::LatestPatchOfPreviousMinor,
ignore_images: vec!["localhost".to_string()],
dry_run: true,
};
let client = Client::new(config);
let image_ref = ImageRef::parse("ghcr.io/schmelczer/vault-link:0.5.1").unwrap();
let versions = client.get_available_versions(&image_ref).await.unwrap();
assert!(!versions.is_empty());
assert!(
versions.iter().any(|v| v.version.to_string() == "0.5.1"),
"Should find version 0.5.1"
);
}
fn create_test_config() -> Config {
let mut registries = HashMap::new();
registries.insert(
@ -159,101 +226,3 @@ fn create_test_config() -> Config {
dry_run: true,
}
}
#[test]
fn test_env_var_substitution() {
// Test environment variable substitution in config
std::env::set_var("TEST_TOKEN", "test_value_123");
let test_yaml = r#"
registries:
"test.registry":
url: "https://test.registry"
auth_token: "${TEST_TOKEN}"
"#;
let expanded = Config::expand_env_vars(test_yaml);
assert!(expanded.contains("test_value_123"));
assert!(!expanded.contains("${TEST_TOKEN}"));
// Clean up
std::env::remove_var("TEST_TOKEN");
}
#[test]
fn test_config_loading_with_env_vars() {
// Set up test environment variable
std::env::set_var("GITHUB_TOKEN", "ghp_test_token_for_testing");
// Test that config loading works with our test config
if std::path::Path::new("config.test.yaml").exists() {
let config = Config::load(Path::new("config.test.yaml").to_path_buf()).unwrap();
if let Some(ghcr_config) = config.registries.get("ghcr.io") {
// Should have resolved the environment variable
assert_eq!(
ghcr_config.auth_token,
Some("ghp_test_token_for_testing".to_string())
);
}
}
// Clean up
std::env::remove_var("GITHUB_TOKEN");
}
#[tokio::test]
#[ignore = "Only run in CI with GITHUB_TOKEN set"]
async fn test_ghcr_authentication_e2e() {
let github_token = std::env::var("GITHUB_TOKEN").unwrap();
// This test validates that Docker Registry v2 authentication works with GHCR
let mut registries = HashMap::new();
registries.insert(
"docker.io".to_string(),
RegistryConfig {
url: "https://registry-1.docker.io".to_string(),
auth_token: None,
},
);
registries.insert(
"ghcr.io".to_string(),
RegistryConfig {
url: "https://ghcr.io".to_string(),
// Use the token we validated above
auth_token: Some(github_token),
},
);
let config = Config {
compose_paths: vec![PathBuf::from("./test")],
schedule: "0 0 2 * * *".to_string(),
registries,
update_strategy: UpdateStrategy::LatestPatchOfPreviousMinor,
ignore_images: vec!["localhost".to_string()],
dry_run: true,
};
let client = Client::new(config);
// Test with the actual image that was failing
let image_ref = ImageRef::parse("ghcr.io/schmelczer/vault-link:0.5.1").unwrap();
let result = client.get_available_versions(&image_ref).await;
match result {
Ok(versions) => {
assert!(!versions.is_empty(), "Should return at least one version");
// Verify that we can find the specific version 0.5.1
let has_target_version = versions.iter().any(|v| v.version.to_string() == "0.5.1");
assert!(
has_target_version,
"Should find target version 0.5.1 in available versions"
);
}
Err(e) => {
panic!("GHCR authentication test failed: {e}");
}
}
}