Skip to content

Commit

Permalink
feat: update max allowed size for recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Yifan Zhang committed Sep 1, 2023
1 parent 38f0ab9 commit d9a0464
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 35 deletions.
3 changes: 1 addition & 2 deletions gdk/commands/component/transformer/BuildRecipeTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def transform(self, build_folders):

# Validate the size of the recipe file before processing its content.
if not utils.valid_recipe_file_size(self.project_config.recipe_file):
logging.error("The size of the recipe exceeds the maximum allowed size. Please ensure the recipe size "
"does not exceed 16 KB.")
logging.error(error_messages.RECIPE_SIZE_INVALID.format(self.project_config.recipe_file))
raise Exception(error_messages.RECIPE_SIZE_INVALID.format(self.project_config.recipe_file))

component_recipe = CaseInsensitiveRecipeFile().read(self.project_config.recipe_file)
Expand Down
18 changes: 8 additions & 10 deletions gdk/common/RecipeValidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,21 @@ def validate_recipe_format_version(self):
Raises
------
Exception
If the RecipeFormatVersion field is missing or unsupported.
If the RecipeFormatVersion field is missing.
"""
recipe_format_version = self.recipe_data.get("recipeformatversion")
if not recipe_format_version:
logging.error("Recipe validation failed for 'RecipeFormatVersion'. This field is required but missing "
"from the recipe. Please correct it and try again.")
raise Exception("The recipe file is invalid. The 'RecipeFormatVersion' field is mandatory in the recipe.")
err_msg = "Recipe validation failed for 'RecipeFormatVersion'. This field is required but missing " \
"from the recipe. Please correct it and try again."
logging.error(err_msg)
raise Exception(err_msg)

supported_recipe_version = ["2020-01-25"]
if recipe_format_version not in supported_recipe_version:
logging.error(f"Recipe validation failed for: 'RecipeFormatVersion: {recipe_format_version}'.")
logging.error(f"The provided RecipeFormatVersion '{recipe_format_version}' is not supported in this gdk "
f"version. Please ensure that it is a valid RecipeFormatVersion compatible with the gdk, "
f"and refer to the list of supported RecipeFormatVersion: {supported_recipe_version}.")
raise Exception("The provided RecipeFormatVersion in the recipe is invalid. Please ensure that it follows "
"the correct format and matches one of the supported versions.")
logging.warning(f"The provided RecipeFormatVersion '{recipe_format_version}' is not supported in this gdk "
f"version. Please ensure that it is a valid RecipeFormatVersion compatible with the gdk, "
f"and refer to the list of supported RecipeFormatVersion: {supported_recipe_version}.")

def validate_semantics(self):
"""
Expand Down
4 changes: 4 additions & 0 deletions gdk/common/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"metavar",
"dest",
]

# FILE SIZE
MAX_RECIPE_FILE_SIZE_BYTES = 16000

# FILES
config_schema_file = "config_schema.json"
user_input_recipe_schema_file = "user_input_recipe_schema.json"
Expand Down
2 changes: 1 addition & 1 deletion gdk/common/exceptions/error_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)
PROJECT_CONFIG_FILE_INVALID = "Project configuration file '{}' is invalid. Please correct its format and try again. Error: {} "
RECIPE_FILE_INVALID = "The input recipe file '{}' is invalid. Please correct its format and try again. Error: {} "
RECIPE_SIZE_INVALID = "The input recipe file '{}' has an invalid size. Please make sure it does not exceed 16KB and try again."
RECIPE_SIZE_INVALID = "The input recipe file '{}' has an invalid size. Please make sure it does not exceed 16kB and try again."
CLI_MODEL_FILE_NOT_EXISTS = "Model validation failed. CLI model file doesn't exist."
USER_INPUT_RECIPE_NOT_EXISTS = (
"Recipe file not found. "
Expand Down
3 changes: 2 additions & 1 deletion gdk/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import gdk
import gdk._version as version
from gdk.common import consts
from gdk.common.exceptions import syntax_error_message


Expand Down Expand Up @@ -229,7 +230,7 @@ def parse_json_schema_errors(error):

def valid_recipe_file_size(file_path):
file_size = os.path.getsize(file_path)
return file_size < 16234 # max allowed byte
return file_size <= consts.MAX_RECIPE_FILE_SIZE_BYTES


error_line = "\n=============================== ERROR ===============================\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,38 @@ def test_transform_missing_recipe_format_version_expect_exception(self):
try:
transformer.transform(build_folders)
except Exception as e:
assert str(e) == "The recipe file is invalid. The 'RecipeFormatVersion' field is mandatory in the recipe."
assert str(e) == "Recipe validation failed for 'RecipeFormatVersion'. This field is required " \
"but missing from the recipe. Please correct it and try again."

mock_size.assert_called_once_with(config.recipe_file)
mock_read.assert_called_once_with(config.recipe_file)
mock_update.assert_not_called()
mock_create.assert_not_called()

def test_transform_invalid_recipe_format_version_expect_exception(self):
def test_transform_invalid_recipe_format_version_expect_early_warning(self):
mock_size = self.mocker.patch("gdk.common.utils.valid_recipe_file_size", return_value=True)
mock_read = self.mocker.patch.object(CaseInsensitiveRecipeFile, "read",
return_value=CaseInsensitiveDict({"RecipeFormatVersion": "77777"}))
return_value=CaseInsensitiveDict({
"RecipeFormatVersion": "2023-01-25",
"ComponentName": "com.example.HelloWorld"
}))
mock_update = self.mocker.patch.object(BuildRecipeTransformer, "update_component_recipe_file",
return_value=None)
mock_create = self.mocker.patch.object(BuildRecipeTransformer, "create_build_recipe_file", return_value=None)
build_folders = [Path("build-folder")]
config = ComponentBuildConfiguration({})
transformer = BuildRecipeTransformer(config)

