#157 dont run anything in the build root
Merged 2 years ago by nphilipp. Opened 2 years ago by scoady.
fedora-infra/ scoady/rpmautospec 141  into  main

@@ -1,6 +1,4 @@ 

- import inspect

  import logging

- import shlex

  

  import koji

  from koji.plugin import callback
@@ -17,33 +15,8 @@ 

  pagure_proxy = None

  

  

- def _steal_buildroot_object_from_frame_stack():

-     buildroot = frame_info = frame = stack = None

-     try:

-         # Skip 2 frames, this fn and its caller

-         stack = inspect.stack()[2:]

-         for frame_info in stack:

-             frame = frame_info.frame

-             if (

-                 type(frame.f_locals.get("self")).__name__ == "BuildSRPMFromSCMTask"

-                 and frame_info.function == "handler"

-             ):

-                 # The handler() method calls this broot internally

-                 buildroot = frame.f_locals.get("broot")

-                 break

-     finally:

-         # Explicitly delete references to frame objects to avoid memory leaks, see:

-         # https://docs.python.org/3/library/inspect.html#the-interpreter-stack

-         del frame, frame_info, stack

- 

-     if not buildroot:

-         raise RuntimeError("Can't steal `broot` from BuildSRPMFromSCMTask.")

- 

-     return buildroot

- 

- 

  @callback("postSCMCheckout")

- def process_distgit_cb(cb_type, *, srcdir, build_tag, session, taskinfo, **kwargs):

+ def process_distgit_cb(cb_type, *, srcdir, taskinfo, **kwargs):

      if taskinfo["method"] != "buildSRPMFromSCM":

          # callback should be run only in this instance

          # i.e. maven and image builds don't have spec-files
@@ -63,33 +36,4 @@ 

              _log.exception(message, CONFIG_FILE)

              return

  

-     buildroot = kwargs.get("buildroot")

-     if not buildroot:

-         _log.debug("Stealing buildroot from caller.")

-         buildroot = _steal_buildroot_object_from_frame_stack()

- 

-     # Save previous log level of the buildroot logger...

-     buildroot_loglevel = buildroot.logger.level

- 

-     # ...and set our own

-     buildroot.logger.setLevel(logging.DEBUG)

- 

-     br_packages = buildroot.getPackageList()

-     if not any(p["name"] == "rpmautospec" for p in br_packages):

-         _log.info("Installing rpmautospec into build root")

-         status = buildroot.mock(["--install", "rpmautospec"])

-         if status:

-             raise koji.BuildError("Installing `rpmautospec` into the build root failed.")

- 

-     srcdir_within = shlex.quote(buildroot.path_without_to_within(srcdir))

-     status = buildroot.mock(

-         ["--shell", f"rpmautospec --debug process-distgit --process-specfile {srcdir_within}"]

-     )

- 

-     if status:

-         raise koji.BuildError(

-             f"Running `rpmautospec` inside the build root failed (status: {status})"

-         )

- 

-     # Restore log level of the buildroot logger

-     buildroot.logger.level = buildroot_loglevel

+     process_distgit.process_specfile(srcdir=srcdir)

@@ -5,7 +5,6 @@ 

  import shutil

  import tempfile

  

- import rpm

  

  from .changelog import produce_changelog

  from .misc import koji_init
@@ -73,7 +72,7 @@ 

      return autochangelog_re.match(line)

  

  

- def get_autorelease(srcdir, dist, session):

+ def get_autorelease(srcdir):

      # Not setting latest_evr, next_epoch_version just goes with what's in the package and latest

      # builds.

      release = calculate_release(srcdir=srcdir)
@@ -122,22 +121,14 @@ 

  

  def process_specfile(

      srcdir,

-     dist,

-     session,

-     has_autorelease,

-     has_autochangelog,

-     changelog_lineno,

-     autochangelog_lineno,

+     has_autorelease=None,

+     has_autochangelog=None,

+     changelog_lineno=None,

+     autochangelog_lineno=None,

  ):

      specfile_name = get_specfile_name(srcdir)

  

-     if not dist:

-         dist = rpm.expandMacro("%dist")

