summaryrefslogtreecommitdiff
path: root/test/end-to-end/with_serve_test_runner.py
diff options
context:
space:
mode:
authorKlaus Aehlig <klaus.aehlig@huawei.com>2023-09-14 16:28:32 +0200
committerKlaus Aehlig <klaus.aehlig@huawei.com>2023-09-15 14:42:47 +0200
commit3c14ffe4648e694faadce7bd3f5e60a50413126a (patch)
treea80c483e7ea135999514fa3047e9fb092821b507 /test/end-to-end/with_serve_test_runner.py
parentf821e6b70c59037384ac6afb3a44517fe46953e6 (diff)
downloadjustbuild-3c14ffe4648e694faadce7bd3f5e60a50413126a.tar.gz
Add infrastructure for end-to-end tests using just serve
Diffstat (limited to 'test/end-to-end/with_serve_test_runner.py')
-rwxr-xr-xtest/end-to-end/with_serve_test_runner.py232
1 files changed, 232 insertions, 0 deletions
diff --git a/test/end-to-end/with_serve_test_runner.py b/test/end-to-end/with_serve_test_runner.py
new file mode 100755
index 00000000..dbbd4237
--- /dev/null
+++ b/test/end-to-end/with_serve_test_runner.py
@@ -0,0 +1,232 @@
+#!/usr/bin/env python3
+# Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
+#
+# 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.
+
+import json
+import os
+import shutil
+import subprocess
+import sys
+import time
+
+from typing import Any, Dict, List
+
+Json = Dict[str, Any]
+
+time_start: float = time.time()
+time_stop: float = 0
+result: str = "UNKNOWN"
+stderr: str = ""
+stdout: str = ""
+
+
+def dump_results() -> None:
+ with open("result", "w") as f:
+ f.write("%s\n" % (result, ))
+ with open("time-start", "w") as f:
+ f.write("%d\n" % (time_start, ))
+ with open("time-stop", "w") as f:
+ f.write("%d\n" % (time_stop, ))
+ with open("stdout", "w") as f:
+ f.write("%s\n" % (stdout, ))
+ with open("stderr", "w") as f:
+ f.write("%s\n" % (stderr, ))
+
+
+def get_remote_execution_address(d: Json) -> str:
+ return "%s:%d" % (d["interface"], int(d["port"]))
+
+
+dump_results()
+
+TEMP_DIR = os.path.realpath("scratch")
+os.makedirs(TEMP_DIR, exist_ok=True)
+
+WORK_DIR = os.path.realpath("work")
+os.makedirs(WORK_DIR, exist_ok=True)
+
+REMOTE_DIR = os.path.realpath("remote")
+os.makedirs(REMOTE_DIR, exist_ok=True)
+REMOTE_LBR = os.path.join(REMOTE_DIR, "build-root")
+
+g_REMOTE_EXECUTION_ADDRESS: str = ""
+
+SERVE_DIR = os.path.realpath("serve")
+SERVE_LBR = os.path.join(SERVE_DIR, "build-root")
+
+remote_proc = None
+
+# start just execute as remote service
+REMOTE_INFO = os.path.join(REMOTE_DIR, "remote-info.json")
+
+if os.path.exists(REMOTE_INFO):
+ print(f"Warning: removing unexpected info file {REMOTE_INFO}")
+ os.remove(REMOTE_INFO)
+
+remote_cmd = [
+ "./bin/just",
+ "execute",
+ "--info-file",
+ REMOTE_INFO,
+ "--local-build-root",
+ REMOTE_LBR,
+ "--log-limit",
+ "6",
+ "--plain-log",
+]
+
+remotestdout = open("remotestdout", "w")
+remotestderr = open("remotestderr", "w")
+remote_proc = subprocess.Popen(
+ remote_cmd,
+ stdout=remotestdout,
+ stderr=remotestderr,
+)
+
+while not os.path.exists(REMOTE_INFO):
+ time.sleep(1)
+
+with open(REMOTE_INFO) as f:
+ info = json.load(f)
+
+g_REMOTE_EXECUTION_ADDRESS = get_remote_execution_address(info)
+
+# start just serve service
+SERVE_INFO = os.path.join(REMOTE_DIR, "serve-info.json")
+SERVE_CONFIG_FILE = os.path.join(REMOTE_DIR, "serve.json")
+
+serve_config = {
+ "local build root": SERVE_DIR,
+ "logging": {"limit": 6, "plain": True},
+ "execution endpoint": {"address": g_REMOTE_EXECUTION_ADDRESS},
+ "remote service": {"info file": SERVE_INFO},
+}
+
+repositories : List[str] = []
+repos_env: Dict[str, str] = {}
+
+REPOS_DIR = os.path.realpath("repos")
+os.makedirs(REPOS_DIR, exist_ok=True)
+DATA_DIR = os.path.realpath("data")
+os.makedirs(DATA_DIR, exist_ok=True)
+
+GIT_NOBODY_ENV: Dict[str, str] = {
+ "GIT_AUTHOR_DATE": "1970-01-01T00:00Z",
+ "GIT_AUTHOR_NAME": "Nobody",
+ "GIT_AUTHOR_EMAIL": "nobody@example.org",
+ "GIT_COMMITTER_DATE": "1970-01-01T00:00Z",
+ "GIT_COMMITTER_NAME": "Nobody",
+ "GIT_COMMITTER_EMAIL": "nobody@example.org",
+ "GIT_CONFIG_GLOBAL": "/dev/null",
+ "GIT_CONFIG_SYSTEM": "/dev/null",
+}
+
+count = 0
+for repo in os.listdir("data"):
+ target = os.path.join(REPOS_DIR, repo)
+ shutil.copytree(
+ os.path.join(DATA_DIR, repo),
+ target,
+ )
+ subprocess.run(
+ ["git", "init"],
+ cwd=target,
+ env=dict(os.environ, **GIT_NOBODY_ENV),
+ stdout = subprocess.DEVNULL,
+ stderr = subprocess.DEVNULL,
+ )
+ subprocess.run(
+ ["git", "add", "-f", "."],
+ cwd=target,
+ env=dict(os.environ, **GIT_NOBODY_ENV),
+ stdout = subprocess.DEVNULL,
+ stderr = subprocess.DEVNULL,
+ )
+ subprocess.run(
+ ["git", "commit", "-m",
+ "Content of %s" % (target,)],
+ cwd=target,
+ env=dict(os.environ, **GIT_NOBODY_ENV),
+ stdout = subprocess.DEVNULL,
+ stderr = subprocess.DEVNULL,
+ )
+ repositories.append(target)
+ repos_env["COMMIT_%d" % count] = subprocess.run(
+ ["git", "log", "-n", "1", "--format=%H"],
+ stdout=subprocess.PIPE,
+ stderr = subprocess.DEVNULL,
+ cwd=target).stdout.decode('utf-8').strip()
+
+serve_config["repositories"] = repositories
+
+with open(SERVE_CONFIG_FILE, "w") as f:
+ json.dump(serve_config, f)
+
+servestdout = open("servestdout", "w")
+servestderr = open("servestderr", "w")
+serve_proc = subprocess.Popen(
+ ["./bin/just", "serve", SERVE_CONFIG_FILE],
+ stdout=servestdout,
+ stderr=servestderr,
+)
+
+while not os.path.exists(SERVE_INFO):
+ time.sleep(1)
+
+with open(SERVE_INFO) as f:
+ serve_info = json.load(f)
+
+SERVE_ADDRESS = get_remote_execution_address(serve_info)
+
+# run the actual test
+
+ENV = dict(
+ os.environ,
+ TEST_TMPDIR=TEMP_DIR,
+ TMPDIR=TEMP_DIR,
+ REMOTE_EXECUTION_ADDRESS=g_REMOTE_EXECUTION_ADDRESS,
+ SERVE=SERVE_ADDRESS,
+ **repos_env
+)
+
+if "COMPATIBLE" in ENV:
+ del ENV["COMPATIBLE"]
+
+for k in ["TLS_CA_CERT", "TLS_CLIENT_CERT", "TLS_CLIENT_KEY"]:
+ if k in ENV:
+ del ENV[k]
+
+time_start = time.time()
+ret = subprocess.run(["sh", "../test.sh"],
+ cwd=WORK_DIR,
+ env=ENV,
+ capture_output=True)
+
+time_stop = time.time()
+result = "PASS" if ret.returncode == 0 else "FAIL"
+stdout = ret.stdout.decode("utf-8")
+stderr = ret.stderr.decode("utf-8")
+
+assert remote_proc
+remote_proc.terminate()
+rout, rerr = remote_proc.communicate()
+
+dump_results()
+
+for f in sys.argv[2:]:
+ keep_file = os.path.join(WORK_DIR, f)
+ if not os.path.exists(keep_file):
+ open(keep_file, "a").close()
+
+if result != "PASS": exit(1)