#1415 New 'buildtype' test for policies
Merged 4 years ago by mikem. Opened 5 years ago by tkopecek.
tkopecek/koji issue1225  into  master

@@ -209,6 +209,9 @@ 

      * for the tag policies, determines the build tag from the build data,

        which will by null for imported builds

  

+ ``buildtype``

+     * checks the build type(s) against the arguments

+ 

  ``skip_tag``

      * checks to see if the --skip-tag option was used

      * only applicable to the build_from_* policies

file modified
+21
@@ -8349,6 +8349,15 @@ 

      return tags

  

  

+ def policy_get_build_types(data):

+     if 'btypes' in data:

+         # btypes can be already populated by caller

+         return set(data['btypes'])

+     if 'build' in data:

+         binfo = get_build(data['build'], strict=True)

+         return set(get_build_type(binfo).keys())

+     return set()

+ 

  class NewPackageTest(koji.policy.BaseSimpleTest):

      """Checks to see if a package exists yet"""

      name = 'is_new_package'
@@ -8504,6 +8513,18 @@ 

          return False

  

  

+ class BuildTypeTest(koji.policy.BaseSimpleTest):

+     """Check the build type(s) of the build"""

+ 

+     name = 'buildtype'

+     def run(self, data):

+         args = self.str.split()[1:]

+         for btype in policy_get_build_types(data):

+             if multi_fnmatch(btype, args):

+                 return True

+         return False

+ 

+ 

  class ImportedTest(koji.policy.BaseSimpleTest):

      """Check if any part of a build was imported

  

@@ -245,3 +245,38 @@ 

          # check no match case

          self.list_tags.return_value = []

          self.assertFalse(obj.run(data))

+ 

+ 

+ class TestBuildTypeTest(unittest.TestCase):

+     def setUp(self):

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

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

+ 

+     def tearDown(self):

+         mock.patch.stopall()

+ 

+     def test_invalid(self):

+         binfo = {'id': 1, 'name': 'nvr-1-2'}

+         self.get_build.return_value = binfo

+         self.get_build_type.return_value = {'rpm': None}

+         obj = kojihub.BuildTypeTest('buildtype foo-*')

+         data = {'build': 'nvr-1-2'}

+         self.assertFalse(obj.run(data))

+         self.get_build_type.assert_called_once_with(binfo)

+ 

+     def test_valid(self):

+         binfo = {'id': 1, 'name': 'nvr-1-2'}

+         self.get_build.return_value = binfo

+         self.get_build_type.return_value = {'rpm': None}

+         obj = kojihub.BuildTypeTest('buildtype rpm')

+         data = {'build': 'nvr-1-2'}

+         self.assertTrue(obj.run(data))

+         self.get_build_type.assert_called_once_with(binfo)

+ 

+     def test_prepopulated(self):

+         #self.get_build.return_value = {'id': 1, 'name': 'nvr-1-2'}

+         self.get_build_type.return_value = {'rpm': None}

+         obj = kojihub.BuildTypeTest('buildtype rpm')

+         data = {'build': 123, 'btypes': set(['rpm'])}

+         self.assertTrue(obj.run(data))

+         self.get_build_type.assert_not_called()

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

4 years ago

first attempt at testing gave

  File "/usr/share/koji-hub/kojihub.py", line 8274, in policy_get_build_types
    if 'btypes' in data['build']:
TypeError: argument of type 'int' is not iterable

Many policy hooks pass just a build id. So like with other policy tests we need to handle that.

I'm also not sure where we should expect called provided btype info. I don't think any current code does. Would it possibly be at top level?

rebased onto 7af49ee6af4dc19fd7ac5e7f1b71f096ba0e5978

4 years ago

Fixed problem with buildinfo.
I've put btypes to top level for now - if buildinfo can be just build_id, it seems to be a safer option.

rebased onto ff2c95f

4 years ago

Commit 9036e0e fixes this pull-request

Pull-Request has been merged by mikem

4 years ago