|
1 | 1 | import logging |
| 2 | +import re |
2 | 3 | import os |
3 | 4 |
|
4 | 5 | from virttest import data_dir, error_context, remote, utils_misc |
| 6 | +from avocado.core import exceptions |
| 7 | +from avocado.utils import process |
5 | 8 |
|
6 | 9 | LOG_JOB = logging.getLogger("avocado.test") |
7 | 10 |
|
@@ -147,3 +150,103 @@ def netperf_record(results, filter_list, header=False, base="17", fbase="2"): |
147 | 150 | record += "%s|" % format_result(results[key], base=base, fbase=fbase) |
148 | 151 | record = record.rstrip("|") |
149 | 152 | return record, key_list |
| 153 | + |
| 154 | + |
| 155 | +def compile_netperf_pkg(params, env, address): |
| 156 | + """ |
| 157 | + Compile netperf source package |
| 158 | +
|
| 159 | + :param params: Test parameters dictionary |
| 160 | + :param env: Test environment object |
| 161 | + :param address: localhost, vm name, or ip address |
| 162 | + :return: netserver_path, netperf_path |
| 163 | + """ |
| 164 | + netperf_link = params.get("netperf_link", "netperf-2.7.1.tar.bz2") |
| 165 | + netperf_src = os.path.join(data_dir.get_deps_dir("netperf"), netperf_link) |
| 166 | + guest_netperf_path = params.get("guest_netperf_path", "/var/tmp/") |
| 167 | + |
| 168 | + if address == "localhost": |
| 169 | + session, install_path = None, params.get("server_path", "/var/tmp") |
| 170 | + elif address in params.get('vms', '').split(): |
| 171 | + vm = env.get_vm(address) |
| 172 | + session, install_path = vm.wait_for_login(), guest_netperf_path |
| 173 | + target_ip, user, pwd = vm.get_address(), params.get("username"), params.get("password") |
| 174 | + elif re.match(r"^(?:\d{1,3}\.){3}\d{1,3}$", address): |
| 175 | + if params.get_boolean("remote_server", False): |
| 176 | + session = remote.remote_login( |
| 177 | + "ssh", |
| 178 | + address, |
| 179 | + "22", |
| 180 | + params.get("server_user"), |
| 181 | + params.get("server_pwd"), |
| 182 | + r'[$#%]' |
| 183 | + ) |
| 184 | + else: |
| 185 | + raise exceptions.TestError( |
| 186 | + f"IP address '{address}' provided but 'remote_server' param is not set. " |
| 187 | + "Set remote_server=yes to compile on remote server, or use 'localhost'." |
| 188 | + ) |
| 189 | + install_path = params.get("server_path", "/var/tmp") |
| 190 | + target_ip, user, pwd = address, params.get("server_user"), params.get("server_pwd") |
| 191 | + else: |
| 192 | + raise exceptions.TestError(f"Unsupported address for compilation: {address}") |
| 193 | + |
| 194 | + if netperf_link.endswith(".tar.bz2"): |
| 195 | + pack_suffix, decomp_tool = ".tar.bz2", "tar jxf" |
| 196 | + elif netperf_link.endswith(".tar.gz"): |
| 197 | + pack_suffix, decomp_tool = ".tar.gz", "tar zxf" |
| 198 | + else: |
| 199 | + raise exceptions.TestError(f"Unsupported compression format for netperf package: {netperf_link}") |
| 200 | + |
| 201 | + full_netperf_dir = os.path.join(install_path, netperf_link[:-len(pack_suffix)]) |
| 202 | + nserver_path = os.path.join(full_netperf_dir, "src", "netserver") |
| 203 | + nperf_path = os.path.join(full_netperf_dir, "src", "netperf") |
| 204 | + |
| 205 | + try: |
| 206 | + if session: |
| 207 | + if session.cmd_status(f"test -f {nperf_path}") == 0: |
| 208 | + LOG_JOB.info(f"Netperf already compiled on {address}, skipping.") |
| 209 | + return nserver_path, nperf_path |
| 210 | + elif os.path.exists(nperf_path): |
| 211 | + LOG_JOB.info("Netperf already compiled on localhost, skipping.") |
| 212 | + return nserver_path, nperf_path |
| 213 | + |
| 214 | + if session: |
| 215 | + arch = session.cmd_output("arch", timeout=10).strip() |
| 216 | + decomp_cmd = f"cd {install_path} && {decomp_tool} {netperf_link}" |
| 217 | + else: |
| 218 | + arch = process.run("arch").stdout_text.strip() |
| 219 | + decomp_cmd = f"{decomp_tool} {netperf_src} -C {install_path}" |
| 220 | + |
| 221 | + build_type_map = { |
| 222 | + "aarch64": "aarch64-unknown-linux-gnu", |
| 223 | + "x86_64": "x86_64-unknown-linux-gnu", |
| 224 | + } |
| 225 | + build_type = build_type_map.get(arch, arch) |
| 226 | + compile_cmd = (f"cd {full_netperf_dir} && ./autogen.sh && " |
| 227 | + f"CFLAGS='-Wno-implicit-function-declaration' ./configure " |
| 228 | + f"--build={build_type} --prefix={install_path} && make") |
| 229 | + |
| 230 | + LOG_JOB.info(f"Compiling netperf from source on {address}...") |
| 231 | + if session: |
| 232 | + remote.copy_files_to( |
| 233 | + target_ip, |
| 234 | + params.get("cp_client", "scp"), |
| 235 | + user, |
| 236 | + pwd, |
| 237 | + params.get("cp_port", "22"), |
| 238 | + netperf_src, |
| 239 | + install_path, |
| 240 | + 600 |
| 241 | + ) |
| 242 | + session.cmd(decomp_cmd, timeout=60) |
| 243 | + session.cmd(compile_cmd, timeout=600) |
| 244 | + else: |
| 245 | + process.run(decomp_cmd, shell=True, timeout=60) |
| 246 | + process.run(compile_cmd, shell=True, timeout=600) |
| 247 | + |
| 248 | + LOG_JOB.info(f"Netperf compilation completed on {address}") |
| 249 | + finally: |
| 250 | + if session: |
| 251 | + session.close() |
| 252 | + return nserver_path, nperf_path |
0 commit comments