debian-koji/tests/test_hub/test_build_image_oz.py
Yuming Zhu ca05418fb5 unittest: use unittest.mock instead of mock
because the absence of unittest.mock on python2.7, we still fallback to
mock
2024-10-23 16:35:30 +00:00

52 lines
2.3 KiB
Python

import unittest
import koji
import kojihub
from unittest import mock
class TestBuildImageOz(unittest.TestCase):
def setUp(self):
self.context = mock.patch('kojihub.kojihub.context').start()
self.exports = kojihub.RootExports()
self.context.session.assertPerm = mock.MagicMock()
self.context.session.hasPerm = mock.MagicMock()
self.parse_arches = mock.patch('koji.parse_arches').start()
self.name = 'image-name'
self.version = 'test-version'
self.arches = ['x86_64', 'i386']
self.target = 'test-target'
self.inst_tree = 'test-tree'
def tearDown(self):
mock.patch.stopall()
def test_name_wrong_type(self):
name = ['image-name']
with self.assertRaises(koji.ParameterError) as cm:
self.exports.buildImageOz(name, self.version, self.arches, self.target, self.inst_tree)
self.assertEqual(f"Invalid type for value '{name}': {type(name)}, "
f"expected type <class 'str'>", str(cm.exception))
def test_inst_tree_wrong_type(self):
inst_tree = ['test-tree']
with self.assertRaises(koji.ParameterError) as cm:
self.exports.buildImageOz(self.name, self.version, self.arches, self.target, inst_tree)
self.assertEqual(f"Invalid type for value '{inst_tree}': {type(inst_tree)}, "
f"expected type <class 'str'>", str(cm.exception))
def test_version_wrong_type(self):
version = ['test-version']
with self.assertRaises(koji.ParameterError) as cm:
self.exports.buildImageOz(self.name, version, self.arches, self.target, self.inst_tree)
self.assertEqual(f"Invalid type for value '{version}': {type(version)}, "
f"expected type <class 'str'>", str(cm.exception))
def test_priority_without_admin(self):
priority = -10
self.context.session.assertPerm.side_effect = None
self.context.session.hasPerm.return_value = False
with self.assertRaises(koji.ActionNotAllowed) as cm:
self.exports.buildImageOz(self.name, self.version, self.arches, self.target,
self.inst_tree, priority=priority)
self.assertEqual("only admins may create high-priority tasks", str(cm.exception))