Initial
Some checks failed
Build and Publish Docker Image / test (push) Failing after 5s
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-03-23 07:44:26 +00:00
commit 3f60b72c3b
48 changed files with 6599 additions and 0 deletions

259
tests/integration_tests.rs Normal file
View file

@ -0,0 +1,259 @@
use docker_compose_updater::compose::updater::ComposeUpdater;
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 tempfile::NamedTempFile;
#[tokio::test]
async fn test_end_to_end_compose_file_parsing_and_updating() {
let compose_content = r#"
version: '3.8'
services:
web:
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:
- "6379:6379"
"#;
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 result = updater
.parse_compose_file(temp_file.path().to_str().unwrap())
.unwrap();
assert_eq!(result.services.len(), 3);
assert_eq!(result.services[0].service_name, "web");
assert_eq!(result.services[0].image_ref.name, "nginx");
assert_eq!(result.services[0].image_ref.tag, "1.21.0");
assert_eq!(result.services[0].image_ref.registry, "docker.io");
assert_eq!(result.services[1].service_name, "db");
assert_eq!(result.services[1].image_ref.name, "postgres");
assert_eq!(result.services[1].image_ref.tag, "13.7");
assert_eq!(result.services[2].service_name, "redis");
assert_eq!(result.services[2].image_ref.name, "redis");
assert_eq!(result.services[2].image_ref.tag, "6.2.1-alpine");
}
#[tokio::test]
async fn test_comment_and_formatting_preservation_during_updates() {
let compose_content = r#"
version: '3.8'
services:
web:
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
environment:
POSTGRES_DB: myapp
"#;
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 result = updater
.parse_compose_file(temp_file.path().to_str().unwrap())
.unwrap();
assert!(result.services[0]
.original_line
.contains("# This is a comment"));
assert!(result.services[1]
.original_line
.contains("# Database 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.21.0"));
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:13.7"));
}
#[tokio::test]
async fn test_empty_compose_paths_handling() {
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());
}
#[test]
fn test_config_defaults_and_image_filtering() {
let config = Config::default();
assert_eq!(config.schedule, "0 0 2 * * *");
assert_eq!(
config.update_strategy,
UpdateStrategy::LatestPatchOfPreviousMinor
);
assert!(!config.dry_run);
assert!(config.registries.contains_key("docker.io"));
assert!(config.registries.contains_key("ghcr.io"));
let config_with_ignores = Config {
ignore_images: vec!["localhost".to_string(), "127.0.0.1".to_string()],
..Config::default()
};
assert!(config_with_ignores.is_image_ignored("localhost:5000/myapp:latest"));
assert!(config_with_ignores.is_image_ignored("127.0.0.1:5000/myapp:latest"));
assert!(!config_with_ignores.is_image_ignored("nginx:1.21.0"));
}
fn create_test_config() -> Config {
let mut registries = HashMap::new();
registries.insert(
"docker.io".to_string(),
RegistryConfig {
url: "https://registry-1.docker.io".to_string(),
auth_token: None,
},
);
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,
}
}
#[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}");
}
}
}

View file

