Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / scripts / tests / happy_test_wrapper.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2020 Project CHIP Authors
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # This is a wrapper for running happy tests.
18
19 import argparse
20 import os
21 import subprocess
22 import sys
23 import tempfile
24
25 CHIP_PATH = os.path.realpath(os.path.join(
26     os.path.dirname(__file__), "../.."))
27 HAPPY_TEST_PATH = os.path.join(CHIP_PATH, "src/test_driver/happy/tests")
28
29 test_environ = os.environ.copy()
30
31 parser = argparse.ArgumentParser(description='Warpper to run happy tests')
32 parser.add_argument('test_script', type=str,
33                     help='The name of test')
34 parser.add_argument('--test-bin-dir', dest='bin_dir', type=str, nargs='?', default='.',
35                     help='The path of test binaries')
36 parser.add_argument('--ci', dest='ci', type=bool, nargs='?', default=False,
37                     help='Set this if running script under venv but happy is installed globally')
38 parser.add_argument('--silent', dest='silent', type=bool, nargs='?', default=False,
39                     help='Set this will mute output when the test finished successfully')
40
41 if __name__ == '__main__':
42     if os.getuid() != 0:
43         os.execvpe("unshare", ["unshare", "--map-root-user",
44                                "-n", "-m", "python3"] + sys.argv, test_environ)
45         print("Failed to run script in new user namespace", file=sys.stderr)
46         exit(1)
47     if os.system("mount --make-private /") != 0 or os.system("mount -t tmpfs tmpfs /run") != 0:
48         print("Failed to setup private mount points", file=sys.stderr)
49         exit(1)
50     args = parser.parse_args()
51     # GN will run Python in venv, which will break happy test
52     if args.ci:
53         test_environ["HAPPY_LOG_DIR"] = "/tmp/happy_test_logs"
54     test_environ["TEST_BIN_DIR"] = args.bin_dir
55     test_environ["HAPPY_MAIN_CONFIG_FILE"] = os.path.realpath(
56         os.path.join(CHIP_PATH, "src/test_driver/happy/conf/main_conf.json"))
57     if args.silent:
58         fp, fname = tempfile.mkstemp()
59         run_res = subprocess.run(["python3", args.test_script],
60                                  stdout=fp, stderr=fp, env=test_environ)
61         if run_res.returncode != 0:
62             with open(fname, 'rb') as test_output:
63                 os.write(sys.stderr.fileno(), test_output.read())
64         exit(run_res.returncode)
65     else:
66         os.execvpe("python3", ["python3", args.test_script], test_environ)