From 48add55a94e44c4a7546eb1ac2c5255257af4281 Mon Sep 17 00:00:00 2001 From: Francisco Rivera Date: Mon, 11 Sep 2023 14:51:21 -0300 Subject: [PATCH] [BB-212] Project name validation (#215) * project.yaml validations on "project create" * adapt test project name * lower case letters, numbers and hypens, 25 chars max * clarify error * adapting integration test * Update leverage/modules/project.py Co-authored-by: Angelo Fenoglio * Update leverage/modules/project.py Co-authored-by: Angelo Fenoglio * Update leverage/modules/project.py --------- Co-authored-by: Angelo Fenoglio --- .../workflows/tests-unit-and-integration.yml | 2 +- leverage/modules/project.py | 15 ++++++++++- tests/test_modules/test_project.py | 25 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/test_modules/test_project.py diff --git a/.github/workflows/tests-unit-and-integration.yml b/.github/workflows/tests-unit-and-integration.yml index 1711181b..e6b0ea9f 100644 --- a/.github/workflows/tests-unit-and-integration.yml +++ b/.github/workflows/tests-unit-and-integration.yml @@ -82,7 +82,7 @@ jobs: - name: Set project file and create run: | printf "[INFO] Setting Project file\n" - sed 's//The Adam Project/' -i project.yaml + sed 's//the-adam-project/' -i project.yaml sed 's//bb/' -i project.yaml sed 's//bb@domainmgmt/' -i project.yaml sed 's//bb@domainsec/' -i project.yaml diff --git a/leverage/modules/project.py b/leverage/modules/project.py index acf230f9..f0d60b67 100644 --- a/leverage/modules/project.py +++ b/leverage/modules/project.py @@ -1,6 +1,7 @@ """ Module for managing Leverage projects. """ +import re from pathlib import Path from shutil import copy2 from shutil import copytree @@ -17,7 +18,7 @@ from leverage.logger import console from leverage.path import get_root_path from leverage.path import NotARepositoryError -from leverage._utils import git +from leverage._utils import git, ExitError from leverage.container import get_docker_client from leverage.container import TerraformContainer @@ -279,6 +280,16 @@ def load_project_config(): raise Exit(1) +def validate_config(config: dict): + """ + Run a battery of validations over the config file (project.yaml). + """ + if not re.match(r"^[a-z0-9]([a-z0-9]|-){1,23}[a-z0-9]$", config["project_name"]): + raise ExitError(1, "Project name is not valid. Only lowercase alphanumeric characters and hyphens are allowed.") + + return True + + @project.command() def create(): """Create the directory structure required by the project configuration and set up each account accordingly.""" @@ -295,6 +306,8 @@ def create(): logger.error("Project has already been created.") return + validate_config(config) + # Make project structure _copy_project_template(config=config) diff --git a/tests/test_modules/test_project.py b/tests/test_modules/test_project.py new file mode 100644 index 00000000..fa8a0aba --- /dev/null +++ b/tests/test_modules/test_project.py @@ -0,0 +1,25 @@ +import pytest + +from leverage._utils import ExitError +from leverage.modules.project import validate_config + + +def test_validate_config_happy_path(): + assert validate_config({"project_name": "fine"}) + assert validate_config({"project_name": "fine123"}) + assert validate_config({"project_name": "123fine"}) + assert validate_config({"project_name": "hyphens-are-allowed"}) + + +def test_validate_config_errors(muted_click_context): + with pytest.raises(ExitError, match="Project name is not valid"): + validate_config({"project_name": "with spaces"}) + + with pytest.raises(ExitError, match="Project name is not valid"): + validate_config({"project_name": "underscores_not_allowed"}) + + with pytest.raises(ExitError, match="Project name is not valid"): + validate_config({"project_name": "not-alph@-characters!"}) + + with pytest.raises(ExitError, match="Project name is not valid"): + validate_config({"project_name": "loooooooooooooooooooooooooooooooooooooooooooooooooooooong"})