Skip to content

Commit

Permalink
Add coverage for behave tests
Browse files Browse the repository at this point in the history
Fixes: #2903
  • Loading branch information
dheyay committed Mar 18, 2024
1 parent f6a4f07 commit eeb5c18
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
53 changes: 53 additions & 0 deletions features/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,59 @@ 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)

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,
)

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")


def after_step(context, step):
"""Collect test artifacts in the event of failure."""
if step.status == "failed":
Expand Down
19 changes: 18 additions & 1 deletion features/steps/machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from behave import given, when
from pycloudlib.instance import BaseInstance # type: ignore

from features.steps.packages import when_i_apt_update
from features.steps.packages import when_i_apt_install, when_i_apt_update
from features.steps.shell import when_i_run_command
from features.steps.ubuntu_advantage_tools import when_i_install_uat
from features.util import (
Expand Down Expand Up @@ -110,6 +110,23 @@ def given_a_machine(
# make sure the machine has up-to-date apt data
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 cleanup:

def cleanup_instance():
Expand Down
13 changes: 12 additions & 1 deletion features/steps/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,18 @@ def when_i_run_command(
):
command = process_template_vars(context, command)
prefix = get_command_prefix_for_user_spec(user_spec)
full_cmd = prefix + shlex.split(command)
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 stdin is not None:
stdin = stdin.replace("\\n", "\n")
Expand Down

0 comments on commit eeb5c18

Please sign in to comment.