Reinforce server_environment base tests
This commit is contained in:
parent
edbefe2162
commit
d3fe970be8
|
|
@ -18,4 +18,8 @@
|
||||||
#
|
#
|
||||||
##############################################################################
|
##############################################################################
|
||||||
from . import models
|
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
|
from .serv_config import serv_config, setboolean
|
||||||
|
|
|
||||||
|
|
@ -178,7 +178,7 @@ class ServerEnvMixin(models.AbstractModel):
|
||||||
value = getter(section_name, field_name)
|
value = getter(section_name, field_name)
|
||||||
else:
|
else:
|
||||||
value = getter(global_section_name, field_name)
|
value = getter(global_section_name, field_name)
|
||||||
except:
|
except Exception:
|
||||||
_logger.exception(
|
_logger.exception(
|
||||||
"error trying to read field %s in section %s",
|
"error trying to read field %s in section %s",
|
||||||
field_name,
|
field_name,
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,7 @@ def _load_config():
|
||||||
config_p = configparser.SafeConfigParser()
|
config_p = configparser.SafeConfigParser()
|
||||||
# options are case-sensitive
|
# options are case-sensitive
|
||||||
config_p.optionxform = str
|
config_p.optionxform = str
|
||||||
|
|
||||||
if _dir:
|
if _dir:
|
||||||
_load_config_from_server_env_files(config_p)
|
_load_config_from_server_env_files(config_p)
|
||||||
_load_config_from_rcfile(config_p)
|
_load_config_from_rcfile(config_p)
|
||||||
|
|
|
||||||
|
|
@ -18,3 +18,4 @@
|
||||||
#
|
#
|
||||||
##############################################################################
|
##############################################################################
|
||||||
from . import test_server_environment
|
from . import test_server_environment
|
||||||
|
from . import test_environment_variable
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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')
|
||||||
|
|
@ -17,11 +17,11 @@
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
##############################################################################
|
##############################################################################
|
||||||
from odoo.tests import common
|
from odoo.addons.server_environment import server_env
|
||||||
from odoo.addons.server_environment import serv_config
|
from . import common
|
||||||
|
|
||||||
|
|
||||||
class TestEnv(common.TransactionCase):
|
class TestEnv(common.ServerEnvironmentCase):
|
||||||
|
|
||||||
def test_view(self):
|
def test_view(self):
|
||||||
model = self.env['server.config']
|
model = self.env['server.config']
|
||||||
|
|
@ -43,5 +43,9 @@ class TestEnv(common.TransactionCase):
|
||||||
self.assertTrue(pass_checked)
|
self.assertTrue(pass_checked)
|
||||||
|
|
||||||
def test_value_retrival(self):
|
def test_value_retrival(self):
|
||||||
val = serv_config.get('external_service.ftp', 'user')
|
with self.set_config_dir('testfiles'):
|
||||||
self.assertEqual(val, 'toto')
|
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')
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
[external_service.ftp]
|
||||||
|
host = sftp.example.com
|
||||||
|
user = foo
|
||||||
|
password = bar
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
[external_service.ftp]
|
||||||
|
user = testing
|
||||||
Loading…
Reference in New Issue