#1442 delete_build: handle results of lazy build_references call
Merged 4 months ago by mikem. Opened 4 months ago by mikem.
mikem/koji delete-lazy-refs  into  master

file modified
+8 -7

@@ -7266,32 +7266,33 @@ 

      context.session.assertPerm('admin')

      binfo = get_build(build, strict=True)

      refs = build_references(binfo['id'], limit=10, lazy=True)

-     if refs['tags']:

+     if refs.get('tags'):

          if strict:

              raise koji.GenericError("Cannot delete build, tagged: %s" % refs['tags'])

          return False

-     if refs['rpms']:

+     if refs.get('rpms'):

          if strict:

              raise koji.GenericError("Cannot delete build, used in buildroots: %s" % refs['rpms'])

          return False

-     if refs['archives']:

+     if refs.get('archives'):

          if strict:

              raise koji.GenericError("Cannot delete build, used in archive buildroots: %s" % refs['archives'])

          return False

-     if refs['images']:

+     if refs.get('component_of'):

          if strict:

-             raise koji.GenericError("Cannot delete build, used in images: %r" % refs['images'])

+             raise koji.GenericError("Cannot delete build, used as component of: %r" % refs['component_of'])

          return False

-     if refs['last_used']:

+     if refs.get('last_used'):

          age = time.time() - refs['last_used']

          if age < min_ref_age:

              if strict:

                  raise koji.GenericError("Cannot delete build, used in recent buildroot")

              return False

-     #otherwise we can delete it

+     # otherwise we can delete it

      _delete_build(binfo)

      return True

  

+ 

  def _delete_build(binfo):

      """Delete a build (no reference checks)

  

@@ -16,7 +16,7 @@ 

      @mock.patch('kojihub.get_build')

      def test_delete_build_raise_error(self, build, context):

          context.session.assertPerm = mock.MagicMock()

-         references = ['tags', 'rpms', 'archives', 'images']

+         references = ['tags', 'rpms', 'archives', 'component_of']

          for ref in references:

              context = mock.MagicMock()

              context.session.return_value = context

@@ -32,7 +32,7 @@ 

      @mock.patch('kojihub.get_build')

      def test_delete_build_return_false(self, build, context):

          context.session.assertPerm = mock.MagicMock()

-         references = ['tags', 'rpms', 'archives', 'images']

+         references = ['tags', 'rpms', 'archives', 'component_of']

          for ref in references:

              context = mock.MagicMock()

              context.session.return_value = context

@@ -48,7 +48,7 @@ 

      @mock.patch('kojihub.get_build')

      def test_delete_build_check_last_used_raise_error(self, build, context):

          context.session.assertPerm = mock.MagicMock()

-         references = ['tags', 'rpms', 'archives', 'images', 'last_used']

+         references = ['tags', 'rpms', 'archives', 'component_of', 'last_used']

          for ref in references:

              context = mock.MagicMock()

              context.session.return_value = context

@@ -59,3 +59,18 @@ 

                      retval[ref] = time.time()+100

                      refs.return_value = retval

                      self.assertFalse(kojihub.delete_build(build='', strict=False))

+ 

+     @mock.patch('kojihub._delete_build')

+     @mock.patch('kojihub.build_references')

+     @mock.patch('kojihub.context')

+     @mock.patch('kojihub.get_build')

+     def test_delete_build_lazy_refs(self, build, context, buildrefs, _delete):

+         '''Test that we can handle lazy return from build_references'''

+         context.session.assertPerm = mock.MagicMock()

+         buildrefs.return_value = {'tags': []}

+         binfo = {'id': 'BUILD ID'}

+         build.return_value = binfo

+         kojihub.delete_build(build=binfo, strict=True)

+ 

+         # no build refs, so we should have called _delete_build

+         _delete.assert_called_with(binfo)