1# This class contains the common logic to deploy the SystemReady ACS pre-built
2# image and set up the testimage environment. It is to be inherited by recipes
3# which contains the URI to download the SystemReady ACS image.
4# This class also contains a testimage "postfunc" called acs_logs_handle which
5# performs the following functions after the tests have completed:
6#   * Extract the acs_results directory from the Wic image to
7#     ${WORKDIR}/testimage
8#   * Create a symlink to the acs_results in ${TMPDIR}/log/oeqa for ease of
9#     access
10#   * Run the test parser, format results, and check results routines
11
12INHIBIT_DEFAULT_DEPS = "1"
13COMPATIBLE_HOST = "aarch64-*"
14PACKAGE_ARCH = "${MACHINE_ARCH}"
15inherit nopackages deploy rootfs-postcommands ${IMAGE_CLASSES} python3native
16
17do_configure[noexec] = "1"
18do_compile[noexec] = "1"
19do_install[noexec] = "1"
20do_testimage[depends] += "mtools-native:do_populate_sysroot"
21
22# Deploy with this suffix so it is picked up in the machine configuration
23IMAGE_DEPLOY_SUFFIX ?= ".wic"
24
25# Post-process commands may write to IMGDEPLOYDIR
26IMGDEPLOYDIR = "${DEPLOYDIR}"
27# Write the test data in IMAGE_POSTPROCESS_COMMAND
28IMAGE_POSTPROCESS_COMMAND += "write_image_test_data; "
29
30python do_deploy() {
31    deploydir = d.getVar('DEPLOYDIR')
32    suffix = d.getVar('IMAGE_DEPLOY_SUFFIX')
33    imgfile = os.path.join(d.getVar('WORKDIR'), d.getVar('IMAGE_FILENAME'))
34    deployfile = os.path.join(deploydir, d.getVar('IMAGE_NAME') + suffix)
35    linkfile = os.path.join(deploydir, d.getVar('IMAGE_LINK_NAME') + suffix)
36
37    # Install the image file in the deploy directory
38    import shutil
39    shutil.copyfile(imgfile, deployfile)
40    if os.path.lexists(linkfile):
41        os.remove(manifest_link)
42    os.symlink(os.path.basename(deployfile), linkfile)
43
44    # Run the image post-process commands
45    from oe.utils import execute_pre_post_process
46    post_process_cmds = d.getVar("IMAGE_POSTPROCESS_COMMAND")
47    execute_pre_post_process(d, post_process_cmds)
48
49    # Copy the report.txt to DEPLOYDIR
50    # The machine-specific implementation can optionally put the report file in
51    # ${WORKDIR}/report.txt. If there is no such file present, use the template.
52    workdir = d.getVar('WORKDIR')
53    report_file = os.path.join(workdir, "report.txt")
54    report_file_dest = os.path.join(deploydir, "report.txt")
55    if os.path.exists(report_file):
56        report_file_to_copy = report_file
57    else:
58        report_file_to_copy = os.path.join(workdir, "systemready-ir-template",
59                                            "report.txt")
60    shutil.copyfile(report_file_to_copy, report_file_dest)
61
62    # Ensure an empty rootfs manifest exists (required by testimage)
63    fname = os.path.join(deploydir, d.getVar('IMAGE_LINK_NAME') + ".manifest")
64    open(fname, 'w').close()
65}
66addtask deploy after do_install before do_image_complete
67
68do_image_complete() {
69    true
70}
71addtask image_complete after do_deploy before do_build
72do_image_complete[depends] += "arm-systemready-firmware:do_image_complete"
73
74ACS_LOG_NAME = "acs_results_${DATETIME}"
75ACS_LOG_NAME[vardepsexclude] += "DATETIME"
76ACS_LOG_DIR = "${TEST_LOG_DIR}/${ACS_LOG_NAME}"
77ACS_LOG_LINK = "${TEST_LOG_DIR}/acs_results"
78TEST_LOG_DIR = "${WORKDIR}/testimage"
79RM_WORK_EXCLUDE_ITEMS += "${@ os.path.basename(d.getVar('TEST_LOG_DIR')) }"
80
81do_testimage[postfuncs] += "acs_logs_handle"
82do_testimage[depends] += "edk2-test-parser-native:do_populate_sysroot \
83                          arm-systemready-scripts-native:do_populate_sysroot"
84
85# Process the logs
86python acs_logs_handle() {
87    import logging
88    from oeqa.utils import make_logger_bitbake_compatible
89    import shutil
90
91    deploy_dir_image = d.getVar('DEPLOY_DIR_IMAGE')
92    systemready_scripts_dir = os.path.join(d.getVar('STAGING_LIBDIR_NATIVE'),
93                                           "systemready_scripts")
94    edk2_test_parser_dir = os.path.join(d.getVar('STAGING_LIBDIR_NATIVE'),
95                                        "edk2_test_parser")
96    deployfile = os.path.join(deploy_dir_image, d.getVar('IMAGE_LINK_NAME')
97                              + d.getVar('IMAGE_DEPLOY_SUFFIX'))
98
99    testimage_dir = d.getVar('TEST_LOG_DIR')
100    logdir = d.getVar('ACS_LOG_DIR')
101    loglink = d.getVar('ACS_LOG_LINK')
102
103    # Copy the report.txt file from DEPLOY_DIR_IMAGE
104    report_file = os.path.join(deploy_dir_image, "report.txt")
105    report_file_dest = os.path.join(testimage_dir, "report.txt")
106    shutil.copyfile(report_file, report_file_dest)
107
108    # Extract the log files from the Wic image to the testimage logs directory
109    resultspath = deployfile + ':3/acs_results'
110    import subprocess
111    subprocess.run(['wic', 'cp', resultspath, logdir], check=True)
112
113    # Create a symlink to the acs_results directory
114    if os.path.lexists(loglink):
115        os.remove(loglink)
116    os.symlink(os.path.basename(logdir), loglink)
117
118    # Create a top-level symlink to the acs_results directory
119    top_logdir = os.path.join(get_testimage_json_result_dir(d), d.getVar("PN"))
120    log_name = d.getVar('ACS_LOG_NAME')
121    top_link = os.path.join(top_logdir, log_name)
122    log_target = os.path.relpath(logdir, top_logdir)
123    os.symlink(log_target, top_link)
124
125    # Parse the logs and generate results file
126    logger = make_logger_bitbake_compatible(logging.getLogger("BitBake"))
127
128    sct_log = os.path.join(logdir, 'sct_results', 'Overall', 'Summary.ekl')
129    sct_seq = os.path.join(logdir, 'sct_results', 'Sequence', 'EBBR.seq')
130
131    parser_path = os.path.join(edk2_test_parser_dir, "parser.py")
132    # format-sr-results.py needs the output file to be called "result.md"
133    subprocess.run([parser_path, sct_log, sct_seq, "--md",
134                   os.path.join(logdir, "result.md")], check=True)
135
136    scripts_path = os.path.join(systemready_scripts_dir,
137                                "format-sr-results.py")
138    summary_path = os.path.join(testimage_dir, "summary.md")
139    subprocess.run([scripts_path, "--dir", testimage_dir, "--md", summary_path],
140                   check=True)
141
142    # Symlink acs-console.log to default_log
143    subprocess.run(["ln", "-sf", os.path.join(testimage_dir, "default_log"),
144                    os.path.join(testimage_dir, "acs-console.log")], check=True)
145
146    # Run the check-sr-results.py systemready script to check the results
147    check_sr_results_log_path = os.path.join(testimage_dir,
148                                             "check_sr_results.log")
149    with open(check_sr_results_log_path, "w") as f:
150        check_sr_results_path = os.path.join(systemready_scripts_dir,
151                                            "check-sr-results.py")
152        try:
153            subprocess.run([check_sr_results_path, "--dir", testimage_dir,
154                            "--print-meta", "--debug"],
155                           stdout=f, stderr=f, text=True, check=True)
156        except subprocess.CalledProcessError:
157            logger.error(f"ACS run failed the check SystemReady results. See "
158                         f"{summary_path} and {check_sr_results_log_path} for "
159                         f"details of the error.")
160            raise bb.BBHandledException()
161}
162