From 07e462526d0cbae40b320e1a4307ce11e197fb0a Mon Sep 17 00:00:00 2001 From: Louis Dionne Date: Tue, 31 Mar 2020 12:09:20 -0400 Subject: [PATCH] [libc++] Allow running .sh.cpp tests with SSHExecutors This commit adds a script that can be used as an %{exec} substitution such that .sh.cpp tests can now run on remote hosts when using the SSHExecutor. --- libcxx/test/libcxx/double_include.sh.cpp | 1 + .../support.dynamic/libcpp_deallocate.sh.cpp | 1 + libcxx/test/libcxx/selftest/exec.sh.cpp | 2 + libcxx/test/libcxx/selftest/test.sh.cpp | 2 + .../libcxx/strings/basic.string/PR42676.sh.cpp | 1 + .../map/PR28469_undefined_behavior_segfault.sh.cpp | 1 + .../wait_terminates.sh.cpp | 1 + libcxx/utils/libcxx/test/config.py | 20 +++-- libcxx/utils/libcxx/test/format.py | 6 +- libcxx/utils/ssh.py | 90 ++++++++++++++++++++++ 10 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 libcxx/utils/ssh.py diff --git a/libcxx/test/libcxx/double_include.sh.cpp b/libcxx/test/libcxx/double_include.sh.cpp index b2d475c..17236df 100644 --- a/libcxx/test/libcxx/double_include.sh.cpp +++ b/libcxx/test/libcxx/double_include.sh.cpp @@ -9,6 +9,7 @@ // Test that we can include each header in two TU's and link them together. +// FILE_DEPENDENCIES: %t.exe // RUN: %{cxx} -c %s -o %t.first.o %{flags} %{compile_flags} // RUN: %{cxx} -c %s -o %t.second.o -DWITH_MAIN %{flags} %{compile_flags} // RUN: %{cxx} -o %t.exe %t.first.o %t.second.o %{flags} %{link_flags} diff --git a/libcxx/test/libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp b/libcxx/test/libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp index 7c6692d4..da5f3b6 100644 --- a/libcxx/test/libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp +++ b/libcxx/test/libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp @@ -39,6 +39,7 @@ // GCC doesn't support the aligned-allocation flags. // XFAIL: gcc +// FILE_DEPENDENCIES: %t.exe // RUN: %{build} -faligned-allocation -fsized-deallocation // RUN: %{run} // RUN: %{build} -faligned-allocation -fno-sized-deallocation -DNO_SIZE diff --git a/libcxx/test/libcxx/selftest/exec.sh.cpp b/libcxx/test/libcxx/selftest/exec.sh.cpp index 19c3d59..de42de3 100644 --- a/libcxx/test/libcxx/selftest/exec.sh.cpp +++ b/libcxx/test/libcxx/selftest/exec.sh.cpp @@ -7,6 +7,8 @@ // //===----------------------------------------------------------------------===// +// FILE_DEPENDENCIES: %t.exe + // RUN: %{build} // RUN: %{exec} %t.exe "HELLO" diff --git a/libcxx/test/libcxx/selftest/test.sh.cpp b/libcxx/test/libcxx/selftest/test.sh.cpp index 5c4db16..ad75f02 100644 --- a/libcxx/test/libcxx/selftest/test.sh.cpp +++ b/libcxx/test/libcxx/selftest/test.sh.cpp @@ -7,6 +7,8 @@ // //===----------------------------------------------------------------------===// +// FILE_DEPENDENCIES: %t.exe + // RUN: %{build} // RUN: %{run} diff --git a/libcxx/test/libcxx/strings/basic.string/PR42676.sh.cpp b/libcxx/test/libcxx/strings/basic.string/PR42676.sh.cpp index 31bf81e..18faaf0 100644 --- a/libcxx/test/libcxx/strings/basic.string/PR42676.sh.cpp +++ b/libcxx/test/libcxx/strings/basic.string/PR42676.sh.cpp @@ -8,6 +8,7 @@ // Regression test for PR42676. +// FILE_DEPENDENCIES: %t.exe // RUN: %{cxx} %{flags} %s -o %t.exe %{compile_flags} %{link_flags} -D_LIBCPP_HIDE_FROM_ABI_PER_TU // RUN: %{run} diff --git a/libcxx/test/std/containers/associative/map/PR28469_undefined_behavior_segfault.sh.cpp b/libcxx/test/std/containers/associative/map/PR28469_undefined_behavior_segfault.sh.cpp index 4bd33ce..83aa36d 100644 --- a/libcxx/test/std/containers/associative/map/PR28469_undefined_behavior_segfault.sh.cpp +++ b/libcxx/test/std/containers/associative/map/PR28469_undefined_behavior_segfault.sh.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +// FILE_DEPENDENCIES: %t.exe // RUN: %{build} -O2 // RUN: %{run} diff --git a/libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_terminates.sh.cpp b/libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_terminates.sh.cpp index df02f7a..0ee0291 100644 --- a/libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_terminates.sh.cpp +++ b/libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_terminates.sh.cpp @@ -13,6 +13,7 @@ // class condition_variable_any; +// FILE_DEPENDENCIES: %t.exe // RUN: %{build} // RUN: %{run} 1 // RUN: %{run} 2 diff --git a/libcxx/utils/libcxx/test/config.py b/libcxx/utils/libcxx/test/config.py index b593707..9c154ba 100644 --- a/libcxx/utils/libcxx/test/config.py +++ b/libcxx/utils/libcxx/test/config.py @@ -1038,13 +1038,21 @@ class Configuration(object): # Configure exec prefix substitutions. # Configure run env substitution. codesign_ident = self.get_lit_conf('llvm_codesign_identity', '') - run_py = os.path.join(self.libcxx_src_root, 'utils', 'run.py') env_vars = ' '.join('%s=%s' % (k, pipes.quote(v)) for (k, v) in self.exec_env.items()) - exec_str = '%s %s --codesign_identity "%s" --working_directory "%%S" ' \ - '--dependencies %%{file_dependencies} --env %s -- ' % \ - (pipes.quote(sys.executable), pipes.quote(run_py), - codesign_ident, env_vars) - sub.append(('%{exec}', exec_str)) + exec_args = [ + '--codesign_identity "{}"'.format(codesign_ident), + '--dependencies %{file_dependencies}', + '--env {}'.format(env_vars) + ] + if isinstance(self.executor, SSHExecutor): + exec_args.append('--host {}'.format(self.executor.user_prefix + self.executor.host)) + executor = os.path.join(self.libcxx_src_root, 'utils', 'ssh.py') + else: + exec_args.append('--working_directory "%S"') + executor = os.path.join(self.libcxx_src_root, 'utils', 'run.py') + sub.append(('%{exec}', '{} {} {} -- '.format(pipes.quote(sys.executable), + pipes.quote(executor), + ' '.join(exec_args)))) sub.append(('%{run}', '%{exec} %t.exe')) # Configure not program substitutions not_py = os.path.join(self.libcxx_src_root, 'utils', 'not.py') diff --git a/libcxx/utils/libcxx/test/format.py b/libcxx/utils/libcxx/test/format.py index a9eb7d9..ff23273 100644 --- a/libcxx/utils/libcxx/test/format.py +++ b/libcxx/utils/libcxx/test/format.py @@ -18,6 +18,7 @@ from lit.TestRunner import ParserKind, IntegratedTestKeywordParser \ # pylint: disable=import-error from libcxx.test.executor import LocalExecutor as LocalExecutor +from libcxx.test.executor import SSHExecutor as SSHExecutor import libcxx.util @@ -168,8 +169,9 @@ class LibcxxTestFormat(object): # Dispatch the test based on its suffix. if is_sh_test: - if not isinstance(self.executor, LocalExecutor): - # We can't run ShTest tests with a executor yet. + if not isinstance(self.executor, LocalExecutor) and not isinstance(self.executor, SSHExecutor): + # We can't run ShTest tests with other executors than + # LocalExecutor and SSHExecutor yet. # For now, bail on trying to run them return lit.Test.UNSUPPORTED, 'ShTest format not yet supported' test.config.environment = self.executor.merge_environments(os.environ, self.exec_env) diff --git a/libcxx/utils/ssh.py b/libcxx/utils/ssh.py new file mode 100644 index 0000000..20acaeb --- /dev/null +++ b/libcxx/utils/ssh.py @@ -0,0 +1,90 @@ +#===----------------------------------------------------------------------===## +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +#===----------------------------------------------------------------------===## + +""" +Runs an executable on a remote host. + +This is meant to be used as an executor when running the C++ Standard Library +conformance test suite. +""" + +import argparse +import os +import subprocess +import sys + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--host', type=str, required=True) + parser.add_argument('--codesign_identity', type=str, required=False) + parser.add_argument('--dependencies', type=str, nargs='*', required=True) + parser.add_argument('--env', type=str, nargs='*', required=True) + (args, remaining) = parser.parse_known_args(sys.argv[1:]) + + if len(remaining) < 2: + sys.stderr.write('Missing actual commands to run') + exit(1) + remaining = remaining[1:] # Skip the '--' + + # HACK: + # If the first argument is a file that ends in `.tmp.exe`, assume it is + # the name of an executable generated by a test file. This allows us to + # do custom processing like codesigning the executable and changing its + # path when running on the remote host. It's possible for there to be no + # such executable, for example in the case of a .sh.cpp test. + exe = None + if os.path.exists(remaining[0]) and remaining[0].endswith('.tmp.exe'): + exe = remaining.pop(0) + + # If there's an executable, do any necessary codesigning. + if exe and args.codesign_identity: + rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={}) + if rc != 0: + sys.stderr.write('Failed to codesign: {}'.format(exe)) + return rc + + ssh = lambda command: ['ssh', '-oBatchMode=yes', args.host, command] + scp = lambda src, dst: ['scp', '-oBatchMode=yes', '-r', src, '{}:{}'.format(args.host, dst)] + + # Create a temporary directory where the test will be run + tmp = subprocess.check_output(ssh('mktemp -d /tmp/libcxx.XXXXXXXXXX')).strip() + + # Ensure the test dependencies exist and scp them to the temporary directory. + # Test dependencies can be either files or directories, so the `scp` command + # needs to use `-r`. + for dep in args.dependencies: + if not os.path.exists(dep): + sys.stderr.write('Missing file or directory {} marked as a dependency of a test'.format(dep)) + exit(1) + subprocess.call(scp(dep, tmp)) + + # If there's an executable, change its path to be in the temporary directory. + # We know it has been copied to the remote host when we handled the test + # dependencies above. + if exe: + exe = os.path.join(tmp, os.path.basename(exe)) + + # If there's an executable, make sure it has 'execute' permissions on the + # remote host. The host that compiled the executable might not have a notion + # of 'executable' permissions. + if exe: + subprocess.call(ssh('chmod +x {}'.format(exe))) + + # Execute the command through SSH in the temporary directory, with the + # correct environment. + command = [exe] + remaining if exe else remaining + res = subprocess.call(ssh('cd {} && env -i {} {}'.format(tmp, ' '.join(args.env), ' '.join(command)))) + + # Remove the temporary directory when we're done. + subprocess.call(ssh('rm -r {}'.format(tmp))) + + return res + +if __name__ == '__main__': + exit(main()) -- 2.7.4