diff --git a/tests/test_ostree_phase.py b/tests/test_ostree_phase.py index e2e721cc..8a4092f1 100755 --- a/tests/test_ostree_phase.py +++ b/tests/test_ostree_phase.py @@ -48,6 +48,21 @@ class OSTreeThreadTest(helpers.PungiTestCase): def setUp(self): super(OSTreeThreadTest, self).setUp() self.repo = os.path.join(self.topdir, 'place/for/atomic') + self.cfg = { + 'source_repo_from': 'Everything', + 'config_url': 'https://git.fedorahosted.org/git/fedora-atomic.git', + 'config_branch': 'f24', + 'treefile': 'fedora-atomic-docker-host.json', + 'ostree_repo': self.repo + } + self.compose = helpers.DummyCompose(self.topdir, { + 'koji_profile': 'koji', + 'runroot_tag': 'rrt', + 'translate_paths': [ + (self.topdir + '/compose', 'http://example.com') + ] + }) + self.pool = mock.Mock() def _dummy_config_repo(self, scm_dict, target, logger=None): helpers.touch(os.path.join(target, 'fedora-atomic-docker-host.json')) @@ -58,41 +73,32 @@ class OSTreeThreadTest(helpers.PungiTestCase): helpers.touch(os.path.join(target, 'fedora-23.repo'), 'baseurl=why-not-zoidberg?') + def _mock_runroot(self, retcode, logs=None): + """Pretend to run a task in runroot, creating a log file with given line""" + def fake_runroot(self, log_file, **kwargs): + if logs: + logdir = os.path.dirname(log_file) + helpers.touch(os.path.join(logdir, 'create-ostree-repo.log'), + '\n'.join(['Doing work', logs])) + return {'task_id': 1234, 'retcode': retcode, 'output': 'Foo bar\n'} + return fake_runroot + @mock.patch('pungi.wrappers.scm.get_dir_from_scm') @mock.patch('pungi.wrappers.kojiwrapper.KojiWrapper') def test_run(self, KojiWrapper, get_dir_from_scm): get_dir_from_scm.side_effect = self._dummy_config_repo - compose = helpers.DummyCompose(self.topdir, { - 'koji_profile': 'koji', - 'runroot_tag': 'rrt', - 'translate_paths': [ - (self.topdir + '/compose', 'http://example.com') - ] - }) - pool = mock.Mock() - cfg = { - 'source_repo_from': 'Everything', - 'config_url': 'https://git.fedorahosted.org/git/fedora-atomic.git', - 'config_branch': 'f24', - 'treefile': 'fedora-atomic-docker-host.json', - 'ostree_repo': self.repo - } koji = KojiWrapper.return_value - koji.run_runroot_cmd.return_value = { - 'task_id': 1234, - 'retcode': 0, - 'output': 'Foo bar\n', - } + koji.run_runroot_cmd.side_effect = self._mock_runroot(0) - t = ostree.OSTreeThread(pool) + t = ostree.OSTreeThread(self.pool) - t.process((compose, compose.variants['Everything'], 'x86_64', cfg), 1) + t.process((self.compose, self.compose.variants['Everything'], 'x86_64', self.cfg), 1) self.assertEqual(get_dir_from_scm.call_args_list, [mock.call({'scm': 'git', 'repo': 'https://git.fedorahosted.org/git/fedora-atomic.git', 'branch': 'f24', 'dir': '.'}, - self.topdir + '/work/ostree-1/config_repo', logger=pool._logger)]) + self.topdir + '/work/ostree-1/config_repo', logger=self.pool._logger)]) self.assertEqual(koji.get_runroot_cmd.call_args_list, [mock.call('rrt', 'x86_64', ['pungi-make-ostree', @@ -107,15 +113,10 @@ class OSTreeThreadTest(helpers.PungiTestCase): [mock.call(koji.get_runroot_cmd.return_value, log_file=self.topdir + '/logs/x86_64/Everything/ostree-1/runroot.log')]) - with open(self.topdir + '/work/ostree-1/config_repo/fedora-rawhide.repo') as f: - self.assertIn('baseurl=http://example.com/Everything/x86_64/os', - f.read()) - with open(self.topdir + '/work/ostree-1/config_repo/fedora-24.repo') as f: - self.assertIn('baseurl=http://example.com/Everything/x86_64/os', - f.read()) - with open(self.topdir + '/work/ostree-1/config_repo/fedora-23.repo') as f: - self.assertIn('baseurl=http://example.com/Everything/x86_64/os', - f.read()) + for fp in ['fedora-rawhide.repo', 'fedora-24.repo', 'fedora-24.repo']: + with open(os.path.join(self.topdir, 'work/ostree-1/config_repo', fp)) as f: + self.assertIn('baseurl=http://example.com/Everything/x86_64/os', + f.read()) self.assertTrue(os.path.isdir(self.repo)) @mock.patch('pungi.wrappers.scm.get_dir_from_scm') @@ -123,31 +124,15 @@ class OSTreeThreadTest(helpers.PungiTestCase): def test_run_fail(self, KojiWrapper, get_dir_from_scm): get_dir_from_scm.side_effect = self._dummy_config_repo - compose = helpers.DummyCompose(self.topdir, { - 'koji_profile': 'koji', - 'runroot_tag': 'rrt', - }) - pool = mock.Mock() - cfg = { - 'source_repo_from': 'Everything', - 'config_url': 'https://git.fedorahosted.org/git/fedora-atomic.git', - 'config_branch': 'f24', - 'treefile': 'fedora-atomic-docker-host.json', - 'ostree_repo': self.repo, - 'failable': ['*'] - } + self.cfg['failable'] = ['*'] koji = KojiWrapper.return_value - koji.run_runroot_cmd.return_value = { - 'task_id': 1234, - 'retcode': 1, - 'output': 'Foo bar\n', - } + koji.run_runroot_cmd.side_effect = self._mock_runroot(1) - t = ostree.OSTreeThread(pool) + t = ostree.OSTreeThread(self.pool) - t.process((compose, compose.variants['Everything'], 'x86_64', cfg), 1) + t.process((self.compose, self.compose.variants['Everything'], 'x86_64', self.cfg), 1) - compose.log_info.assert_has_calls([ + self.compose.log_info.assert_has_calls([ mock.call('[FAIL] Ostree (variant Everything, arch x86_64) failed, but going on anyway.'), mock.call('Runroot task failed: 1234. See %s for more details.' % (self.topdir + '/logs/x86_64/Everything/ostree-1/runroot.log')) @@ -158,31 +143,76 @@ class OSTreeThreadTest(helpers.PungiTestCase): def test_run_handle_exception(self, KojiWrapper, get_dir_from_scm): get_dir_from_scm.side_effect = self._dummy_config_repo - compose = helpers.DummyCompose(self.topdir, { - 'koji_profile': 'koji', - 'runroot_tag': 'rrt', - }) - pool = mock.Mock() - cfg = { - 'source_repo_from': 'Everything', - 'config_url': 'https://git.fedorahosted.org/git/fedora-atomic.git', - 'config_branch': 'f24', - 'treefile': 'fedora-atomic-docker-host.json', - 'ostree_repo': self.repo, - 'failable': ['*'] - } + self.cfg['failable'] = ['*'] koji = KojiWrapper.return_value koji.run_runroot_cmd.side_effect = helpers.boom - t = ostree.OSTreeThread(pool) + t = ostree.OSTreeThread(self.pool) - t.process((compose, compose.variants['Everything'], 'x86_64', cfg), 1) + t.process((self.compose, self.compose.variants['Everything'], 'x86_64', self.cfg), 1) - compose.log_info.assert_has_calls([ + self.compose.log_info.assert_has_calls([ mock.call('[FAIL] Ostree (variant Everything, arch x86_64) failed, but going on anyway.'), mock.call('BOOM') ]) + @mock.patch('pungi.wrappers.scm.get_dir_from_scm') + @mock.patch('pungi.wrappers.kojiwrapper.KojiWrapper') + def test_run_send_message(self, KojiWrapper, get_dir_from_scm): + get_dir_from_scm.side_effect = self._dummy_config_repo + + self.compose.notifier = mock.Mock() + + koji = KojiWrapper.return_value + koji.run_runroot_cmd.side_effect = self._mock_runroot( + 0, 'fedora-atomic/25/x86_64 => fca3465861a') + t = ostree.OSTreeThread(self.pool) + + t.process((self.compose, self.compose.variants['Everything'], 'x86_64', self.cfg), 1) + + self.assertEqual(self.compose.notifier.send.mock_calls, + [mock.call('ostree', + variant='Everything', + arch='x86_64', + ref='fedora-atomic/25/x86_64', + commitid='fca3465861a')]) + + @mock.patch('pungi.wrappers.scm.get_dir_from_scm') + @mock.patch('pungi.wrappers.kojiwrapper.KojiWrapper') + def test_run_send_message_without_commit_id(self, KojiWrapper, get_dir_from_scm): + get_dir_from_scm.side_effect = self._dummy_config_repo + + self.compose.notifier = mock.Mock() + + koji = KojiWrapper.return_value + koji.run_runroot_cmd.side_effect = self._mock_runroot(0, 'Weird output') + t = ostree.OSTreeThread(self.pool) + + t.process((self.compose, self.compose.variants['Everything'], 'x86_64', self.cfg), 1) + + self.assertEqual(self.compose.notifier.send.mock_calls, + [mock.call('ostree', + variant='Everything', + arch='x86_64', + ref=None, + commitid=None)]) + + @mock.patch('pungi.wrappers.scm.get_dir_from_scm') + @mock.patch('pungi.wrappers.kojiwrapper.KojiWrapper') + def test_run_send_no_message_on_failure(self, KojiWrapper, get_dir_from_scm): + get_dir_from_scm.side_effect = self._dummy_config_repo + + self.compose.notifier = mock.Mock() + + koji = KojiWrapper.return_value + koji.run_runroot_cmd.side_effect = self._mock_runroot(1) + t = ostree.OSTreeThread(self.pool) + + self.assertRaises(RuntimeError, t.process, + (self.compose, self.compose.variants['Everything'], 'x86_64', self.cfg), + 1) + self.assertEqual(self.compose.notifier.send.mock_calls, []) + if __name__ == '__main__': unittest.main()