Skip to content

Commit

Permalink
Fix CLI unit tests (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcaiazzi committed May 18, 2023
1 parent e1f6551 commit bfb616e
Show file tree
Hide file tree
Showing 11 changed files with 540 additions and 334 deletions.
59 changes: 38 additions & 21 deletions tests/cli/connect_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,86 @@
from src.Kathara.model.Lab import Lab


@mock.patch("src.Kathara.manager.Kathara.Kathara.connect_tty")
@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")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_no_params(mock_lab, mock_parse_lab, mock_connect_tty):
def test_run_no_params(mock_lab, mock_parse_lab, mock_docker_manager, mock_manager_get_instance):
mock_manager_get_instance.return_value = mock_docker_manager
mock_parse_lab.return_value = mock_lab
command = ConnectCommand()
command.run('.', ['pc1'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=mock_lab.hash, shell=None, logs=False)
mock_docker_manager.connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=mock_lab.hash, shell=None,
logs=False)


@mock.patch("src.Kathara.manager.Kathara.Kathara.connect_tty")
@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")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_with_directory(mock_lab, mock_parse_lab, mock_connect_tty):
def test_run_with_directory(mock_lab, mock_parse_lab, mock_docker_manager, mock_manager_get_instance):
mock_manager_get_instance.return_value = mock_docker_manager
mock_parse_lab.return_value = mock_lab
command = ConnectCommand()
command.run('.', ['-d', '/test/path', 'pc1'])
mock_parse_lab.assert_called_once_with('/test/path')
mock_connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=mock_lab.hash, shell=None, logs=False)
mock_docker_manager.connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=mock_lab.hash, shell=None,
logs=False)


@mock.patch("src.Kathara.manager.Kathara.Kathara.connect_tty")
@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")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_with_logs(mock_lab, mock_parse_lab, mock_connect_tty):
def test_run_with_logs(mock_lab, mock_parse_lab, mock_docker_manager, mock_manager_get_instance):
mock_manager_get_instance.return_value = mock_docker_manager
mock_parse_lab.return_value = mock_lab
command = ConnectCommand()
command.run('.', ['--logs', 'pc1'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=mock_lab.hash, shell=None, logs=True)
mock_docker_manager.connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=mock_lab.hash, shell=None,
logs=True)


@mock.patch("src.Kathara.manager.Kathara.Kathara.connect_tty")
@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")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_with_shell(mock_lab, mock_parse_lab, mock_connect_tty):
def test_run_with_shell(mock_lab, mock_parse_lab, mock_docker_manager, mock_manager_get_instance):
mock_manager_get_instance.return_value = mock_docker_manager
mock_parse_lab.return_value = mock_lab
command = ConnectCommand()
command.run('.', ['--shell', '/custom/shell', 'pc1'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=mock_lab.hash, shell='/custom/shell',
logs=False)
mock_docker_manager.connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=mock_lab.hash,
shell='/custom/shell',
logs=False)


@mock.patch("src.Kathara.manager.Kathara.Kathara.connect_tty")
@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_with_v_option(mock_parse_lab, mock_connect_tty):
def test_run_with_v_option(mock_parse_lab, mock_docker_manager, mock_manager_get_instance):
mock_manager_get_instance.return_value = mock_docker_manager
lab = Lab('kathara_vlab')
command = ConnectCommand()
command.run('.', ['-v', 'pc1'])
assert not mock_parse_lab.called
mock_connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=lab.hash, shell=None,
logs=False)
mock_docker_manager.connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=lab.hash, shell=None,
logs=False)


@mock.patch("src.Kathara.manager.Kathara.Kathara.connect_tty")
@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")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_all_params(mock_lab, mock_parse_lab, mock_connect_tty):
def test_run_all_params(mock_lab, mock_parse_lab, mock_docker_manager, mock_manager_get_instance):
mock_manager_get_instance.return_value = mock_docker_manager
mock_parse_lab.return_value = mock_lab
command = ConnectCommand()
command.run('.', ['-d', '/test/path', '--logs', '--shell', '/custom/shell', 'pc1'])
mock_parse_lab.assert_called_once_with('/test/path')
mock_connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=mock_lab.hash, shell='/custom/shell',
logs=True)
mock_docker_manager.connect_tty.assert_called_once_with(machine_name="pc1", lab_hash=mock_lab.hash,
shell='/custom/shell',
logs=True)
78 changes: 50 additions & 28 deletions tests/cli/exec_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,115 +18,137 @@ def output_generator():
return output_generator()


