ostree: Add tests for sending ostree messages

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-09-15 14:20:26 +02:00
parent 2c401a4a5c
commit 23e00fd87d

View File

@ -48,6 +48,21 @@ class OSTreeThreadTest(helpers.PungiTestCase):
def setUp(self): def setUp(self):
super(OSTreeThreadTest, self).setUp() super(OSTreeThreadTest, self).setUp()
self.repo = os.path.join(self.topdir, 'place/for/atomic') 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): def _dummy_config_repo(self, scm_dict, target, logger=None):
helpers.touch(os.path.join(target, 'fedora-atomic-docker-host.json')) 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'), helpers.touch(os.path.join(target, 'fedora-23.repo'),
'baseurl=why-not-zoidberg?') '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.scm.get_dir_from_scm')
@mock.patch('pungi.wrappers.kojiwrapper.KojiWrapper') @mock.patch('pungi.wrappers.kojiwrapper.KojiWrapper')
def test_run(self, KojiWrapper, get_dir_from_scm): def test_run(self, KojiWrapper, get_dir_from_scm):
get_dir_from_scm.side_effect = self._dummy_config_repo 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 = KojiWrapper.return_value
koji.run_runroot_cmd.return_value = { koji.run_runroot_cmd.side_effect = self._mock_runroot(0)
'task_id': 1234,
'retcode': 0,
'output': 'Foo bar\n',
}
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, self.assertEqual(get_dir_from_scm.call_args_list,
[mock.call({'scm': 'git', 'repo': 'https://git.fedorahosted.org/git/fedora-atomic.git', [mock.call({'scm': 'git', 'repo': 'https://git.fedorahosted.org/git/fedora-atomic.git',
'branch': 'f24', 'dir': '.'}, '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, self.assertEqual(koji.get_runroot_cmd.call_args_list,
[mock.call('rrt', 'x86_64', [mock.call('rrt', 'x86_64',
['pungi-make-ostree', ['pungi-make-ostree',
@ -107,13 +113,8 @@ class OSTreeThreadTest(helpers.PungiTestCase):
[mock.call(koji.get_runroot_cmd.return_value, [mock.call(koji.get_runroot_cmd.return_value,
log_file=self.topdir + '/logs/x86_64/Everything/ostree-1/runroot.log')]) 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: for fp in ['fedora-rawhide.repo', 'fedora-24.repo', 'fedora-24.repo']:
self.assertIn('baseurl=http://example.com/Everything/x86_64/os', with open(os.path.join(self.topdir, 'work/ostree-1/config_repo', fp)) as f:
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', self.assertIn('baseurl=http://example.com/Everything/x86_64/os',
f.read()) f.read())
self.assertTrue(os.path.isdir(self.repo)) self.assertTrue(os.path.isdir(self.repo))
@ -123,31 +124,15 @@ class OSTreeThreadTest(helpers.PungiTestCase):
def test_run_fail(self, KojiWrapper, get_dir_from_scm): def test_run_fail(self, KojiWrapper, get_dir_from_scm):
get_dir_from_scm.side_effect = self._dummy_config_repo get_dir_from_scm.side_effect = self._dummy_config_repo
compose = helpers.DummyCompose(self.topdir, { self.cfg['failable'] = ['*']
'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': ['*']
}
koji = KojiWrapper.return_value koji = KojiWrapper.return_value
koji.run_runroot_cmd.return_value = { koji.run_runroot_cmd.side_effect = self._mock_runroot(1)
'task_id': 1234,
'retcode': 1,
'output': 'Foo bar\n',
}
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('[FAIL] Ostree (variant Everything, arch x86_64) failed, but going on anyway.'),
mock.call('Runroot task failed: 1234. See %s for more details.' mock.call('Runroot task failed: 1234. See %s for more details.'
% (self.topdir + '/logs/x86_64/Everything/ostree-1/runroot.log')) % (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): def test_run_handle_exception(self, KojiWrapper, get_dir_from_scm):
get_dir_from_scm.side_effect = self._dummy_config_repo get_dir_from_scm.side_effect = self._dummy_config_repo
compose = helpers.DummyCompose(self.topdir, { self.cfg['failable'] = ['*']
'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': ['*']
}
koji = KojiWrapper.return_value koji = KojiWrapper.return_value
koji.run_runroot_cmd.side_effect = helpers.boom 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('[FAIL] Ostree (variant Everything, arch x86_64) failed, but going on anyway.'),
mock.call('BOOM') 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__': if __name__ == '__main__':
unittest.main() unittest.main()