Skip to content

Commit

Permalink
Fix lconfig error handling (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcaiazzi committed Dec 15, 2023
1 parent 7943db9 commit 7f34553
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Kathara/cli/command/LconfigCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def run(self, current_path: str, argv: List[str]) -> None:
Kathara.get_instance().update_lab_from_api(lab)

machine_name = args['name']
device = lab.get_or_new_machine(machine_name)
device = lab.get_machine(machine_name)

if args['to_add']:
for cd in args['to_add']:
Expand Down
5 changes: 4 additions & 1 deletion src/Kathara/manager/docker/DockerMachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ... import utils
from ...event.EventDispatcher import EventDispatcher
from ...exceptions import MountDeniedError, MachineAlreadyExistsError, MachineNotFoundError, DockerPluginError, \
MachineBinaryError
MachineBinaryError, MachineNotRunningError
from ...model.Lab import Lab
from ...model.Link import Link, BRIDGE_LINK_NAME
from ...model.Machine import Machine, MACHINE_CAPABILITIES
Expand Down Expand Up @@ -317,6 +317,9 @@ def connect_to_link(machine: Machine, link: Link) -> None:
DockerPluginError: If Kathara has been left in an inconsistent state.
APIError: If the Docker APIs return an error.
"""
if not machine.api_object:
raise MachineNotRunningError(machine.name)

machine.api_object.reload()
attached_networks = machine.api_object.attrs["NetworkSettings"]["Networks"]

Expand Down
2 changes: 1 addition & 1 deletion src/Kathara/parser/netkit/DepParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def parse(path: str) -> Optional[List[str]]:

dependencies = {}

# Reads lab.dep in memory so it is faster.
# Reads lab.dep in memory, so it is faster.
try:
with open(lab_dep_path, 'r') as dep_file:
dep_mem_file = mmap.mmap(dep_file.fileno(), 0, access=mmap.ACCESS_READ)
Expand Down
14 changes: 14 additions & 0 deletions tests/cli/lconfig_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from src.Kathara.model.Machine import Machine
from src.Kathara.model.Link import Link
from src.Kathara.model.Lab import Lab
from src.Kathara.exceptions import MachineNotFoundError


@pytest.fixture()
Expand Down Expand Up @@ -127,6 +128,19 @@ def test_run_remove_two_links(mock_parse_lab, mock_docker_manager, mock_manager_
test_lab.get_or_new_link('B'))


@mock.patch("src.Kathara.manager.Kathara.Kathara.get_instance")
@mock.patch("src.Kathara.manager.docker.DockerManager.DockerManager")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_machine_not_found_error(mock_parse_lab, mock_docker_manager, mock_manager_get_instance, test_lab):
mock_parse_lab.return_value = test_lab
mock_manager_get_instance.return_value = mock_docker_manager
command = LconfigCommand()
with pytest.raises(MachineNotFoundError):
command.run('.', ['-n', 'pc10', '--add', 'A'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_docker_manager.update_lab_from_api.assert_called_once_with(test_lab)


def test_run_system_exit_error():
command = LconfigCommand()
with pytest.raises(SystemExit):
Expand Down
10 changes: 9 additions & 1 deletion tests/manager/docker/docker_manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from src.Kathara.utils import generate_urlsafe_hash
from src.Kathara.manager.docker.stats.DockerLinkStats import DockerLinkStats
from src.Kathara.manager.docker.stats.DockerMachineStats import DockerMachineStats
from src.Kathara.exceptions import MachineNotFoundError, LabNotFoundError, InvocationError, LinkNotFoundError
from src.Kathara.exceptions import MachineNotFoundError, LabNotFoundError, InvocationError, LinkNotFoundError, \
MachineNotRunningError


#
Expand Down Expand Up @@ -292,6 +293,13 @@ def test_connect_machine_to_link_no_link_lab(docker_manager, default_device, def
docker_manager.connect_machine_to_link(default_device, default_link)


def test_connect_machine_to_link_machine_not_running_error(docker_manager, default_device, default_link):
default_device.api_object = None

with pytest.raises(MachineNotRunningError):
docker_manager.connect_machine_to_link(default_device, default_link)


#
# TEST: disconnect_machine_from_link
#
Expand Down
5 changes: 2 additions & 3 deletions tests/parser/lab_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import pytest

from src.Kathara.exceptions import MachineCollisionDomainError
from src.Kathara.parser.netkit.LabParser import LabParser

sys.path.insert(0, './')

from src.Kathara.exceptions import MachineCollisionDomainError
from src.Kathara.parser.netkit.LabParser import LabParser

def test_one_device():
lab = LabParser.parse("tests/parser/labconf/one_device")
Expand Down

0 comments on commit 7f34553

Please sign in to comment.