diff --git a/aws_project_costs/schema.py b/aws_project_costs/schema.py index cd509bb..59616e8 100644 --- a/aws_project_costs/schema.py +++ b/aws_project_costs/schema.py @@ -31,7 +31,7 @@ def validate(projects, raise_on_error=False): ) for group in acc.get("project-groups", []): - if set(p["costshare"] for p in group) == {0}: + if set(p["costshare"] for p in projects["project-groups"][group]) == {0}: errors.append(f"All projects in project-group {group} have costshare=0") if raise_on_error and errors: diff --git a/tests/test_project_costs.py b/tests/test_project_costs.py index ab1caa7..649869f 100644 --- a/tests/test_project_costs.py +++ b/tests/test_project_costs.py @@ -4,19 +4,10 @@ import yaml from aws_project_costs.project_costs import analyse_costs_csv -from aws_project_costs.schema import validate EXAMPLE_DIR = Path(__file__).parent / ".." / "example" -def test_schema_validate(): - config_yaml = EXAMPLE_DIR / "projects.yaml" - with (config_yaml).open() as f: - cfg = yaml.safe_load(f) - errors = validate(cfg, raise_on_error=False) - assert len(errors) == 0 - - def test_analyse_costs_csv(tmp_path): config_yaml = EXAMPLE_DIR / "projects.yaml" input_csv = EXAMPLE_DIR / "2024-01-01_2024-02-01.csv" diff --git a/tests/test_schema.py b/tests/test_schema.py new file mode 100644 index 0000000..f328962 --- /dev/null +++ b/tests/test_schema.py @@ -0,0 +1,72 @@ +from pathlib import Path + +import pytest +import yaml + +from aws_project_costs.schema import validate + +EXAMPLE_DIR = Path(__file__).parent / ".." / "example" + + +def test_schema_validate_example(): + config_yaml = EXAMPLE_DIR / "projects.yaml" + with (config_yaml).open() as f: + cfg = yaml.safe_load(f) + errors = validate(cfg, raise_on_error=False) + assert len(errors) == 0 + + +@pytest.mark.parametrize("raise_on_error", [True, False]) +def test_schema_validate_errors(raise_on_error): + config_yaml = """ +proj-tag-names: + project-001: Project A + +accounts: + - name: aws-auth + billing-type: shared + project-tagname: Proj + project-groups: + - tre + - non-existent + + - name: aws-AUTH + billing-type: project-specific + project: x + + - name: invalid-costshare + billing-type: shared + project-groups: + - invalid + +project-groups: + tre: + - name: Project A + costshare: 1 + - name: Project B + costshare: 0 + web: + - name: Project A + costshare: 2 + - name: Project C + costshare: 1 + invalid: + - name: Project D + costshare: 0 +""" + cfg = yaml.safe_load(config_yaml) + + if raise_on_error: + with pytest.raises(ValueError) as excinfo: + validate(cfg, raise_on_error=raise_on_error) + errors = excinfo.value.args[0] + else: + errors = validate(cfg, raise_on_error=raise_on_error) + + assert len(errors) == 3 + expected_errors = [ + "project-group non-existent in account aws-auth does not exist!", + "Multiple entries for account name=aws-auth", + "All projects in project-group invalid have costshare=0", + ] + assert errors == expected_errors