Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Make regexp backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
rthallisey committed Oct 8, 2019
1 parent b098826 commit 208b6d1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,22 @@ organization:
{
"old": "quay.io",
"new": "example.com",
"regexp": "",
},
]
```
All specified `old` registries will be replaced by `new` in all manifests yaml
files for that organization.

You can pattern match registry strings with the regexp field instead of matching
whole strings with 'old':
You can pattern match and replace registry strings with the regexp field instead
of matching whole strings. Both `old` and `new` will be evalutated as regexes
when `regexp` is set to `True`. If `regexp` is missing it defaults to `False`.
Here's an example:
```
"replace_registry": [
{
"old": "",
"old": "quay.io$",
"new": "example.com",
"regexp": "quay.io$",
"regexp": "True",
},
]
```
Expand Down
14 changes: 9 additions & 5 deletions omps/quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,17 @@ def replace_registries(self, text):
return text

for conf in self._replace_registry_conf:
old = conf['old']
new = conf['new']
regexp = conf['regexp']
old = conf['old']
regexp = False

if 'regexp' in conf.keys():
regexp = conf['regexp']

if regexp is not "":
self.logger.info("Replacing registries with regexp '%s'", regexp)
text = re.sub(regexp, new, text, flags=re.MULTILINE)
if regexp:
if re.search(old, text, flags=re.MULTILINE):
self.logger.info("Replacing registries with regexp '%s'", regexp)
text = re.sub(old, new, text, flags=re.MULTILINE)
elif old in text:
self.logger.info("Replacing registry '%s' with '%s'", old, new)
text = text.replace(old, new)
Expand Down
9 changes: 5 additions & 4 deletions tests/test_quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def test_publish_repo_error(self):
def test_registry_replacing_enabled(self, enabled):
"""Test if property returns correct value"""
if enabled:
replace_conf = [{'old': 'reg_old', 'new': 'reg_new', 'regexp': ''}]
replace_conf = [{'old': 'reg_old', 'new': 'reg_new'}]
else:
replace_conf = None

Expand All @@ -468,7 +468,7 @@ def test_registry_replacing_enabled(self, enabled):
])
def test_replace_registries(self, text, expected):
"""Test if registries are replaced properly"""
replace_conf = [{'old': 'reg_old', 'new': 'reg_new', 'regexp': ''}]
replace_conf = [{'old': 'reg_old', 'new': 'reg_new'}]
org = 'testorg'
qo = QuayOrganization(org, TOKEN, replace_registry_conf=replace_conf)
assert qo.replace_registries(text) == expected
Expand All @@ -490,12 +490,13 @@ def test_replace_registries_unconfigured(self):
)
])
def test_regexp_replace_registries(self, text, expected):
"""Test if registries are replaced properly"""
replace_conf = [{'old': '', 'new': 'reg_new', 'regexp': 'reg_old$'}]
"""Test if registries are replaced properly with regexp"""
replace_conf = [{'old': 'reg_old$', 'new': 'reg_new', 'regexp': 'True'}]
org = 'testorg'
qo = QuayOrganization(org, TOKEN, replace_registry_conf=replace_conf)
assert qo.replace_registries(text) == expected


class TestOrgManager:
"""Tets for OrgManager class"""

Expand Down
11 changes: 5 additions & 6 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def test_organizations():
{
'old': 'quay.io',
'new': 'example.com',
'regexp': '',
},
]
}
Expand All @@ -131,17 +130,17 @@ class ConfClass(DefaultConfig):
assert conf.organizations == expected


def test_regexp():
"""Regexp Test"""
def test_organziation_regexp():
"""Organziation with regexp Test"""
expected = {
'myorg': {
'public': False,
'oauth_token': 'token',
'replace_registry': [
{
'old': 'quay.io/',
'old': 'quay.io/$',
'new': 'example.com',
'regexp': 'quay.io$',
'regexp': 'True',
},
]
}
Expand Down Expand Up @@ -188,7 +187,7 @@ class ConfClass(DefaultConfig):
}, {
'organization': {
'replace_registry': [
{'regexp': 'expected regexp too'}
{'regexp': 'False'}
]
}
}
Expand Down

0 comments on commit 208b6d1

Please sign in to comment.