Skip to content

Commit 3926815

Browse files
committed
updated docs, wrote tests for rule
1 parent 55d6aca commit 3926815

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ to open a new issue or contribute to an open issue!
6767
- [x] Publish a new image to ghcr when a new tag is created
6868
- [x] Add static configuration file which takes priority over label configuration. The file should have hot reload
6969
- [x] Add docs
70-
- [ ] Add configuration whether the garbage collector should be run inside the container
70+
- [x] Add configuration whether the garbage collector should be run inside the container
7171
- [x] Add tests to rule parsing and rule affections
7272
- [ ] Add ping to registry container in instance creation
7373
- [x] Add policy to match tags by pattern
74+
- [ ] Add policy to match tags by size
7475

7576
## Credits
7677

docs/policies.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ five of tags which were previously marked for deletion since they don't fulfil t
2727

2828
In a sense the `Requirement` policies are stronger than the `Target` policies.
2929

30+
> [!IMPORTANT]
31+
> 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
32+
> `Target` policies and not for matching itself
33+
3034
## Tag policies
3135

3236
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
3842
>
3943
> Default: `15`
4044
41-
The revision policy aims to only keep a specified amount of tags for an image in the registry. In combination with other
42-
policies it can be less than the specified amount of tags is kept. When there are more tags than specified in the policy it
45+
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
4346
marks the excess ones for deletion. The tags are marked for deletion from oldest to newest (by creation date).
4447

48+
> [!IMPORTANT]
49+
> When used with other tag policies the real revision count can be higher than the specified value since there could be the case
50+
> 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
51+
4552
```yaml
4653
# Only keep the latest 15 tags of the image
4754
revisions: 15

src/rule.rs

+34-14
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ impl Rule{
3838
affected.extend(affects)
3939
}
4040

41-
if requirements.len() == self.repository_policies.len() && !requirements.is_empty() {
42-
// there are no target policies and therefore every repository should be affected
43-
affected.extend(repositories)
44-
}
45-
4641
let mut affected = affected.into_iter().collect::<Vec<_>>();
4742

4843
for requirement in requirements {
@@ -67,11 +62,6 @@ impl Rule{
6762
affected.extend(affects)
6863
}
6964

70-
if requirements.len() == self.tag_policies.len() && !requirements.is_empty() {
71-
// there are no target policies and therefore every tag should be affected
72-
affected.extend(tags)
73-
}
74-
7565
let mut affected = affected.into_iter().collect::<Vec<_>>();
7666

7767
for requirement in requirements {
@@ -282,12 +272,34 @@ mod test {
282272

283273
#[test]
284274
fn test_only_target_tag_policies() {
285-
todo!()
275+
let labels = get_labels(vec![
276+
("tag.pattern", "test-.+")
277+
]);
278+
let rule = parse_rule(String::from("test-rule"), labels);
279+
assert!(rule.is_some());
280+
let parsed = rule.unwrap();
281+
282+
let tags = get_tags_by_name(vec!["test-", "test-asdf", "not a match"], Duration::seconds(1), 1);
283+
assert_eq!(parsed.affected_tags(tags.clone()), vec![tags[1].clone()]);
286284
}
287285

288286
#[test]
289287
fn test_only_requirement_tag_policies() {
290-
todo!()
288+
let labels = get_labels(vec![
289+
("age.min", "10m")
290+
]);
291+
let rule = parse_rule(String::from("test-rule"), labels);
292+
assert!(rule.is_some());
293+
let parsed = rule.unwrap();
294+
295+
let tags = get_tags(vec![
296+
("test", Duration::seconds(10), 10),
297+
("asdf", Duration::minutes(10), 10),
298+
("another", Duration::hours(10), 10),
299+
("new", Duration::minutes(9), 10)
300+
]);
301+
// requirement policies are only used for filtering, not for matching
302+
assert_eq!(parsed.affected_tags(tags.clone()), vec![]);
291303
}
292304

293305
#[test]
@@ -307,12 +319,20 @@ mod test {
307319

308320
#[test]
309321
fn test_only_target_repository_policies() {
310-
todo!()
322+
let labels = get_labels(vec![
323+
("image.pattern", "test-.+")
324+
]);
325+
let rule = parse_rule(String::from("test-rule"), labels);
326+
assert!(rule.is_some());
327+
let parsed = rule.unwrap();
328+
329+
let repositories = get_repositories(vec!["test", "test-asdf", "not matching", "test-match"]);
330+
assert_eq!(parsed.affected_repositories(repositories.clone()), vec![repositories[1].clone(), repositories[3].clone()])
311331
}
312332

313333
#[test]
314334
fn test_only_requirement_repository_policies() {
315-
todo!()
335+
// TODO: Implement this test case as soon as there is a requirement repository policy
316336
}
317337

318338
#[test]

0 commit comments

Comments
 (0)