Skip to content

Commit

Permalink
Fixed run_manage and assert_in_file actions
Browse files Browse the repository at this point in the history
  • Loading branch information
fernando79513 committed Jan 22, 2024
1 parent cef973d commit 7dfd1d6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 71 deletions.
50 changes: 28 additions & 22 deletions metabox/metabox/core/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(
mode,
*releases,
controller_revision="current",
agent_revision="current"
agent_revision="current",
):
self.mode = mode
self.releases = releases
Expand All @@ -68,11 +68,12 @@ def __init__(
self.controller_revision = controller_revision
self.agent_machine = None
self.agent_revision = agent_revision
self.start_session = True
self._checks = []
self._ret_code = None
self._stdout = ""
self._stderr = ""
self._oudstr_full = ""
self._outstr_full = ""
self._pts = None

def get_output_streams(self):
Expand All @@ -86,7 +87,12 @@ def has_passed(self):

def run(self):
# Simple scenarios don't need to specify a START step
if not any([isinstance(s, Start) for s in self.steps]):
# If there's no START step, add one unless the scenario
# explicitly says not to start a session
if (
not any([isinstance(s, Start) for s in self.steps])
and self.start_session
):
self.steps.insert(0, Start())
for i, step in enumerate(self.steps):
# Check how to start checkbox, interactively or not
Expand Down Expand Up @@ -160,8 +166,8 @@ def assertEqual(self, first, second):
def assertNotEqual(self, first, second):
self._checks.append(first != second)

def start(self, cmd='', interactive=False, timeout=0):
if self.mode == 'remote':
def start(self, cmd="", interactive=False, timeout=0):
if self.mode == "remote":
outcome = self.start_all(interactive=interactive, timeout=timeout)
if interactive:
self._pts = outcome
Expand Down Expand Up @@ -230,14 +236,22 @@ def select_test_plan(self, testplan_id, timeout=60):
def run_cmd(self, cmd, env={}, interactive=False, timeout=0, target="all"):
if self.mode == "remote":
if target == "controller":
self.controller_machine.run_cmd(cmd, env, interactive, timeout)
result = self.controller_machine.run_cmd(
cmd, env, interactive, timeout
)
elif target == "agent":
self.agent_machine.run_cmd(cmd, env, interactive, timeout)
result = self.agent_machine.run_cmd(
cmd, env, interactive, timeout
)
else:
self.controller_machine.run_cmd(cmd, env, interactive, timeout)
self.agent_machine.run_cmd(cmd, env, interactive, timeout)
result = self.agent_machine.run_cmd(
cmd, env, interactive, timeout
)
else:
self.local_machine.run_cmd(cmd, env, interactive, timeout)
result = self.local_machine.run_cmd(cmd, env, interactive, timeout)

return result

def reboot(self, timeout=0, target="all"):
if self.mode == "remote":
Expand Down Expand Up @@ -306,17 +320,13 @@ def mktree(self, path, privileged=False, timeout=0, target="all"):
cmd_str = shlex.join(cmd)
self.run_cmd(cmd_str, target=target, timeout=timeout)

def run_manage(self, command, privileged=False, timeout=0, target="all"):
def run_manage(self, args, privileged=False, timeout=0, target="all"):
"""
Runs the manage.py script with some arguments
"""
path = "/home/ubuntu/checkbox/metabox/metabox/metabox-provider"
cmd = ["manage.py", command, path]
if privileged:
cmd = ["sudo"] + cmd
cmd_str = shlex.join(cmd)
cmd_str = f"cd {path}; " + cmd_str
self.run_cmd(cmd_str, target=target, timeout=timeout)
cmd = f"bash -c 'cd {path} ; python3 manage.py {args}'"
self.run_cmd(cmd, target=target, timeout=timeout)

def assert_in_file(self, pattern, path):
"""
Expand All @@ -325,10 +335,6 @@ def assert_in_file(self, pattern, path):
:param patter: regular expresion to check against the lines.
:param path: path to the file
"""
result = self.run_cmd(f"cat {path}")
regex = re.compile(pattern)
with open(path, "r") as file:
data = file.read()
self._checks.append(
bool(regex.search(data))
or bool(regex.search(data))
)
self._checks.append(bool(regex.search(result.stdout)))
79 changes: 30 additions & 49 deletions metabox/metabox/scenarios/packaging/deb_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from . import units

provider_path = "/home/ubuntu/checkbox/metabox/metabox/metabox-provider"
packaging_pxu_path = f"{provider_path}/untis/packaging.pxu"
packaging_pxu_path = f"{provider_path}/units/packaging.pxu"
substvar_path = f"{provider_path}/debian/metabox-provider.substvars"


Expand All @@ -42,13 +42,13 @@ class DebPackagingJammy(Scenario):
"""

modes = ["local"]
start_session = False
config_override = {"local": {"releases": ["jammy"]}}
packaging_pxu = read_text(units, "packaging.pxu")

steps = [
Put(provider_path+"units/packaging.pxu", packaging_pxu),
RunManage(command="packaging"),
AssertRetCode(0),
Put(f"{provider_path}/units/packaging.pxu", packaging_pxu),
RunManage(args="packaging"),
AssertInFile("dep-pack-gt-20", substvar_path),
AssertInFile("rec-pack-gt-20", substvar_path),
AssertInFile("sug-pack-gt-20", substvar_path),
Expand All @@ -57,60 +57,41 @@ class DebPackagingJammy(Scenario):


@tag("packaging")
class DebPackagingJammy2(Scenario):
class DebPackagingFocal(Scenario):
"""
Verifies that the deb-packaging test pass on jammy (22.04).
Verifies that the deb-packaging test pass on focal (20.04).
"""

modes = ["local"]
config_override = {"local": {"releases": ["jammy"]}}
start_session = False
config_override = {"local": {"releases": ["focal"]}}
packaging_pxu = read_text(units, "packaging.pxu")

steps = [
Put(path, packaging_pxu),
Start("run 2021.com.canonical.certification::deb-packaging"),
AssertRetCode(0),
AssertPrinted("dep-pack-gt-20"),
AssertPrinted("rec-pack-gt-20"),
AssertPrinted("sug-pack-gt-20"),
Put(f"{provider_path}/units/packaging.pxu", packaging_pxu),
RunManage(args="packaging"),
AssertInFile("dep-pack-le-20", substvar_path),
AssertInFile("rec-pack-le-20", substvar_path),
AssertInFile("sug-pack-le-20", substvar_path),
RunCmd(f"rm -f {substvar_path}"),
]


# @tag("packaging")
# class DebPackagingFocal(Scenario):
# """
# Verifies that the deb-packaging test pass on focal (20.04).
# """

# modes = ["local"]
# config_override = {"local": {"releases": ["focal"]}}
# packaging_pxu = read_text(units, "packaging.pxu")

# steps = [
# Put(path, packaging_pxu),
# Start("run 2021.com.canonical.certification::deb-packaging"),
# AssertRetCode(0),
# AssertPrinted("dep-pack-le-20"),
# AssertPrinted("rec-pack-le-20"),
# AssertPrinted("sug-pack-le-20"),
# ]


# @tag("packaging")
# class DebPackagingBionic(Scenario):
# """
# Verifies that the deb-packaging test pass on bionic (18.04).
# """
@tag("packaging")
class DebPackagingBionic(Scenario):
"""
Verifies that the deb-packaging test pass on bionic (18.04).
"""

# modes = ["local"]
# config_override = {"local": {"releases": ["bionic"]}}
# packaging_pxu = read_text(units, "packaging.pxu")
modes = ["local"]
config_override = {"local": {"releases": ["bionic"]}}
packaging_pxu = read_text(units, "packaging.pxu")

# steps = [
# Put(path, packaging_pxu),
# Start("run 2021.com.canonical.certification::deb-packaging"),
# AssertRetCode(0),
# AssertPrinted("dep-pack-le-20"),
# AssertPrinted("rec-pack-le-20"),
# AssertPrinted("sug-pack-le-20"),
# ]
steps = [
Put(f"{provider_path}/units/packaging.pxu", packaging_pxu),
RunManage(args="packaging"),
AssertInFile("dep-pack-le-20", substvar_path),
AssertInFile("rec-pack-le-20", substvar_path),
AssertInFile("sug-pack-le-20", substvar_path),
RunCmd(f"rm -f {substvar_path}"),
]

0 comments on commit 7dfd1d6

Please sign in to comment.