@mock.patch("src.Kathara.manager.Kathara.Kathara.exec")
@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")
@mock.patch("src.Kathara.model.Lab.Lab")
@mock.patch('sys.stdout.write')
@mock.patch('sys.stderr.write')
def test_run_no_params(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_exec, exec_output):
def test_run_no_params(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_docker_manager,
mock_manager_get_instance, exec_output):
mock_parse_lab.return_value = mock_lab
mock_exec.return_value = exec_output
mock_manager_get_instance.return_value = mock_docker_manager
mock_docker_manager.exec.return_value = exec_output
command = ExecCommand()
command.run('.', ['pc1', 'test command'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_exec.assert_called_once_with("pc1", ['test command'], lab_hash=mock_lab.hash)
mock_docker_manager.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.manager.Kathara.Kathara.get_instance")
@mock.patch("src.Kathara.manager.docker.DockerManager.DockerManager")
@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_absolute_path(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_exec,
def test_run_with_directory_absolute_path(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab,
mock_docker_manager, mock_manager_get_instance,
exec_output):
mock_parse_lab.return_value = mock_lab
mock_exec.return_value = exec_output
mock_manager_get_instance.return_value = mock_docker_manager
mock_manager_get_instance.return_value = mock_docker_manager
mock_docker_manager.exec.return_value = exec_output
command = ExecCommand()
command.run('.', ['-d', '/test/path', 'pc1', 'test command'])
mock_parse_lab.assert_called_once_with('/test/path')
mock_exec.assert_called_once_with("pc1", ['test command'], lab_hash=mock_lab.hash)
mock_docker_manager.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.manager.Kathara.Kathara.get_instance")
@mock.patch("src.Kathara.manager.docker.DockerManager.DockerManager")
@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,
def test_run_with_directory_relative_path(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab,
mock_docker_manager, mock_manager_get_instance,
exec_output):
mock_parse_lab.return_value = mock_lab
mock_exec.return_value = exec_output
mock_manager_get_instance.return_value = mock_docker_manager
mock_docker_manager.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_docker_manager.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.manager.Kathara.Kathara.get_instance")
@mock.patch("src.Kathara.manager.docker.DockerManager.DockerManager")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
@mock.patch('sys.stdout.write')
@mock.patch('sys.stderr.write')
def test_run_with_v_option(mock_stderr_write, mock_stdout_write, mock_parse_lab, mock_exec, exec_output):
def test_run_with_v_option(mock_stderr_write, mock_stdout_write, mock_parse_lab, mock_docker_manager,
mock_manager_get_instance, exec_output):
mock_manager_get_instance.return_value = mock_docker_manager
lab = Lab('kathara_vlab')
mock_exec.return_value = exec_output
mock_docker_manager.exec.return_value = exec_output
command = ExecCommand()
command.run('.', ['-v', 'pc1', 'test command'])
assert not mock_parse_lab.called
mock_exec.assert_called_once_with("pc1", ['test command'], lab_hash=lab.hash)
mock_docker_manager.exec.assert_called_once_with("pc1", ['test command'], lab_hash=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.manager.Kathara.Kathara.get_instance")
@mock.patch("src.Kathara.manager.docker.DockerManager.DockerManager")
@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_no_stdout(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_exec, exec_output):
def test_run_no_stdout(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_docker_manager,
mock_manager_get_instance, exec_output):
mock_parse_lab.return_value = mock_lab
mock_exec.return_value = exec_output
mock_manager_get_instance.return_value = mock_docker_manager
mock_docker_manager.exec.return_value = exec_output
command = ExecCommand()
command.run('.', ['--no-stdout', 'pc1', 'test command'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_exec.assert_called_once_with("pc1", ['test command'], lab_hash=mock_lab.hash)
mock_docker_manager.exec.assert_called_once_with("pc1", ['test command'], lab_hash=mock_lab.hash)
assert not mock_stdout_write.called
mock_stderr_write.assert_called_once_with('stderr')


@mock.patch("src.Kathara.manager.Kathara.Kathara.exec")
@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")
@mock.patch("src.Kathara.model.Lab.Lab")
@mock.patch('sys.stdout.write')
@mock.patch('sys.stderr.write')
def test_run_no_stderr(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_exec, exec_output):
def test_run_no_stderr(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_docker_manager,
mock_manager_get_instance, exec_output):
mock_parse_lab.return_value = mock_lab
mock_exec.return_value = exec_output
mock_manager_get_instance.return_value = mock_docker_manager
mock_docker_manager.exec.return_value = exec_output
command = ExecCommand()
command.run('.', ['--no-stderr', 'pc1', 'test command'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_exec.assert_called_once_with("pc1", ['test command'], lab_hash=mock_lab.hash)
mock_docker_manager.exec.assert_called_once_with("pc1", ['test command'], lab_hash=mock_lab.hash)
mock_stdout_write.assert_called_once_with('stdout')
assert not mock_stderr_write.called


@mock.patch("src.Kathara.manager.Kathara.Kathara.exec")
@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")
@mock.patch("src.Kathara.model.Lab.Lab")
@mock.patch('sys.stdout.write')
@mock.patch('sys.stderr.write')
def test_run_no_stdout_no_stderr(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_exec,
def test_run_no_stdout_no_stderr(mock_stderr_write, mock_stdout_write, mock_lab, mock_parse_lab, mock_docker_manager,
mock_manager_get_instance,
exec_output):
mock_parse_lab.return_value = mock_lab
mock_exec.return_value = exec_output
mock_manager_get_instance.return_value = mock_docker_manager
mock_docker_manager.exec.return_value = exec_output
command = ExecCommand()
command.run('.', ['--no-stdout', '--no-stderr', 'pc1', 'test command'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_exec.assert_called_once_with("pc1", ['test command'], lab_hash=mock_lab.hash)
mock_docker_manager.exec.assert_called_once_with("pc1", ['test command'], lab_hash=mock_lab.hash)
assert not mock_stdout_write.called
assert not mock_stderr_write.called
32 changes: 20 additions & 12 deletions tests/cli/lclean_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,53 @@
from src.Kathara.cli.command.LcleanCommand import LcleanCommand


@mock.patch("src.Kathara.manager.Kathara.Kathara.undeploy_lab")
@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")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_no_params(mock_lab, mock_parse_lab, mock_undeploy_lab):
def test_run_no_params(mock_lab, mock_parse_lab, mock_docker_manager, mock_manager_get_instance):
mock_parse_lab.return_value = mock_lab
mock_manager_get_instance.return_value = mock_docker_manager
command = LcleanCommand()
command.run('.', [])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_undeploy_lab.assert_called_once_with(lab_hash=mock_lab.hash, selected_machines=None)
mock_docker_manager.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.manager.Kathara.Kathara.get_instance")
@mock.patch("src.Kathara.manager.docker.DockerManager.DockerManager")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_with_directory_absolute_path(mock_lab, mock_parse_lab, mock_undeploy_lab):
def test_run_with_directory_absolute_path(mock_lab, mock_parse_lab, mock_docker_manager, mock_manager_get_instance):
mock_parse_lab.return_value = mock_lab
mock_manager_get_instance.return_value = mock_docker_manager
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_docker_manager.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.manager.Kathara.Kathara.get_instance")
@mock.patch("src.Kathara.manager.docker.DockerManager.DockerManager")
@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):
def test_run_with_directory_relative_path(mock_lab, mock_parse_lab, mock_docker_manager, mock_manager_get_instance):
mock_parse_lab.return_value = mock_lab
mock_manager_get_instance.return_value = mock_docker_manager
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_docker_manager.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.manager.Kathara.Kathara.get_instance")
@mock.patch("src.Kathara.manager.docker.DockerManager.DockerManager")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_with_selected_machines(mock_lab, mock_parse_lab, mock_undeploy_lab):
def test_run_with_selected_machines(mock_lab, mock_parse_lab, mock_docker_manager, mock_manager_get_instance):
mock_parse_lab.return_value = mock_lab
mock_manager_get_instance.return_value = mock_docker_manager
command = LcleanCommand()
command.run('.', ['pc1', 'pc2'])
mock_parse_lab.assert_called_once_with(os.getcwd())
mock_undeploy_lab.assert_called_once_with(lab_hash=mock_lab.hash, selected_machines={'pc1', 'pc2'})
mock_docker_manager.undeploy_lab.assert_called_once_with(lab_hash=mock_lab.hash, selected_machines={'pc1', 'pc2'})
Loading

0 comments on commit bfb616e

Please sign in to comment.