Skip to content

Commit

Permalink
fix: validate version before validating config (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
saranyailla authored Jul 21, 2023
1 parent 5d87492 commit 956c234
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 31 deletions.
7 changes: 5 additions & 2 deletions gdk/common/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ def get_configuration():
with open(project_config_file, "r") as config_file:
config_data = json.loads(config_file.read())
try:
validate_configuration(config_data)
validate_cli_version(config_data)
validate_configuration(config_data)

return config_data
except jsonschema.exceptions.ValidationError as err:
raise Exception(error_messages.PROJECT_CONFIG_FILE_INVALID.format(project_config_file.name, err.message))
Expand Down Expand Up @@ -59,7 +60,9 @@ def validate_configuration(data):

def validate_cli_version(config_data):
cli_version = utils.cli_version
config_version = config_data["gdk_version"]
config_version = config_data.get("gdk_version")
if not config_version:
return
if Version(cli_version) < Version(config_version):
update_command = f"pip3 install git+https://github.com/aws-greengrass/aws-greengrass-gdk-cli.git@v{config_version}"
raise Exception(
Expand Down
3 changes: 1 addition & 2 deletions gdk/static/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@
"zip_name": {
"type": "string"
}
},
"additionalProperties": false
}
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions integration_tests/gdk/common/test_integ_configuration.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging
from pathlib import Path

import gdk.common.configuration as config
import gdk.common.consts as consts
import gdk.common.exceptions.error_messages as error_messages
import gdk.common.utils as utils
import pytest
import json


def test_get_static_file_path_cli_schema():
Expand All @@ -24,7 +24,6 @@ def test_get_configuration_invalid_gdk_version(mocker, file_name):
"gdk.common.configuration._get_project_config_file",
return_value=Path(".").joinpath("tests/gdk/static").joinpath(file_name).resolve(),
)
spy_log_debug = mocker.spy(logging, "debug")
with pytest.raises(Exception) as err:
config.get_configuration()
assert mock_get_project_config_file.called
Expand All @@ -34,8 +33,6 @@ def test_get_configuration_invalid_gdk_version(mocker, file_name):
== err.value.args[0]
)

assert spy_log_debug.call_count == 2


@pytest.mark.parametrize(
"file_name",
Expand All @@ -58,18 +55,16 @@ def test_get_configuration_invalid_config_file(mocker, file_name):
["config.json"],
)
def test_get_configuration_valid_component_config_found(mocker, file_name):
config_file = Path(".").joinpath("tests/gdk/static").joinpath(file_name).resolve()
mock_get_project_config_file = mocker.patch(
"gdk.common.configuration._get_project_config_file",
return_value=Path(".").joinpath("tests/gdk/static").joinpath(file_name).resolve(),
return_value=config_file,
)
spy_log_debug = mocker.spy(logging, "debug")

config.get_configuration()
_config = config.get_configuration()
with open(config_file, "r") as f:
assert _config == json.loads(f.read())
assert mock_get_project_config_file.called
spy_log_debug.assert_called_with(
"This gdk project configuration (gdk-1.0.0) is compatible with the existing gdk cli version"
f" (gdk-{utils.cli_version})."
)


def test_get_project_config_file_not_exists(mocker):
Expand Down
9 changes: 0 additions & 9 deletions tests/gdk/build_system/test_zip_build_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,3 @@ def configuration_base(options: dict) -> dict:
)
def test_valid_configuration_options(options):
validate_configuration(configuration_base(options))


@pytest.mark.parametrize(
"options",
[{"exclude": []}, {fake.color_name(): fake.color_name()}],
)
def test_invalid_configuration_options(options):
with pytest.raises(Exception):
validate_configuration(configuration_base(options))
7 changes: 0 additions & 7 deletions tests/gdk/common/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,9 @@ def test_get_configuration_valid_component_config_found(mocker):
"gdk.common.configuration._get_project_config_file",
return_value=Path(".").joinpath("tests/gdk/static").joinpath("config.json"),
)
spy_log_debug = mocker.spy(logging, "debug")

assert config.get_configuration() == expected_config
assert mock_get_project_config_file.called
spy_log_debug.assert_called_with(
"This gdk project configuration (gdk-1.0.0) is compatible with the existing gdk cli version"
f" (gdk-{utils.cli_version})."
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -68,12 +63,10 @@ def test_get_configuration_invalid_gdk_version(mocker, file_name):
"gdk.common.configuration._get_project_config_file",
return_value=Path(".").joinpath("tests/gdk/static").joinpath(file_name).resolve(),
)
mock_validate_configuration = mocker.patch("gdk.common.configuration.validate_configuration", return_value=None)
spy_log_debug = mocker.spy(logging, "debug")
with pytest.raises(Exception) as err:
config.get_configuration()
assert mock_get_project_config_file.called
assert mock_validate_configuration.called
assert (
"This gdk project requires gdk cli version '1000.0.0' or above. Please update the cli using the command `pip3 install"
" git+https://github.com/aws-greengrass/aws-greengrass-gdk-cli.git@v1000.0.0` before proceeding."
Expand Down

0 comments on commit 956c234

Please sign in to comment.