Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion artifactory_cleanup/rules/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ def get_version(self, artifact) -> Tuple:
match = re.match(self.custom_regexp, value)
if not match:
raise ValueError(f"Can not find version in '{artifact}'")
version_str = match.group()
version_str = '.'.join(map(str, match.groups()))
version_str = re.sub(r'\.+', '.', version_str)
if version_str.startswith("v"):
version_str = version_str[1:]
return tuple(["v"] + list(map(int, version_str.split("."))))
Expand Down
133 changes: 133 additions & 0 deletions tests/test_rules_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,136 @@ def test_filter(self):
"stats": {},
},
]
def test_filter_version_with_prefix(self):
# Skip collecting docker size
RuleForDocker._collect_docker_size = lambda self, x: x

data = [
{
"properties": {"docker.manifest": "uat-0.1.100"},
"path": "foobar/uat-0.1.100",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-0.1.200"},
"path": "foobar/uat-0.1.200",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-0.1.99"},
"path": "foobar/uat-0.1.99",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-1.1.1"},
"path": "foobar/uat-1.1.1",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-1.2.1"},
"path": "foobar/uat-1.2.1",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-1.3.1"},
"path": "foobar/uat-1.3.1",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-2.1.1"},
"path": "foobar/uat-2.1.1",
"name": "manifest.json",
},
]
artifacts = ArtifactsList.from_response(data)
policy = CleanupPolicy(
"test",
# DeleteDockerImagesOlderThan here just to test how KeepLatestNVersionImagesByProperty works together
DeleteDockerImagesOlderThan(days=1),
KeepLatestNVersionImagesByProperty(
count=2,
number_of_digits_in_version=1,
custom_regexp=r"^uat-(\d+\.\d+\.\d+$)",
),
)
assert policy.filter(artifacts) == [
{
"name": "uat-0.1.99",
"path": "foobar",
"properties": {"docker.manifest": "uat-0.1.99"},
"stats": {},
},
{
"name": "uat-1.1.1",
"path": "foobar",
"properties": {"docker.manifest": "uat-1.1.1"},
"stats": {},
},
]
def test_filter_custom_version(self):
# Skip collecting docker size
RuleForDocker._collect_docker_size = lambda self, x: x

# example data uses an overly custom version to ensure it will match any usecase
data = [
{
"properties": {"docker.manifest": "uat-0-XXX-1-100"},
"path": "foobar/uat-0-XXX-1-100",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-0-XXX-1-200"},
"path": "foobar/uat-0-XXX-1-200",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-0-XXX-1-99"},
"path": "foobar/uat-0-XXX-1-99",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-1-XXX-1-1"},
"path": "foobar/uat-1-XXX-1-1",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-1-XXX-2-1"},
"path": "foobar/uat-1-XXX-2-1",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-1-XXX-3-1"},
"path": "foobar/uat-1-XXX-3-1",
"name": "manifest.json",
},
{
"properties": {"docker.manifest": "uat-2-XXX-1-1"},
"path": "foobar/uat-2-XXX-1-1",
"name": "manifest.json",
},
]
artifacts = ArtifactsList.from_response(data)
policy = CleanupPolicy(
"test",
# DeleteDockerImagesOlderThan here just to test how KeepLatestNVersionImagesByProperty works together
DeleteDockerImagesOlderThan(days=1),
KeepLatestNVersionImagesByProperty(
count=2,
number_of_digits_in_version=1,
custom_regexp=r"^uat-(\d+)-XXX-(\d+)-(\d+)$",
),
)
assert policy.filter(artifacts) == [
{
"name": "uat-0-XXX-1-99",
"path": "foobar",
"properties": {"docker.manifest": "uat-0-XXX-1-99"},
"stats": {},
},
{
"name": "uat-1-XXX-1-1",
"path": "foobar",
"properties": {"docker.manifest": "uat-1-XXX-1-1"},
"stats": {},
},
]