CI: Add Bazel coverage

This CL adds a bot to the CI that tests bazel builds.
It also adds a tools/run_test_like_ci script that
runs docker from the local checkout to repro CI failures
locally.

Bug: 140126865
Change-Id: I5a496e9c89b6137150ac928e418b175d892e95c9
diff --git a/tools/run_test_like_ci b/tools/run_test_like_ci
new file mode 100755
index 0000000..3a94ef9
--- /dev/null
+++ b/tools/run_test_like_ci
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+# Copyright (C) 2019 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from __future__ import print_function
+import argparse
+import sys
+import shutil
+import subprocess
+import os
+
+REPO_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
+sys.path.append(os.path.join(REPO_ROOT, 'infra', 'ci'))
+from config import JOB_CONFIGS, SANDBOX_IMG
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('config', choices=JOB_CONFIGS.keys())
+    args = parser.parse_args()
+
+    # Check that the directory is clean.
+    git_cmd = ['git', '-C', REPO_ROOT, 'status', '--porcelain']
+    modified_files = subprocess.check_output(git_cmd)
+    if modified_files:
+      print('The current Git repo has modified/untracked files.')
+      print('The sandboxed VM will fetch the HEAD of your current git repo.')
+      print('This is probably not the state you want to be in.')
+      print('I suggest you press CTRL+C, commit and then re-run this script')
+      print('Modified files:\n' + modified_files)
+      raw_input('If you think you know what you are doing, press Enter instead')
+
+    env = {
+        'PERFETTO_TEST_GIT_REF': 'file:///ci/source',
+    }
+    env.update(JOB_CONFIGS[args.config])
+
+    workdir = os.path.join(REPO_ROOT, 'out', 'tmp.ci')
+    cmd = [
+        'sudo', '--', 'docker', 'run', '-it', '--name', 'perfetto_ci',
+        '--cap-add', 'SYS_PTRACE', '--rm', '--volume',
+        '%s:/ci/ramdisk' % workdir, '--tmpfs', '/tmp:exec',
+        '--volume=%s:/ci/source:ro' % REPO_ROOT
+    ]
+    for kv in env.items():
+        cmd += ['--env', '%s=%s' % kv]
+    cmd += [SANDBOX_IMG]
+    cmd += [
+        'bash', '-c',
+        'cd /ci/ramdisk; bash /ci/init.sh || sudo -u perfetto -EH bash -i'
+    ]
+
+    print('About to run\n', ' '.join(cmd))
+    print('')
+    print('The VM will have read-only acess to: %s, mounted as /ci/source' %
+          REPO_ROOT)
+    print('The VM workdir /ci/ramdisk will be mounted into: %s' % workdir)
+    print(
+        'The contents of %s will be deleted before starting the VM' % workdir)
+    raw_input('Press a key to continue')
+
+    shutil.rmtree(workdir, ignore_errors=True)
+    os.makedirs(workdir)
+    os.execvp(cmd[0], cmd[1:])
+
+
+if __name__ == '__main__':
+    sys.exit(main())