#2721 API: createWinBuild with wrong win/build info
Merged a year ago by tkopecek. Opened a year ago by jcupova.
jcupova/koji issue1103  into  master

file modified
+25 -4
@@ -7156,6 +7156,12 @@ 

      win_info must contain a 'platform' key.

      """

      build_id = build_info['id']

+     if not isinstance(win_info, dict):

+         raise koji.GenericError('Invalid type for win_info: %s' % type(win_info))

+     if win_info == {}:

+         raise koji.GenericError("Windows info is empty")

+     if 'platform' not in win_info.keys():

+         raise koji.GenericError("Windows info doesn't have mandatory platform key")

      current = get_win_build(build_id, strict=False)

      if current:

          if current['platform'] != win_info['platform']:
@@ -10739,16 +10745,31 @@ 

  

      def createWinBuild(self, build_info, win_info):

          """

-         Associate Windows metadata with an existing build.  The build must

-         not already have associated Windows metadata.  win_info must be a dict

-         containing a platform entry.

+         Associate Windows metadata with an existing build. When build isn`t existing, creates

+         a build. The build must not already have associated Windows metadata.  win_info must be

+         a dict containing a platform entry.

+         :param build_info: a str (build name) if build is existing

+                            or a dict:

+                                - name: build name

+                                - version: build version

+                                - release: build release

+                                - epoch: build epoch

+         :param dict win_info:

+                                - platform: build platform

+         :raises: GenericError if type for build_info is not dict, when build isn`t existing.

