Skip to content

Commit

Permalink
updated docs, wrote tests for rule
Browse files Browse the repository at this point in the history
  • Loading branch information
WhySoBad committed Feb 18, 2024
1 parent 55d6aca commit 3926815
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions docs/policies.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
48 changes: 34 additions & 14 deletions src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();

for requirement in requirements {
Expand All @@ -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::<Vec<_>>();

for requirement in requirements {
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit 3926815

Please sign in to comment.