Skip to content

Commit

Permalink
Added tests for the resume feature. + minor bugfix
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Curlej <[email protected]>
  • Loading branch information
mcurlej committed Jan 11, 2022
1 parent 31cf2f9 commit 2dcd80a
Show file tree
Hide file tree
Showing 4 changed files with 553 additions and 33 deletions.
27 changes: 13 additions & 14 deletions module_build/builders/mock_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def build(self, module_stream, resume, context_to_build=None):

# when the batch has finished building all its components, we will turn the batch
# dir into a module stream. `finalize_batch` will add a modules.yaml file so the dir
# and its build rpms can be used in the next batch as modular dependencies
# and its built rpms can be used in the /next batch as modular dependencies
self.finalize_batch(position, context_name)

build_context["build_batches"][position]["batch_state"] = self.states[3]
Expand Down Expand Up @@ -639,13 +639,10 @@ def find_and_set_resume_point(self):
for context_name, context in self.build_contexts.items():
build_batches = context["build_batches"]

# if the context dir for the next context does not exist. The resume point is the next
# context in line, the first batch and the first component of this batch.
# if the context dir does not exists the build process did not start yet for that
# context
if context["nsvca"] not in context_dirs:
resume_point["context"] = context_name
resume_point["batch"] = 0
resume_point["component"] = build_batches[0]["components"][0]["name"]
break
continue

cd_path = self.workdir + "/" + context["nsvca"]
# we look for the finished filename in the context dir
Expand Down Expand Up @@ -877,11 +874,14 @@ def find_and_set_resume_point(self):
resume_point["component"] = next_comp["name"]
break
else:
# if the next batch to build is not present between the batch dirs
# the batch and its first component should be set as the resume point.
resume_point["batch"] = position
resume_point["component"] = build_batches[position]["components"][0]["name"]
break
if "batch" not in resume_point:
# if all of the existing batches are finished and the batch resume point
# was not found we set the resume point for the next batch in line and
# its first component
first_component = build_batches[position]["components"][0]
resume_point["batch"] = position
resume_point["component"] = first_component["name"]
break

context["dir"] = cd_path

Expand All @@ -890,6 +890,7 @@ def find_and_set_resume_point(self):
# context to resume. When we have a resume point we need to remove final repo because
# the number of build RPMs can change.
# TODO put the removing of final repo to its own method
context_name = resume_point["context"]
context_dir_path = self.build_contexts[context_name]["dir"]
final_repo_path = context_dir_path + "/final_repo"

Expand All @@ -899,8 +900,6 @@ def find_and_set_resume_point(self):
shutil.rmtree(final_repo_path)

if len(resume_point) and len(resume_point) == 1 and "context" in resume_point:
context_name = resume_point["context"]

msg = ("The context '{name}' has finished building all its batches and components."
"It seems it was not finalized. Finalizing context...").format(
name=context_name)
Expand Down
16 changes: 15 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,32 @@ def mock_mmdv3_and_version(modulemd_path="modulemd/perl-bootstrap.yaml",
return mmd, version


def fake_buildroot_run(self):
def fake_buildroot_run(self, component_to_fail=None, context=None):
""" Fake function which creates dummy rpm file in the result directory of a component. The
NEVRA of the RPM is fake. The only real parts are the name and the modular rpm suffix which
in real life overrides the %{dist} macro. The function represents a succesfull build in the
mock buildroot """
if component_to_fail == self.component["name"] and not context:
msg = "Build of component '{component}' failed!!".format(
component=self.component["name"])
raise Exception(msg)

if (context and self.modularity_label.endswith(context)
and component_to_fail == self.component["name"]):
msg = "Build of component '{component}' of context '{context}' failed!!".format(
component=self.component["name"],
context=context)
raise Exception(msg)

rpm_filename = "/{name}-0:1.0-1{dist}.x86_64.rpm".format(name=self.component["name"],
dist=self.rpm_suffix)

with open(self.result_dir_path + rpm_filename, "w") as f:
f.write("dummy")

with open(self.result_dir_path + "/finished", "w") as f:
f.write("dummy")

self.finished = True

return "", 0
Expand Down
34 changes: 16 additions & 18 deletions tests/builders/test_mock_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

def test_create_mock_builder(tmpdir):
""" Test for an instance of MockBuilder. This serves as a sanity test. """
cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = cwd + "/rootdir"
mock_cfg_path = "/etc/mock/fedora-35-x86_64.cfg"
external_repos = ["/repo1", "/repo2"]
Expand All @@ -31,7 +31,7 @@ def test_create_mock_builder(tmpdir):
def test_generate_buildbatches(tmpdir):
""" Test the generation of build batches for the build process of a module. This serves as a
sanity test. """
cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = None
mock_cfg_path = get_full_data_path("mock_cfg/fedora-35-x86_64.cfg")
external_repos = []
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_generate_buildbatches(tmpdir):

def test_generate_buildbatches_with_empty_buildorder_property(tmpdir):
""" Test the generation of build batches when some components are missing buildorders """
cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = None
mock_cfg_path = get_full_data_path("mock_cfg/fedora-35-x86_64.cfg")
external_repos = []
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_generate_buildbatches_with_empty_buildorder_property(tmpdir):

def test_generate_buildbatches_with_no_buildorder(tmpdir):
""" Test the generation of build batches when there is no buildorder set """
cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = None
mock_cfg_path = get_full_data_path("mock_cfg/fedora-35-x86_64.cfg")
external_repos = []
Expand All @@ -123,7 +123,7 @@ def test_create_build_contexts(mock_config, tmpdir):
""" Test for creation and initialization of the metadata which will keep track and the state of
build process for each context defined in a module stream. This serves as a sanity test.
"""
cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = None
mock_cfg_path = get_full_data_path("mock_cfg/fedora-35-x86_64.cfg")
external_repos = []
Expand Down Expand Up @@ -178,7 +178,7 @@ def test_create_build_contexts(mock_config, tmpdir):
return_value={"target_arch": "x86_64", "dist": "fc35"})
def test_create_build_context_dir(mock_config, tmpdir):
""" Test for the creating a `context` dir inside our working directory """
cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = None
mock_cfg_path = get_full_data_path("mock_cfg/fedora-35-x86_64.cfg")
external_repos = []
Expand All @@ -198,15 +198,15 @@ def test_create_build_context_dir(mock_config, tmpdir):
for name in context_names:
builder.create_build_context_dir(name)

