From 12652ad1fad4f4d57404ecb1833063f1f371dcee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Mon, 9 May 2016 15:59:02 +0200 Subject: [PATCH] [ostree] Initialize empty repo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make sure the directory exists before calling Koji (because otherwise the mounting will fail). Update the runroot script to initialize the repo when there are no files in the target destination. Signed-off-by: Lubomír Sedlář --- pungi/ostree.py | 2 +- pungi/phases/ostree.py | 4 ++++ tests/test_ostree_phase.py | 15 +++++++++----- tests/test_ostree_script.py | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/pungi/ostree.py b/pungi/ostree.py index 846daeea..f36c794e 100644 --- a/pungi/ostree.py +++ b/pungi/ostree.py @@ -31,7 +31,7 @@ def make_log_file(log_dir, filename): def init_ostree_repo(repo, log_dir=None): """If the ostree repo does not exist, initialize it.""" log_file = make_log_file(log_dir, 'init-ostree-repo') - if not os.path.isdir(repo): + if not os.path.isdir(repo) or not os.listdir(repo): ensure_dir(repo) shortcuts.run(['ostree', 'init', '--repo={}'.format(repo), '--mode=archive-z2'], show_cmd=True, stdout=True, logfile=log_file) diff --git a/pungi/phases/ostree.py b/pungi/phases/ostree.py index 170d8011..53947ffa 100644 --- a/pungi/phases/ostree.py +++ b/pungi/phases/ostree.py @@ -55,6 +55,10 @@ class OSTreeThread(WorkerThread): self._clone_repo(repodir, config['config_url'], config.get('config_branch', 'master')) self._tweak_mirrorlist(repodir, source_repo) + # Ensure target directory exists, otherwise Koji task will fail to + # mount it. + util.makedirs(config['ostree_repo']) + self._run_ostree_cmd(compose, variant, arch, config, repodir) self.pool.log_info('[DONE ] %s' % msg) diff --git a/tests/test_ostree_phase.py b/tests/test_ostree_phase.py index c8c29506..f5060042 100755 --- a/tests/test_ostree_phase.py +++ b/tests/test_ostree_phase.py @@ -74,6 +74,10 @@ class OSTreePhaseTest(helpers.PungiTestCase): class OSTreeThreadTest(helpers.PungiTestCase): + def setUp(self): + super(OSTreeThreadTest, self).setUp() + self.repo = os.path.join(self.topdir, 'place/for/atomic') + 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-rawhide.repo'), @@ -101,7 +105,7 @@ class OSTreeThreadTest(helpers.PungiTestCase): 'config_url': 'https://git.fedorahosted.org/git/fedora-atomic.git', 'config_branch': 'f24', 'treefile': 'fedora-atomic-docker-host.json', - 'ostree_repo': '/other/place/for/atomic' + 'ostree_repo': self.repo } koji = KojiWrapper.return_value koji.run_runroot_cmd.return_value = { @@ -124,8 +128,8 @@ class OSTreeThreadTest(helpers.PungiTestCase): '--log-dir={}/logs/x86_64/ostree'.format(self.topdir), '--treefile={}/fedora-atomic-docker-host.json'.format( self.topdir + '/work/ostree/config_repo'), - '/other/place/for/atomic'], - channel=None, mounts=[self.topdir, '/other/place/for/atomic'], + self.repo], + channel=None, mounts=[self.topdir, self.repo], packages=['pungi', 'ostree', 'rpm-ostree'], task_id=True, use_shell=True)]) self.assertEqual(koji.run_runroot_cmd.call_args_list, @@ -141,6 +145,7 @@ class OSTreeThreadTest(helpers.PungiTestCase): with open(self.topdir + '/work/ostree/config_repo/fedora-23.repo') as f: self.assertIn('baseurl=http://example.com/Everything/x86_64/os'.format(self.topdir), f.read()) + self.assertTrue(os.path.isdir(self.repo)) @mock.patch('pungi.wrappers.scm.get_dir_from_scm') @mock.patch('pungi.wrappers.kojiwrapper.KojiWrapper') @@ -160,7 +165,7 @@ class OSTreeThreadTest(helpers.PungiTestCase): 'config_url': 'https://git.fedorahosted.org/git/fedora-atomic.git', 'config_branch': 'f24', 'treefile': 'fedora-atomic-docker-host.json', - 'ostree_repo': '/other/place/for/atomic' + 'ostree_repo': self.repo, } koji = KojiWrapper.return_value koji.run_runroot_cmd.return_value = { @@ -197,7 +202,7 @@ class OSTreeThreadTest(helpers.PungiTestCase): 'config_url': 'https://git.fedorahosted.org/git/fedora-atomic.git', 'config_branch': 'f24', 'treefile': 'fedora-atomic-docker-host.json', - 'ostree_repo': '/other/place/for/atomic' + 'ostree_repo': self.repo, } koji = KojiWrapper.return_value koji.run_runroot_cmd.side_effect = helpers.boom diff --git a/tests/test_ostree_script.py b/tests/test_ostree_script.py index c9c6c35e..318565fa 100755 --- a/tests/test_ostree_script.py +++ b/tests/test_ostree_script.py @@ -36,6 +36,46 @@ class OstreeScriptTest(helpers.PungiTestCase): self.topdir + '/fedora-atomic-docker-host.json'], logfile=self.topdir + '/logs/Atomic/create-ostree-repo.log', show_cmd=True, stdout=True)]) + @mock.patch('kobo.shortcuts.run') + def test_run_on_existing_empty_dir(self, run): + repo = os.path.join(self.topdir, 'atomic') + + os.mkdir(repo) + + ostree.main([ + '--log-dir={}'.format(os.path.join(self.topdir, 'logs', 'Atomic')), + '--treefile={}/fedora-atomic-docker-host.json'.format(self.topdir), + repo, + ]) + + self.maxDiff = None + self.assertItemsEqual( + run.call_args_list, + [mock.call(['ostree', 'init', '--repo={}'.format(repo), '--mode=archive-z2'], + logfile=self.topdir + '/logs/Atomic/init-ostree-repo.log', show_cmd=True, stdout=True), + mock.call(['rpm-ostree', 'compose', 'tree', '--repo={}'.format(repo), + self.topdir + '/fedora-atomic-docker-host.json'], + logfile=self.topdir + '/logs/Atomic/create-ostree-repo.log', show_cmd=True, stdout=True)]) + + @mock.patch('kobo.shortcuts.run') + def test_run_on_initialized_repo(self, run): + repo = os.path.join(self.topdir, 'atomic') + + helpers.touch(os.path.join(repo, 'initialized')) + + ostree.main([ + '--log-dir={}'.format(os.path.join(self.topdir, 'logs', 'Atomic')), + '--treefile={}/fedora-atomic-docker-host.json'.format(self.topdir), + repo, + ]) + + self.maxDiff = None + self.assertItemsEqual( + run.call_args_list, + [mock.call(['rpm-ostree', 'compose', 'tree', '--repo={}'.format(repo), + self.topdir + '/fedora-atomic-docker-host.json'], + logfile=self.topdir + '/logs/Atomic/create-ostree-repo.log', show_cmd=True, stdout=True)]) + if __name__ == '__main__': unittest.main()