Skip to content

Commit

Permalink
bundle_replacements should only be a dictionary
Browse files Browse the repository at this point in the history
If it is not configured, it should be set as an empty dictionary instead
of None.

Signed-off-by: arewm <arewm@users.noreply.github.com>
  • Loading branch information
arewm committed Oct 25, 2022
1 parent 18d3629 commit 420bb5b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,6 @@ dmypy.json
/iib_data/
/ca-bundle.crt
compose-files/docker/

# temporary files
**/.DS_Store
11 changes: 5 additions & 6 deletions iib/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ class RequestRegenerateBundle(Request):
@property
def bundle_replacements(self):
"""Return the Python representation of the JSON bundle_replacements."""
return json.loads(self._bundle_replacements) if self._bundle_replacements else None
return json.loads(self._bundle_replacements) if self._bundle_replacements else {}

@bundle_replacements.setter
def bundle_replacements(self, bundle_replacements):
Expand All @@ -1201,9 +1201,7 @@ def bundle_replacements(self, bundle_replacements):
:param dict bundle_replacements: the dictionary of the bundle_replacements or ``None``
"""
self._bundle_replacements = (
json.dumps(bundle_replacements, sort_keys=True)
if bundle_replacements is not None
else None
json.dumps(bundle_replacements, sort_keys=True) if bundle_replacements else None
)

@classmethod
Expand All @@ -1224,8 +1222,8 @@ def from_json(cls, kwargs, batch=None):
optional_params={'bundle_replacements', 'organization', 'registry_auths'},
)
# Validate bundle_replacements is correctly provided
bundle_replacements = request_kwargs.get('bundle_replacements', None)
if bundle_replacements is not None:
bundle_replacements = request_kwargs.get('bundle_replacements', {})
if bundle_replacements:
if not isinstance(bundle_replacements, dict):
raise ValidationError('The value of "bundle_replacements" must be a JSON object')

Expand Down Expand Up @@ -1281,6 +1279,7 @@ def to_json(self, verbose=True):
self.from_bundle_image_resolved, 'pull_specification', None
)
rv['organization'] = self.organization
rv['bundle_replacements'] = self.bundle_replacements
if (
current_app.config['IIB_REQUEST_RELATED_BUNDLES_DIR']
or current_app.config['IIB_AWS_S3_BUCKET_NAME']
Expand Down
2 changes: 1 addition & 1 deletion iib/workers/tasks/build_regenerate_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def handle_regenerate_bundle_request(
organization: str,
request_id: int,
registry_auths: Optional[Dict[str, Any]] = None,
bundle_replacements: Optional[Dict[str, str]] = None,
bundle_replacements: Optional[Dict[str, str]] = {},
) -> None:
"""
Coordinate the work needed to regenerate the operator bundle image.
Expand Down
7 changes: 6 additions & 1 deletion tests/test_web/test_api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ def test_patch_request_regenerate_bundle_success(
'batch': 1,
'batch_annotations': None,
'bundle_image': 'bundle:image',
'bundle_replacements': {},
'from_bundle_image': minimal_request_regenerate_bundle.from_bundle_image.pull_specification,
'from_bundle_image_resolved': 'from-bundle-image:resolved',
'id': minimal_request_regenerate_bundle.id,
Expand Down Expand Up @@ -1348,6 +1349,7 @@ def test_regenerate_bundle_success(mock_smfsc, mock_hrbr, db, auth_env, client):
'batch': 1,
'batch_annotations': None,
'bundle_image': None,
'bundle_replacements': {},
'from_bundle_image': 'registry.example.com/bundle-image:latest',
'from_bundle_image_resolved': None,
'logs': {
Expand Down Expand Up @@ -1486,7 +1488,10 @@ def test_regenerate_bundle_batch_success(
'registry_auths': {'auths': {'registry2.example.com': {'auth': 'dummy_auth'}}},
'bundle_replacements': {'foo': 'bar:baz'},
},
{'from_bundle_image': 'registry.example.com/bundle-image2:latest'},
{
'from_bundle_image': 'registry.example.com/bundle-image2:latest',
'bundle_replacements': None,
},
]
}
if annotations:
Expand Down
1 change: 1 addition & 0 deletions tests/test_web/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,4 @@ def test_request_logs_and_related_bundles_in_response(
rv = minimal_request_regenerate_bundle.to_json(verbose=True)
assert rv['logs']['url'] == 'some-url-for-data'
assert rv['related_bundles']['url'] == 'some-url-for-data'
assert rv['bundle_replacements'] == {}

0 comments on commit 420bb5b

Please sign in to comment.