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

Commit

Permalink
Merge b098826 into f5b2244
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Hallisey committed Oct 8, 2019
2 parents f5b2244 + b098826 commit aa77776
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 8 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,25 @@ 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. Replacement happen during pushing manifests into
application registry.
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':
```
"replace_registry": [
{
"old": "",
"new": "example.com",
"regexp": "quay.io$",
},
]
```
Replacements occur when pushing manifests into the application registry.

### Greenwave integration

Expand Down
1 change: 1 addition & 0 deletions example.test.env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private_org:
replace_registry:
- old: registry.stage.redhat.io
new: registry.redhat.io
regex: ""
# Config of the instance tested to check if Greenwave policies are met for the
# NVR
greenwave:
Expand Down
7 changes: 6 additions & 1 deletion omps/quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,12 @@ def replace_registries(self, text):
for conf in self._replace_registry_conf:
old = conf['old']
new = conf['new']
if old in text:
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)
elif old in text:
self.logger.info("Replacing registry '%s' with '%s'", old, new)
text = text.replace(old, new)

Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _yield_yaml_files(dir_path):
old = 'quay.io'
new = 'example.com'
qo = QuayOrganization('testorg', 'random token', replace_registry_conf=[
{'old': old, 'new': new}
{'old': old, 'new': new, 'regexp': ""}
])

should_be_replaced = set()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def koji_registries(replace_conf, path):
the manifest at 'path'
Args:
replace_conf: List of {'old': ..., 'new': ...} dictionaries.
replace_conf: List of {'old': ..., 'new': ..., 'regexp': ...} dictionaries.
path: Path of the manifest.
Returns:
Expand Down
16 changes: 14 additions & 2 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'}]
replace_conf = [{'old': 'reg_old', 'new': 'reg_new', 'regexp': ''}]
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'}]
replace_conf = [{'old': 'reg_old', 'new': 'reg_new', 'regexp': ''}]
org = 'testorg'
qo = QuayOrganization(org, TOKEN, replace_registry_conf=replace_conf)
assert qo.replace_registries(text) == expected
Expand All @@ -483,6 +483,18 @@ def test_replace_registries_unconfigured(self):
assert res == text
assert id(res) == id(text)

@pytest.mark.parametrize('text,expected', [
(
'Registry reg_old will be replaced using a regexp: reg_old',
'Registry reg_old will be replaced using a regexp: reg_new',
)
])
def test_regexp_replace_registries(self, text, expected):
"""Test if registries are replaced properly"""
replace_conf = [{'old': '', 'new': 'reg_new', 'regexp': 'reg_old$'}]
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
30 changes: 30 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ def test_organizations():
{
'old': 'quay.io',
'new': 'example.com',
'regexp': '',
},
]
}
}

class ConfClass(DefaultConfig):
ORGANIZATIONS = expected

conf = Config(ConfClass)
assert conf.organizations == expected


def test_regexp():
"""Regexp Test"""
expected = {
'myorg': {
'public': False,
'oauth_token': 'token',
'replace_registry': [
{
'old': 'quay.io/',
'new': 'example.com',
'regexp': 'quay.io$',
},
]
}
Expand Down Expand Up @@ -161,6 +185,12 @@ class ConfClass(DefaultConfig):
{'old': 'expected new too'}
]
}
}, {
'organization': {
'replace_registry': [
{'regexp': 'expected regexp too'}
]
}
}
])
Expand Down

0 comments on commit aa77776

Please sign in to comment.