From 7eb264eb4cb348f0167d3678d7b0e90be8b2c0ef Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Thu, 30 Mar 2017 11:24:41 +0200 Subject: [PATCH] updated tests --- plugins/builder/save_failed_tree.py | 2 + tests/test_cli/test_save_failed_tree.py | 62 +++++++++++++------ .../test_save_failed_tree_builder.py | 39 ++++++------ 3 files changed, 67 insertions(+), 36 deletions(-) diff --git a/plugins/builder/save_failed_tree.py b/plugins/builder/save_failed_tree.py index d9b325cf..138572f7 100644 --- a/plugins/builder/save_failed_tree.py +++ b/plugins/builder/save_failed_tree.py @@ -43,6 +43,8 @@ class SaveFailedTreeTask(tasks.BaseTaskHandler): read_config() brinfo = self.session.getBuildroot(buildrootID) + if brinfo is None: + raise koji.GenericError("Nonexistent buildroot: %s" % buildrootID) host_id = self.session.host.getHost()['id'] if brinfo['host_id'] != host_id: raise koji.GenericError("Task is run on wrong builder") diff --git a/tests/test_cli/test_save_failed_tree.py b/tests/test_cli/test_save_failed_tree.py index b2ad7dff..b8acf123 100644 --- a/tests/test_cli/test_save_failed_tree.py +++ b/tests/test_cli/test_save_failed_tree.py @@ -27,12 +27,14 @@ class TestSaveFailedTree(unittest.TestCase): def test_handle_save_failed_tree_simple(self, activate_session_mock): # koji save-failed-tree 123456 task_id = 123456 + broot_id = 321 arguments = [task_id] options = mock.MagicMock() options.full = False options.nowait = True self.parser.parse_args.return_value = [options, arguments] self.session.getAPIVersion.return_value = koji.API_VERSION + self.session.listBuildroots.return_value = [{'id': 321}] # Mock out the xmlrpc server self.session.saveFailedTree.return_value = 123 @@ -42,18 +44,46 @@ class TestSaveFailedTree(unittest.TestCase): # Finally, assert that things were called as we expected. activate_session_mock.assert_called_once_with(self.session) - self.session.saveFailedTree.assert_called_once_with(task_id, options.full) + self.session.listBuildroots.assert_called_once_with(taskID=task_id) + self.session.saveFailedTree.assert_called_once_with(broot_id, options.full) + + @mock.patch('koji_cli.activate_session') + def test_handle_save_failed_tree_buildroots(self, activate_session_mock): + # koji save-failed-tree --buildroot 123456 + broot_id = 321 + arguments = [broot_id] + options = mock.MagicMock() + options.full = False + options.nowait = True + options.mode = "buildroot" + self.parser.parse_args.return_value = [options, arguments] + self.session.getAPIVersion.return_value = koji.API_VERSION + self.session.listBuildroots.return_value = [{'id': 321}] + + # Mock out the xmlrpc server + self.session.saveFailedTree.return_value = 123 + + # Run it and check immediate output + cli.handle_save_failed_tree(self.options, self.session, self.args) + + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.listBuildroots.assert_not_called() + self.session.saveFailedTree.assert_called_once_with(broot_id, options.full) + @mock.patch('koji_cli.activate_session') def test_handle_save_failed_tree_full(self, activate_session_mock): # koji save-failed-tree 123456 --full task_id = 123456 + broot_id = 321 arguments = [task_id] options = mock.MagicMock() options.full = True options.nowait = True self.parser.parse_args.return_value = [options, arguments] self.session.getAPIVersion.return_value = koji.API_VERSION + self.session.listBuildroots.return_value = [{'id': 321}] # Mock out the xmlrpc server self.session.saveFailedTree.return_value = 123 @@ -63,13 +93,15 @@ class TestSaveFailedTree(unittest.TestCase): # Finally, assert that things were called as we expected. activate_session_mock.assert_called_once_with(self.session) - self.session.saveFailedTree.assert_called_once_with(task_id, options.full) + self.session.listBuildroots.assert_called_once_with(taskID=task_id) + self.session.saveFailedTree.assert_called_once_with(broot_id, options.full) @mock.patch('koji_cli.activate_session') @mock.patch('koji_cli.watch_tasks') def test_handle_save_failed_tree_wait(self, watch_tasks_mock, activate_session_mock): # koji save-failed-tree 123456 --full task_id = 123456 + broot_id = 321 arguments = [task_id] options = mock.MagicMock() options.full = True @@ -77,6 +109,7 @@ class TestSaveFailedTree(unittest.TestCase): options.quiet = False self.parser.parse_args.return_value = [options, arguments] self.session.getAPIVersion.return_value = koji.API_VERSION + self.session.listBuildroots.return_value = [{'id': 321}] # Mock out the xmlrpc server spawned_id = 123 @@ -86,7 +119,8 @@ class TestSaveFailedTree(unittest.TestCase): cli.handle_save_failed_tree(self.options, self.session, self.args) # Finally, assert that things were called as we expected. - self.session.saveFailedTree.assert_called_once_with(task_id, options.full) + self.session.listBuildroots.assert_called_once_with(taskID=task_id) + self.session.saveFailedTree.assert_called_once_with(broot_id, options.full) activate_session_mock.assert_called_once_with(self.session) self.session.logout.assert_called_once_with() watch_tasks_mock.assert_called_once_with(self.session, [spawned_id], @@ -102,6 +136,7 @@ class TestSaveFailedTree(unittest.TestCase): self.parser.parse_args.return_value = [options, arguments] self.parser.error.side_effect = Exception() self.session.getAPIVersion.return_value = koji.API_VERSION + self.session.listBuildroots.return_value = [{'id': 321}] self.assertRaises(Exception, cli.handle_save_failed_tree, self.options, self.session, self.args) @@ -120,18 +155,9 @@ class TestSaveFailedTree(unittest.TestCase): actual = stdout.getvalue() self.assertTrue('The save_failed_tree plugin appears to not be installed' in actual) - # Task which is not FAILED - stdout.seek(0) - stdout.truncate() - self.session.saveFailedTree.side_effect = koji.PreBuildError('Only failed tasks can upload their buildroots.') - cli.handle_save_failed_tree(self.options, self.session, self.args) - actual = stdout.getvalue() - self.assertTrue('Only failed tasks can upload their buildroots.' in actual) - - # Disabled/unsupported task - stdout.seek(0) - stdout.truncate() - self.session.saveFailedTree.side_effect = koji.PreBuildError('tasks can upload their buildroots (Task') - cli.handle_save_failed_tree(self.options, self.session, self.args) - actual = stdout.getvalue() - self.assertTrue('Task of this type has disabled support for uploading' in actual) + # Task which is not FAILED, disabled in config, wrong owner + self.session.saveFailedTree.side_effect = koji.PreBuildError('placeholder') + with self.assertRaises(koji.PreBuildError) as cm: + cli.handle_save_failed_tree(self.options, self.session, self.args) + e = cm.exception + self.assertEqual(e, self.session.saveFailedTree.side_effect) diff --git a/tests/test_plugins/test_save_failed_tree_builder.py b/tests/test_plugins/test_save_failed_tree_builder.py index 76dedf9c..56c92a53 100644 --- a/tests/test_plugins/test_save_failed_tree_builder.py +++ b/tests/test_plugins/test_save_failed_tree_builder.py @@ -27,34 +27,37 @@ class TestSaveFailedTree(unittest.TestCase): @mock.patch('os.unlink') @mock.patch('tarfile.open') - def testNonExistentTask(self, tarfile, os_unlink): - # empty tarball + def testNonExistentBuildroot(self, tarfile, os_unlink): tfile = mock.MagicMock(name='tfile') tarfile.return_value = tfile + self.session.getBuildroot.return_value = None - self.t.handler(1) + with self.assertRaises(koji.GenericError) as cm: + self.t.handler(1) + self.assertTrue('Nonexistent buildroot' in str(cm.exception)) - tarfile.assert_called_once_with( - '/tmp/nonexistentdirectory/tasks/123/123/broots-task-1.tar.gz', - 'w:gz' - ) + tarfile.assert_not_called() tfile.add.assert_not_called() - tfile.close.assert_called_once_with() - os_unlink.assert_called_once_with('/tmp/nonexistentdirectory/tasks/123/123/broots-task-1.tar.gz') + tfile.close.assert_not_called() + os_unlink.assert_not_called() + @mock.patch('os.path.exists') @mock.patch('os.unlink') @mock.patch('tarfile.open') - def testCorrect(self, tarfile, os_unlink): + def testCorrect(self, tarfile, os_unlink, os_exists): def getBuildroot(bid): tmp = { 'tag_name': 'tag_name', 'repo_id': 'repo_id', + 'host_id': 1, } if bid == 1: + tmp['id'] = 1 tmp['task_id'] = 1000 tmp['arch'] = 'x86_64' tmp['tag_id'] = 5000 elif bid == 2: + tmp['id'] = 2 tmp['task_id'] = 1001 tmp['arch'] = 'i386' tmp['tag_id'] = 5001 @@ -64,20 +67,19 @@ class TestSaveFailedTree(unittest.TestCase): tfile = mock.MagicMock(name='tfile') tfile.add = mock.MagicMock() tarfile.return_value = tfile - # simplified return values, only id should be used - self.session.listBuildroots.return_value = [{'id': 1, 'host_id': 1}, {'id': 2, 'host_id': 1}] + os_exists.return_value = True self.t.handler(1) tarfile.assert_called_once_with( - '/tmp/nonexistentdirectory/tasks/123/123/broots-task-1.tar.gz', + '/tmp/nonexistentdirectory/tasks/123/123/broot-1.tar.gz', 'w:gz' ) + + tfile.add.assert_called_once() self.assertEqual(tfile.add.call_args_list[0][0][0], '/tmp/mockdir/tag_name-1-repo_id/root/builddir') - self.assertEqual(tfile.add.call_args_list[1][0][0], '/tmp/mockdir/tag_name-2-repo_id/root/builddir') - self.assertEqual(len(tfile.add.call_args_list), 2) tfile.close.assert_called_once_with() - os_unlink.assert_called_once_with('/tmp/nonexistentdirectory/tasks/123/123/broots-task-1.tar.gz') + os_unlink.assert_called_once_with('/tmp/nonexistentdirectory/tasks/123/123/broot-1.tar.gz') @mock.patch('os.unlink') @mock.patch('tarfile.open') @@ -86,12 +88,15 @@ class TestSaveFailedTree(unittest.TestCase): tmp = { 'tag_name': 'tag_name', 'repo_id': 'repo_id', + 'host_id': 2000, } if bid == 1: + tmp['id'] = 1 tmp['task_id'] = 1000 tmp['arch'] = 'x86_64' tmp['tag_id'] = 5000 elif bid == 2: + tmp['id'] = 2 tmp['task_id'] = 1001 tmp['arch'] = 'i386' tmp['tag_id'] = 5001 @@ -100,8 +105,6 @@ class TestSaveFailedTree(unittest.TestCase): self.session.getBuildroot.side_effect = getBuildroot tfile = mock.MagicMock(name='tfile') tarfile.return_value = tfile - # simplified return values, only id should be used - self.session.listBuildroots.return_value = [{'id': 1, 'host_id': 2}, {'id': 2, 'host_id': 2}] with self.assertRaises(koji.GenericError): self.t.handler(1)