xref: /trunk/test/odf-validate/consumer_smoke.py (revision fbc76c7d28640d3e663ac0dbfe0daecb23c9f5a0)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20
21# usage: consumer_smoke.py <tag> [--port N] [--stall SECONDS] [--dir DIR]
22# Import smoke test for the ODF 1.4 additions: reloads every package of fixtures/t14/consumer (made by make-consumer.py)
23# with the office of the app copy, dumps every property of the objects it touches (dump14.py, or dump7.py with --dumper; the gate's stall watchdog) and compares
24# every package with its '-base' twin (the same document without the construct, diff7.py). Reports per package: loaded or
25# not (an error marker in the dump), and the number of differing dump keys against the twin. A package without a twin
26# is only checked for loading. Results in runs/consumer-<tag>/ (summary in summary.json).
27import argparse, importlib.util, json, os, shutil, subprocess, sys
28
29D = os.path.dirname(os.path.abspath(__file__))
30spec = importlib.util.spec_from_file_location("gate", D + "/gate.py")
31gate = importlib.util.module_from_spec(spec)
32spec.loader.exec_module(gate)
33
34
35def main():
36    ap = argparse.ArgumentParser()
37    ap.add_argument("tag")
38    ap.add_argument("--port", type=int, default=2950)
39    ap.add_argument("--stall", type=int, default=120)
40    ap.add_argument("--dir", default=os.path.join(D, "fixtures", "t14", "consumer"))
41    ap.add_argument("--dumper", default="dump14.py", help="dump14.py (every property of the touched objects) or dump7.py (the gate's fixed set)")
42    a = ap.parse_args()
43    root = os.path.join(D, "runs", "consumer-" + a.tag)
44    shutil.rmtree(root, ignore_errors=True)
45    os.makedirs(root)
46    dest = os.path.join(root, "dump")
47    log = os.path.join(root, "dump.log")
48    r = gate.dump_dir(a.dir, dest, "consumer_" + a.tag, a.port, a.stall, log, dumper=a.dumper)
49    gate.kill_office("consumer_" + a.tag)
50    names = sorted(f[:-5] for f in os.listdir(dest) if f.endswith(".json"))
51    summary = {}
52    for n in names:
53        d = json.load(open(os.path.join(dest, n + ".json")))
54        summary[n] = {"loaded": "error" not in d or d.get("error") is None, "error": d.get("error"), "body_error": d.get("body_error")}
55    base = {n[:-5]: n for n in names if os.path.splitext(n)[0].endswith("-base")}
56    for n in names:
57        stem, ext = os.path.splitext(n)
58        twin = stem + "-base" + ext
59        if twin not in names:
60            continue
61        pa, pb = os.path.join(root, "pairA"), os.path.join(root, "pairB")
62        for p in (pa, pb):
63            os.makedirs(p, exist_ok=True)
64        shutil.copy(os.path.join(dest, n + ".json"), os.path.join(pa, n + ".json"))
65        shutil.copy(os.path.join(dest, twin + ".json"), os.path.join(pb, n + ".json"))
66    if os.path.isdir(os.path.join(root, "pairA")):
67        out = os.path.join(root, "pairs.json")
68        subprocess.run([sys.executable, "-I", os.path.join(D, "diff7.py"), os.path.join(root, "pairA"), os.path.join(root, "pairB"), out],
69                       capture_output=True, check=True)
70        for n, v in json.load(open(out)).items():
71            summary[n]["ndiff_vs_base"] = v["ndiff"]
72            summary[n]["diff_keys"] = [x[0] for x in v["diffs"][:8]]
73    json.dump(summary, open(os.path.join(root, "summary.json"), "w"), indent=1)
74    print("dump:", r)
75    for n in sorted(summary):
76        s = summary[n]
77        print("%-36s %-6s %s" % (n, "loaded" if s["loaded"] else "FAILED", ("diff vs base: %d %s" % (s["ndiff_vs_base"], s["diff_keys"][:3])) if "ndiff_vs_base" in s else ""))
78    bad = [n for n, s in summary.items() if not s["loaded"]]
79    print("not loaded:", bad)
80    sys.exit(1 if bad else 0)
81
82
83main()
84