Skip to content

Commit

Permalink
Tests update
Browse files Browse the repository at this point in the history
  • Loading branch information
xDaile committed Feb 5, 2024
1 parent 2cc8c5f commit 99cb767
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 83 deletions.
9 changes: 5 additions & 4 deletions iib/common/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class AddPydanticModel(PydanticRequestBaseModel):
AfterValidator(length_validator),
AfterValidator(binary_image_check),
] = None
build_tags: Optional[List[str]] = []
build_tags: Optional[List[str]] = None
bundles: Annotated[
List[str],
AfterValidator(length_validator),
Expand Down Expand Up @@ -156,7 +156,7 @@ class RmPydanticModel(PydanticRequestBaseModel):
Optional[str],
AfterValidator(binary_image_check),
] = None
build_tags: Optional[List[str]] = []
build_tags: Optional[List[str]] = None
distribution_scope: Annotated[
Optional[DISTRIBUTION_SCOPE_LITERAL],
BeforeValidator(distribution_scope_lower),
Expand Down Expand Up @@ -256,7 +256,7 @@ class MergeIndexImagePydanticModel(PydanticRequestBaseModel):
AfterValidator(image_format_check),
AfterValidator(binary_image_check),
] = None
build_tags: Optional[List[str]] = []
build_tags: Optional[List[str]] = None
deprecation_list: Annotated[
Optional[List[str]],
AfterValidator(get_unique_deprecation_list_items),
Expand All @@ -267,6 +267,7 @@ class MergeIndexImagePydanticModel(PydanticRequestBaseModel):
BeforeValidator(distribution_scope_lower),
] = None
graph_update_mode: Optional[GRAPH_MODE_LITERAL] = None
ignore_bundle_ocp_version: Optional[bool] = False
overwrite_target_index: Optional[bool] = False
overwrite_target_index_token: Optional[SecretStr] = None
source_from_index: Annotated[str, AfterValidator(image_format_check)]
Expand Down Expand Up @@ -373,7 +374,7 @@ class FbcOperationsPydanticModel(PydanticRequestBaseModel):
AfterValidator(get_unique_bundles),
AfterValidator(images_format_check),
] = None
build_tags: Optional[List[str]] = []
build_tags: Optional[List[str]] = None
distribution_scope: Annotated[
Optional[DISTRIBUTION_SCOPE_LITERAL],
BeforeValidator(distribution_scope_lower),
Expand Down
4 changes: 2 additions & 2 deletions iib/common/pydantic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def from_index_add_arches(from_index: Optional[str], add_arches: Optional[List[s
# RequestIndexImageMixin
def binary_image_check(binary_image: str) -> str:
"""Validate binary_image is correctly provided."""
if not binary_image and not current_app.config['IIB_BINARY_IMAGE_CONFIG']:
if not binary_image:# and not current_app.config['IIB_BINARY_IMAGE_CONFIG']:
raise ValidationError('The "binary_image" value must be a non-empty string')
return binary_image

Expand Down Expand Up @@ -118,7 +118,7 @@ def distribution_scope_lower(distribution_scope: str) -> str:

def length_validator(model_property: Any) -> Any:
"""Validate length of the given model property."""
if len(model_property) == 0:
if model_property is not None and len(model_property) == 0:
raise ValidationError(
f"The {type(model_property)} {model_property} should have at least 1 item."
)
Expand Down
6 changes: 3 additions & 3 deletions iib/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,9 @@ def from_json_replacement(
request_kwargs['batch'] = batch

request = cls(**request_kwargs)

for bt in payload.build_tags:
request.add_build_tag(bt)
if payload.model_fields.get("build_tags") and payload.build_tags:
for bt in payload.build_tags:
request.add_build_tag(bt)

request.add_state('in_progress', 'The request was initiated')
return request
Expand Down
153 changes: 88 additions & 65 deletions tests/test_workers/test_tasks/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from iib.workers.tasks import build
from iib.workers.tasks.utils import RequestConfigAddRm
from iib.workers.config import get_worker_config
from iib.common.pydantic_models import AddPydanticModel, RmPydanticModel
from operator_manifest.operator import ImageName

worker_config = get_worker_config()
Expand Down Expand Up @@ -622,7 +623,9 @@ def test_buildah_fail_max_retries(mock_run_cmd: mock.MagicMock) -> None:
@mock.patch('iib.workers.tasks.build._get_present_bundles')
@mock.patch('iib.workers.tasks.build.set_registry_token')
@mock.patch('iib.workers.tasks.build.is_image_fbc')
@mock.patch('iib.common.pydantic_models.binary_image_check')
def test_handle_add_request(
mock_binary_image_check,
mock_iifbc,
mock_srt,
mock_gpb,
Expand Down Expand Up @@ -692,24 +695,24 @@ def side_effect(*args, base_dir, **kwargs):
mock_ors.return_value = (port, my_mock)
mock_run_cmd.return_value = '{"packageName": "package1", "version": "v1.0", \
"bundlePath": "bundle1"\n}'

build.handle_add_request(
bundles,
3,
binary_image,
'from-index:latest',
['s390x'],
cnr_token,
organization,
force_backport,
False,
None,
None,
greenwave_config,
binary_image_config=binary_image_config,
add_pydantic_model = AddPydanticModel.model_construct(
bundles=bundles,
binary_image=binary_image,
from_index='from_index:latest',
cnr_token=cnr_token,
organization=organization,
force_backport=force_backport,
overwrite_from_index=False,
overwrite_from_index_token=None,
deprecation_list=deprecation_list,
build_tags=["extra_tag1", "extra_tag2"],
)
build.handle_add_request(
payload=add_pydantic_model,
request_id=3,
greenwave_config=greenwave_config,
binary_image_config=binary_image_config,
)

mock_ors.assert_called_once()
mock_run_cmd.assert_called_once()
Expand Down Expand Up @@ -778,21 +781,24 @@ def side_effect(*args, base_dir, **kwargs):
def test_handle_add_request_raises(mock_iifbc, mock_runcmd, mock_c):
mock_iifbc.return_value = True
with pytest.raises(IIBError):
build.handle_add_request(
add_pydantic_model = AddPydanticModel.model_construct(
bundles=['some-bundle:2.3-1', 'some-deprecation-bundle:1.1-1'],
request_id=3,
binary_image='binary-image:latest',
from_index='from-index:latest',
add_arches=['s390x'],
from_index='from_index:latest',
cnr_token='token',
organization='org',
force_backport=True,
overwrite_from_index=False,
overwrite_from_index_token=None,
distribution_scope=None,
deprecation_list=[],
)
build.handle_add_request(
payload=add_pydantic_model,
request_id=3,
greenwave_config={'some_key': 'other_value'},
binary_image_config={'prod': {'v4.5': 'some_image'}},
deprecation_list=[],
)


Expand Down Expand Up @@ -897,21 +903,24 @@ def deprecate_bundles_mock(*args, **kwargs):
]
mock_sqlite.execute.return_value = 200

add_pydantic_model = AddPydanticModel.model_construct(
bundles=bundles,
binary_image='binary-image:latest',
add_arches=['s390x'],
from_index='from_index:latest',
cnr_token=cnr_token,
organization=organization,
force_backport=True,
overwrite_from_index=False,
overwrite_from_index_token=None,
distribution_scope=None,
deprecation_list=deprecation_list,
)
build.handle_add_request(
bundles,
3,
'binary-image:latest',
'from-index:latest',
['s390x'],
cnr_token,
organization,
True,
False,
None,
None,
greenwave_config,
payload=add_pydantic_model,
request_id=3,
greenwave_config=greenwave_config,
binary_image_config=binary_image_config,
deprecation_list=deprecation_list,
)

mock_ors.assert_called_once()
Expand Down Expand Up @@ -983,19 +992,21 @@ def test_handle_add_request_gating_failure(
organization = 'org'
greenwave_config = {'some_key': 'other_value'}
with pytest.raises(IIBError, match=error_msg):
add_pydantic_model = AddPydanticModel.model_construct(
bundles=bundles,
binary_image='binary-image:latest',
add_arches=['s390x'],
from_index='from_index:latest',
cnr_token=cnr_token,
organization=organization,
overwrite_from_index=False,
overwrite_from_index_token=None,
distribution_scope=None,
)
build.handle_add_request(
bundles,
'binary-image:latest',
3,
'from-index:latest',
['s390x'],
cnr_token,
organization,
None,
False,
None,
None,
greenwave_config,
payload=add_pydantic_model,
request_id=3,
greenwave_config=greenwave_config,
)
assert mock_cleanup.call_count == 1
mock_srs2.assert_called_once()
Expand All @@ -1014,17 +1025,20 @@ def test_handle_add_request_bundle_resolution_failure(mock_grb, mock_srs, mock_c
organization = 'org'
greenwave_config = {'some_key': 'other_value'}
with pytest.raises(IIBError, match=error_msg):
add_pydantic_model = AddPydanticModel.model_construct(
bundles=bundles,
binary_image='binary-image:latest',
add_arches=['s390x'],
from_index='from_index:latest',
cnr_token=cnr_token,
organization=organization,
force_backport=False,
overwrite_from_index=False,
overwrite_from_index_token=None,
)
build.handle_add_request(
bundles,
'binary-image:latest',
3,
'from-index:latest',
['s390x'],
cnr_token,
organization,
False,
False,
None,
payload=add_pydantic_model,
request_id=3,
greenwave_config=greenwave_config,
)
assert mock_cleanup.call_count == 1
Expand Down Expand Up @@ -1073,11 +1087,14 @@ def test_handle_rm_request(
'distribution_scope': 'PROD',
}
binary_image_config = {'prod': {'v4.6': 'some_image'}}
rm_pydantic_model = RmPydanticModel.model_construct(
operators=['some_operator'],
from_index='from-index:latest',
binary_image=binary_image,
)
build.handle_rm_request(
['some-operator'],
3,
'from-index:latest',
binary_image,
payload=rm_pydantic_model,
request_id=3,
binary_image_config=binary_image_config,
)

Expand Down Expand Up @@ -1162,11 +1179,14 @@ def test_handle_rm_request_fbc(
mock_om.return_value = "/tmp/xyz/catalog"
mock_orrf.return_value = "/tmp/fbc_dir", "/tmp/cache_dir"
mock_gcd.return_value = "/some/path"
build.handle_rm_request(
operators=['some-operator'],
request_id=5,
rm_pydantic_model = RmPydanticModel.model_construct(
operators=['some_operator'],
from_index='from-index:latest',
binary_image='binary-image:latest',
)
build.handle_rm_request(
payload=rm_pydantic_model,
request_id=5,
binary_image_config={'prod': {'v4.6': 'some_image'}},
)
mock_prfb.assert_called_once_with(
Expand Down Expand Up @@ -1446,9 +1466,8 @@ def test_handle_add_request_check_related_images_fail(
mock_grb.return_value = ['some-bundle@sha256:123']
mock_iri.side_effect = IIBError(error_msg)
with pytest.raises(IIBError, match=re.escape(error_msg)):
build.handle_add_request(
add_pydantic_model = AddPydanticModel.model_construct(
bundles=bundles,
request_id=3,
binary_image='binary-image:latest',
from_index='from-index:latest',
add_arches=['s390x'],
Expand All @@ -1458,13 +1477,17 @@ def test_handle_add_request_check_related_images_fail(
overwrite_from_index=False,
overwrite_from_index_token=None,
distribution_scope=None,
greenwave_config=None,
binary_image_config={'prod': {'v4.5': 'some_image'}},
deprecation_list=[],
build_tags=None,
graph_update_mode=None,
check_related_images=True,
)
build.handle_add_request(
payload=add_pydantic_model,
request_id=3,
greenwave_config=None,
binary_image_config={'prod': {'v4.5': 'some_image'}},
)
assert mock_cleanup.call_count == 1
mock_srs.assert_called_once()
mock_grb.assert_called_once_with(bundles)
Expand Down
23 changes: 17 additions & 6 deletions tests/test_workers/test_tasks/test_build_create_empty_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from iib.exceptions import IIBError
from iib.workers.tasks import build_create_empty_index
from iib.workers.tasks.utils import RequestConfigCreateIndexImage
from iib.common.pydantic_models import CreateEmptyIndexPydanticModel


@mock.patch('iib.workers.tasks.build_create_empty_index.grpcurl_get_db_data')
Expand Down Expand Up @@ -81,12 +82,15 @@ def test_handle_create_empty_index_request(
output_pull_spec = 'quay.io/namespace/some-image:3'
mock_capml.return_value = output_pull_spec

build_create_empty_index.handle_create_empty_index_request(
create_empty_index_pydantic_model = CreateEmptyIndexPydanticModel.model_construct(
from_index=from_index,
request_id=3,
output_fbc=False,
binary_image=binary_image,
labels=labels,
)
build_create_empty_index.handle_create_empty_index_request(
payload=create_empty_index_pydantic_model,
request_id=3,
binary_image_config=binary_image_config,
)

Expand Down Expand Up @@ -141,12 +145,16 @@ def test_handle_create_empty_index_request_raises(mock_prfb, mock_iifbc, mock_c)
IIBError, match=('Cannot create SQLite index image from File-Based Catalog index image')
):
mock_iifbc.return_value = True
build_create_empty_index.handle_create_empty_index_request(

create_empty_index_pydantic_model = CreateEmptyIndexPydanticModel.model_construct(
from_index=from_index,
request_id=3,
output_fbc=False,
binary_image=binary_image,
labels={"version": "v4.5"},
)
build_create_empty_index.handle_create_empty_index_request(
payload=create_empty_index_pydantic_model,
request_id=3,
binary_image_config={'prod': {'v4.5': 'some_image'}},
)

Expand Down Expand Up @@ -194,12 +202,15 @@ def test_handle_create_empty_index_request_fbc(
output_pull_spec = 'quay.io/namespace/some-image:3'
mock_capml.return_value = output_pull_spec

build_create_empty_index.handle_create_empty_index_request(
create_empty_index_pydantic_model = CreateEmptyIndexPydanticModel.model_construct(
from_index=from_index,
request_id=3,
output_fbc=True,
binary_image=binary_image,
labels={"version": "v4.5"},
)
build_create_empty_index.handle_create_empty_index_request(
payload=create_empty_index_pydantic_model,
request_id=3,
binary_image_config={'prod': {'v4.5': 'some_image'}},
)

Expand Down
Loading

0 comments on commit 99cb767

Please sign in to comment.