Skip to content

Commit 99cb767

Browse files
committed
Tests update
1 parent 2cc8c5f commit 99cb767

File tree

6 files changed

+123
-83
lines changed

6 files changed

+123
-83
lines changed

iib/common/pydantic_models.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class AddPydanticModel(PydanticRequestBaseModel):
6262
AfterValidator(length_validator),
6363
AfterValidator(binary_image_check),
6464
] = None
65-
build_tags: Optional[List[str]] = []
65+
build_tags: Optional[List[str]] = None
6666
bundles: Annotated[
6767
List[str],
6868
AfterValidator(length_validator),
@@ -156,7 +156,7 @@ class RmPydanticModel(PydanticRequestBaseModel):
156156
Optional[str],
157157
AfterValidator(binary_image_check),
158158
] = None
159-
build_tags: Optional[List[str]] = []
159+
build_tags: Optional[List[str]] = None
160160
distribution_scope: Annotated[
161161
Optional[DISTRIBUTION_SCOPE_LITERAL],
162162
BeforeValidator(distribution_scope_lower),
@@ -256,7 +256,7 @@ class MergeIndexImagePydanticModel(PydanticRequestBaseModel):
256256
AfterValidator(image_format_check),
257257
AfterValidator(binary_image_check),
258258
] = None
259-
build_tags: Optional[List[str]] = []
259+
build_tags: Optional[List[str]] = None
260260
deprecation_list: Annotated[
261261
Optional[List[str]],
262262
AfterValidator(get_unique_deprecation_list_items),
@@ -267,6 +267,7 @@ class MergeIndexImagePydanticModel(PydanticRequestBaseModel):
267267
BeforeValidator(distribution_scope_lower),
268268
] = None
269269
graph_update_mode: Optional[GRAPH_MODE_LITERAL] = None
270+
ignore_bundle_ocp_version: Optional[bool] = False
270271
overwrite_target_index: Optional[bool] = False
271272
overwrite_target_index_token: Optional[SecretStr] = None
272273
source_from_index: Annotated[str, AfterValidator(image_format_check)]
@@ -373,7 +374,7 @@ class FbcOperationsPydanticModel(PydanticRequestBaseModel):
373374
AfterValidator(get_unique_bundles),
374375
AfterValidator(images_format_check),
375376
] = None
376-
build_tags: Optional[List[str]] = []
377+
build_tags: Optional[List[str]] = None
377378
distribution_scope: Annotated[
378379
Optional[DISTRIBUTION_SCOPE_LITERAL],
379380
BeforeValidator(distribution_scope_lower),

iib/common/pydantic_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def from_index_add_arches(from_index: Optional[str], add_arches: Optional[List[s
8282
# RequestIndexImageMixin
8383
def binary_image_check(binary_image: str) -> str:
8484
"""Validate binary_image is correctly provided."""
85-
if not binary_image and not current_app.config['IIB_BINARY_IMAGE_CONFIG']:
85+
if not binary_image:# and not current_app.config['IIB_BINARY_IMAGE_CONFIG']:
8686
raise ValidationError('The "binary_image" value must be a non-empty string')
8787
return binary_image
8888

@@ -118,7 +118,7 @@ def distribution_scope_lower(distribution_scope: str) -> str:
118118

119119
def length_validator(model_property: Any) -> Any:
120120
"""Validate length of the given model property."""
121-
if len(model_property) == 0:
121+
if model_property is not None and len(model_property) == 0:
122122
raise ValidationError(
123123
f"The {type(model_property)} {model_property} should have at least 1 item."
124124
)

iib/web/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,9 @@ def from_json_replacement(
540540
request_kwargs['batch'] = batch
541541

542542
request = cls(**request_kwargs)
543-
544-
for bt in payload.build_tags:
545-
request.add_build_tag(bt)
543+
if payload.model_fields.get("build_tags") and payload.build_tags:
544+
for bt in payload.build_tags:
545+
request.add_build_tag(bt)
546546

547547
request.add_state('in_progress', 'The request was initiated')
548548
return request

tests/test_workers/test_tasks/test_build.py

Lines changed: 88 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from iib.workers.tasks import build
1313
from iib.workers.tasks.utils import RequestConfigAddRm
1414
from iib.workers.config import get_worker_config
15+
from iib.common.pydantic_models import AddPydanticModel, RmPydanticModel
1516
from operator_manifest.operator import ImageName
1617

1718
worker_config = get_worker_config()
@@ -622,7 +623,9 @@ def test_buildah_fail_max_retries(mock_run_cmd: mock.MagicMock) -> None:
622623
@mock.patch('iib.workers.tasks.build._get_present_bundles')
623624
@mock.patch('iib.workers.tasks.build.set_registry_token')
624625
@mock.patch('iib.workers.tasks.build.is_image_fbc')
626+
@mock.patch('iib.common.pydantic_models.binary_image_check')
625627
def test_handle_add_request(
628+
mock_binary_image_check,
626629
mock_iifbc,
627630
mock_srt,
628631
mock_gpb,
@@ -692,24 +695,24 @@ def side_effect(*args, base_dir, **kwargs):
692695
mock_ors.return_value = (port, my_mock)
693696
mock_run_cmd.return_value = '{"packageName": "package1", "version": "v1.0", \
694697
"bundlePath": "bundle1"\n}'
695-
696-
build.handle_add_request(
697-
bundles,
698-
3,
699-
binary_image,
700-
'from-index:latest',
701-
['s390x'],
702-
cnr_token,
703-
organization,
704-
force_backport,
705-
False,
706-
None,
707-
None,
708-
greenwave_config,
709-
binary_image_config=binary_image_config,
698+
add_pydantic_model = AddPydanticModel.model_construct(
699+
bundles=bundles,
700+
binary_image=binary_image,
701+
from_index='from_index:latest',
702+
cnr_token=cnr_token,
703+
organization=organization,
704+
force_backport=force_backport,
705+
overwrite_from_index=False,
706+
overwrite_from_index_token=None,
710707
deprecation_list=deprecation_list,
711708
build_tags=["extra_tag1", "extra_tag2"],
712709
)
710+
build.handle_add_request(
711+
payload=add_pydantic_model,
712+
request_id=3,
713+
greenwave_config=greenwave_config,
714+
binary_image_config=binary_image_config,
715+
)
713716

714717
mock_ors.assert_called_once()
715718
mock_run_cmd.assert_called_once()
@@ -778,21 +781,24 @@ def side_effect(*args, base_dir, **kwargs):
778781
def test_handle_add_request_raises(mock_iifbc, mock_runcmd, mock_c):
779782
mock_iifbc.return_value = True
780783
with pytest.raises(IIBError):
781-
build.handle_add_request(
784+
add_pydantic_model = AddPydanticModel.model_construct(
782785
bundles=['some-bundle:2.3-1', 'some-deprecation-bundle:1.1-1'],
783-
request_id=3,
784786
binary_image='binary-image:latest',
785-
from_index='from-index:latest',
786787
add_arches=['s390x'],
788+
from_index='from_index:latest',
787789
cnr_token='token',
788790
organization='org',
789791
force_backport=True,
790792
overwrite_from_index=False,
791793
overwrite_from_index_token=None,
792794
distribution_scope=None,
795+
deprecation_list=[],
796+
)
797+
build.handle_add_request(
798+
payload=add_pydantic_model,
799+
request_id=3,
793800
greenwave_config={'some_key': 'other_value'},
794801
binary_image_config={'prod': {'v4.5': 'some_image'}},
795-
deprecation_list=[],
796802
)
797803

798804

@@ -897,21 +903,24 @@ def deprecate_bundles_mock(*args, **kwargs):
897903
]
898904
mock_sqlite.execute.return_value = 200
899905

906+
add_pydantic_model = AddPydanticModel.model_construct(
907+
bundles=bundles,
908+
binary_image='binary-image:latest',
909+
add_arches=['s390x'],
910+
from_index='from_index:latest',
911+
cnr_token=cnr_token,
912+
organization=organization,
913+
force_backport=True,
914+
overwrite_from_index=False,
915+
overwrite_from_index_token=None,
916+
distribution_scope=None,
917+
deprecation_list=deprecation_list,
918+
)
900919
build.handle_add_request(
901-
bundles,
902-
3,
903-
'binary-image:latest',
904-
'from-index:latest',
905-
['s390x'],
906-
cnr_token,
907-
organization,
908-
True,
909-
False,
910-
None,
911-
None,
912-
greenwave_config,
920+
payload=add_pydantic_model,
921+
request_id=3,
922+
greenwave_config=greenwave_config,
913923
binary_image_config=binary_image_config,
914-
deprecation_list=deprecation_list,
915924
)
916925

917926
mock_ors.assert_called_once()
@@ -983,19 +992,21 @@ def test_handle_add_request_gating_failure(
983992
organization = 'org'
984993
greenwave_config = {'some_key': 'other_value'}
985994
with pytest.raises(IIBError, match=error_msg):
995+
add_pydantic_model = AddPydanticModel.model_construct(
996+
bundles=bundles,
997+
binary_image='binary-image:latest',
998+
add_arches=['s390x'],
999+
from_index='from_index:latest',
1000+
cnr_token=cnr_token,
1001+
organization=organization,
1002+
overwrite_from_index=False,
1003+
overwrite_from_index_token=None,
1004+
distribution_scope=None,
1005+
)
9861006
build.handle_add_request(
987-
bundles,
988-
'binary-image:latest',
989-
3,
990-
'from-index:latest',
991-
['s390x'],
992-
cnr_token,
993-
organization,
994-
None,
995-
False,
996-
None,
997-
None,
998-
greenwave_config,
1007+
payload=add_pydantic_model,
1008+
request_id=3,
1009+
greenwave_config=greenwave_config,
9991010
)
10001011
assert mock_cleanup.call_count == 1
10011012
mock_srs2.assert_called_once()
@@ -1014,17 +1025,20 @@ def test_handle_add_request_bundle_resolution_failure(mock_grb, mock_srs, mock_c
10141025
organization = 'org'
10151026
greenwave_config = {'some_key': 'other_value'}
10161027
with pytest.raises(IIBError, match=error_msg):
1028+
add_pydantic_model = AddPydanticModel.model_construct(
1029+
bundles=bundles,
1030+
binary_image='binary-image:latest',
1031+
add_arches=['s390x'],
1032+
from_index='from_index:latest',
1033+
cnr_token=cnr_token,
1034+
organization=organization,
1035+
force_backport=False,
1036+
overwrite_from_index=False,
1037+
overwrite_from_index_token=None,
1038+
)
10171039
build.handle_add_request(
1018-
bundles,
1019-
'binary-image:latest',
1020-
3,
1021-
'from-index:latest',
1022-
['s390x'],
1023-
cnr_token,
1024-
organization,
1025-
False,
1026-
False,
1027-
None,
1040+
payload=add_pydantic_model,
1041+
request_id=3,
10281042
greenwave_config=greenwave_config,
10291043
)
10301044
assert mock_cleanup.call_count == 1
@@ -1073,11 +1087,14 @@ def test_handle_rm_request(
10731087
'distribution_scope': 'PROD',
10741088
}
10751089
binary_image_config = {'prod': {'v4.6': 'some_image'}}
1090+
rm_pydantic_model = RmPydanticModel.model_construct(
1091+
operators=['some_operator'],
1092+
from_index='from-index:latest',
1093+
binary_image=binary_image,
1094+
)
10761095
build.handle_rm_request(
1077-
['some-operator'],
1078-
3,
1079-
'from-index:latest',
1080-
binary_image,
1096+
payload=rm_pydantic_model,
1097+
request_id=3,
10811098
binary_image_config=binary_image_config,
10821099
)
10831100

@@ -1162,11 +1179,14 @@ def test_handle_rm_request_fbc(
11621179
mock_om.return_value = "/tmp/xyz/catalog"
11631180
mock_orrf.return_value = "/tmp/fbc_dir", "/tmp/cache_dir"
11641181
mock_gcd.return_value = "/some/path"
1165-
build.handle_rm_request(
1166-
operators=['some-operator'],
1167-
request_id=5,
1182+
rm_pydantic_model = RmPydanticModel.model_construct(
1183+
operators=['some_operator'],
11681184
from_index='from-index:latest',
11691185
binary_image='binary-image:latest',
1186+
)
1187+
build.handle_rm_request(
1188+
payload=rm_pydantic_model,
1189+
request_id=5,
11701190
binary_image_config={'prod': {'v4.6': 'some_image'}},
11711191
)
11721192
mock_prfb.assert_called_once_with(
@@ -1446,9 +1466,8 @@ def test_handle_add_request_check_related_images_fail(
14461466
mock_grb.return_value = ['some-bundle@sha256:123']
14471467
mock_iri.side_effect = IIBError(error_msg)
14481468
with pytest.raises(IIBError, match=re.escape(error_msg)):
1449-
build.handle_add_request(
1469+
add_pydantic_model = AddPydanticModel.model_construct(
14501470
bundles=bundles,
1451-
request_id=3,
14521471
binary_image='binary-image:latest',
14531472
from_index='from-index:latest',
14541473
add_arches=['s390x'],
@@ -1458,13 +1477,17 @@ def test_handle_add_request_check_related_images_fail(
14581477
overwrite_from_index=False,
14591478
overwrite_from_index_token=None,
14601479
distribution_scope=None,
1461-
greenwave_config=None,
1462-
binary_image_config={'prod': {'v4.5': 'some_image'}},
14631480
deprecation_list=[],
14641481
build_tags=None,
14651482
graph_update_mode=None,
14661483
check_related_images=True,
14671484
)
1485+
build.handle_add_request(
1486+
payload=add_pydantic_model,
1487+
request_id=3,
1488+
greenwave_config=None,
1489+
binary_image_config={'prod': {'v4.5': 'some_image'}},
1490+
)
14681491
assert mock_cleanup.call_count == 1
14691492
mock_srs.assert_called_once()
14701493
mock_grb.assert_called_once_with(bundles)

tests/test_workers/test_tasks/test_build_create_empty_index.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from iib.exceptions import IIBError
77
from iib.workers.tasks import build_create_empty_index
88
from iib.workers.tasks.utils import RequestConfigCreateIndexImage
9+
from iib.common.pydantic_models import CreateEmptyIndexPydanticModel
910

1011

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

84-
build_create_empty_index.handle_create_empty_index_request(
85+
create_empty_index_pydantic_model = CreateEmptyIndexPydanticModel.model_construct(
8586
from_index=from_index,
86-
request_id=3,
8787
output_fbc=False,
8888
binary_image=binary_image,
8989
labels=labels,
90+
)
91+
build_create_empty_index.handle_create_empty_index_request(
92+
payload=create_empty_index_pydantic_model,
93+
request_id=3,
9094
binary_image_config=binary_image_config,
9195
)
9296

@@ -141,12 +145,16 @@ def test_handle_create_empty_index_request_raises(mock_prfb, mock_iifbc, mock_c)
141145
IIBError, match=('Cannot create SQLite index image from File-Based Catalog index image')
142146
):
143147
mock_iifbc.return_value = True
144-
build_create_empty_index.handle_create_empty_index_request(
148+
149+
create_empty_index_pydantic_model = CreateEmptyIndexPydanticModel.model_construct(
145150
from_index=from_index,
146-
request_id=3,
147151
output_fbc=False,
148152
binary_image=binary_image,
149153
labels={"version": "v4.5"},
154+
)
155+
build_create_empty_index.handle_create_empty_index_request(
156+
payload=create_empty_index_pydantic_model,
157+
request_id=3,
150158
binary_image_config={'prod': {'v4.5': 'some_image'}},
151159
)
152160

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

197-
build_create_empty_index.handle_create_empty_index_request(
205+
create_empty_index_pydantic_model = CreateEmptyIndexPydanticModel.model_construct(
198206
from_index=from_index,
199-
request_id=3,
200207
output_fbc=True,
201208
binary_image=binary_image,
202209
labels={"version": "v4.5"},
210+
)
211+
build_create_empty_index.handle_create_empty_index_request(
212+
payload=create_empty_index_pydantic_model,
213+
request_id=3,
203214
binary_image_config={'prod': {'v4.5': 'some_image'}},
204215
)
205216

0 commit comments

Comments
 (0)