Skip to content

Commit

Permalink
[consutil][test] Add unit tests for consutil clear command (sonic-net…
Browse files Browse the repository at this point in the history
…#1274)

* [consutil] Add UT for consutil clear command
* Fix test cases

Signed-off-by: Jing Kan [email protected]
  • Loading branch information
Blueve authored Dec 1, 2020
1 parent a4f663e commit 6fabbed
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions tests/console_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ""

0 comments on commit 6fabbed

Please sign in to comment.