Skip to content

Commit d2e8ad9

Browse files
andreeaflorescuJulian Stecklina
authored and
Julian Stecklina
committed
CI: refactor cargo build
Cargo doesn't make any distinction between flags and extra arguments. There is no reason we should do so in our CI either. Removed the flags parameter from the cargo_build function. Instead of having many test function for different build types, we can have one function and use the pytest parametrize annotation. This way we can get all the required configuration without duplicating code. Signed-off-by: Andreea Florescu <[email protected]>
1 parent 9cc3867 commit d2e8ad9

File tree

2 files changed

+35
-50
lines changed

2 files changed

+35
-50
lines changed

tests/host_tools/cargo_build.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
RELEASE_BINARIES_REL_PATH = 'x86_64-unknown-linux-musl/release/'
1818

1919

20-
def cargo_build(path, flags='', extra_args=''):
20+
def cargo_build(path, extra_args=''):
2121
"""Trigger build depending on flags provided."""
22-
cmd = 'CARGO_TARGET_DIR={} cargo build {} {}'.format(
22+
cmd = 'CARGO_TARGET_DIR={} cargo build {}'.format(
2323
path,
24-
flags,
2524
extra_args
2625
)
2726
run(cmd, shell=True, check=True)
@@ -54,7 +53,6 @@ def get_firecracker_binaries(root_path):
5453
)
5554
cargo_build(
5655
build_path,
57-
flags='--release',
58-
extra_args='>/dev/null 2>&1'
56+
extra_args='--release >/dev/null 2>&1'
5957
)
6058
return fc_binary_path, jailer_binary_path

tests/integration_tests/build/test_build.py

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,47 @@
22
# SPDX-License-Identifier: Apache-2.0
33
"""Tests that check if both the debug and the release builds pass."""
44

5+
import itertools
56
import os
7+
import pytest
68

79
import host_tools.cargo_build as host # pylint:disable=import-error
810

9-
10-
CARGO_DEBUG_REL_PATH = os.path.join(host.CARGO_BUILD_REL_PATH, 'debug')
11-
CARGO_DEBUG_REL_PATH_FEATURES = os.path.join(
12-
host.CARGO_BUILD_REL_PATH,
13-
'debug-features'
14-
)
15-
CARGO_RELEASE_REL_PATH_FEATURES = os.path.join(
16-
host.CARGO_BUILD_REL_PATH,
17-
'release-features'
18-
)
11+
FEATURES = ["", "vsock"]
12+
BUILD_TYPES = ["debug", "release"]
1913

2014

21-
def test_build_debug(test_session_root_path):
22-
"""Test if a debug-mode build works."""
23-
build_path = os.path.join(
24-
test_session_root_path,
25-
CARGO_DEBUG_REL_PATH
26-
)
27-
host.cargo_build(build_path)
28-
29-
30-
def test_build_debug_with_features(test_session_root_path):
31-
"""Test if a debug-mode build works for supported features."""
32-
build_path = os.path.join(
33-
test_session_root_path,
34-
CARGO_DEBUG_REL_PATH_FEATURES
15+
@pytest.mark.parametrize(
16+
"features, build_type",
17+
itertools.product(FEATURES, BUILD_TYPES)
18+
)
19+
def test_build(test_session_root_path, features, build_type):
20+
"""
21+
Test build using a cartesian product of possible features and build
22+
types.
23+
"""
24+
extra_args = ""
25+
26+
if build_type == "release":
27+
extra_args += "--release "
28+
29+
# The relative path of the binaries is computed using the build_type
30+
# (either release or debug) and if any features are provided also using
31+
# the features names.
32+
# For example, a default release build with no features will end up in
33+
# the relative directory "release", but for a vsock release build the
34+
# relative directory will be "release-vsock".
35+
rel_path = os.path.join(
36+
host.CARGO_BUILD_REL_PATH,
37+
build_type
3538
)
36-
# Building with multiple features is as simple as:
37-
# cargo build --features "feature1 feature2". We are currently
38-
# supporting only one features: vsock.
39-
host.cargo_build(build_path, '--features "{}"'.format('vsock'))
39+
if features:
40+
rel_path += "-{}".format(features)
41+
extra_args = "--features {} ".format(features)
4042

41-
42-
def test_build_release(test_session_root_path):
43-
"""Test if a release-mode build works."""
4443
build_path = os.path.join(
4544
test_session_root_path,
46-
host.CARGO_RELEASE_REL_PATH
45+
rel_path
4746
)
48-
host.cargo_build(build_path, '--release')
49-
5047

51-
def test_build_release_with_features(test_session_root_path):
52-
"""Test if a release-mode build works for supported features."""
53-
build_path = os.path.join(
54-
test_session_root_path,
55-
CARGO_RELEASE_REL_PATH_FEATURES
56-
)
57-
host.cargo_build(
58-
build_path,
59-
'--features "{}"'.format('vsock'),
60-
'--release'
61-
)
48+
host.cargo_build(build_path, extra_args=extra_args)

0 commit comments

Comments
 (0)