Skip to content

Commit

Permalink
apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielcocenza committed Apr 25, 2024
1 parent a12216f commit 2722dc5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
29 changes: 29 additions & 0 deletions tests/mocked_plans/conftest.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,18 @@
from unittest.mock import patch

import pytest
from utils import sample_plans

from cou.commands import CLIargs
from cou.steps.analyze import Analysis
from cou.steps.plan import generate_plan


@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
17 changes: 5 additions & 12 deletions tests/mocked_plans/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")]

0 comments on commit 2722dc5

Please sign in to comment.