try:
transformer.transform(build_folders)
except Exception as e:
assert str(e) == "The provided RecipeFormatVersion in the recipe is invalid. Please ensure that it " \
"follows the correct format and matches one of the supported versions."
with mock.patch('gdk.common.RecipeValidator.logging') as mock_logging:
with pytest.raises(Exception):
transformer.transform(build_folders)

assert mock_logging.warning.call_count == 1
warnings = mock_logging.warning.call_args[0]
expected_warnings = "The provided RecipeFormatVersion '2023-01-25' is not supported in this gdk version. " \
"Please ensure that it is a valid RecipeFormatVersion compatible with the gdk, " \
"and refer to the list of supported RecipeFormatVersion: ['2020-01-25']."
assert any(expected_warnings in arg for arg in warnings)

mock_size.assert_called_once_with(config.recipe_file)
mock_read.assert_called_once_with(config.recipe_file)
Expand Down
26 changes: 13 additions & 13 deletions tests/gdk/common/test_RecipeValidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ def test_validate_missing_recipe_format_version_expect_exception(self):
validator = RecipeValidator(invalid_recipe)
with pytest.raises(Exception) as e:
validator.validate_recipe_format_version()
self.assertEqual(str(e.value), "The recipe file is invalid. The 'RecipeFormatVersion' field is mandatory in "
"the recipe.")
self.assertEqual(str(e.value), "Recipe validation failed for 'RecipeFormatVersion'. This field is required "
"but missing from the recipe. Please correct it and try again.")
mock_logging_error.assert_called_with("Recipe validation failed for 'RecipeFormatVersion'. This field is "
"required but missing from the recipe. Please correct it and try again.")

def test_validate_invalid_recipe_format_version_expect_exception(self):
mock_logging_error = self.mocker.patch('logging.error')
invalid_recipe = CaseInsensitiveDict({"RecipeFormatVersion": "99999"})
validator = RecipeValidator(invalid_recipe)
with pytest.raises(Exception) as e:
def test_validate_incompatible_recipe_format_version_expect_warning(self):
recipe_data = CaseInsensitiveDict({"RecipeFormatVersion": "2023-01-25"})
validator = RecipeValidator(recipe_data)
with mock.patch('gdk.common.RecipeValidator.logging') as mock_logging:
validator.validate_recipe_format_version()
self.assertEqual(str(e.value), "The provided RecipeFormatVersion in the recipe is invalid. Please ensure that "
"it follows the correct format and matches one of the supported versions.")
mock_logging_error.assert_called_with("The provided RecipeFormatVersion '99999' is not supported in this gdk "
"version. Please ensure that it is a valid RecipeFormatVersion "
"compatible with the gdk, and refer to the list of supported "
"RecipeFormatVersion: ['2020-01-25'].")
assert mock_logging.warning.call_count == 1
warnings = mock_logging.warning.call_args[0]
expected_warnings = "The provided RecipeFormatVersion '2023-01-25' is not supported in this " \
"gdk version. Please ensure that it is a valid RecipeFormatVersion " \
"compatible with the gdk, and refer to the list of supported " \
"RecipeFormatVersion: ['2020-01-25']."
assert any(expected_warnings in arg for arg in warnings)

def test_validate_semantics_valid_recipe(self):
valid_recipe = CaseInsensitiveDict({
Expand Down

0 comments on commit d9a0464

Please sign in to comment.