#1149 hub: new addArchiveType RPC
Merged 2 years ago by tkopecek. Opened 3 years ago by ktdreyer.
ktdreyer/koji add-archive-type  into  master

file modified
+33
@@ -6729,6 +6729,37 @@ 

      else:

          return None

  

+ def add_archive_type(name, description, extensions):

+     """

+     Add new archive type.

+ 

+     Use this to tell Koji about new builds' files' extensions before

+     importing the files.

+ 

+     :param str name: archive type name, eg. "yaml"

+     :param str description: eg. "YAML Ain't Markup Language"

+     :param str extensions: space-separated list of descriptions, eg. "yaml yml"

+     """

+     context.session.assertPerm('admin')

+     data = {'name': name,

+             'description': description,

+             'extensions': extensions,

+     }

+     if get_archive_type(type_name=name):

+         raise koji.GenericError("archivetype %s already exists" % name)

+     # No invalid or duplicate extensions

+     for ext in extensions.split(' '):

+         if not ext.replace('.', '').isalnum():

+             raise koji.GenericError('invalid %s file extension' % ext)

+         select = r"""SELECT id FROM archivetypes

+                       WHERE extensions ~* E'(\\s|^)%s(\\s|$)'""" % ext

+         results = _multiRow(select, {}, ('id',))

+         if len(results) > 0:

+             raise koji.GenericError('file extension %s already exists' % ext)

+     insert = InsertProcessor('archivetypes', data=data)

+     insert.execute()

+ 

+ 

  def new_maven_build(build, maven_info):

      """

      Add Maven metadata to an existing build.
@@ -10331,6 +10362,8 @@ 

      listBTypes = staticmethod(list_btypes)

      addBType = staticmethod(add_btype)

  

+     addArchiveType = staticmethod(add_archive_type)

+ 

      def getChangelogEntries(self, buildID=None, taskID=None, filepath=None, author=None, before=None, after=None, queryOpts=None):

          """Get changelog entries for the build with the given ID,

             or for the rpm generated by the given task at the given path

@@ -0,0 +1,53 @@ 

+ from __future__ import absolute_import

+ try:

+     import unittest2 as unittest

+ except ImportError:

+     import unittest

+ import mock

+ 

+ import koji

+ import kojihub

+ 

+ IP = kojihub.InsertProcessor

+ 

+ 

+ class TestAddArchiveType(unittest.TestCase):

+ 

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

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

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

+     def test_add_archive_type(self, InsertProcessor, get_archive_type,

+                               _multiRow):

+         # Not sure why mock can't patch kojihub.context, so we do this

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

+         mocks = [InsertProcessor, get_archive_type, session]

+         # It seems MagicMock will not automatically handle attributes that

+         # start with "assert"

+         session.assertPerm = mock.MagicMock()

+ 

+         # expected case

+         get_archive_type.return_value = None

+         insert = InsertProcessor.return_value

+         kojihub.add_archive_type('deb', 'Debian package', 'deb')

+         InsertProcessor.assert_called_once()

+         insert.execute.assert_called_once()

+ 

+         args, kwargs = InsertProcessor.call_args

+         ip = IP(*args, **kwargs)

+         self.assertEquals(ip.table, 'archivetypes')

+         self.assertEquals(ip.data, {'name': 'deb',

+                                     'description': 'Debian package',

+                                     'extensions': 'deb'})

+         self.assertEquals(ip.rawdata, {})

+         session.assertPerm.assert_called_with('admin')

+ 

+         for m in mocks:

+             m.reset_mock()

+         session.assertPerm = mock.MagicMock()

+ 

+         # already exists

+         get_archive_type.return_value = True

+         with self.assertRaises(koji.GenericError):

+             kojihub.add_archive_type('deb', 'Debian package', 'deb')

+         InsertProcessor.assert_not_called()

+         session.assertPerm.assert_called_with('admin')

Add a new hub method for inserting new archivetype records.

This closely matches addBType.

The purpose of this change is to make it easier to permit content generators to import files with extensions that we have not defined in the upstream packaged koji SQL file.

This might also require duplicate checking for 'extensions'

rebased onto 0daa145a2d0b8bd8ff50a3111be705f551eb7bb3

3 years ago

Thanks @julian8628 . I've added checks for duplicate extensions.

@ktdreyer One test failure:

======================================================================
ERROR: test_add_archive_type (tests.test_hub.test_add_archivetype.TestAddArchiveType)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/srv/jenkins/workspace/koji/label/F26/kojienv/lib/python2.7/site-packages/mock/mock.py", line 1305, in patched
    return func(*args, **keywargs)
  File "/srv/jenkins/workspace/koji/label/F26/tests/test_hub/test_add_archivetype.py", line 29, in test_add_archive_type
    kojihub.add_archive_type('deb', 'Debian package', 'deb')
  File "/srv/jenkins/workspace/koji/label/F26/hub/kojihub.py", line 6241, in add_archive_type
    results = _multiRow(select, {}, ('id',))
  File "/srv/jenkins/workspace/koji/label/F26/hub/kojihub.py", line 4568, in _multiRow
    return [dict(zip(fields, row)) for row in _fetchMulti(query, values)]
  File "/srv/jenkins/workspace/koji/label/F26/hub/kojihub.py", line 4539, in _fetchMulti
    c = context.cnx.cursor()
  File "/srv/jenkins/workspace/koji/label/F26/koji/context.py", line 46, in __getattr__
    return object.__getattribute__(data, key)
AttributeError: '_data' object has no attribute 'cnx'

The tests pass for me on Fedora 29... Koji developers, any clue what I've done wrong?

I don't see how this test could pass anywhere. You haven't mocked _multiRow().

rebased onto 69de58e1042ec1ce3180015291d3e9cb91cf1ac5

3 years ago

Sorry Mike, and thank you for pointing to my mistake there.

I've re-pushed a new version that mocks _multiRow().

rebased onto 2e65624a26eb40d3a0c7e4fdcbf6b3bb2e2018d5

3 years ago

rebased onto 05b6b00d8c1eb14a4d12af55d106ce81ba15a86c

2 years ago

rebased onto 85afbbe514c518802b349dcb78728eafcda509b4

2 years ago

rebased onto 6256d7f

2 years ago

Commit de15f88 fixes this pull-request

Pull-Request has been merged by tkopecek

2 years ago

Metadata Update from @jcupova:
- Pull-request tagged with: testing-done

2 years ago