Skip to content

Commit

Permalink
Add coveragerc file
Browse files Browse the repository at this point in the history
  • Loading branch information
dheyay authored and renanrodrigo committed Mar 21, 2024
1 parent 59e16f9 commit b2f913a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 71 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[paths]
source =
uaclient/
/usr/lib/python3/dist-packages/uaclient/
94 changes: 47 additions & 47 deletions features/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -414,55 +417,52 @@ 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")
os.makedirs(cov_dir, exist_ok=True)

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,
)
os.makedirs(new_artifacts_dir, exist_ok=True)
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):
Expand Down
17 changes: 3 additions & 14 deletions features/steps/machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 9 additions & 10 deletions features/steps/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,17 @@ 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,

if context.pro_config.collect_coverage:
coverage_command = (
"python3 -m coverage run "
"--source=/usr/lib/python3/dist-packages/uaclient /usr/bin/pro"
)
split_command = command.split()
if split_command[0] == "pro":
command = " ".join([coverage_command] + split_command[1:])

split_command = shlex.split(command)
print(split_command)
full_cmd = prefix + split_command
full_cmd = prefix + shlex.split(command)

if stdin is not None:
stdin = stdin.replace("\\n", "\n")
Expand Down

0 comments on commit b2f913a

Please sign in to comment.