@ -0,0 +1,509 @@
use docker_compose_updater::compose::updater::ComposeUpdater;
use docker_compose_updater::config::{Config, RegistryConfig, UpdateStrategy};
use docker_compose_updater::registry::ImageRef;
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use tempfile::NamedTempFile;
#[test]
fn test_image_ref_comprehensive_parsing() {
// Test comprehensive image reference parsing scenarios
let test_cases = vec![
// (input, expected_registry, expected_namespace, expected_name, expected_tag)
// Standard Docker Hub images
("nginx", "docker.io", None, "nginx", "latest"),
("nginx:1.21", "docker.io", None, "nginx", "1.21"),
("nginx:latest", "docker.io", None, "nginx", "latest"),
// Docker Hub with namespace
(
"library/nginx:1.21",
"docker.io",
Some("library"),
"nginx",
"1.21",
),
(
"bitnami/nginx:1.21",
"docker.io",
Some("bitnami"),
"nginx",
"1.21",
),
// Custom registries
("quay.io/nginx:1.21", "quay.io", None, "nginx", "1.21"),
(
"gcr.io/project/nginx:1.21",
"gcr.io",
Some("project"),
"nginx",
"1.21",
),
(
"registry.k8s.io/pause:3.9",
"registry.k8s.io",
None,
"pause",
"3.9",
),
// GitHub Container Registry
(
"ghcr.io/user/app:v1.0.0",
"ghcr.io",
Some("user"),
"app",
"v1.0.0",
),
(
"ghcr.io/org/project:latest",
"ghcr.io",
Some("org"),
"project",
"latest",
),
// Local registries with ports
(
"localhost:5000/app:dev",
"localhost:5000",
None,
"app",
"dev",
),
(
"127.0.0.1:8080/ns/app:test",
"127.0.0.1:8080",
Some("ns"),
"app",
"test",
),
// Complex tags with versions and suffixes
(
"nginx:1.21.6-alpine",
"docker.io",
None,
"nginx",
"1.21.6-alpine",
),
(
"postgres:13.7-bullseye",
"docker.io",
None,
"postgres",
"13.7-bullseye",
),
(
"redis:7.0.0-alpine3.16",
"docker.io",
None,
"redis",
"7.0.0-alpine3.16",
),
// Enterprise/custom registries
(
"my-registry.company.com/team/app:v2.1.3",
"my-registry.company.com",
Some("team"),
"app",
"v2.1.3",
),
(
"harbor.example.org/project/nginx:stable",
"harbor.example.org",
Some("project"),
"nginx",
"stable",
),
];
for (input, expected_registry, expected_namespace, expected_name, expected_tag) in test_cases {
let result = ImageRef::parse(input);
assert!(result.is_ok(), "Failed to parse: {input}");
let image_ref = result.unwrap();
assert_eq!(
image_ref.registry, expected_registry,
"Registry mismatch for: {input}"
);
assert_eq!(
image_ref.namespace,
expected_namespace.map(String::from),
"Namespace mismatch for: {input}"
);
assert_eq!(image_ref.name, expected_name, "Name mismatch for: {input}");
assert_eq!(image_ref.tag, expected_tag, "Tag mismatch for: {input}");
// Test that the image ref can be converted back to string representation
let display_string = image_ref.to_string();
assert!(
!display_string.is_empty(),
"Display string should not be empty for: {input}"
);
// For Docker Hub images, the display should omit the registry
if expected_registry == "docker.io" {
assert!(
!display_string.starts_with("docker.io"),
"Docker Hub images should not show registry in display: {input}"
);
}
}
}
#[tokio::test]
async fn test_compose_file_parsing_edge_cases() {
let config = create_test_config();
let updater = ComposeUpdater::new(config);
// Test various compose file formats and edge cases
let test_cases = vec![
// Standard compose file
(
"standard_compose",
r#"
version: '3.8'
services:
web:
image: nginx:1.21.0
ports:
- "80:80"
db:
image: postgres:13.7
"#,
2, // Expected number of services
),
// Compose file with comments and whitespace
(
"with_comments",
r#"
# This is a comment
version: '3.8'
services:
# Web service comment
web:
image: nginx:1.21.0 # Inline comment
ports:
- "80:80"
# Database service
db:
image: postgres:13.7
# More comments
environment:
POSTGRES_DB: test
"#,
2,
),
// Compose file with complex image references
(
"complex_images",
r#"
version: '3.8'
services:
frontend:
image: ghcr.io/company/frontend:v2.1.0
backend:
image: my-registry.com:5000/team/backend:latest
cache:
image: redis:7.0.0-alpine3.16
database:
image: postgres:14.5-bullseye
"#,
4,
),
// Minimal compose file
(
"minimal",
r#"
version: '3'
services:
app:
image: hello-world
"#,
1,
),
// Compose file with extends and other features
(
"with_extends",
r#"
version: '3.8'
services:
base:
image: ubuntu:20.04
environment:
- ENV=production
app:
extends:
service: base
image: myapp:latest
ports:
- "3000:3000"
"#,
2,
),
];
for (test_name, compose_content, expected_services) in test_cases {
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(compose_content.as_bytes()).unwrap();
let result = updater.parse_compose_file(temp_file.path().to_str().unwrap());
assert!(result.is_ok(), "Failed to parse compose file: {test_name}");
let compose_file = result.unwrap();
assert_eq!(
compose_file.services.len(),
expected_services,
"Service count mismatch for: {test_name}"
);
// Verify that all services have valid image references
for service in &compose_file.services {
assert!(
!service.service_name.is_empty(),
"Service name should not be empty in: {test_name}"
);
assert!(
!service.image_ref.name.is_empty(),
"Image name should not be empty in: {test_name}"
);
assert!(
!service.image_ref.tag.is_empty(),
"Image tag should not be empty in: {test_name}"
);
assert!(
!service.image_ref.registry.is_empty(),
"Image registry should not be empty in: {test_name}"
);
}
}
}
#[tokio::test]
async fn test_compose_content_replacement() {
let config = create_test_config();
let updater = ComposeUpdater::new(config);
let original_content = r#"
version: '3.8'
services:
web:
image: nginx:1.20.0 # Current version
ports:
- "80:80"
db:
image: postgres:13.6
"#;
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(original_content.as_bytes()).unwrap();
let compose_file = updater
.parse_compose_file(temp_file.path().to_str().unwrap())
.unwrap();
// Test replacing nginx image
let nginx_service = compose_file
.services
.iter()
.find(|s| s.service_name == "web")
.unwrap();
let result =
updater.replace_image_in_content(&compose_file.content, nginx_service, "nginx:1.21.0");
assert!(result.is_ok(), "Should successfully replace image");
let new_content = result.unwrap();
assert!(
new_content.contains("nginx:1.21.0"),
"Should contain new image version"
);
assert!(
!new_content.contains("nginx:1.20.0"),
"Should not contain old image version"
);
assert!(
new_content.contains("# Current version"),
"Should preserve comments"
);
assert!(
new_content.contains("postgres:13.6"),
"Should preserve other services unchanged"
);
}
#[test]
fn test_compose_service_image_replacement_edge_cases() {
let config = create_test_config();
let updater = ComposeUpdater::new(config);
// Test cases with tricky image replacement scenarios
let test_cases = vec![
// Multiple occurrences of similar image names
(
r#"
version: '3.8'
services:
nginx-proxy:
image: nginx:1.20.0
nginx-cache:
image: nginx:1.21.0
web:
image: nginx:1.20.0
"#,
"nginx-proxy",
"nginx:1.20.0",
"nginx:1.22.0",
),
// Images with similar names but different registries
(
r#"
version: '3.8'
services:
public-nginx:
image: nginx:1.20.0
private-nginx:
image: my-registry.com/nginx:1.20.0
"#,
"public-nginx",
"nginx:1.20.0",
"nginx:1.21.0",
),
// Complex image with registry and namespace
(
r#"
version: '3.8'
services:
app:
image: ghcr.io/company/app:v1.0.0
environment:
- IMAGE_VERSION=v1.0.0
"#,
"app",
"ghcr.io/company/app:v1.0.0",
"ghcr.io/company/app:v1.1.0",
),
];
for (compose_content, service_name, old_image, new_image) in test_cases {
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(compose_content.as_bytes()).unwrap();
let compose_file = updater
.parse_compose_file(temp_file.path().to_str().unwrap())
.unwrap();
let target_service = compose_file
.services
.iter()
.find(|s| s.service_name == service_name)
.unwrap();
let result =
updater.replace_image_in_content(&compose_file.content, target_service, new_image);
assert!(
result.is_ok(),
"Should replace image for service: {service_name}"
);
let new_content = result.unwrap();
assert!(
new_content.contains(new_image),
"Should contain new image: {new_image}"
);
// Count occurrences to ensure only the right image was replaced
let old_count = compose_content.matches(old_image).count();
let new_old_count = new_content.matches(old_image).count();
assert_eq!(
new_old_count,
old_count - 1,
"Should replace exactly one occurrence for: {service_name}"
);
}
}
#[test]
fn test_image_ignore_patterns_comprehensive() {
// Test comprehensive ignore pattern scenarios
let test_cases = vec![
// Basic pattern matching
(vec!["localhost"], "localhost:5000/app:latest", true),
(vec!["localhost"], "nginx:latest", false),
// Multiple patterns
(
vec!["localhost", "127.0.0.1"],
"127.0.0.1:5000/app:latest",
true,
),
(
vec!["localhost", "127.0.0.1"],
"192.168.1.1:5000/app:latest",
false,
),
// Registry-based patterns
(vec!["ghcr.io"], "ghcr.io/user/app:latest", true),
(vec!["ghcr.io"], "docker.io/user/app:latest", false),
// Prefix patterns
(vec!["test-"], "test-app:latest", true),
(vec!["test-"], "app-test:latest", false),
(vec!["test-"], "testing:latest", false),
// Complex registry patterns
(
vec!["internal.company.com"],
"internal.company.com/team/app:v1.0",
true,
),
(
vec!["internal.company.com"],
"external.company.com/team/app:v1.0",
false,
),
// Partial matches
(vec!["redis"], "redis:latest", true),
(vec!["redis"], "valkey:latest", false),
(vec!["redis"], "my-redis:latest", true), // This should match as substring
];
for (ignore_patterns, image, should_be_ignored) in test_cases {
let config = Config {
ignore_images: ignore_patterns.iter().map(|s| s.to_string()).collect(),
..create_test_config()
};
let result = config.is_image_ignored(image);
assert_eq!(
result, should_be_ignored,
"Pattern {ignore_patterns:?} with image '{image}' should be ignored: {should_be_ignored}"
);
}
}
fn create_test_config() -> Config {
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: None,
},
);
Config {
compose_paths: vec![PathBuf::from("./test")],
schedule: "0 0 2 * * *".to_string(),
registries,
update_strategy: UpdateStrategy::Latest,
ignore_images: vec![],
dry_run: true,
}
}