+         :raises: GenericError if build info doesn't have mandatory keys.

          """

          context.session.assertPerm('win-import')

          if not context.opts.get('EnableWin'):

              raise koji.GenericError("Windows support not enabled")

          build = get_build(build_info)

          if not build:

-             build_id = new_build(dslice(build_info, ('name', 'version', 'release', 'epoch')))

+             if not isinstance(build_info, dict):

+                 raise koji.GenericError('Invalid type for build_info: %s' % type(build_info))

+             try:

+                 build_id = new_build(dslice(build_info, ('name', 'version', 'release', 'epoch')))

+             except KeyError as cm:

+                 raise koji.GenericError("Build info doesn't have mandatory %s key" % cm)

              build = get_build(build_id, strict=True)

          new_win_build(build, win_info)

  

@@ -0,0 +1,133 @@ 

+ import unittest

+ 

+ import mock

+ 

+ import koji

+ import kojihub

+ 

+ IP = kojihub.InsertProcessor

+ 

+ 

+ class TestCreateWinBuild(unittest.TestCase):

+ 

+     def setUp(self):

+         self.get_build = mock.patch('kojihub.get_build').start()

+         self.exports = kojihub.RootExports()

+         self.session = mock.MagicMock()

+         self.context = mock.patch('kojihub.context').start()

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

+         self.InsertProcessor = mock.patch('kojihub.InsertProcessor',

+                                           side_effect=self.getInsert).start()

+         self.inserts = []

+         self.insert_execute = mock.MagicMock()

+         self.get_build_info = {

+             'id': 100,

+             'name': 'test_name',

+             'version': 'test_version',

+             'release': 'test_release',

+             'epoch': 'test_epoch',

+             'owner': 'test_owner',

+             'extra': {'extra_key': 'extra_value'},

+             'build_id': 2,

+         }

+         self.build_info = 'test-build-11-12'

+ 

+     def getInsert(self, *args, **kwargs):

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

+         insert.execute = self.insert_execute

+         self.inserts.append(insert)

+         return insert

+ 

+     def test_win_info_empty_dict(self):

+         win_info = {}

+         self.get_build.return_value = self.get_build_info

+         with self.assertRaises(koji.GenericError) as cm:

+             self.exports.createWinBuild(self.build_info, win_info)

+         self.assertEqual("Windows info is empty", str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

+ 

+     def test_win_info_without_platform(self):

+         win_info = {

+             'test-key': 'test-value'

+         }

+         self.get_build.return_value = self.get_build_info

+         with self.assertRaises(koji.GenericError) as cm:

+             self.exports.createWinBuild(self.build_info, win_info)

+         self.assertEqual("Windows info doesn't have mandatory platform key", str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

+ 

+     def test_empty_wrong_format_win_info(self):

+         win_info = 'platform'

+         self.get_build.return_value = self.get_build_info

+         with self.assertRaises(koji.GenericError) as cm:

+             self.exports.createWinBuild(self.build_info, win_info)

+         self.assertEqual('Invalid type for win_info: %s' % type(win_info), str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

+ 

+     def test_empty_wrong_format_non_exist_build_info(self):

+         win_info = {

+             'platform': 'test-value'

+         }

+         self.get_build.return_value = None

+         with self.assertRaises(koji.GenericError) as cm:

+             self.exports.createWinBuild(self.build_info, win_info)

+         self.assertEqual(

+             'Invalid type for build_info: %s' % type(self.build_info), str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

+ 

+     def test_build_info_without_some_mandatory_key(self):

+         win_info = {

+             'platform': 'test-value'

+         }

+ 

+         # build_info without name

+         get_build_info = {

+             'id': 100,

+             'version': 'test_version',

+             'release': 'test_release',

+             'epoch': 'test_epoch',

+         }

+         self.get_build.return_value = None

+         with self.assertRaises(koji.GenericError) as cm:

+             self.exports.createWinBuild(get_build_info, win_info)

+         self.assertEqual("Build info doesn't have mandatory 'name' key", str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

+ 

+         # build_info without version

+         get_build_info = {

+             'id': 100,

+             'name': 'test_name',

+             'release': 'test_release',

+             'epoch': 'test_epoch',

+         }

+         self.get_build.return_value = None

+         with self.assertRaises(koji.GenericError) as cm:

+             self.exports.createWinBuild(get_build_info, win_info)

+         self.assertEqual("Build info doesn't have mandatory 'version' key", str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

+ 

+         # build_info without release

+         get_build_info = {

+             'id': 100,

+             'name': 'test_name',

+             'version': 'test_version',

+             'epoch': 'test_epoch',

+         }

+         self.get_build.return_value = None

+         with self.assertRaises(koji.GenericError) as cm:

+             self.exports.createWinBuild(get_build_info, win_info)

+         self.assertEqual("Build info doesn't have mandatory 'release' key", str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

+ 

+         # build_info without epoch

+         get_build_info = {

+             'id': 100,

+             'name': 'test_name',

+             'version': 'test_version',

+             'release': 'test_release',

+         }

+         self.get_build.return_value = None

+         with self.assertRaises(koji.GenericError) as cm:

+             self.exports.createWinBuild(get_build_info, win_info)

+         self.assertEqual("Build info doesn't have mandatory 'epoch' key", str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

@@ -0,0 +1,55 @@ 

+ import unittest

+ 

+ import mock

+ 

+ import koji

+ import kojihub

+ 

+ IP = kojihub.InsertProcessor

+ 

+ 

+ class TestNewWinBuild(unittest.TestCase):

+     def setUp(self):

+         self.InsertProcessor = mock.patch('kojihub.InsertProcessor',

+                                           side_effect=self.getInsert).start()

+         self.inserts = []

+         self.insert_execute = mock.MagicMock()

+ 

+         self.build_info = {

+             'id': 100,

+             'name': 'test_name',

+             'version': 'test_version',

+             'release': 'test_release',

+             'epoch': 'test_epoch',

+             'owner': 'test_owner',

+             'extra': {'extra_key': 'extra_value'},

+         }

+ 

+     def getInsert(self, *args, **kwargs):

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

+         insert.execute = self.insert_execute

+         self.inserts.append(insert)

+         return insert

+ 

+     def test_empty_win_info(self):

+         win_info = {}

+         with self.assertRaises(koji.GenericError) as cm:

+             kojihub.new_win_build(self.build_info, win_info)

+         self.assertEqual("Windows info is empty", str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

+ 

+     def test_win_info_without_platform(self):

+         win_info = {

+             'test-key': 'test-value'

+         }

+         with self.assertRaises(koji.GenericError) as cm:

+             kojihub.new_win_build(self.build_info, win_info)

+         self.assertEqual("Windows info doesn't have mandatory platform key", str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

+ 

+     def test_wrong_format_win_info(self):

+         win_info = 'test-win-info'

+         with self.assertRaises(koji.GenericError) as cm:

+             kojihub.new_win_build(self.build_info, win_info)

+         self.assertEqual('Invalid type for win_info: %s' % type(win_info), str(cm.exception))

+         self.assertEqual(len(self.inserts), 0)

if not wininfo can catch more problems.

info is missing or empty (with previous change)

rebased onto 658e6ff31cad812ea8f1a07164fdd3d67dc5a6ec

a year ago

@tkopecek added when info is missing
added also fix for issue 1102

I would move all wininfo checks into new_win_build which is called from more places.

rebased onto d38e3a6a6cb97e901277ffab75b38116dec6c609

a year ago

rebased onto fb81e1af82b95c1bfd7a25a97edf0e20c4936490

a year ago

We've line length 99 (see .flake8), so it is ok to put these on one line.

Metadata Update from @tkopecek:
- Pull-request tagged with: testing-ready

a year ago

rebased onto f0bc5305e47f4e80199b8d2de4b0bab9560e255e

a year ago

rebased onto 413848ebacc37ac527dfe01a6cc129dcc9495c2d

a year ago

rebased onto 4288ba620c5b8e5ec619d7a4741cd74c71b26edd

a year ago

rebased onto 371b7c8

a year ago

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

a year ago

Commit 33efc94 fixes this pull-request

Pull-Request has been merged by tkopecek

a year ago