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