View file

@ -0,0 +1,346 @@
use docker_compose_updater::config::{Config, RegistryConfig, UpdateStrategy};
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use tempfile::NamedTempFile;
#[test]
fn test_config_default_values() {
let config = Config::default();
// Verify all default values are reasonable
assert_eq!(config.schedule, "0 0 2 * * *");
assert_eq!(
config.update_strategy,
UpdateStrategy::LatestPatchOfPreviousMinor
);
assert!(!config.dry_run);
assert!(config.ignore_images.is_empty());
// compose_paths should be a valid Vec (length check is always true for usize)
let _paths_count = config.compose_paths.len();
// Should have default registries configured
assert!(config.registries.contains_key("docker.io"));
assert!(config.registries.contains_key("ghcr.io"));
let docker_registry = &config.registries["docker.io"];
assert_eq!(docker_registry.url, "https://registry-1.docker.io");
assert!(docker_registry.auth_token.is_none());
let ghcr_registry = &config.registries["ghcr.io"];
assert_eq!(ghcr_registry.url, "https://ghcr.io");
assert!(ghcr_registry.auth_token.is_none());
}
#[test]
fn test_config_from_different_file_formats() {
// Test loading from a properly formatted YAML file
let valid_yaml = r#"
compose_paths:
- "./docker-compose.yml"
- "./services/"
schedule: "0 0 3 * * *"
update_strategy: "Latest"
dry_run: true
ignore_images:
- "localhost"
- "127.0.0.1"
registries:
docker.io:
url: "https://registry-1.docker.io"
custom.registry.com:
url: "https://custom.registry.com"
auth_token: "secret-token"
"#;
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(valid_yaml.as_bytes()).unwrap();
temp_file.flush().unwrap();
let result = Config::load(temp_file.path().to_path_buf());
assert!(result.is_ok(), "Should load valid YAML config");
let config = result.unwrap();
assert_eq!(config.compose_paths.len(), 2);
assert_eq!(config.schedule, "0 0 3 * * *");
assert_eq!(config.update_strategy, UpdateStrategy::Latest);
assert!(config.dry_run);
assert_eq!(config.ignore_images.len(), 2);
assert!(config.registries.contains_key("custom.registry.com"));
let custom_registry = &config.registries["custom.registry.com"];
assert_eq!(custom_registry.url, "https://custom.registry.com");
assert_eq!(custom_registry.auth_token, Some("secret-token".to_string()));
}
#[test]
fn test_config_with_invalid_yaml_syntax() {
let invalid_yamls = [
// Invalid YAML syntax
r#"
compose_paths:
- "./docker-compose.yml"
invalid_indent:
schedule: "0 0 2 * * *"
"#,
// Invalid field types
r#"
compose_paths: "not an array"
schedule: 123
dry_run: "not a boolean"
"#,
// Completely invalid YAML
r#"
{ invalid yaml syntax [
"#,
];
for (i, invalid_yaml) in invalid_yamls.iter().enumerate() {
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(invalid_yaml.as_bytes()).unwrap();
temp_file.flush().unwrap();
let result = Config::load(temp_file.path().to_path_buf());
// Should either fail gracefully or fall back to defaults
if result.is_ok() {
let config = result.unwrap();
// If it succeeds, should have reasonable defaults
assert!(
!config.schedule.is_empty(),
"Schedule should not be empty for test case {i}"
);
}
// If it fails, that's also acceptable behavior for invalid YAML
}
}
#[test]
fn test_config_update_strategy_parsing() {
let strategies = vec![
("Latest", UpdateStrategy::Latest),
(
"LatestPatchOfPreviousMinor",
UpdateStrategy::LatestPatchOfPreviousMinor,
),
(
"LatestPatchOfPreviousMinor",
UpdateStrategy::LatestPatchOfPreviousMinor,
),
];
for (strategy_str, expected_strategy) in strategies {
let yaml_config = format!(
r#"
compose_paths: []
schedule: "0 0 2 * * *"
update_strategy: "{strategy_str}"
"#
);
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(yaml_config.as_bytes()).unwrap();
temp_file.flush().unwrap();
let result = Config::load(temp_file.path().to_path_buf());
assert!(
result.is_ok(),
"Should parse valid strategy: {strategy_str}"
);
let config = result.unwrap();
assert_eq!(config.update_strategy, expected_strategy);
}
}
#[test]
fn test_config_with_invalid_update_strategy() {
let yaml_config = r#"
compose_paths: []
schedule: "0 0 2 * * *"
update_strategy: "InvalidStrategy"
"#;
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(yaml_config.as_bytes()).unwrap();
temp_file.flush().unwrap();
let result = Config::load(temp_file.path().to_path_buf());
assert!(result.is_err(), "Should not parse invalid update strategy");
}
#[test]
fn test_image_ignore_patterns_functionality() {
// Test various ignore patterns
let test_cases = vec![
// (ignore_pattern, image_to_test, should_be_ignored)
("localhost", "localhost:5000/app:latest", true),
("localhost", "nginx:latest", false),
("127.0.0.1", "127.0.0.1:5000/app:latest", true),
("127.0.0.1", "192.168.1.1:5000/app:latest", false),
("ghcr.io", "ghcr.io/user/app:latest", true),
("ghcr.io", "docker.io/nginx:latest", false),
("test-", "test-app:latest", true),
("test-", "app-test:latest", false),
];
for (ignore_pattern, image_to_test, should_be_ignored) in test_cases {
let config = Config {
ignore_images: vec![ignore_pattern.to_string()],
..Config::default()
};
let result = config.is_image_ignored(image_to_test);
assert_eq!(
result, should_be_ignored,
"Pattern '{ignore_pattern}' with image '{image_to_test}' should be ignored: {should_be_ignored}"
);
}
}
#[test]
fn test_multiple_ignore_patterns() {
let config = Config {
ignore_images: vec![
"localhost".to_string(),
"127.0.0.1".to_string(),
"test-".to_string(),
"ghcr.io".to_string(),
],
..Config::default()
};
let test_cases = vec![
("localhost:5000/app:latest", true),
("127.0.0.1:5000/app:latest", true),
("test-app:latest", true),
("ghcr.io/user/app:latest", true),
("docker.io/nginx:latest", false),
("registry.example.com/app:latest", false),
("app-test:latest", false),
];
for (image, should_be_ignored) in test_cases {
let result = config.is_image_ignored(image);
assert_eq!(
result, should_be_ignored,
"Image '{image}' should be ignored: {should_be_ignored}"
);
}
}
#[test]
fn test_registry_configuration_validation() {
// Test registry configs with various configurations
let mut registries = HashMap::new();
// Registry with just URL
registries.insert(
"simple.registry.com".to_string(),
RegistryConfig {
url: "https://simple.registry.com".to_string(),
auth_token: None,
},
);
// Registry with auth token
registries.insert(
"auth.registry.com".to_string(),
RegistryConfig {
url: "https://auth.registry.com".to_string(),
auth_token: Some("secret-token".to_string()),
},
);
let config = Config {
registries,
..Config::default()
};
// Verify registries are properly configured
assert!(config.registries.contains_key("simple.registry.com"));
assert!(config.registries.contains_key("auth.registry.com"));
let simple_registry = &config.registries["simple.registry.com"];
assert_eq!(simple_registry.url, "https://simple.registry.com");
assert!(simple_registry.auth_token.is_none());
let auth_registry = &config.registries["auth.registry.com"];
assert_eq!(auth_registry.url, "https://auth.registry.com");
assert_eq!(auth_registry.auth_token, Some("secret-token".to_string()));
}
#[test]
fn test_config_with_empty_fields() {
let yaml_config = r#"
compose_paths: []
schedule: ""
ignore_images: []
registries: {}
"#;
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(yaml_config.as_bytes()).unwrap();
temp_file.flush().unwrap();
let result = Config::load(temp_file.path().to_path_buf());
if result.is_ok() {
let config = result.unwrap();
assert!(config.compose_paths.is_empty());
assert!(config.ignore_images.is_empty());
// Empty schedule should either be rejected or have a default
assert!(!config.schedule.is_empty() || config.schedule.is_empty()); // Either is acceptable
}
// If loading fails for empty schedule, that's also acceptable
}
#[test]
fn test_config_compose_paths_handling() {
// Test various compose path configurations
let test_cases = vec![
// Single file path
vec!["./docker-compose.yml"],
// Multiple file paths
vec!["./docker-compose.yml", "./docker-compose.override.yml"],
// Directory paths
vec!["./services/", "./environments/"],
// Mixed paths
vec!["./docker-compose.yml", "./services/", "./override.yml"],
// Relative and absolute-looking paths
vec![
"docker-compose.yml",
"/app/config/compose.yml",
"../compose.yml",
],
];
for paths in test_cases {
let yaml_config = format!(
r#"
compose_paths:
{}
schedule: "0 0 2 * * *"
"#,
paths
.iter()
.map(|p| format!(" - \"{p}\""))
.collect::<Vec<_>>()
.join("\n")
);
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(yaml_config.as_bytes()).unwrap();
temp_file.flush().unwrap();
let result = Config::load(temp_file.path().to_path_buf());
assert!(result.is_ok(), "Should handle paths: {paths:?}");
let config = result.unwrap();
assert_eq!(config.compose_paths.len(), paths.len());
for (i, expected_path) in paths.iter().enumerate() {
assert_eq!(config.compose_paths[i], PathBuf::from(expected_path));
}
}
}

View file

@ -0,0 +1,279 @@
use docker_compose_updater::compose::updater::ComposeUpdater;
use docker_compose_updater::config::{Config, RegistryConfig, UpdateStrategy};
use docker_compose_updater::registry::{Client as RegistryClient, ImageRef};
use docker_compose_updater::scheduler::Scheduler;
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use tempfile::{NamedTempFile, TempDir};
#[test]
fn test_scheduler_invalid_cron_expressions() {
// Test that scheduler now properly rejects invalid cron expressions instead of falling back
let invalid_cron_configs = [
"invalid cron",
"0 0 25 * * *", // Invalid hour
"60 0 2 * * *", // Invalid minute
"", // Empty string
"0 0 2 * *", // Missing field
];
for invalid_cron in invalid_cron_configs {
let config = Config {
compose_paths: vec![PathBuf::from("./test")],
schedule: invalid_cron.to_string(),
registries: HashMap::new(),
update_strategy: UpdateStrategy::Latest,
ignore_images: vec![],
dry_run: true,
};
let result = Scheduler::new(config, None);
assert!(
result.is_err(),
"Expected error for invalid cron expression: '{invalid_cron}'"
);
}
}
#[test]
fn test_scheduler_valid_cron_expressions() {
// Test that scheduler accepts valid cron expressions
let valid_cron_configs = [
"0 0 2 * * *", // Default - 2 AM daily
"0 30 1 * * *", // 1:30 AM daily
"0 0 */6 * * *", // Every 6 hours
"0 0 2 * * MON", // Mondays at 2 AM
"0 15 14 1 * *", // 1st of month at 2:15 PM
];
for valid_cron in valid_cron_configs {
let config = Config {
compose_paths: vec![PathBuf::from("./test")],
schedule: valid_cron.to_string(),
registries: HashMap::new(),
update_strategy: UpdateStrategy::Latest,
ignore_images: vec![],
dry_run: true,
};
let result = Scheduler::new(config, None);
assert!(
result.is_ok(),
"Expected success for valid cron expression: '{valid_cron}'"
);
}
}
#[test]
fn test_compose_invalid_file_paths() {
let config = create_test_config();
let updater = ComposeUpdater::new(config);
// Test with completely invalid path
let result = updater.parse_compose_file("/nonexistent/path/file.yml");
assert!(result.is_err(), "Should fail for nonexistent file");
// Test with directory instead of file
let temp_dir = TempDir::new().unwrap();
let result = updater.parse_compose_file(temp_dir.path().to_str().unwrap());
assert!(result.is_err(), "Should fail when path is a directory");
}
#[test]
fn test_compose_malformed_yaml() {
let config = create_test_config();
let updater = ComposeUpdater::new(config);
let malformed_yamls = [
// Invalid YAML syntax
r#"
version: '3.8'
services:
web:
image: nginx:1.21.0
invalid_indent
ports:
- "80:80"
"#,
// Missing required fields
r#"
version: '3.8'
# Missing services section
"#,
// Completely empty file
"",
// Invalid version format
r#"
version: invalid
services:
web:
image: nginx:1.21.0
"#,
];
for (i, malformed_yaml) in malformed_yamls.iter().enumerate() {
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(malformed_yaml.as_bytes()).unwrap();
let result = updater.parse_compose_file(temp_file.path().to_str().unwrap());
// We expect these to either fail or return empty services (graceful degradation)
if result.is_ok() {
let compose_file = result.unwrap();
// If it parses successfully, it should at least not crash
assert!(
compose_file.services.is_empty() || !compose_file.services.is_empty(),
"Test case {i} should either fail or succeed gracefully"
);
}
// If it fails, that's also acceptable behavior for malformed YAML
}
}
#[test]
fn test_image_ref_parsing_edge_cases() {
// Test that image parsing doesn't crash on edge cases
let long_name = "a".repeat(300);
let edge_case_refs = [
"", // Empty string
":", // Just colon
"image:", // Missing tag
":tag", // Missing image
"registry.com/", // Missing image name
&long_name, // Extremely long name
];
for edge_case_ref in edge_case_refs {
let result = ImageRef::parse(edge_case_ref);
// The main goal is that parsing doesn't crash and returns a result
// Some might succeed (with defaults) or fail - both are acceptable
if result.is_ok() {
let _image_ref = result.unwrap();
// If it succeeds, that's fine - the parser is robust
// We don't make strict assertions about the content since
// the parser may use defaults for edge cases
}
// If they fail, that's also perfectly acceptable behavior
}
}
#[test]
fn test_registry_unknown_registry() {
let config = Config {
compose_paths: vec![],
schedule: "0 0 2 * * *".to_string(),
registries: HashMap::new(), // Empty registries
update_strategy: UpdateStrategy::Latest,
ignore_images: vec![],
dry_run: true,
};
let registry_client = RegistryClient::new(config);
// Test with an unknown registry
let image_ref = ImageRef {
registry: "unknown.registry.com".to_string(),
namespace: Some("user".to_string()),
name: "app".to_string(),
tag: "1.0.0".to_string(),
};
// This should fail because the registry is not configured
let rt = tokio::runtime::Runtime::new().unwrap();
let result = rt.block_on(registry_client.get_available_versions(&image_ref));
assert!(result.is_err(), "Should fail for unknown registry");
let error_msg = result.unwrap_err().to_string();
assert!(
error_msg.contains("Unknown registry"),
"Error should mention unknown registry, got: {error_msg}"
);
}
#[tokio::test]
async fn test_compose_update_with_empty_paths() {
let config = Config {
compose_paths: vec![], // Empty paths
..create_test_config()
};
let updater = ComposeUpdater::new(config);
// Should handle empty paths gracefully
let result = updater.update_all_compose_files().await;
assert!(result.is_ok(), "Should handle empty paths gracefully");
assert!(
result.unwrap().is_empty(),
"Should return empty list for empty paths"
);
}
#[test]
fn test_config_edge_cases() {
// Test config with minimal valid data
let minimal_config = Config {
compose_paths: vec![], // Empty paths
schedule: "0 0 2 * * *".to_string(),
registries: HashMap::new(), // No registries
update_strategy: UpdateStrategy::Latest,
ignore_images: vec![],
dry_run: true,
};
// Should be able to create scheduler with minimal config
let scheduler_result = Scheduler::new(minimal_config.clone(), None);
assert!(
scheduler_result.is_ok(),
"Should accept minimal valid config"
);
// Should be able to create compose updater with minimal config
let _updater = ComposeUpdater::new(minimal_config.clone());
// This should not panic
assert!(!minimal_config.is_image_ignored("any-image:tag"));
}
#[test]
fn test_version_selection_with_mismatched_prefixes_suffixes() {
use docker_compose_updater::strategy::create_selector;
use docker_compose_updater::version::VersionInfo;
let config = create_test_config();
let selector = create_selector(&config.update_strategy);
let current_prefix = Some("v".to_string());
let current_suffix = Some("-alpine".to_string());
let available = [
VersionInfo::from_tag("1.3.0").unwrap(),
VersionInfo::from_tag("v1.3.0-ubuntu").unwrap(),
VersionInfo::from_tag("release-1.1.5-alpine").unwrap(),
];
let target = selector.select_target_version(&available, current_prefix, current_suffix);
assert!(
target.is_none(),
"Should not find target when prefix/suffix don't match"
);
}
fn create_test_config() -> Config {
let mut registries = HashMap::new();
registries.insert(
"docker.io".to_string(),
RegistryConfig {
url: "https://registry-1.docker.io".to_string(),
auth_token: None,
},
);
Config {
compose_paths: vec![PathBuf::from("./test")],
schedule: "0 0 2 * * *".to_string(),
registries,
update_strategy: UpdateStrategy::Latest,
ignore_images: vec![],
dry_run: true,
}
}

View file

@ -0,0 +1,44 @@
use docker_compose_updater::compose::updater::ComposeUpdater;
use docker_compose_updater::config::{Config, UpdateStrategy};
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use tempfile::NamedTempFile;
#[tokio::test]
async fn test_compose_update_with_suffix_preservation() {
let compose_content = r#"
version: '3.8'
services:
web:
image: nginx:1.21.0-alpine
ports:
- "80:80"
"#;
let mut temp_file = NamedTempFile::new().unwrap();
temp_file.write_all(compose_content.as_bytes()).unwrap();
let config = Config {
compose_paths: vec![PathBuf::from(temp_file.path().to_str().unwrap())],
schedule: "0 0 2 * * *".to_string(),
registries: HashMap::new(),
update_strategy: UpdateStrategy::Latest,
ignore_images: vec![],
dry_run: true,
};
let updater = ComposeUpdater::new(config);
let result = updater
.parse_compose_file(temp_file.path().to_str().unwrap())
.unwrap();
assert_eq!(result.services[0].image_ref.tag, "1.21.0-alpine");
let new_content = updater
.replace_image_in_content(&result.content, &result.services[0], "nginx:1.22.0-alpine")
.unwrap();
assert!(new_content.contains("nginx:1.22.0-alpine"));
assert!(!new_content.contains("nginx:1.21.0-alpine"));
}