From 9d8425e0a0ddacfbd041afeb6c6e54fa3c52ed45 Mon Sep 17 00:00:00 2001 From: Rahix Date: Thu, 13 Apr 2023 14:06:49 +0200 Subject: [PATCH] selftest: contrib: Add some GDB startup tests These tests should catch a couple of edge-cases of GDB startup where tbot is supposed to properly throw a certain exception. Signed-off-by: Harald Seiler --- selftest/tests/contrib/test_gdb.py | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/selftest/tests/contrib/test_gdb.py b/selftest/tests/contrib/test_gdb.py index f1b33c4e..0b426537 100644 --- a/selftest/tests/contrib/test_gdb.py +++ b/selftest/tests/contrib/test_gdb.py @@ -1,14 +1,48 @@ import re +from typing import Tuple, Type import pytest import testmachines import tbot import tbot_contrib.gdb +from tbot.machine import linux RESULT_PATTERN = re.compile(r"\$\d+ = 0x[0-9a-f]+ (?:<.*> )?\"(.*)\"") +@pytest.mark.parametrize( # type: ignore + "failure_mode", + [ + ("true", linux.CommandEndedException), + ("false", tbot.error.CommandFailure), + ], + ids=lambda failure_mode: failure_mode[1].__name__, +) +def test_gdb_start_crash( + tbot_context: tbot.Context, failure_mode: Tuple[str, Type[Exception]] +) -> None: + command, exception = failure_mode + with tbot_context.request(testmachines.Localhost) as lh: + with pytest.raises(exception): + # Run `echo` instead of GDB to force an initialization error + with tbot_contrib.gdb.GDB(lh, gdb=command) as gdb: + gdb.exec("help") + + lh.exec0("uname", "-r") + + +def test_gdb_init_hang(tbot_context: tbot.Context) -> None: + with tbot_context.request(testmachines.Localhost) as lh: + with pytest.raises(TimeoutError): + # Run `echo` instead of GDB to force and hack in a sleep + # to let the initialization hang forever + with tbot_contrib.gdb.GDB(lh, linux.Then, "sleep", "30", gdb="echo") as gdb: # type: ignore + gdb.exec("help") + + lh.exec0("uname", "-r") + + def test_gdb_machine(tbot_context: tbot.Context) -> None: with tbot_context.request(testmachines.Localhost) as lh: program = lh.exec0("which", "echo").strip()