From d861d6e2fca0b4b9115275499adc48e998e31cb7 Mon Sep 17 00:00:00 2001 From: Disconnect3d Date: Sun, 10 Oct 2021 03:54:03 +0200 Subject: [PATCH] Skip attachp tests when cant attach --- tests/test_attachp.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_attachp.py b/tests/test_attachp.py index 770b26a45..0dfa5836c 100644 --- a/tests/test_attachp.py +++ b/tests/test_attachp.py @@ -10,6 +10,17 @@ import tests from .utils import run_gdb_with_script +can_attach = False + +if os.getuid() == 0: + can_attach = True +else: + # see `man ptrace` + with open('/proc/sys/kernel/yama/ptrace_scope') as f: + can_attach = f.read() == '0' + +REASON_CANNOT_ATTACH = 'Test skipped due to inability to attach (needs sudo or sysctl -w kernel.yama.ptrace_scope=0' + @pytest.fixture def launched_bash_binary(): @@ -25,6 +36,7 @@ def launched_bash_binary(): os.remove(path) # Cleanup +@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) def test_attachp_command_attaches_to_procname(launched_bash_binary): pid, binary_path = launched_bash_binary @@ -36,6 +48,8 @@ def test_attachp_command_attaches_to_procname(launched_bash_binary): assert re.search(r'Detaching from program: %s, process %s' % (binary_path, pid), result) + +@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) def test_attachp_command_attaches_to_pid(launched_bash_binary): pid, binary_path = launched_bash_binary @@ -46,6 +60,8 @@ def test_attachp_command_attaches_to_pid(launched_bash_binary): assert re.search(r'Detaching from program: %s, process %s' % (binary_path, pid), result) + +@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) def test_attachp_command_attaches_to_procname_too_many_pids(launched_bash_binary): pid, binary_path = launched_bash_binary @@ -65,10 +81,13 @@ def test_attachp_command_attaches_to_procname_too_many_pids(launched_bash_binary assert matches == expected_pids + +@pytest.mark.skipif(can_attach == False, reason=REASON_CANNOT_ATTACH) def test_attachp_command_nonexistent_procname(): result = run_gdb_with_script(pyafter='attachp some-nonexistent-process-name') # No chance there is a process name like this assert 'Process some-nonexistent-process-name not found' in result + def test_attachp_command_no_pids(): result = run_gdb_with_script(pyafter='attachp 99999999') # No chance there is a PID like this assert 'Error: ptrace: No such process.' in result