Skip to content

Commit

Permalink
Add cli/exec_command_test (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcaiazzi committed Jan 30, 2023
1 parent ea6e98e commit 216a73a
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 11 deletions.
22 changes: 11 additions & 11 deletions tests/cli/connect_command_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
from unittest import mock

Expand All @@ -13,8 +14,8 @@
def test_run_no_params(mock_lab, mock_parse_lab, mock_connect_tty):
mock_parse_lab.return_value = mock_lab
command = ConnectCommand()
command.run('test', ['pc1'])
mock_parse_lab.assert_called_once()
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)


Expand All @@ -24,7 +25,7 @@ def test_run_no_params(mock_lab, mock_parse_lab, mock_connect_tty):
def test_run_with_directory(mock_lab, mock_parse_lab, mock_connect_tty):
mock_parse_lab.return_value = mock_lab
command = ConnectCommand()
command.run('test', ['-d', '/test/path', 'pc1'])
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)

Expand All @@ -35,8 +36,8 @@ def test_run_with_directory(mock_lab, mock_parse_lab, mock_connect_tty):
def test_run_with_logs(mock_lab, mock_parse_lab, mock_connect_tty):
mock_parse_lab.return_value = mock_lab
command = ConnectCommand()
command.run('test', ['--logs', 'pc1'])
mock_parse_lab.assert_called_once()
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)


Expand All @@ -46,19 +47,18 @@ def test_run_with_logs(mock_lab, mock_parse_lab, mock_connect_tty):
def test_run_with_shell(mock_lab, mock_parse_lab, mock_connect_tty):
mock_parse_lab.return_value = mock_lab
command = ConnectCommand()
command.run('test', ['--shell', '/custom/shell', 'pc1'])
mock_parse_lab.assert_called_once()
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.patch("src.Kathara.manager.Kathara.Kathara.connect_tty")
@mock.patch("src.Kathara.parser.netkit.LabParser.LabParser.parse")
@mock.patch("src.Kathara.model.Lab.Lab")
def test_run_with_v_option(mock_lab, mock_parse_lab, mock_connect_tty):
def test_run_with_v_option(mock_parse_lab, mock_connect_tty):
lab = Lab('kathara_vlab')
command = ConnectCommand()
command.run('test', ['-v', 'pc1'])
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)
Expand All @@ -70,7 +70,7 @@ def test_run_with_v_option(mock_lab, mock_parse_lab, mock_connect_tty):
def test_run_all_params(mock_lab, mock_parse_lab, mock_connect_tty):
mock_parse_lab.return_value = mock_lab
command = ConnectCommand()
command.run('test', ['-d', '/test/path', '--logs', '--shell', '/custom/shell', 'pc1'])
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)
111 changes: 111 additions & 0 deletions tests/cli/exec_command_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import os
import sys
from unittest import mock

import pytest

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

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


@pytest.fixture
def exec_output():
def output_generator():
yield 'stdout'.encode(), 'stderr'.encode()
return output_generator()

@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_no_params(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('.', ['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_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("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):
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('/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')
@mock.patch('sys.stderr.write')
def test_run_with_v_option(mock_stderr_write, mock_stdout_write, mock_parse_lab, mock_exec, exec_output):
lab = Lab('kathara_vlab')
mock_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_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("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):
mock_parse_lab.return_value = mock_lab
mock_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)
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.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):
mock_parse_lab.return_value = mock_lab
mock_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_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.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, exec_output):
mock_parse_lab.return_value = mock_lab
mock_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)
assert not mock_stdout_write.called
assert not mock_stderr_write.called

0 comments on commit 216a73a

Please sign in to comment.