Skip to content

Commit

Permalink
- change machines from set to list
Browse files Browse the repository at this point in the history
- small change on mock of unit tests
  • Loading branch information
gabrielcocenza committed Feb 5, 2024
1 parent ef42634 commit 6b0ff78
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
24 changes: 12 additions & 12 deletions cou/steps/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,53 +379,53 @@ async def generate_plan(analysis_result: Analysis, args: CLIargs) -> UpgradePlan
return plan


async def filter_hypervisors_machines(args: CLIargs, analysis_result: Analysis) -> set[Machine]:
async def filter_hypervisors_machines(args: CLIargs, analysis_result: Analysis) -> list[Machine]:
"""Filter the hypervisors to generate plan and upgrade.
:param args: CLI arguments
:type args: CLIargs
:param analysis_result: Analysis result
:type analysis_result: Analysis
:return: hypervisors filtered to generate plan and upgrade.
:rtype: set[Machine]
:rtype: list[Machine]
"""
hypervisors_machines = await _get_upgradable_hypervisors_machines(args.force, analysis_result)

if cli_machines := args.machines:
return {machine for machine in hypervisors_machines if machine.machine_id in cli_machines}
return [machine for machine in hypervisors_machines if machine.machine_id in cli_machines]

if cli_hostnames := args.hostnames:
return {machine for machine in hypervisors_machines if machine.hostname in cli_hostnames}
return [machine for machine in hypervisors_machines if machine.hostname in cli_hostnames]

if cli_azs := args.availability_zones:
return {machine for machine in hypervisors_machines if machine.az in cli_azs}
return [machine for machine in hypervisors_machines if machine.az in cli_azs]

return hypervisors_machines


async def _get_upgradable_hypervisors_machines(
cli_force: bool, analysis_result: Analysis
) -> set[Machine]:
) -> list[Machine]:
"""Get the hypervisors that are possible to upgrade.
:param cli_force: If force is used, it gets all hypervisors, otherwise just the empty ones
:type cli_force: bool
:param analysis_result: Analysis result
:type analysis_result: Analysis
:return: Set of nova-compute units to upgrade
:rtype: set[Machine]
:return: List of nova-compute units to upgrade
:rtype: list[Machine]
"""
nova_compute_units = {
nova_compute_units = [
unit
for app in analysis_result.apps_data_plane
for unit in app.units
if app.charm == "nova-compute"
}
]

if cli_force:
return {unit.machine for unit in nova_compute_units}
return [unit.machine for unit in nova_compute_units]

return await get_empty_hypervisors(list(nova_compute_units), analysis_result.model)
return await get_empty_hypervisors(nova_compute_units, analysis_result.model)


async def create_upgrade_group(
Expand Down
8 changes: 4 additions & 4 deletions cou/utils/nova_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
from cou.utils.juju_utils import COUModel


async def get_empty_hypervisors(units: list[ApplicationUnit], model: COUModel) -> set[Machine]:
async def get_empty_hypervisors(units: list[ApplicationUnit], model: COUModel) -> list[Machine]:
"""Get the empty hypervisors in the model.
:param units: all nova-compute units.
:type units: list[ApplicationUnit]
:param model: COUModel object
:type model: COUModel
:return: Set with just the empty hypervisors machines.
:rtype: set[Machine]
:return: List with just the empty hypervisors machines.
:rtype: list[Machine]
"""
tasks = [get_instance_count(unit.name, model) for unit in units]
instances = await asyncio.gather(*tasks)
units_instances = zip(units, instances)
return {unit.machine for unit, instances in units_instances if instances == 0}
return [unit.machine for unit, instances in units_instances if instances == 0]


async def get_instance_count(unit: str, model: COUModel) -> int:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ def cli_args() -> MagicMock:
return MagicMock(spec_set=CLIargs(command="plan"))()


def _generate_mock_machine(machine_id, hostname, az):
def generate_mock_machine(machine_id, hostname, az):
mock_machine = MagicMock(spec_set=Machine(machine_id, hostname, az))
mock_machine.machine_id = machine_id
mock_machine.hostname = hostname
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/steps/test_steps_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from cou.utils import app_utils
from cou.utils.openstack import OpenStackRelease
from tests.unit.apps.utils import add_steps
from tests.unit.conftest import KEYSTONE_MACHINES, NOVA_MACHINES, _generate_mock_machine
from tests.unit.conftest import KEYSTONE_MACHINES, NOVA_MACHINES, generate_mock_machine


def generate_expected_upgrade_plan_principal(app, target, model):
Expand Down Expand Up @@ -636,13 +636,13 @@ async def test_filter_hypervisors_machines(
):

empty_hypervisors_machines = {
_generate_mock_machine(
generate_mock_machine(
str(machine_id), f"juju-c307f8-{machine_id}", f"zone-{machine_id + 1}"
)
for machine_id in range(2)
}
# assuming that machine-2 has some VMs running
non_empty_hypervisor_machine = _generate_mock_machine("2", "juju-c307f8-2", "zone-3")
non_empty_hypervisor_machine = generate_mock_machine("2", "juju-c307f8-2", "zone-3")

upgradable_hypervisors = empty_hypervisors_machines
if force:
Expand Down Expand Up @@ -691,7 +691,7 @@ async def test_get_upgradable_hypervisors_machines(
analysis_result,
):
mock_empty_hypervisors.return_value = {
_generate_mock_machine(
generate_mock_machine(
str(machine_id), f"juju-c307f8-{machine_id}", f"zone-{machine_id + 1}"
)
for machine_id in empty_hypervisors
Expand Down
9 changes: 4 additions & 5 deletions tests/unit/utils/test_nova_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from cou.apps.base import ApplicationUnit
from cou.utils import nova_compute
from tests.unit.conftest import _generate_mock_machine
from tests.unit.conftest import generate_mock_machine


@pytest.mark.asyncio
Expand Down Expand Up @@ -65,11 +65,10 @@ async def test_get_instance_count_invalid_result(model, result_key, value):
)
@pytest.mark.asyncio
@patch("cou.utils.nova_compute.get_instance_count")
@patch("cou.utils.nova_compute.asyncio.gather", new_callable=AsyncMock)
async def test_get_empty_hypervisors(
mock_gather, mock_instance_count, hypervisors_count, expected_result, model
mock_instance_count, hypervisors_count, expected_result, model
):
mock_gather.return_value = [count for _, count in hypervisors_count]
mock_instance_count.side_effect = [count for _, count in hypervisors_count]
result = await nova_compute.get_empty_hypervisors(
[_mock_nova_unit(nova_unit) for nova_unit, _ in hypervisors_count], model
)
Expand All @@ -79,7 +78,7 @@ async def test_get_empty_hypervisors(
def _mock_nova_unit(nova_unit):
mock_nova_unit = MagicMock(spec_set=ApplicationUnit(MagicMock(), MagicMock(), MagicMock()))
mock_nova_unit.name = f"nova-compute/{nova_unit}"
nova_machine = _generate_mock_machine(
nova_machine = generate_mock_machine(
str(nova_unit), f"juju-c307f8-{nova_unit}", f"zone-{nova_unit + 1}"
)
mock_nova_unit.machine = nova_machine
Expand Down

0 comments on commit 6b0ff78

Please sign in to comment.