From d167954020fa454d8dbff0f0508a8f02b72174eb Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Mon, 27 Nov 2023 23:45:15 +0200 Subject: [PATCH] Add drtest demo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This tiny tool reads test suites yaml and run the tests in parallel logging test logs to separate files. A test suite binds tests (e.g. basic-test) to application configurations (e.g. busybox-regional-rbd-deploy). We have 2 test suites: $ tree suites/ suites/ ├── basic-k8s.yaml └── basic-odr.yaml Example run with drenv created environment: $ ./drtest --outdir /tmp/k8s-logs suites/basic-k8s.yaml envs/regional-dr.yaml 2023-11-28 00:55:54,099 INFO Running 'Basic Kubernetes Regional DR tests' 2023-11-28 00:55:54,099 INFO Storing output to '/tmp/k8s-logs' 2023-11-28 00:55:54,101 INFO Starting test 'deploymnet' 2023-11-28 00:55:54,101 INFO Starting test 'statefulset' 2023-11-28 00:55:54,102 INFO Starting test 'daemonset' 2023-11-28 01:04:23,274 INFO Test 'daemonset' PASS 2023-11-28 01:04:24,161 INFO Test 'deploymnet' PASS 2023-11-28 01:04:53,600 INFO Test 'statefulset' PASS 2023-11-28 01:04:53,600 INFO PASS (3 pass, 0 fail) The test logs to separate file: $ tree /tmp/k8s-logs /tmp/k8s-logs ├── daemonset.log ├── deploymnet.log └── statefulset.log To test with OpenShift we need to create a tiny environment file: $ cat env.yaml ramen: hub: hub clusters: [cluster1, cluster2] topology: regional-dr And use a kubeconfig file with the clusters. The file can be created with `oc login` and some `oc config` commands, or using the oc-clusterset plugin: $ cat config.yaml clusters: - name: cluster1 url: perf1.example.com:6443 username: kubeadmin password: PeSkM-R6YcH-LyPZa-oTOO1 - name: cluster2 url: perf2.example.com:6443 username: kubeadmin password: ZjIZn-SFUyR-aE4gI-fJcfL - name: hub url: perf3.example.com:6443 username: kubeadmin password: 7C700-oVS3Q-25rtx-YMew5 current-context: hub $ oc clusterset login --config config.yaml --kubeconfig kubeconfig $ oc config get-contexts --kubeconfig kubeconfig CURRENT NAME CLUSTER AUTHINFO NAMESPACE cluster1 perf1-example-com:6443 kube:admin/perf1-example-com:6443 default cluster2 perf2-example-com:6443 kube:admin/perf2-example-com:6443 default * hub perf3-example-com:6443 kube:admin/perf3-example-com:6443 default Example run with the OpenShift environment: $ ./drtest --kubeconfig kubeconfig --outdir /tmp/odr-logs suites/basic-odr.yaml env.yaml 2023-11-29 23:45:14,849 INFO Running 'Basic OpenShift Regional DR tests' 2023-11-29 23:45:14,849 INFO Storing output to '/tmp/odr-logs' 2023-11-29 23:45:14,850 INFO Starting test 'rbd' 2023-11-29 23:45:14,850 INFO Starting test 'cephfs' 2023-11-29 23:54:24,599 INFO Test 'rbd' PASS 2023-11-29 23:54:51,461 INFO Test 'cephfs' PASS 2023-11-29 23:54:51,461 INFO PASS (2 pass, 0 fail) Signed-off-by: Nir Soffer --- test/drtest | 79 ++++++++++++++++++++++++++++++++++++++ test/suites/basic-k8s.yaml | 15 ++++++++ test/suites/basic-odr.yaml | 12 ++++++ 3 files changed, 106 insertions(+) create mode 100755 test/drtest create mode 100644 test/suites/basic-k8s.yaml create mode 100644 test/suites/basic-odr.yaml diff --git a/test/drtest b/test/drtest new file mode 100755 index 0000000000..51ecfdd85e --- /dev/null +++ b/test/drtest @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +# SPDX-FileCopyrightText: The RamenDR authors +# SPDX-License-Identifier: Apache-2.0 + +import argparse +import collections +import concurrent.futures +import logging +import os +import subprocess + +import yaml + + +def main(): + p = argparse.ArgumentParser("drtest") + p.add_argument("-k", "--kubeconfig", help="If set, use speified kubeconfig path") + p.add_argument("-o", "--outdir", default=".", help="Output directory") + p.add_argument("-v", "--verbose", action="store_true", help="Be more verbose") + p.add_argument("suite", help="Test suite file") + p.add_argument("env", help="Environemnt file") + args = p.parse_args() + + logging.basicConfig( + level=logging.DEBUG if args.verbose else logging.INFO, + format="%(asctime)s %(levelname)-7s %(message)s", + ) + + with open(args.suite) as f: + suite = yaml.safe_load(f) + + os.makedirs(args.outdir, exist_ok=True) + + logging.info("Running '%s'", suite["name"]) + logging.info("Storing output to '%s'", args.outdir) + + stats = collections.Counter({"pass": 0, "fail": 0}) + + with concurrent.futures.ThreadPoolExecutor() as e: + futures = {} + + for test in suite["tests"]: + logging.info("Starting test '%s'", test["name"]) + f = e.submit(run_test, test, args) + futures[f] = test["name"] + + for f in concurrent.futures.as_completed(futures): + try: + f.result() + except Exception: + logging.error("Test '%s' FAIL", futures[f]) + stats["fail"] += 1 + else: + logging.info("Test '%s' PASS", futures[f]) + stats["pass"] += 1 + + logging.info( + "%s (%s pass, %s fail)", + "PASS" if stats["fail"] == 0 else "FAIL", + stats["pass"], + stats["fail"], + ) + + +def run_test(test, args): + log = os.path.join(args.outdir, test["name"] + ".log") + cmd = [test["command"], "--config", test["config"], args.env] + if args.kubeconfig: + env = dict(os.environ) + env["KUBECONFIG"] = args.kubeconfig + else: + env = None + with open(log, "w") as f: + subprocess.run(cmd, stderr=f, check=True, env=env) + + +if __name__ == "__main__": + main() diff --git a/test/suites/basic-k8s.yaml b/test/suites/basic-k8s.yaml new file mode 100644 index 0000000000..c62b0e942c --- /dev/null +++ b/test/suites/basic-k8s.yaml @@ -0,0 +1,15 @@ +# SPDX-FileCopyrightText: The RamenDR authors +# SPDX-License-Identifier: Apache-2.0 + +--- +name: Basic Kubernetes Regional DR tests +tests: + - name: deploymnet + command: basic-test/run + config: configs/k8s/busybox-regional-rbd-deploy.yaml + - name: statefulset + command: basic-test/run + config: configs/k8s/busybox-regional-rbd-sts.yaml + - name: daemonset + command: basic-test/run + config: configs/k8s/busybox-regional-rbd-ds.yaml diff --git a/test/suites/basic-odr.yaml b/test/suites/basic-odr.yaml new file mode 100644 index 0000000000..b05d118dc7 --- /dev/null +++ b/test/suites/basic-odr.yaml @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: The RamenDR authors +# SPDX-License-Identifier: Apache-2.0 + +--- +name: Basic OpenShift Regional DR tests +tests: + - name: rbd + command: basic-test/run + config: configs/odr/busybox-regional-rbd-deploy.yaml + - name: cephfs + command: basic-test/run + config: configs/odr/busybox-regional-cephfs-deploy.yaml