- 

-     if dist.startswith("."):

-         dist = dist[1:]

- 

-     autorelease_number = get_autorelease(srcdir, dist, session)

+     autorelease_number = get_autorelease(srcdir)

      with open(specfile_name, "r") as specfile, tempfile.NamedTemporaryFile("w") as tmp_specfile:

          # Process the spec file into a temporary file...

          if has_autorelease:
@@ -208,7 +199,6 @@ 

          process_specfile(

              srcdir,

              dist,

-             session,

              has_autorelease,

              has_autochangelog,

              changelog_lineno,

@@ -1,7 +1,5 @@ 

- import shlex

  from unittest import mock

  

- import koji

  import pytest

  

  from koji_plugins.rpmautospec_builder import process_distgit_cb
@@ -33,66 +31,41 @@ 

          "user": None,

      }

  

-     data_build_tag = {

-         "id": 11522,

-         "name": "f32-build",

-         "arches": "armv7hl i686 x86_64 aarch64 ppc64le s390x",

-         "extra": {},

-         "locked": False,

-         "maven_include_all": False,

-         "maven_support": False,

-         "perm": "admin",

-         "perm_id": 1,

-     }

- 

      @pytest.mark.parametrize(

          "testcase",

          (

              "normal",

-             "no buildroot supplied",

-             "rpmautospec installed",

              "other taskinfo method",

              "skip processing",

-             "buildroot install fails",

-             "buildroot cmd fails",

          ),

      )

+     @mock.patch("rpmautospec.process_distgit.process_specfile")

      @mock.patch("rpmautospec.process_distgit.needs_processing")

-     @mock.patch("koji_plugins.rpmautospec_builder._steal_buildroot_object_from_frame_stack")

-     @mock.patch("koji_plugins.rpmautospec_builder.pagure_proxy")

      @mock.patch("koji.read_config_files")

      def test_process_distgit_cb(

          self,

          read_config_files,

-         pagure_proxy,

-         steal_buildroot_fn,

          needs_processing_fn,

+         process_specfile_fn,

          testcase,

      ):

          """Test the process_distgit_cb() function"""

          read_config_files.return_value = MockConfig()

  

-         buildroot_supplied = testcase != "no buildroot supplied"

-         rpmautospec_installed = testcase == "rpmautospec installed"

          taskinfo_method_responsible = testcase != "other taskinfo method"

          skip_processing = testcase == "skip processing"

-         buildroot_install_fails = testcase == "buildroot install fails"

-         buildroot_cmd_fails = testcase == "buildroot cmd fails"

  

          # prepare test environment

-         srcdir_within = "/builddir/build/BUILD/something with spaces"

-         unpacked_repo_dir = f"/var/lib/mock/some-root/{srcdir_within}"

+         specfile_dir = "some dummy path"

          args = ["postSCMCheckout"]

          koji_session = mock.MagicMock()

          kwargs = {

              "scminfo": self.data_scm_info,

-             "build_tag": self.data_build_tag,

              "scratch": mock.MagicMock(),

-             "srcdir": unpacked_repo_dir,

+             "srcdir": specfile_dir,

              "taskinfo": {"method": "buildSRPMFromSCM"},

              "session": koji_session,

          }

-         mock_retvals = []

  

          if not taskinfo_method_responsible:

              kwargs["taskinfo"]["method"] = "not the method you're looking for"
@@ -100,71 +73,14 @@ 

          if skip_processing:

              needs_processing_fn.return_value = False

  

-         buildroot = mock.MagicMock()

-         installed_packages = [

-             {"name": "foo", "version": "1.0", "release": "3"},

-             {"name": "bar", "version": "1.1", "release": "1"},

-             {"name": "baz", "version": "2.0", "release": "2"},

-         ]

- 

-         if rpmautospec_installed:

-             installed_packages.append({"name": "rpmautospec", "version": "0.1", "release": "1"})

-         else:

-             if buildroot_install_fails:

-                 mock_retvals.append(1)

-             else:

-                 mock_retvals.append(0)

- 

-         buildroot.getPackageList.return_value = installed_packages

