From 2880a4ab428b2de948d7231f0eda6db5e1a566c8 Mon Sep 17 00:00:00 2001 From: Qixiang Wan Date: Oct 27 2017 04:47:42 +0000 Subject: Check whether Pungi config template exists at startup We should verify Pungi config template exists at startup rather than detect that while generating a compose later. --- diff --git a/server/conf/config.py b/server/conf/config.py index 28fc2bb..64ada9b 100644 --- a/server/conf/config.py +++ b/server/conf/config.py @@ -128,6 +128,7 @@ class DevConfiguration(BaseConfiguration): if ex.errno != errno.EEXIST: raise RuntimeError("Can't create compose target dir %s: %s" % (TARGET_DIR, ex.strerror)) + PUNGI_CONF_PATH = path.join(confdir, 'pungi.conf') AUTH_BACKEND = 'noauth' AUTH_OPENIDC_USERINFO_URI = 'https://iddev.fedorainfracloud.org/openidc/UserInfo' @@ -140,6 +141,7 @@ class TestConfiguration(BaseConfiguration): SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format( path.join(dbdir, 'test_odcs.db')) + PUNGI_CONF_PATH = path.join(confdir, 'pungi.conf') # Global network-related values, in seconds NET_TIMEOUT = 3 NET_RETRY_INTERVAL = 1 diff --git a/server/odcs/server/config.py b/server/odcs/server/config.py index 1225245..5c33776 100644 --- a/server/odcs/server/config.py +++ b/server/odcs/server/config.py @@ -329,6 +329,11 @@ class Config(object): def _setifok_target_dir(self, s): if not os.path.isabs(s): raise ValueError("Compose target dir is not an absolute path: %s" % s) - if not os.path.exists(s): - raise ValueError("Compose target dir doesn't exist: %s" % s) + if not (os.path.exists(s) and os.path.isdir(s)): + raise ValueError("Compose target dir doesn't exist or not a directory: %s" % s) self._target_dir = s + + def _setifok_pungi_conf_path(self, s): + if not os.path.isfile(s): + raise ValueError("Pungi config template doesn't exist: %s" % s) + self._pungi_conf_path = s diff --git a/server/odcs/server/pungi.py b/server/odcs/server/pungi.py index 00ec110..4a3a539 100644 --- a/server/odcs/server/pungi.py +++ b/server/odcs/server/pungi.py @@ -132,8 +132,8 @@ class PungiConfig(object): return template.render(config=self) except Exception as e: log.exception( - "Failed to open pungi conf {!r}: {}".format(conf.pungi_conf_path, - str(e))) + "Failed to render pungi conf template {!r}: {}".format(conf.pungi_conf_path, + str(e))) class Pungi(object): diff --git a/server/tests/test_pungi.py b/server/tests/test_pungi.py index d9d0afb..34f26dc 100644 --- a/server/tests/test_pungi.py +++ b/server/tests/test_pungi.py @@ -28,7 +28,6 @@ import unittest from mock import patch from kobo.conf import PyConfigParser -import odcs.server from odcs.server.pungi import (Pungi, PungiConfig, PungiSourceType, COMPOSE_RESULTS) @@ -39,15 +38,9 @@ class TestPungiConfig(unittest.TestCase): def setUp(self): super(TestPungiConfig, self).setUp() - patched_pungi_conf_path = os.path.join(test_dir, '../conf/pungi.conf') - self.patch_pungi_conf_path = patch.object(odcs.server.conf, - 'pungi_conf_path', - new=patched_pungi_conf_path) - self.patch_pungi_conf_path.start() def tearDown(self): super(TestPungiConfig, self).tearDown() - self.patch_pungi_conf_path.stop() def _load_pungi_cfg(self, cfg): conf = PyConfigParser() @@ -95,10 +88,14 @@ class TestPungiConfig(unittest.TestCase): def test_get_pungi_conf_exception(self, log): pungi_cfg = PungiConfig("MBS-512", "1", PungiSourceType.MODULE, "testmodule-master") - mock_path = "/tmp/non_existant_pungi_conf" + _, mock_path = tempfile.mkstemp(suffix='-pungi.conf') + with open(mock_path, 'w') as f: + # write an invalid jinja2 template file + f.write('{{\n') with patch("odcs.server.pungi.conf.pungi_conf_path", mock_path): pungi_cfg.get_pungi_config() log.exception.assert_called_once() + os.remove(mock_path) def test_get_pungi_conf_iso(self): _, mock_path = tempfile.mkstemp() @@ -119,15 +116,9 @@ class TestPungi(unittest.TestCase): def setUp(self): super(TestPungi, self).setUp() - patched_pungi_conf_path = os.path.join(test_dir, '../conf/pungi.conf') - self.patch_pungi_conf_path = patch.object(odcs.server.conf, - 'pungi_conf_path', - new=patched_pungi_conf_path) - self.patch_pungi_conf_path.start() def tearDown(self): super(TestPungi, self).tearDown() - self.patch_pungi_conf_path.stop() @patch("odcs.server.utils.execute_cmd") def test_pungi_run(self, execute_cmd):