|
8 | 8 | target should be put in `s3://spec.firecracker` and automatically updated.
|
9 | 9 | """
|
10 | 10 |
|
11 |
| - |
12 | 11 | import os
|
13 |
| -import platform |
14 |
| -import re |
15 |
| -import shutil |
16 | 12 | import pytest
|
17 | 13 |
|
18 | 14 | from framework import utils
|
19 |
| -import host_tools.cargo_build as host # pylint: disable=import-error |
20 | 15 | from host_tools import proc
|
21 | 16 |
|
22 | 17 | # We have different coverages based on the host kernel version. This is
|
|
29 | 24 | # Checkout the cpuid crate. In the future other
|
30 | 25 | # differences may appear.
|
31 | 26 | if utils.is_io_uring_supported():
|
32 |
| - COVERAGE_DICT = {"Intel": 82.99, "AMD": 82.31, "ARM": 82.41} |
| 27 | + COVERAGE_DICT = {"Intel": 90.84, "AMD": 90.11, "ARM": 90.51} |
33 | 28 | else:
|
34 |
| - COVERAGE_DICT = {"Intel": 80.15, "AMD": 79.48, "ARM": 79.59} |
| 29 | + COVERAGE_DICT = {"Intel": 88.39, "AMD": 87.67, "ARM": 87.95} |
35 | 30 |
|
36 | 31 | PROC_MODEL = proc.proc_type()
|
37 | 32 |
|
38 |
| -COVERAGE_MAX_DELTA = 0.05 |
39 |
| - |
40 |
| -CARGO_KCOV_REL_PATH = os.path.join(host.CARGO_BUILD_REL_PATH, "kcov") |
41 |
| - |
42 |
| -KCOV_COVERAGE_FILE = "index.js" |
43 |
| -"""kcov will aggregate coverage data in this file.""" |
| 33 | +# Toolchain target architecture. |
| 34 | +if ("Intel" in PROC_MODEL) or ("AMD" in PROC_MODEL): |
| 35 | + ARCH = "x86_64" |
| 36 | +elif "ARM" in PROC_MODEL: |
| 37 | + ARCH = "aarch64" |
| 38 | +else: |
| 39 | + raise Exception(f"Unsupported processor model ({PROC_MODEL})") |
44 | 40 |
|
45 |
| -KCOV_COVERED_LINES_REGEX = r'"covered_lines":"(\d+)"' |
46 |
| -"""Regex for extracting number of total covered lines found by kcov.""" |
| 41 | +# Toolchain target. |
| 42 | +# Currently profiling with `aarch64-unknown-linux-musl` is unsupported (see |
| 43 | +# https://github.com/rust-lang/rustup/issues/3095#issuecomment-1280705619) therefore we profile and |
| 44 | +# run coverage with the `gnu` toolchains and run unit tests with the `musl` toolchains. |
| 45 | +TARGET = f"{ARCH}-unknown-linux-gnu" |
47 | 46 |
|
48 |
| -KCOV_TOTAL_LINES_REGEX = r'"total_lines" : "(\d+)"' |
49 |
| -"""Regex for extracting number of total executable lines found by kcov.""" |
| 47 | +# We allow coverage to have a max difference of `COVERAGE_MAX_DELTA` as percentage before failing |
| 48 | +# the test. |
| 49 | +COVERAGE_MAX_DELTA = 0.05 |
50 | 50 |
|
51 |
| -SECCOMPILER_BUILD_DIR = "../build/seccompiler" |
| 51 | +# grcov 0.8.* requires GLIBC >2.27, this is not present in ubuntu 18.04, when we update the docker |
| 52 | +# container with a newer version of ubuntu we can also update this. |
| 53 | +GRCOV_VERSION = "0.7.1" |
52 | 54 |
|
53 | 55 |
|
54 | 56 | @pytest.mark.timeout(400)
|
55 |
| -def test_coverage(test_fc_session_root_path, test_session_tmp_path): |
56 |
| - """Test line coverage for rust tests is within bounds. |
57 |
| -
|
58 |
| - The result is extracted from the $KCOV_COVERAGE_FILE file created by kcov |
59 |
| - after a coverage run. |
| 57 | +def test_coverage(): |
| 58 | + """Test code coverage |
60 | 59 |
|
61 | 60 | @type: build
|
62 | 61 | """
|
63 |
| - proc_model = [item for item in COVERAGE_DICT if item in PROC_MODEL] |
64 |
| - assert len(proc_model) == 1, "Could not get processor model!" |
65 |
| - coverage_target_pct = COVERAGE_DICT[proc_model[0]] |
66 |
| - exclude_pattern = ( |
67 |
| - "${CARGO_HOME:-$HOME/.cargo/}," |
68 |
| - "build/," |
69 |
| - "tests/," |
70 |
| - "usr/lib/gcc," |
71 |
| - "lib/x86_64-linux-gnu/," |
72 |
| - "test_utils.rs," |
73 |
| - # The following files/directories are auto-generated |
74 |
| - "bootparam.rs," |
75 |
| - "elf.rs," |
76 |
| - "mpspec.rs," |
77 |
| - "msr_index.rs," |
78 |
| - "bindings.rs," |
79 |
| - "_gen" |
| 62 | + # Get coverage target. |
| 63 | + processor_model = [item for item in COVERAGE_DICT if item in PROC_MODEL] |
| 64 | + assert len(processor_model) == 1, "Could not get processor model!" |
| 65 | + coverage_target = COVERAGE_DICT[processor_model[0]] |
| 66 | + |
| 67 | + # Re-direct to repository root. |
| 68 | + os.chdir("..") |
| 69 | + |
| 70 | + # Generate test profiles. |
| 71 | + utils.run_cmd( |
| 72 | + f'\ |
| 73 | + env RUSTFLAGS="-Cinstrument-coverage" \ |
| 74 | + LLVM_PROFILE_FILE="coverage-%p-%m.profraw" \ |
| 75 | + cargo test --all --target={TARGET} -- --test-threads=1 \ |
| 76 | + ' |
80 | 77 | )
|
81 |
| - exclude_region = "'mod tests {'" |
82 |
| - target = "{}-unknown-linux-musl".format(platform.machine()) |
83 |
| - |
84 |
| - cmd = ( |
85 |
| - 'CARGO_WRAPPER="kcov" RUSTFLAGS="{}" CARGO_TARGET_DIR={} ' |
86 |
| - "cargo kcov --all " |
87 |
| - "--target {} --output {} -- " |
88 |
| - "--exclude-pattern={} " |
89 |
| - "--exclude-region={} --verify" |
90 |
| - ).format( |
91 |
| - host.get_rustflags(), |
92 |
| - os.path.join(test_fc_session_root_path, CARGO_KCOV_REL_PATH), |
93 |
| - target, |
94 |
| - test_session_tmp_path, |
95 |
| - exclude_pattern, |
96 |
| - exclude_region, |
97 |
| - ) |
98 |
| - # We remove the seccompiler custom build directory, created by the |
99 |
| - # vmm-level `build.rs`. |
100 |
| - # If we don't delete it before and after running the kcov command, we will |
101 |
| - # run into linker errors. |
102 |
| - shutil.rmtree(SECCOMPILER_BUILD_DIR, ignore_errors=True) |
103 |
| - # By default, `cargo kcov` passes `--exclude-pattern=$CARGO_HOME --verify` |
104 |
| - # to kcov. To pass others arguments, we need to include the defaults. |
105 |
| - utils.run_cmd(cmd) |
106 |
| - |
107 |
| - shutil.rmtree(SECCOMPILER_BUILD_DIR) |
108 |
| - |
109 |
| - coverage_file = os.path.join(test_session_tmp_path, KCOV_COVERAGE_FILE) |
110 |
| - with open(coverage_file, encoding="utf-8") as cov_output: |
111 |
| - contents = cov_output.read() |
112 |
| - covered_lines = int(re.findall(KCOV_COVERED_LINES_REGEX, contents)[0]) |
113 |
| - total_lines = int(re.findall(KCOV_TOTAL_LINES_REGEX, contents)[0]) |
114 |
| - coverage = covered_lines / total_lines * 100 |
115 |
| - print("Number of executable lines: {}".format(total_lines)) |
116 |
| - print("Number of covered lines: {}".format(covered_lines)) |
117 |
| - print("Thus, coverage is: {:.2f}%".format(coverage)) |
118 |
| - |
119 |
| - coverage_low_msg = ( |
120 |
| - "Current code coverage ({:.2f}%) is >{:.2f}% below the target ({}%).".format( |
121 |
| - coverage, COVERAGE_MAX_DELTA, coverage_target_pct |
122 |
| - ) |
123 |
| - ) |
124 |
| - |
125 |
| - assert coverage >= coverage_target_pct - COVERAGE_MAX_DELTA, coverage_low_msg |
126 | 78 |
|
127 |
| - # Get the name of the variable that needs updating. |
128 |
| - namespace = globals() |
129 |
| - cov_target_name = [name for name in namespace if namespace[name] is COVERAGE_DICT][ |
130 |
| - 0 |
131 |
| - ] |
132 |
| - |
133 |
| - coverage_high_msg = ( |
134 |
| - "Current code coverage ({:.2f}%) is >{:.2f}% above the target ({}%).\n" |
135 |
| - "Please update the value of {}.".format( |
136 |
| - coverage, COVERAGE_MAX_DELTA, coverage_target_pct, cov_target_name |
137 |
| - ) |
| 79 | + # Generate coverage report. |
| 80 | + utils.run_cmd( |
| 81 | + f'\ |
| 82 | + cargo install --version {GRCOV_VERSION} grcov \ |
| 83 | + && grcov . \ |
| 84 | + -s . \ |
| 85 | + --binary-path ./build/cargo_target/{TARGET}/debug/ \ |
| 86 | + --ignore "build/*" \ |
| 87 | + -t html \ |
| 88 | + --branch \ |
| 89 | + --ignore-not-existing \ |
| 90 | + -o ./build/cargo_target/{TARGET}/debug/coverage \ |
| 91 | + ' |
138 | 92 | )
|
139 | 93 |
|
140 |
| - assert coverage <= coverage_target_pct + COVERAGE_MAX_DELTA, coverage_high_msg |
141 |
| - |
142 |
| - return (f"{coverage}%", f"{coverage_target_pct}% +/- {COVERAGE_MAX_DELTA * 100}%") |
| 94 | + # Extract coverage from html report. |
| 95 | + # |
| 96 | + # The line looks like `<abbr title="44724 / 49237">90.83 %</abbr></p>` and is the first |
| 97 | + # occurrence of the `<abbr>` element in the file. |
| 98 | + # |
| 99 | + # When we update grcov to 0.8.* we can update this to pull the coverage from a generated .json |
| 100 | + # file. |
| 101 | + index = open( |
| 102 | + f"./build/cargo_target/{TARGET}/debug/coverage/index.html", encoding="utf-8" |
| 103 | + ) |
| 104 | + index_contents = index.read() |
| 105 | + end = index_contents.find(" %</abbr></p>") |
| 106 | + start = index_contents[:end].rfind(">") |
| 107 | + coverage_str = index_contents[start + 1 : end] |
| 108 | + coverage = float(coverage_str) |
| 109 | + |
| 110 | + # Compare coverage. |
| 111 | + high = coverage_target * (1.0 + COVERAGE_MAX_DELTA) |
| 112 | + low = coverage_target * (1.0 - COVERAGE_MAX_DELTA) |
| 113 | + assert ( |
| 114 | + coverage >= low |
| 115 | + ), f"Current code coverage ({coverage:.2f}%) is more than {COVERAGE_MAX_DELTA:.2f}% below \ |
| 116 | + the target ({coverage_target:.2f}%)" |
| 117 | + assert ( |
| 118 | + coverage <= high |
| 119 | + ), f"Current code coverage ({coverage:.2f}%) is more than {COVERAGE_MAX_DELTA:.2f}% above \ |
| 120 | + the target ({coverage_target:.2f}%)" |
0 commit comments