- 

-         if buildroot_supplied:

-             kwargs["buildroot"] = buildroot

-         else:

-             steal_buildroot_fn.return_value = buildroot

- 

-         buildroot.path_without_to_within.return_value = srcdir_within

- 

-         buildroot.mock.side_effect = mock_retvals

- 

-         if buildroot_cmd_fails:

-             mock_retvals.append(128)

-         else:

-             mock_retvals.append(0)

- 

-         # test the callback

-         if any(retval != 0 for retval in mock_retvals):

-             with pytest.raises(koji.BuildError):

-                 process_distgit_cb(*args, **kwargs)

-             return

-         else:

-             process_distgit_cb(*args, **kwargs)

- 

          # verify what the callback did

          if not taskinfo_method_responsible:

              needs_processing_fn.assert_not_called()

              return

  

-         needs_processing_fn.assert_called_once_with(unpacked_repo_dir)

- 

-         if skip_processing:

-             buildroot.getPackageList.assert_not_called()

-             return

- 

-         buildroot.getPackageList.assert_called_once()

-         buildroot.path_without_to_within.assert_called_once()

- 

-         if buildroot_supplied:

-             steal_buildroot_fn.assert_not_called()

-         else:

-             steal_buildroot_fn.assert_called_once_with()

+         needs_processing_fn.return_value = True

+         process_specfile_fn.return_value = None

  

-         if rpmautospec_installed:

-             assert not any("--install" in call[0] for call in buildroot.mock.call_args_list)

-         else:

-             buildroot.mock.assert_any_call(["--install", "rpmautospec"])

+         process_distgit_cb(*args, **kwargs)

  

-         mock_args = [

-             "--shell",

-             f"rpmautospec --debug process-distgit --process-specfile {shlex.quote(srcdir_within)}",

-         ]

-         buildroot.mock.assert_called_with(mock_args)

+         process_specfile_fn.assert_called_once()

@@ -1,34 +0,0 @@ 

- # Our dummy-test-packages are named after canary varieties, meet Gloster, Rubino and Crested

- # Source: https://www.omlet.co.uk/guide/finches_and_canaries/canary/canary_varieties

- Name:           dummy-test-package-gloster

- 

- Version:        0

- Release:        %{autorelease}

- Summary:        Dummy Test Package called Gloster

- License:        MIT

- URL:            http://fedoraproject.org/wiki/DummyTestPackages

- 

- # The tarball contains a file with an uuid to test later and a LICENSE

- Source0:        %{name}-%{version}.tar.gz

- 

- BuildArch:      noarch

- 

- %description

- This is a dummy test package for the purposes of testing if the Fedora CI

- pipeline is working. There is nothing useful here.

- 

- %prep

- %autosetup

- 

- %build

- # nothing to do

- 

- %install

- mkdir -p %{buildroot}%{_datadir}

- cp -p uuid %{buildroot}%{_datadir}/%{name}

- 

- %files

- %license LICENSE

- %{_datadir}/%{name}

- 

- %autochangelog

Build failed. More information on how to proceed and troubleshoot errors available at https://fedoraproject.org/wiki/Zuul-based-ci

rebased onto 51140f4367f9be150e958ab6a6bb954127209413

2 years ago

Build failed. More information on how to proceed and troubleshoot errors available at https://fedoraproject.org/wiki/Zuul-based-ci

rebased onto 23f49f26e462be86a7c60a59d92af291242b913c

2 years ago

Build succeeded.

Metadata Update from @nphilipp:
- Request assigned

2 years ago

The PR removes this from the function signature, so we can get rid of it in the tests, too (here and above in data_build_tag = ..., right?

Not sure what this is for… up to this point, we've only messed around with mocks in the test method. :wink:

rebased onto df472fb3f1f2fbf42017a4336a26f8c66ae5f75b

2 years ago

Build succeeded.

rebased onto adf0a8c4e85d70b694b6e49fbab85f4c883dbc03

2 years ago

Build succeeded.

rebased onto a601ed6

2 years ago

Pull-Request has been merged by nphilipp

2 years ago

Build succeeded.