diff --git a/server_environment/__init__.py b/server_environment/__init__.py index 3824827..0ff0f53 100644 --- a/server_environment/__init__.py +++ b/server_environment/__init__.py @@ -18,4 +18,8 @@ # ############################################################################## from . import models +# Add an alias to access to the 'serv_config' module as it is shadowed +# the next line by an import of a variable with the same name. +# We can't change the import of serv_config for backward compatibility. +from . import serv_config as server_env from .serv_config import serv_config, setboolean diff --git a/server_environment/models/server_env_mixin.py b/server_environment/models/server_env_mixin.py index 7c3b717..09c5264 100644 --- a/server_environment/models/server_env_mixin.py +++ b/server_environment/models/server_env_mixin.py @@ -178,7 +178,7 @@ class ServerEnvMixin(models.AbstractModel): value = getter(section_name, field_name) else: value = getter(global_section_name, field_name) - except: + except Exception: _logger.exception( "error trying to read field %s in section %s", field_name, diff --git a/server_environment/serv_config.py b/server_environment/serv_config.py index 9807e13..4af4061 100644 --- a/server_environment/serv_config.py +++ b/server_environment/serv_config.py @@ -130,6 +130,7 @@ def _load_config(): config_p = configparser.SafeConfigParser() # options are case-sensitive config_p.optionxform = str + if _dir: _load_config_from_server_env_files(config_p) _load_config_from_rcfile(config_p) diff --git a/server_environment/tests/__init__.py b/server_environment/tests/__init__.py index 990f3cb..4c7aa90 100644 --- a/server_environment/tests/__init__.py +++ b/server_environment/tests/__init__.py @@ -18,3 +18,4 @@ # ############################################################################## from . import test_server_environment +from . import test_environment_variable diff --git a/server_environment/tests/common.py b/server_environment/tests/common.py new file mode 100644 index 0000000..37f3fb0 --- /dev/null +++ b/server_environment/tests/common.py @@ -0,0 +1,43 @@ +# Copyright 2018 Camptocamp (https://www.camptocamp.com). +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import os +from contextlib import contextmanager +from unittest.mock import patch + +from odoo.tests import common +from odoo.addons.server_environment import server_env +from odoo.tools.config import config + + +class ServerEnvironmentCase(common.TransactionCase): + + def setUp(self): + super().setUp() + self._original_running_env = config.get('running_env') + config['running_env'] = 'testing' + + def tearDown(self): + super().tearDown() + config['running_env'] = self._original_running_env + + @contextmanager + def set_config_dir(self, path): + original_dir = server_env._dir + if path and not os.path.isabs(path): + path = os.path.join(os.path.dirname(__file__,), path) + server_env._dir = path + try: + yield + finally: + server_env._dir = original_dir + + @contextmanager + def set_env_variables(self, public=None, secret=None): + newkeys = {} + if public: + newkeys['SERVER_ENV_CONFIG'] = public + if secret: + newkeys['SERVER_ENV_CONFIG_SECRET'] = secret + with patch.dict('os.environ', newkeys): + yield diff --git a/server_environment/tests/test_environment_variable.py b/server_environment/tests/test_environment_variable.py new file mode 100644 index 0000000..6d29dc5 --- /dev/null +++ b/server_environment/tests/test_environment_variable.py @@ -0,0 +1,45 @@ +# Copyright 2018 Camptocamp (https://www.camptocamp.com). +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from odoo.addons.server_environment import server_env +from .common import ServerEnvironmentCase + + +class TestEnvironmentVariables(ServerEnvironmentCase): + + def test_env_variables(self): + public = ( + "[section]\n" + "foo=bar\n" + "bar=baz\n" + ) + secret = ( + "[section]\n" + "bar=foo\n" + "alice=bob\n" + ) + with self.set_config_dir(None), \ + self.set_env_variables(public, secret): + parser = server_env._load_config() + self.assertEqual( + list(parser.keys()), + ['DEFAULT', 'section'] + ) + self.assertDictEqual( + dict(parser['section'].items()), + {'alice': 'bob', + 'bar': 'foo', + 'foo': 'bar'} + ) + + def test_env_variables_override(self): + public = ( + "[external_service.ftp]\n" + "user=foo\n" + ) + with self.set_config_dir('testfiles'), \ + self.set_env_variables(public): + parser = server_env._load_config() + val = parser.get('external_service.ftp', 'user') + self.assertEqual(val, 'foo') diff --git a/server_environment/tests/test_server_environment.py b/server_environment/tests/test_server_environment.py index 012f93d..2212a6c 100644 --- a/server_environment/tests/test_server_environment.py +++ b/server_environment/tests/test_server_environment.py @@ -17,11 +17,11 @@ # along with this program. If not, see . # ############################################################################## -from odoo.tests import common -from odoo.addons.server_environment import serv_config +from odoo.addons.server_environment import server_env +from . import common -class TestEnv(common.TransactionCase): +class TestEnv(common.ServerEnvironmentCase): def test_view(self): model = self.env['server.config'] @@ -43,5 +43,9 @@ class TestEnv(common.TransactionCase): self.assertTrue(pass_checked) def test_value_retrival(self): - val = serv_config.get('external_service.ftp', 'user') - self.assertEqual(val, 'toto') + with self.set_config_dir('testfiles'): + parser = server_env._load_config() + val = parser.get('external_service.ftp', 'user') + self.assertEqual(val, 'testing') + val = parser.get('external_service.ftp', 'host') + self.assertEqual(val, 'sftp.example.com') diff --git a/server_environment/tests/testfiles/default/base.conf b/server_environment/tests/testfiles/default/base.conf new file mode 100644 index 0000000..566de2b --- /dev/null +++ b/server_environment/tests/testfiles/default/base.conf @@ -0,0 +1,4 @@ +[external_service.ftp] +host = sftp.example.com +user = foo +password = bar \ No newline at end of file diff --git a/server_environment/tests/testfiles/testing/base.conf b/server_environment/tests/testfiles/testing/base.conf new file mode 100644 index 0000000..544e95b --- /dev/null +++ b/server_environment/tests/testfiles/testing/base.conf @@ -0,0 +1,2 @@ +[external_service.ftp] +user = testing