From 3926815796f317d0dba8e9d6759943248f3dfcea Mon Sep 17 00:00:00 2001 From: WhySoBad <49595640+WhySoBad@users.noreply.github.com> Date: Sun, 18 Feb 2024 16:45:08 +0100 Subject: [PATCH] updated docs, wrote tests for rule --- README.md | 3 ++- docs/policies.md | 11 +++++++++-- src/rule.rs | 48 ++++++++++++++++++++++++++++++++++-------------- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index fd0d1a5..f450a5c 100644 --- a/README.md +++ b/README.md @@ -67,10 +67,11 @@ to open a new issue or contribute to an open issue! - [x] Publish a new image to ghcr when a new tag is created - [x] Add static configuration file which takes priority over label configuration. The file should have hot reload - [x] Add docs -- [ ] Add configuration whether the garbage collector should be run inside the container +- [x] Add configuration whether the garbage collector should be run inside the container - [x] Add tests to rule parsing and rule affections - [ ] Add ping to registry container in instance creation - [x] Add policy to match tags by pattern +- [ ] Add policy to match tags by size ## Credits diff --git a/docs/policies.md b/docs/policies.md index 883b1ff..3c82346 100644 --- a/docs/policies.md +++ b/docs/policies.md @@ -27,6 +27,10 @@ five of tags which were previously marked for deletion since they don't fulfil t In a sense the `Requirement` policies are stronger than the `Target` policies. +> [!IMPORTANT] +> A rule with only `Requirement` policies without any `Target` policies doesn't match anything since the `Requirement` policies are only used to filter the matches of the +> `Target` policies and not for matching itself + ## Tag policies Tag policies are used to determine which tags on an image should be marked for deletion @@ -38,10 +42,13 @@ Tag policies are used to determine which tags on an image should be marked for d > > Default: `15` -The revision policy aims to only keep a specified amount of tags for an image in the registry. In combination with other -policies it can be less than the specified amount of tags is kept. When there are more tags than specified in the policy it +The revision policy aims to only keep a specified amount of tags for an image in the registry. When there are more tags than specified in the policy it marks the excess ones for deletion. The tags are marked for deletion from oldest to newest (by creation date). +> [!IMPORTANT] +> When used with other tag policies the real revision count can be higher than the specified value since there could be the case +> where tags which would be deleted by the revision policy are filtered out by a policy of type `Requirement` which retains them from being deleted + ```yaml # Only keep the latest 15 tags of the image revisions: 15 diff --git a/src/rule.rs b/src/rule.rs index 95624ea..3000bd4 100644 --- a/src/rule.rs +++ b/src/rule.rs @@ -38,11 +38,6 @@ impl Rule{ affected.extend(affects) } - if requirements.len() == self.repository_policies.len() && !requirements.is_empty() { - // there are no target policies and therefore every repository should be affected - affected.extend(repositories) - } - let mut affected = affected.into_iter().collect::>(); for requirement in requirements { @@ -67,11 +62,6 @@ impl Rule{ affected.extend(affects) } - if requirements.len() == self.tag_policies.len() && !requirements.is_empty() { - // there are no target policies and therefore every tag should be affected - affected.extend(tags) - } - let mut affected = affected.into_iter().collect::>(); for requirement in requirements { @@ -282,12 +272,34 @@ mod test { #[test] fn test_only_target_tag_policies() { - todo!() + let labels = get_labels(vec![ + ("tag.pattern", "test-.+") + ]); + let rule = parse_rule(String::from("test-rule"), labels); + assert!(rule.is_some()); + let parsed = rule.unwrap(); + + let tags = get_tags_by_name(vec!["test-", "test-asdf", "not a match"], Duration::seconds(1), 1); + assert_eq!(parsed.affected_tags(tags.clone()), vec![tags[1].clone()]); } #[test] fn test_only_requirement_tag_policies() { - todo!() + let labels = get_labels(vec![ + ("age.min", "10m") + ]); + let rule = parse_rule(String::from("test-rule"), labels); + assert!(rule.is_some()); + let parsed = rule.unwrap(); + + let tags = get_tags(vec![ + ("test", Duration::seconds(10), 10), + ("asdf", Duration::minutes(10), 10), + ("another", Duration::hours(10), 10), + ("new", Duration::minutes(9), 10) + ]); + // requirement policies are only used for filtering, not for matching + assert_eq!(parsed.affected_tags(tags.clone()), vec![]); } #[test] @@ -307,12 +319,20 @@ mod test { #[test] fn test_only_target_repository_policies() { - todo!() + let labels = get_labels(vec![ + ("image.pattern", "test-.+") + ]); + let rule = parse_rule(String::from("test-rule"), labels); + assert!(rule.is_some()); + let parsed = rule.unwrap(); + + let repositories = get_repositories(vec!["test", "test-asdf", "not matching", "test-match"]); + assert_eq!(parsed.affected_repositories(repositories.clone()), vec![repositories[1].clone(), repositories[3].clone()]) } #[test] fn test_only_requirement_repository_policies() { - todo!() + // TODO: Implement this test case as soon as there is a requirement repository policy } #[test]