diff --git a/tests/mocked_plans/conftest.py b/tests/mocked_plans/conftest.py new file mode 100644 index 00000000..a756f669 --- /dev/null +++ b/tests/mocked_plans/conftest.py @@ -0,0 +1,29 @@ +# Copyright 2023 Canonical Limited +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +from cou.utils.juju_utils import Model +from tests.mocked_plans.utils import get_sample_files, get_sample_plan + + +@pytest.fixture(params=get_sample_files(), ids=[path.name for path in get_sample_files()]) +def sample_plans(request) -> tuple[Model, str]: + """Return all sample plans in a directory. + + This parametrized fixture return a tuple with a cou.utils.juju_utils.Model object and the + expected plan in string format as the value. The get_applications function of this Model object + returns the applications read from a YAML file, from which the expected plan is also parsed. + """ + return get_sample_plan(request.param) diff --git a/tests/mocked_plans/test_base_plan.py b/tests/mocked_plans/test_sample_plans.py similarity index 76% rename from tests/mocked_plans/test_base_plan.py rename to tests/mocked_plans/test_sample_plans.py index efd2877f..4f59eb8d 100644 --- a/tests/mocked_plans/test_base_plan.py +++ b/tests/mocked_plans/test_sample_plans.py @@ -15,7 +15,6 @@ from unittest.mock import patch import pytest -from utils import sample_plans from cou.commands import CLIargs from cou.steps.analyze import Analysis @@ -23,15 +22,11 @@ @pytest.mark.asyncio -@pytest.mark.parametrize( - "file_name, model, exp_plan", - sample_plans(), - ids=[file_name for file_name, *_ in sample_plans()], -) @patch("cou.utils.nova_compute.get_instance_count", return_value=0) -async def test_base_plan(_, file_name, model, exp_plan): - """Testing the base plans.""" +async def test_plans_with_empty_hypervisors(_, sample_plans): + """Testing all the plans on sample_plans folder considering all hypervisors empty.""" + model, exp_plan = sample_plans args = CLIargs("plan", auto_approve=True) analysis_results = await Analysis.create(model) plan = await generate_plan(analysis_results, args) - assert str(plan) == exp_plan, f"{file_name} failed" + assert str(plan) == exp_plan diff --git a/tests/mocked_plans/utils.py b/tests/mocked_plans/utils.py index 5a80a8c4..7a566cc7 100644 --- a/tests/mocked_plans/utils.py +++ b/tests/mocked_plans/utils.py @@ -21,7 +21,7 @@ from tests.unit.utils import dedent_plan -def get_sample_plan(source: Path) -> tuple[str, Model, str]: +def get_sample_plan(source: Path) -> tuple[Model, str]: """Help function to get dict of Applications and expected upgrade plan from file. This function can load applications from yaml format, where each app is string representation @@ -74,17 +74,10 @@ def get_sample_plan(source: Path) -> tuple[str, Model, str]: type(model).name = PropertyMock(return_value=source.stem) model.get_applications = AsyncMock(return_value=applications) - return source.name, model, dedent_plan(data["plan"]) + return model, dedent_plan(data["plan"]) -def sample_plans() -> list[tuple[str, Model, str]]: - """Return all sample plans in a directory. - - This function return a list of tuples consisting of the filename, a cou.utils.juju_utils.Model - object and the expected plan in string format as the value. The get_applications function of - this Model object returns the applications read from a YAML file, from which the expected plan - is also parsed. - """ +def get_sample_files() -> list[Path]: + """Get all the yaml files on the sample_plans folder.""" directory = Path(__file__).parent / "sample_plans" - - return [get_sample_plan(sample_file) for sample_file in directory.glob("*.yaml")] + return [sample_file for sample_file in directory.glob("*.yaml")]