From bcc93572c82a0974e6a1b4da1da75caceea63b72 Mon Sep 17 00:00:00 2001 From: Dheyay Date: Fri, 8 Mar 2024 14:49:01 -0800 Subject: [PATCH] Add coveragerc file --- .coveragerc | 4 ++ features/environment.py | 96 +++++++++++++++++++------------------- features/steps/machines.py | 17 ++----- features/steps/shell.py | 20 ++++---- 4 files changed, 65 insertions(+), 72 deletions(-) create mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000000..7419b91901 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,4 @@ +[paths] +source = + uaclient/ + /usr/lib/python3/dist-packages/uaclient/ diff --git a/features/environment.py b/features/environment.py index 4170d534cf..bdb906336e 100644 --- a/features/environment.py +++ b/features/environment.py @@ -69,6 +69,7 @@ class UAClientBehaveConfig: # environment variable input to the appropriate Python types for use within # the test framework boolean_options = [ + "collect_coverage", "destroy_instances", "ephemeral_instance", "snapshot_strategy", @@ -114,6 +115,7 @@ def __init__( self, *, cloud_credentials_path: Optional[str] = None, + collect_coverage: bool = False, destroy_instances: bool = True, ephemeral_instance: bool = False, snapshot_strategy: bool = False, @@ -141,6 +143,7 @@ def __init__( ) -> None: # First, store the values we've detected self.cloud_credentials_path = cloud_credentials_path + self.collect_coverage = collect_coverage self.ephemeral_instance = ephemeral_instance self.snapshot_strategy = snapshot_strategy self.sbuild_output_to_terminal = sbuild_output_to_terminal @@ -414,55 +417,54 @@ def before_scenario(context: Context, scenario: Scenario): def after_scenario(context, scenario): """Collect the coverage files after the scenario is run.""" - cov_dir = os.path.join(context.pro_config.artifact_dir, "coverage") - if not os.path.exists(cov_dir): - os.makedirs(cov_dir) + if context.pro_config.collect_coverage: + cov_dir = os.path.join(context.pro_config.artifact_dir, "coverage") + if not os.path.exists(cov_dir): + os.makedirs(cov_dir) - inner_dir = os.path.join( - datetime.datetime.now().strftime("%Y-%m-%d"), - "{}".format(os.path.basename(scenario.filename.replace(".", "_"))), - ) - new_artifacts_dir = os.path.join( - cov_dir, - inner_dir, - ) - if not os.path.exists(new_artifacts_dir): - os.makedirs(new_artifacts_dir) - if hasattr(context, "machines") and SUT in context.machines: - try: - scenario_name = ( - os.path.basename(scenario.filename.replace(".", "_")) - + "_" - + str(scenario.line) - ) - cov_filename = ".coverage.{}".format(scenario_name) - context.machines[SUT].instance.execute( - [ - "bash", - "-c", - "mv .coverage /tmp/{cov_filename}".format( - cov_filename=cov_filename - ), - ], - use_sudo=True, - ) - context.machines[SUT].instance.execute( - [ - "chmod", - "666", - "/tmp/{cov_filename}".format(cov_filename=cov_filename), - ], - use_sudo=True, - ) + inner_dir = os.path.basename(scenario.filename.replace(".", "_")) + new_artifacts_dir = os.path.join( + cov_dir, + inner_dir, + ) + if not os.path.exists(new_artifacts_dir): + os.makedirs(new_artifacts_dir) + if hasattr(context, "machines") and SUT in context.machines: + try: + scenario_name = ( + os.path.basename(scenario.filename.replace(".", "_")) + + "_" + + str(scenario.line) + ) + cov_filename = ".coverage.{}".format(scenario_name) + tmp_covfile_path = "/tmp/{cov_filename}".format( + cov_filename=cov_filename + ) + context.machines[SUT].instance.execute( + [ + "mv", + "/home/ubuntu/.coverage", + tmp_covfile_path, + ], + use_sudo=True, + ) + context.machines[SUT].instance.execute( + [ + "chmod", + "666", + tmp_covfile_path, + ], + use_sudo=True, + ) - dest = os.path.join(new_artifacts_dir, cov_filename) - context.machines[SUT].instance.pull_file( - "/tmp/{cov_filename}".format(cov_filename=cov_filename), dest - ) - logging.warning("Done collecting coverage.") - except Exception as e: - logging.error(str(e)) - logging.warning("Failed to collect coverage") + dest = os.path.join(new_artifacts_dir, cov_filename) + context.machines[SUT].instance.pull_file( + tmp_covfile_path, dest + ) + logging.warning("Done collecting coverage.") + except Exception as e: + logging.error(str(e)) + logging.warning("Failed to collect coverage") def after_step(context, step): diff --git a/features/steps/machines.py b/features/steps/machines.py index 44e5fee318..a0c618cb3a 100644 --- a/features/steps/machines.py +++ b/features/steps/machines.py @@ -111,20 +111,9 @@ def given_a_machine( when_i_apt_update(context, machine_name=machine_name) # add coverage - when_i_apt_install(context, "python3-coverage", machine_name=machine_name) - - # Create the .coveragerc file in the lxd container - if hasattr(context, "machines") and SUT in context.machines: - context.machines[SUT].instance.execute( - [ - "bash", - "-c", - "echo -e '[run]\nrelative_files = True\n\n[paths]\nsource = \ - \n\tuaclient/ \ - \n\tusr/lib/python3/dist-packages/uaclient/' > \ - /usr/lib/python3/dist-packages/uaclient/.coveragerc", - ], - use_sudo=True, + if context.pro_config.collect_coverage: + when_i_apt_install( + context, "python3-coverage", machine_name=machine_name ) if cleanup: diff --git a/features/steps/shell.py b/features/steps/shell.py index d6045a2ca4..110f48f5d0 100644 --- a/features/steps/shell.py +++ b/features/steps/shell.py @@ -64,18 +64,16 @@ def when_i_run_command( ): command = process_template_vars(context, command) prefix = get_command_prefix_for_user_spec(user_spec) - if "pro" in command.split(): - command = command.replace( - "pro", - "python3 -m coverage run \ - --rcfile=/usr/lib/python3/dist-packages/uaclient/.coveragerc \ - --source=/usr/lib/python3/dist-packages/uaclient /usr/bin/pro", - 1, - ) - split_command = shlex.split(command) - print(split_command) - full_cmd = prefix + split_command + if context.pro_config.collect_coverage: + if command.split()[0] == "pro": + split_command = command.split() + split_command[ + 0 + ] = "python3 -m coverage run --source=/usr/lib/python3/dist-packages/uaclient /usr/bin/pro" # noqa: E501 + command = " ".join(split_command) + + full_cmd = prefix + shlex.split(command) if stdin is not None: stdin = stdin.replace("\\n", "\n")