[NFC] Cleanup the code used to run shell commands from tests

Previously we had 3 different method to run shell commands on the
target and 4 copy of code waiting until a given file appears on the
target device (used for syncronization). This CL merges these methods
to 1 run_platform_command and 1 wait_for_file_on_target functions
located in some utility classes.

Differential revision: http://reviews.llvm.org/D18789

llvm-svn: 265398
This commit is contained in:
Tamas Berghammer
2016-04-05 14:08:18 +00:00
parent d3fb38cae5
commit 19fc1d4e3c
6 changed files with 34 additions and 71 deletions

View File

@@ -19,12 +19,6 @@ class AttachDeniedTestCase(TestBase):
mydir = TestBase.compute_mydir(__file__)
def run_platform_command(self, cmd):
platform = self.dbg.GetSelectedPlatform()
shell_command = lldb.SBPlatformShellCommand(cmd)
err = platform.Run(shell_command)
return (err, shell_command.GetStatus(), shell_command.GetOutput())
@skipIfWindows
@skipIfiOSSimulator
def test_attach_to_process_by_id_denied(self):
@@ -41,21 +35,7 @@ class AttachDeniedTestCase(TestBase):
popen = self.spawnSubprocess(exe, [pid_file_path])
self.addTearDownHook(self.cleanupSubprocesses)
max_attempts = 5
for i in range(max_attempts):
err, retcode, msg = self.run_platform_command("ls %s" % pid_file_path)
if err.Success() and retcode == 0:
break
else:
print(msg)
if i < max_attempts:
# Exponential backoff!
time.sleep(pow(2, i) * 0.25)
else:
self.fail("Child PID file %s not found even after %d attempts." % (pid_file_path, max_attempts))
err, retcode, pid = self.run_platform_command("cat %s" % (pid_file_path))
self.assertTrue(err.Success() and retcode == 0,
"Failed to read file %s: %s, retcode: %d" % (pid_file_path, err.GetCString(), retcode))
pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
self.expect('process attach -p ' + pid,
startstr = 'error: attach failed:',

View File

@@ -36,23 +36,7 @@ class ChangeProcessGroupTestCase(TestBase):
popen = self.spawnSubprocess(exe, [pid_file_path])
self.addTearDownHook(self.cleanupSubprocesses)
max_attempts = 5
for i in range(max_attempts):
err, retcode, msg = self.run_platform_command("ls %s" % pid_file_path)
if err.Success() and retcode == 0:
break
else:
print(msg)
if i < max_attempts:
# Exponential backoff!
time.sleep(pow(2, i) * 0.30)
else:
self.fail("Child PID file %s not found even after %d attempts." % (pid_file_path, max_attempts))
err, retcode, pid = self.run_platform_command("cat %s" % (pid_file_path))
self.assertTrue(err.Success() and retcode == 0,
"Failed to read file %s: %s, retcode: %d" % (pid_file_path, err.GetCString(), retcode))
pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
# make sure we cleanup the forked child also
def cleanupChild():
@@ -100,9 +84,3 @@ class ChangeProcessGroupTestCase(TestBase):
# run to completion
process.Continue()
self.assertEqual(process.GetState(), lldb.eStateExited)
def run_platform_command(self, cmd):
platform = self.dbg.GetSelectedPlatform()
shell_command = lldb.SBPlatformShellCommand(cmd)
err = platform.Run(shell_command)
return (err, shell_command.GetStatus(), shell_command.GetOutput())

View File

@@ -1946,6 +1946,12 @@ class TestBase(Base):
else:
self.fail("Can't build for debug info: %s" % self.debug_info)
def run_platform_command(self, cmd):
platform = self.dbg.GetSelectedPlatform()
shell_command = lldb.SBPlatformShellCommand(cmd)
err = platform.Run(shell_command)
return (err, shell_command.GetStatus(), shell_command.GetOutput())
# =================================================
# Misc. helper methods for debugging test execution
# =================================================

View File

@@ -1029,3 +1029,20 @@ def skip_if_library_missing(test, target, library):
def find_library_callable(test):
return find_library(target, library)
return skip_if_callable(test, find_library_callable, "could not find library matching '%s' in target %s" % (library, target))
def wait_for_file_on_target(testcase, file_path, max_attempts = 6):
for i in range(max_attempts):
err, retcode, msg = testcase.run_platform_command("ls %s" % file_path)
if err.Success() and retcode == 0:
break
if i < max_attempts:
# Exponential backoff!
time.sleep(pow(2, i) * 0.25)
else:
testcase.fail("File %s not found even after %d attempts." % (file_path, max_attempts))
err, retcode, data = testcase.run_platform_command("cat %s" % (file_path))
testcase.assertTrue(err.Success() and retcode == 0,
"Failed to read file %s: %s, retcode: %d" % (file_path, err.GetCString(), retcode))
return data

View File

@@ -214,30 +214,21 @@ class GdbRemoteTestCaseBase(TestBase):
return stub_port
def run_shell_cmd(self, cmd):
platform = self.dbg.GetSelectedPlatform()
shell_cmd = lldb.SBPlatformShellCommand(cmd)
err = platform.Run(shell_cmd)
if err.Fail() or shell_cmd.GetStatus():
m = "remote_platform.RunShellCommand('%s') failed:\n" % cmd
m += ">>> return code: %d\n" % shell_cmd.GetStatus()
if err.Fail():
m += ">>> %s\n" % str(err).strip()
m += ">>> %s\n" % (shell_cmd.GetOutput() or
"Command generated no output.")
raise Exception(m)
return shell_cmd.GetOutput().strip()
def init_llgs_test(self, use_named_pipe=True):
if lldb.remote_platform:
# Remote platforms don't support named pipe based port negotiation
use_named_pipe = False
# Grab the ppid from /proc/[shell pid]/stat
shell_stat = self.run_shell_cmd("cat /proc/$$/stat")
err, retcode, shell_stat = self.run_platform_command("cat /proc/$$/stat")
self.assertTrue(err.Success() and retcode == 0,
"Failed to read file /proc/$$/stat: %s, retcode: %d" % (err.GetCString(), retcode))
# [pid] ([executable]) [state] [*ppid*]
pid = re.match(r"^\d+ \(.+\) . (\d+)", shell_stat).group(1)
ls_output = self.run_shell_cmd("ls -l /proc/%s/exe" % pid)
err, retcode, ls_output = self.run_platform_command("ls -l /proc/%s/exe" % pid)
self.assertTrue(err.Success() and retcode == 0,
"Failed to read file /proc/%s/exe: %s, retcode: %d" % (pid, err.GetCString(), retcode))
exe = ls_output.split()[-1]
# If the binary has been deleted, the link name has " (deleted)" appended.
@@ -346,12 +337,6 @@ class GdbRemoteTestCaseBase(TestBase):
commandline_args += ["--named-pipe", self.named_pipe_path]
return commandline_args
def run_platform_command(self, cmd):
platform = self.dbg.GetSelectedPlatform()
shell_command = lldb.SBPlatformShellCommand(cmd)
err = platform.Run(shell_command)
return (err, shell_command.GetOutput())
def launch_debug_monitor(self, attach_pid=None, logfile=None):
# Create the command line.
commandline_args = self.get_debug_monitor_command_line_args(attach_pid=attach_pid)

View File

@@ -38,11 +38,8 @@ class TestPlatformProcessConnect(gdbremote_testcase.GdbRemoteTestCaseBase):
commandline_args = ["platform", "--listen", listen_url, "--socket-file", port_file, "--", "%s/a.out" % working_dir, "foo"]
self.spawnSubprocess(self.debug_monitor_exe, commandline_args, install_remote=False)
self.addTearDownHook(self.cleanupSubprocesses)
# Wait until the port_file have been created. Doing it with 1 shell command will fail because
# of a bug in LLDB shell escaping code
_, _ = self.run_platform_command("while [ ! -f %s ]; do sleep 0.25; done" % port_file)
_, socket_id = self.run_platform_command("cat %s" % port_file)
socket_id = lldbutil.wait_for_file_on_target(self, port_file)
new_debugger = lldb.SBDebugger.Create()
new_debugger.SetAsync(False)