for directory in cwd.listdir():
assert directory.basename in expected_dir_names
for name in expected_dir_names:
assert name in os.listdir(cwd)


def test_create_build_context_dir_raises_no_build_context_metadata(tmpdir):
""" Test the builder to raise when creating a `context` directory when the builder was not
initialized correctly.
"""
cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = None
mock_cfg_path = get_full_data_path("mock_cfg/fedora-35-x86_64.cfg")
external_repos = []
Expand All @@ -225,7 +225,7 @@ def test_create_build_context_dir_raises_no_build_context_metadata(tmpdir):
return_value={"target_arch": "x86_64", "dist": "fc35"})
def test_create_build_batch_dir(mock_config, tmpdir):
""" Test for the creating a `build_batches` and `batch` directory """
cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = None
mock_cfg_path = get_full_data_path("mock_cfg/fedora-35-x86_64.cfg")
external_repos = []
Expand All @@ -242,12 +242,10 @@ def test_create_build_batch_dir(mock_config, tmpdir):

builder.create_build_batch_dir(context_names[0], 0)

batch_dir = cwd.listdir()[0].listdir()[0]
build_batches_dir = os.listdir(cwd + "/" + os.listdir(cwd)[0] + "/build_batches")

for directory in batch_dir.listdir():
assert "batch_0" == directory.basename
expected_path = "perl-bootstrap:devel:20210925131649:f26devel:x86_64/build_batches/batch_0"
assert expected_path in str(directory)
assert len(build_batches_dir) == 1
assert "batch_0" == build_batches_dir[0]


def test_create_build_batch_dir_raises_no_build_context_metadata(tmpdir):
Expand Down Expand Up @@ -276,7 +274,7 @@ def test_build_perl_bootstrap(mock_config, tmpdir):
""" This is a sanity test for the build process. We are testing here if everything is created
as expected. We will use `fake_buildroot_run` to fake the actual build in a mock buildroot. """

cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = None
mock_cfg_path = get_full_data_path("mock_cfg/fedora-35-x86_64.cfg")
external_repos = []
Expand Down Expand Up @@ -361,7 +359,7 @@ def test_build_perl_bootstrap(mock_config, tmpdir):
return_value={"target_arch": "x86_64", "dist": "fc35"})
def test_build_specific_context(mock_config, tmpdir):

cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = None
mock_cfg_path = get_full_data_path("mock_cfg/fedora-35-x86_64.cfg")
external_repos = []
Expand Down Expand Up @@ -403,7 +401,7 @@ def test_build_invalid_context(mock_config, tmpdir):
stream.
"""

cwd = tmpdir.mkdir("workdir")
cwd = tmpdir.mkdir("workdir").strpath
rootdir = None
mock_cfg_path = get_full_data_path("mock_cfg/fedora-35-x86_64.cfg")
external_repos = []
Expand Down
Loading

0 comments on commit 2dcd80a

Please sign in to comment.