Skip to content

Commit

Permalink
Add linfo_command_test + fix to LinfoCommand docs (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcaiazzi committed Jan 31, 2023
1 parent 75f1334 commit 492b1ac
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 15 deletions.
17 changes: 9 additions & 8 deletions docs/kathara-linfo.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ kathara-linfo(1) -- Show information about a Kathara network scenario

## SYNOPSIS

`kathara linfo` [`-h`] [`-d` <DIRECTORY>] [`-l` \| `-c`] [`-n` <DEVICE_NAME>]
`kathara linfo` [`-h`] [`-d` <DIRECTORY>] [`-w` \| `-c`] [`-n` <DEVICE_NAME>]


## DESCRIPTION

Show exhaustive status of each device of the current network scenario. Can only be used when after the network scenario is started.
Show exhaustive status of each device of the current network scenario.

The following information are displayed:

Expand All @@ -31,13 +31,13 @@ The following information are displayed:

If no `-d` option is provided, assume the network scenario is located in the current directory.

* `-l`, `--live`:
Live update of network scenario status.
* `-w`, `-l`, `--watch`, `--live`:
Watch updates of network scenario status.

To quit the live screen, pass the `SIGINT` `signal`(7) to the process (usually CTRL+C).
To quit the watch screen, pass the `SIGINT` `signal`(7) to the process (usually CTRL+C).

* `-c`, `--conf`
Read information from lab.conf and display the following:
* `-c`, `--conf`:
Read static information from lab.conf and display the following:

* Descriptive information (retrieved from `kathara-lab.conf`(5))
* Number of devices that make up the network scenario
Expand All @@ -47,7 +47,8 @@ The following information are displayed:
Show only info about a specified device.

Show information about a device `DEVICE_NAME`.
Can be used in conjuction with `-l` to retrieve live information.
Can be used in conjunction with `-w` to retrieve live information.
Can be used in conjunction with `-c` to retrieve static information.

m4_include(footer.txt)

Expand Down
8 changes: 4 additions & 4 deletions src/Kathara/cli/command/LinfoCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ def __init__(self) -> None:
group = self.parser.add_mutually_exclusive_group(required=False)

group.add_argument(
'-l', '--live',
'-w', '-l', '--watch', '--live',
required=False,
action='store_true',
help='Live mode, can be used only when a network scenario is launched.'
help='Watch mode, can be used only when a network scenario is launched.'
)

group.add_argument(
'-c', '--conf',
required=False,
action='store_true',
help='Read information from lab.conf.'
help='Read static information from lab.conf.'
)

self.parser.add_argument(
Expand All @@ -71,7 +71,7 @@ def run(self, current_path: str, argv: List[str]) -> None:
except (Exception, IOError):
lab = Lab(None, path=lab_path)

if args['live']:
if args['watch']:
if args['name']:
self._get_machine_live_info(lab, args['name'])
else:
Expand Down
20 changes: 19 additions & 1 deletion tests/cli/exec_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def test_run_no_params(mock_stderr_write, mock_stdout_write, mock_lab, mock_pars
@mock.patch("src.Kathara.model.Lab.Lab")
@mock.patch('sys.stdout.write')
@mock.patch('sys.stderr.write')
def test_run_with_directory(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_exec, exec_output):
def test_run_with_directory_absolute_path(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_exec,
exec_output):
mock_parse_lab.return_value = mock_lab
mock_exec.return_value = exec_output
command = ExecCommand()
Expand All @@ -50,6 +51,23 @@ def test_run_with_directory(mock_stderr_write, mock_stdout_write, mock_lab, mock
mock_stderr_write.assert_called_once_with('stderr')


@mock.patch("src.Kathara.manager.Kathara.Kathara.exec")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
@mock.patch("src.Kathara.model.Lab.Lab")
@mock.patch('sys.stdout.write')
@mock.patch('sys.stderr.write')
def test_run_with_directory_relative_path(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_exec,
exec_output):
mock_parse_lab.return_value = mock_lab
mock_exec.return_value = exec_output
command = ExecCommand()
command.run('.', ['-d', 'test/path', 'pc1', 'test command'])
mock_parse_lab.assert_called_once_with(os.path.join(os.getcwd(), 'test/path'))
mock_exec.assert_called_once_with("pc1", ['test command'], lab_hash=mock_lab.hash)
mock_stdout_write.assert_called_once_with('stdout')
mock_stderr_write.assert_called_once_with('stderr')


@mock.patch("src.Kathara.manager.Kathara.Kathara.exec")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
@mock.patch('sys.stdout.write')
Expand Down
13 changes: 12 additions & 1 deletion tests/cli/lclean_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,25 @@ def test_run_no_params(mock_lab, mock_parse_lab, mock_undeploy_lab):
@mock.patch("src.Kathara.manager.Kathara.Kathara.undeploy_lab")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_with_directory(mock_lab, mock_parse_lab, mock_undeploy_lab):
def test_run_with_directory_absolute_path(mock_lab, mock_parse_lab, mock_undeploy_lab):
mock_parse_lab.return_value = mock_lab
command = LcleanCommand()
command.run('.', ['-d', '/test/path'])
mock_parse_lab.assert_called_once_with('/test/path')
mock_undeploy_lab.assert_called_once_with(lab_hash=mock_lab.hash, selected_machines=None)


@mock.patch("src.Kathara.manager.Kathara.Kathara.undeploy_lab")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_with_directory_relative_path(mock_lab, mock_parse_lab, mock_undeploy_lab):
mock_parse_lab.return_value = mock_lab
command = LcleanCommand()
command.run('.', ['-d', 'test/path'])
mock_parse_lab.assert_called_once_with(os.path.join(os.getcwd(), 'test/path'))
mock_undeploy_lab.assert_called_once_with(lab_hash=mock_lab.hash, selected_machines=None)


@mock.patch("src.Kathara.manager.Kathara.Kathara.undeploy_lab")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
@mock.patch("src.Kathara.model.Lab.Lab")
Expand Down
16 changes: 15 additions & 1 deletion tests/cli/lconfig_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_run_remove_link(mock_parse_lab, mock_update_lab_from_api, mock_disconne
@mock.patch("src.Kathara.manager.Kathara.Kathara.disconnect_machine_from_link")
@mock.patch("src.Kathara.manager.Kathara.Kathara.update_lab_from_api")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_remove_link_with_directory(mock_parse_lab, mock_update_lab_from_api, mock_disconnect_machine_from_link,
def test_run_remove_link_with_directory_absolute_path(mock_parse_lab, mock_update_lab_from_api, mock_disconnect_machine_from_link,
test_lab):
mock_parse_lab.return_value = test_lab
command = LconfigCommand()
Expand All @@ -91,6 +91,20 @@ def test_run_remove_link_with_directory(mock_parse_lab, mock_update_lab_from_api
test_lab.get_or_new_link('A'))


@mock.patch("src.Kathara.manager.Kathara.Kathara.disconnect_machine_from_link")
@mock.patch("src.Kathara.manager.Kathara.Kathara.update_lab_from_api")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_remove_link_with_directory_relative_path(mock_parse_lab, mock_update_lab_from_api, mock_disconnect_machine_from_link,
test_lab):
mock_parse_lab.return_value = test_lab
command = LconfigCommand()
command.run('.', ['-d', 'test/path', '-n', 'pc1', '--rm', 'A'])
mock_parse_lab.assert_called_once_with(os.path.join(os.getcwd(), 'test/path'))
mock_update_lab_from_api.assert_called_once_with(test_lab)
mock_disconnect_machine_from_link.assert_called_once_with(test_lab.get_or_new_machine('pc1'),
test_lab.get_or_new_link('A'))


@mock.patch("src.Kathara.manager.Kathara.Kathara.disconnect_machine_from_link")
@mock.patch("src.Kathara.manager.Kathara.Kathara.update_lab_from_api")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
Expand Down
132 changes: 132 additions & 0 deletions tests/cli/linfo_command_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import os
import sys
from unittest import mock
from unittest.mock import MagicMock

import pytest

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

from src.Kathara.cli.command.LinfoCommand import LinfoCommand
from src.Kathara.model.Lab import Lab


@pytest.fixture()
def test_lab():
lab = Lab('test_lab')
lab.get_or_new_machine('pc1')
lab.connect_machine_to_link(machine_name='pc1', link_name='A')
lab.connect_machine_to_link(machine_name='pc1', link_name='B')
return lab


@mock.patch("src.Kathara.manager.Kathara.Kathara.get_machines_stats")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_no_params(mock_parse_lab, mock_get_machines_stats, test_lab):
mock_parse_lab.return_value = test_lab
command = LinfoCommand()
command.run('.', [])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_get_machines_stats.assert_called_once_with(test_lab.hash)


@mock.patch("src.Kathara.manager.Kathara.Kathara.get_machines_stats")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_with_directory_absolute_path(mock_parse_lab, mock_get_machines_stats, test_lab):
mock_parse_lab.return_value = test_lab
command = LinfoCommand()
command.run('.', ['-d', '/test/path'])
mock_parse_lab.assert_called_once_with('/test/path')
mock_get_machines_stats.assert_called_once_with(test_lab.hash)


@mock.patch("src.Kathara.manager.Kathara.Kathara.get_machines_stats")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_with_directory_relative_path(mock_parse_lab, mock_get_machines_stats, test_lab):
mock_parse_lab.return_value = test_lab
command = LinfoCommand()
command.run('.', ['-d', 'test/path'])
mock_parse_lab.assert_called_once_with(os.path.join(os.getcwd(), 'test/path'))
mock_get_machines_stats.assert_called_once_with(test_lab.hash)


@mock.patch("src.Kathara.cli.command.LinfoCommand.LinfoCommand._get_lab_live_info")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_watch(mock_parse_lab, mock_get_lab_live_info, test_lab):
mock_parse_lab.return_value = test_lab
command = LinfoCommand()
command.run('.', ['-w'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_get_lab_live_info.assert_called_once_with(test_lab)


@mock.patch("src.Kathara.cli.command.LinfoCommand.LinfoCommand._get_machine_live_info")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_watch_with_name(mock_parse_lab, mock_get_machine_live_info, test_lab):
mock_parse_lab.return_value = test_lab
command = LinfoCommand()
command.run('.', ['-w', '-n', 'pc1'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_get_machine_live_info.assert_called_once_with(test_lab, 'pc1')


@mock.patch("src.Kathara.cli.command.LinfoCommand.LinfoCommand._get_conf_info")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_with_conf(mock_parse_lab, mock_get_conf_info, test_lab):
mock_parse_lab.return_value = test_lab
command = LinfoCommand()
command.run('.', ['-c'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_get_conf_info.assert_called_once_with(test_lab, machine_name=None)


@mock.patch("src.Kathara.cli.command.LinfoCommand.LinfoCommand._get_conf_info")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_with_conf_and_name(mock_parse_lab, mock_get_conf_info, test_lab):
mock_parse_lab.return_value = test_lab
command = LinfoCommand()
command.run('.', ['-c', '-n', 'pc1'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_get_conf_info.assert_called_once_with(test_lab, machine_name='pc1')


@mock.patch("src.Kathara.manager.Kathara.Kathara.get_machine_stats")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
def test_run_with_name(mock_parse_lab, mock_get_machine_stats, test_lab):
mock_parse_lab.return_value = test_lab
command = LinfoCommand()
command.run('.', ['-n', 'pc1'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_get_machine_stats.assert_called_once_with('pc1', test_lab.hash)


@mock.patch("src.Kathara.trdparty.curses.curses.Curses.close")
@mock.patch("src.Kathara.manager.Kathara.Kathara.get_machine_stats")
@mock.patch("src.Kathara.trdparty.curses.curses.Curses.print_string", side_effect=KeyboardInterrupt)
@mock.patch("src.Kathara.trdparty.curses.curses.Curses.init_window")
def test_get_machine_live_info(mock_init_window, mock_print_string, mock_get_machine_stats,
mock_close, test_lab):
with pytest.raises(KeyboardInterrupt):
LinfoCommand._get_machine_live_info(test_lab, 'pc1')
mock_init_window.assert_called_once()
mock_print_string.assert_called_once()
mock_get_machine_stats.assert_called_once_with('pc1', test_lab.hash)
mock_close.assert_called_once()


@mock.patch("src.Kathara.trdparty.curses.curses.Curses.close")
@mock.patch("src.Kathara.trdparty.curses.curses.Curses.print_string")
@mock.patch("src.Kathara.trdparty.curses.curses.Curses.init_window")
@mock.patch("src.Kathara.cli.command.LinfoCommand.create_table")
@mock.patch("src.Kathara.manager.Kathara.Kathara.get_machines_stats")
def test_get_lab_live_info(mock_get_machines_stats, mock_create_table, mock_init_window, mock_print_string, mock_close,
test_lab):
machine_stats = map(lambda x: x, [{"A": MagicMock()}])
mock_get_machines_stats.return_value = machine_stats
mock_create_table.return_value = machine_stats
LinfoCommand._get_lab_live_info(test_lab)
mock_get_machines_stats.assert_called_once_with(test_lab.hash)
mock_create_table.assert_called_once_with(machine_stats)
mock_init_window.assert_called_once()
mock_print_string.assert_called_once()
mock_close.assert_called_once()

0 comments on commit 492b1ac

Please sign in to comment.