diff options
Diffstat (limited to 'src/pybind/mgr/dashboard/tests/test_nvmeof_cli.py')
-rw-r--r-- | src/pybind/mgr/dashboard/tests/test_nvmeof_cli.py | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/pybind/mgr/dashboard/tests/test_nvmeof_cli.py b/src/pybind/mgr/dashboard/tests/test_nvmeof_cli.py new file mode 100644 index 00000000000..b17940bb0cb --- /dev/null +++ b/src/pybind/mgr/dashboard/tests/test_nvmeof_cli.py @@ -0,0 +1,87 @@ +import errno +from unittest.mock import MagicMock + +import pytest +from mgr_module import CLICommand, HandleCommandResult + +from ..services.nvmeof_cli import NvmeofCLICommand + + +@pytest.fixture(scope="class", name="sample_command") +def fixture_sample_command(): + test_cmd = "test command" + + @NvmeofCLICommand(test_cmd) + def func(_): # noqa # pylint: disable=unused-variable + return {'a': '1', 'b': 2} + yield test_cmd + del NvmeofCLICommand.COMMANDS[test_cmd] + assert test_cmd not in NvmeofCLICommand.COMMANDS + + +@pytest.fixture(name='base_call_mock') +def fixture_base_call_mock(monkeypatch): + mock_result = {'a': 'b'} + super_mock = MagicMock() + super_mock.return_value = mock_result + monkeypatch.setattr(CLICommand, 'call', super_mock) + return super_mock + + +@pytest.fixture(name='base_call_return_none_mock') +def fixture_base_call_return_none_mock(monkeypatch): + mock_result = None + super_mock = MagicMock() + super_mock.return_value = mock_result + monkeypatch.setattr(CLICommand, 'call', super_mock) + return super_mock + + +class TestNvmeofCLICommand: + def test_command_being_added(self, sample_command): + assert sample_command in NvmeofCLICommand.COMMANDS + assert isinstance(NvmeofCLICommand.COMMANDS[sample_command], NvmeofCLICommand) + + def test_command_return_cmd_result_default_format(self, base_call_mock, sample_command): + result = NvmeofCLICommand.COMMANDS[sample_command].call(MagicMock(), {}) + assert isinstance(result, HandleCommandResult) + assert result.retval == 0 + assert result.stdout == '{"a": "b"}' + assert result.stderr == '' + base_call_mock.assert_called_once() + + def test_command_return_cmd_result_json_format(self, base_call_mock, sample_command): + result = NvmeofCLICommand.COMMANDS[sample_command].call(MagicMock(), {'format': 'json'}) + assert isinstance(result, HandleCommandResult) + assert result.retval == 0 + assert result.stdout == '{"a": "b"}' + assert result.stderr == '' + base_call_mock.assert_called_once() + + def test_command_return_cmd_result_yaml_format(self, base_call_mock, sample_command): + result = NvmeofCLICommand.COMMANDS[sample_command].call(MagicMock(), {'format': 'yaml'}) + assert isinstance(result, HandleCommandResult) + assert result.retval == 0 + assert result.stdout == 'a: b\n' + assert result.stderr == '' + base_call_mock.assert_called_once() + + def test_command_return_cmd_result_invalid_format(self, base_call_mock, sample_command): + mock_result = {'a': 'b'} + super_mock = MagicMock() + super_mock.call.return_value = mock_result + + result = NvmeofCLICommand.COMMANDS[sample_command].call(MagicMock(), {'format': 'invalid'}) + assert isinstance(result, HandleCommandResult) + assert result.retval == -errno.EINVAL + assert result.stdout == '' + assert result.stderr + base_call_mock.assert_called_once() + + def test_command_return_empty_cmd_result(self, base_call_return_none_mock, sample_command): + result = NvmeofCLICommand.COMMANDS[sample_command].call(MagicMock(), {}) + assert isinstance(result, HandleCommandResult) + assert result.retval == 0 + assert result.stdout == '' + assert result.stderr == '' + base_call_return_none_mock.assert_called_once() |