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 517dd5e
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 152 deletions.
10 changes: 5 additions & 5 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] = None
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 @@ -303,7 +304,6 @@ def _get_all_keys_to_check_in_db(self):
"deprecation_list",
"source_from_index",
"target_index",
"target_index",
]


Expand Down Expand Up @@ -373,7 +373,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
2 changes: 1 addition & 1 deletion iib/common/pydantic_utils.py
Original file line number Diff line number Diff line change
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
149 changes: 80 additions & 69 deletions tests/test_web/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
import pytest

from iib.web.models import (
Request,
RequestAdd,
RequestMergeIndexImage,
RequestRegenerateBundle,
RequestRm,
RequestCreateEmptyIndex,
)

from iib.common.pydantic_models import (
AddPydanticModel,
RmPydanticModel,
MergeIndexImagePydanticModel,
RegenerateBundlePydanticModel,
CreateEmptyIndexPydanticModel,
)

INITIAL_DB_REVISION = '274ba38408e8'

Expand All @@ -25,19 +32,19 @@ def test_migrate_to_polymorphic_requests(app, auth_env, client, db):
for i in range(total_requests):
request_class = random.choice((RequestAdd, RequestRm))
if request_class == RequestAdd:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'bundles': [f'quay.io/namespace/bundle:{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestAdd.from_json(data)
data = AddPydanticModel(
binary_image='quay.io/namespace/binary_image:latest',
bundles=[f'quay.io/namespace/bundle:{i}'],
from_index=f'quay.io/namespace/repo:{i}',
)
request = RequestAdd.from_json_replacement(payload=data)
elif request_class == RequestRm:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'operators': [f'operator-{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestRm.from_json(data)
data = RmPydanticModel(
binary_image='quay.io/namespace/binary_image:latest',
operators=[f'operator-{i}'],
from_index=f'quay.io/namespace/repo:{i}',
)
request = RequestRm.from_json_replacement(data)

if i % 5 == 0:
# Simulate failed request
Expand All @@ -63,26 +70,26 @@ def test_migrate_to_merge_index_endpoints(app, auth_env, client, db):
for i in range(total_requests):
request_class = random.choice((RequestAdd, RequestMergeIndexImage, RequestRm))
if request_class == RequestAdd:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'bundles': [f'quay.io/namespace/bundle:{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestAdd.from_json(data)
data = AddPydanticModel(
binary_image='quay.io/namespace/binary_image:latest',
bundles=[f'quay.io/namespace/bundle:{i}'],
from_index=f'quay.io/namespace/repo:{i}',
)
request = RequestAdd.from_json_replacement(data)
elif request_class == RequestRm:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'operators': [f'operator-{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestRm.from_json(data)
data = RmPydanticModel(
binary_image='quay.io/namespace/binary_image:latest',
operators=[f'operator-{i}'],
from_index=f'quay.io/namespace/repo:{i}',
)
request = RequestRm.from_json_replacement(data)
elif request_class == RequestMergeIndexImage:
data = {
'source_from_index': f'quay.io/namespace/repo:{i}',
'target_index': f'quay.io/namespace/repo:{i}',
'binary_image': 'quay.io/namespace/binary_image:latest',
}
request = RequestMergeIndexImage.from_json(data)
data = MergeIndexImagePydanticModel(
source_from_index=f'quay.io/namespace/repo:{i}',
target_index=f'quay.io/namespace/repo:{i}',
binary_image='quay.io/namespace/binary_image:latest',
)
request = RequestMergeIndexImage.from_json_replacement(data)

if i % 5 == 0:
# Simulate failed request
Expand All @@ -104,31 +111,35 @@ def test_abort_when_downgrading_from_regenerate_bundle_request(app, auth_env, cl
# flask_login.current_user is used in Request*.from_json which requires a request context
with app.test_request_context(environ_base=auth_env):
# Always add a RequestRegenerateBundle to ensure sufficient test data is available
data = {'from_bundle_image': 'quay.io/namespace/bundle-image:latest'}
request = RequestRegenerateBundle.from_json(data)
data = RegenerateBundlePydanticModel(
from_bundle_image='quay.io/namespace/bundle-image:latest'
)
request = RequestRegenerateBundle.from_json_replacement(data)
db.session.add(request)

# One request was already added, let's add the remaining ones
for i in range(total_requests - 1):
request_class = random.choice((RequestAdd, RequestRm, RequestRegenerateBundle))
if request_class == RequestAdd:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'bundles': [f'quay.io/namespace/bundle:{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestAdd.from_json(data)
data = AddPydanticModel(
binary_image='quay.io/namespace/binary_image:latest',
bundles=[f'quay.io/namespace/bundle:{i}'],
from_index=f'quay.io/namespace/repo:{i}',
)
request = RequestAdd.from_json_replacement(data)

elif request_class == RequestRm:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'operators': [f'operator-{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestRm.from_json(data)
data = RmPydanticModel(
binary_image='quay.io/namespace/binary_image:latest',
operators=[f'operator-{i}'],
from_index=f'quay.io/namespace/repo:{i}',
)
request = RequestRm.from_json_replacement(data)
else:
data = {'from_bundle_image': 'quay.io/namespace/bundle-image:latest'}
request = RequestRegenerateBundle.from_json(data)
data = RegenerateBundlePydanticModel(
from_bundle_image='quay.io/namespace/bundle-image:latest'
)
request = RequestRegenerateBundle.from_json_replacement(data)
db.session.add(request)

db.session.commit()
Expand All @@ -148,35 +159,35 @@ def test_create_empty_index_image_request(app, auth_env, client, db):
# which requires a request context
with app.test_request_context(environ_base=auth_env):
# Generate some data to verify migration
data = {
'from_index': 'quay.io/namespace/index_image:latest',
'binary_image': 'quay.io/namespace/binary_image:latest',
}
request = RequestCreateEmptyIndex.from_json(data)
data = CreateEmptyIndexPydanticModel(
from_index='quay.io/namespace/index_image:latest',
binary_image='quay.io/namespace/binary_image:latest',
)
request = RequestCreateEmptyIndex.from_json_replacement(data)
db.session.add(request)

for i in range(total_requests):
request_class = random.choice((RequestAdd, RequestRm, RequestCreateEmptyIndex))
if request_class == RequestAdd:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'bundles': [f'quay.io/namespace/bundle:{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestAdd.from_json(data)
data = AddPydanticModel(
binary_image='quay.io/namespace/binary_image:latest',
bundles=[f'quay.io/namespace/bundle:{i}'],
from_index=f'quay.io/namespace/repo:{i}',
)
request = RequestAdd.from_json_replacement(data)
elif request_class == RequestRm:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'operators': [f'operator-{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestRm.from_json(data)
data = RmPydanticModel(
binary_image='quay.io/namespace/binary_image:latest',
operators=[f'operator-{i}'],
from_index=f'quay.io/namespace/repo:{i}',
)
request = RequestRm.from_json_replacement(data)
elif request_class == RequestCreateEmptyIndex:
data = {
'from_index': f'quay.io/namespace/index_image:{i}',
'binary_image': 'quay.io/namespace/binary_image:latest',
}
request = RequestCreateEmptyIndex.from_json(data)
data = CreateEmptyIndexPydanticModel(
from_index=f'quay.io/namespace/index_image:{i}',
binary_image='quay.io/namespace/binary_image:latest',
)
request = RequestCreateEmptyIndex.from_json_replacement(data)

if i % 5 == 0:
# Simulate failed request
Expand Down
Loading

0 comments on commit 517dd5e

Please sign in to comment.