From 6fabbedc8ba0f7961bec3ec0adf7bb9d5c71eb2b Mon Sep 17 00:00:00 2001 From: Blueve <672454911@qq.com> Date: Tue, 1 Dec 2020 10:30:01 +0800 Subject: [PATCH] [consutil][test] Add unit tests for consutil clear command (#1274) * [consutil] Add UT for consutil clear command * Fix test cases Signed-off-by: Jing Kan jika@microsoft.com --- tests/console_test.py | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tests/console_test.py b/tests/console_test.py index 03887cf42a66..6152c0d0dbe4 100644 --- a/tests/console_test.py +++ b/tests/console_test.py @@ -578,3 +578,64 @@ def test_connect_success(self): print(sys.stderr, result.output) assert result.exit_code == 0 assert result.output == "Successful connection to line [1]\nPress ^A ^X to disconnect\n" + +class TestConsutilClear(object): + @classmethod + def setup_class(cls): + print("SETUP") + + @mock.patch('consutil.lib.SysInfoProvider.list_console_ttys', mock.MagicMock(return_value=["/dev/ttyUSB1"])) + @mock.patch('consutil.lib.SysInfoProvider.init_device_prefix', mock.MagicMock(return_value=None)) + @mock.patch('os.geteuid', mock.MagicMock(return_value=1)) + def test_clear_without_root(self): + runner = CliRunner() + db = Db() + + result = runner.invoke(consutil.consutil.commands["clear"], ['1'], obj=db) + print(result.exit_code) + print(sys.stderr, result.output) + assert result.exit_code == 2 + assert "Root privileges are required for this operation" in result.output + + @mock.patch('consutil.lib.SysInfoProvider.list_console_ttys', mock.MagicMock(return_value=["/dev/ttyUSB1"])) + @mock.patch('consutil.lib.SysInfoProvider.init_device_prefix', mock.MagicMock(return_value=None)) + @mock.patch('os.geteuid', mock.MagicMock(return_value=0)) + def test_clear_line_not_found(self): + runner = CliRunner() + db = Db() + + result = runner.invoke(consutil.consutil.commands["clear"], ['2'], obj=db) + print(result.exit_code) + print(sys.stderr, result.output) + assert result.exit_code == 3 + assert "Target [2] does not exist" in result.output + + @mock.patch('consutil.lib.SysInfoProvider.list_console_ttys', mock.MagicMock(return_value=["/dev/ttyUSB1"])) + @mock.patch('consutil.lib.SysInfoProvider.init_device_prefix', mock.MagicMock(return_value=None)) + @mock.patch('os.geteuid', mock.MagicMock(return_value=0)) + @mock.patch('consutil.lib.ConsolePortInfo.clear_session', mock.MagicMock(return_value=False)) + def test_clear_idle(self): + runner = CliRunner() + db = Db() + db.cfgdb.set_entry("CONSOLE_PORT", 1, { "remote_device" : "switch1", "baud_rate" : "9600" }) + + result = runner.invoke(consutil.consutil.commands["clear"], ['1'], obj=db) + print(result.exit_code) + print(sys.stderr, result.output) + assert result.exit_code == 0 + assert "No process is connected to line 1" in result.output + + @mock.patch('consutil.lib.SysInfoProvider.list_console_ttys', mock.MagicMock(return_value=["/dev/ttyUSB1"])) + @mock.patch('consutil.lib.SysInfoProvider.init_device_prefix', mock.MagicMock(return_value=None)) + @mock.patch('os.geteuid', mock.MagicMock(return_value=0)) + @mock.patch('consutil.lib.ConsolePortInfo.clear_session', mock.MagicMock(return_value=True)) + def test_clear_success(self): + runner = CliRunner() + db = Db() + db.cfgdb.set_entry("CONSOLE_PORT", 1, { "remote_device" : "switch1", "baud_rate" : "9600" }) + + result = runner.invoke(consutil.consutil.commands["clear"], ['1'], obj=db) + print(result.exit_code) + print(sys.stderr, result.output) + assert result.exit_code == 0 + assert result.output == ""