From b037be339677c2f24b7ba676fc9ff893ad474305 Mon Sep 17 00:00:00 2001 From: Andreea Florescu Date: Thu, 28 Oct 2021 10:38:38 +0300 Subject: [PATCH] update test_coverage to allow running it on crates The test was assuming that we always want to run all the tests in an workspace, but that is not the case when we have multiple crates that we want to publish part of the same workspace. To address this issue, we can now select to run the coverage test only on the crate in scope. This can be achieved by passing the parameter `--test-scope` with the value "crate". By default, tests are still running as a workspace so that we do not need to update all the other rust-vmm components that were counting on this feature. The `test-scope` option can also be reused in other tests that are making use of `--all`. For example, cargo build. Signed-off-by: Andreea Florescu --- integration_tests/conftest.py | 24 +++++++++++++++++++++++- integration_tests/test_coverage.py | 18 +++++++++++------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py index 9fc5ef4..72dfe3f 100644 --- a/integration_tests/conftest.py +++ b/integration_tests/conftest.py @@ -1,4 +1,4 @@ -# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import pytest @@ -6,6 +6,9 @@ import pytest PROFILE_CI = "ci" PROFILE_DEVEL = "devel" +WORKSPACE = "workspace" +CRATE = "crate" + def pytest_addoption(parser): parser.addoption( @@ -26,6 +29,16 @@ def pytest_addoption(parser): "removed." ) + parser.addoption( + "--test-scope", + default=WORKSPACE, + choices=[WORKSPACE, CRATE], + help="Defines the scope of running tests: {} or {}".format( + WORKSPACE, + CRATE + ) + ) + @pytest.fixture def profile(request): @@ -37,7 +50,16 @@ def no_cleanup(request): return request.config.getoption("--no-cleanup") +@pytest.fixture +def test_scope(request): + return request.config.getoption("--test-scope") + + # This is used for defining global variables in pytest. def pytest_configure(): + # These constants are needed in tests, so this is the way that we can + # export them. pytest.profile_ci = PROFILE_CI pytest.profile_devel = PROFILE_DEVEL + pytest.workspace = WORKSPACE + pytest.crate = CRATE diff --git a/integration_tests/test_coverage.py b/integration_tests/test_coverage.py index 37217aa..1b815be 100644 --- a/integration_tests/test_coverage.py +++ b/integration_tests/test_coverage.py @@ -1,4 +1,4 @@ -# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """Test the coverage and update the threshold when coverage is increased.""" @@ -63,7 +63,7 @@ def _write_coverage_config(coverage_config): json.dump(coverage_config, outfile) -def _get_current_coverage(coverage_config, no_cleanup): +def _get_current_coverage(coverage_config, no_cleanup, test_scope): """Helper function that returns the coverage computed with kcov.""" kcov_output_dir = os.path.join(REPO_ROOT_PATH, "kcov_output") @@ -89,11 +89,15 @@ def _get_current_coverage(coverage_config, no_cleanup): exclude_pattern += ',' + additional_exclude_path additional_kcov_param = '' + + if test_scope == pytest.workspace: + additional_kcov_param += '--all ' + crate_features = coverage_config["crate_features"] if crate_features: additional_kcov_param += '--features=' + crate_features - kcov_cmd = "CARGO_TARGET_DIR={} cargo kcov {} --all " \ + kcov_cmd = "CARGO_TARGET_DIR={} cargo kcov {} " \ "--output {} -- " \ "--exclude-region={} " \ "--exclude-pattern={} " \ @@ -127,15 +131,15 @@ def _get_current_coverage(coverage_config, no_cleanup): return coverage -def test_coverage(profile, no_cleanup): +def test_coverage(profile, no_cleanup, test_scope): coverage_config = _read_test_config() - current_coverage = _get_current_coverage(coverage_config, no_cleanup) + current_coverage = _get_current_coverage(coverage_config, no_cleanup, test_scope) previous_coverage = coverage_config["coverage_score"] if previous_coverage < current_coverage: if profile == pytest.profile_ci: # In the CI Profile we expect the coverage to be manually updated. - assert False,\ - "Coverage is increased from {} to {}. "\ + assert False, \ + "Coverage is increased from {} to {}. " \ "Please update the coverage in coverage_config_{}.json."\ .format( previous_coverage,