Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""Tests of the Odoo conf module"""
import os
import tempfile
import unittest
from configparser import ConfigParser
from odoo_scripts.odoo_conf_inject_env_var import inject_env_var, main
def clean_up_env():
"""Clean up environment"""
for name in (
"ODOO_OPTIONS_DB_USER",
"ODOO_OPTIONS_DB_PASSWORD",
"ODOO_OPTIONS_DB_NAME",
"ODOO_OPTIONS_DB_HOST",
"ODOO_OPTIONS_MAX_CRON_THREADS",
"ODOO_OPTIONS_DATA_DIR",
"ODOO_OPTIONS_LOAD_LANGUAGE",
"ODOO_OPTIONS_ADDONS_PATH",
):
os.environ.pop(name, None)
class OdooConfTestCase(unittest.TestCase):
"""Tests of the Odoo conf env var class"""
@classmethod
def setUpClass(cls) -> None:
"""Create the conf file used in the tests"""
cls.conf_path = "odoo.conf"
with open(cls.conf_path, "w", encoding="UTF-8") as file_io:
file_io.write(
"""# This is a test file similar to an Odoo configuration file
[options]
db_user = db_user
db_password = db_password
db_name = db_name
max_cron_threads = 3
load_language = fr_FR,en
data_dir = /var/lib/odoo
db_host = db_host
addons_path = /mnt/extra-addons
something_else = something_else
[anothersection]
anothersection_key = value
"""
)
@classmethod
def tearDownClass(cls) -> None:
"""Clean up temporary files"""
if os.path.exists(cls.conf_path):
os.remove(cls.conf_path)
def test_no_env_var(self):
"""Test when there is no environment variable"""
with tempfile.TemporaryDirectory() as tmpdirname:
new_conf = os.path.join(tmpdirname, "odoo.conf")
inject_env_var(self.conf_path, new_conf)
config_parser = ConfigParser()
config_parser.read(new_conf)
self.assertTrue(config_parser.has_section("options"))
self.assertEqual(config_parser.get("options", "db_user"), "db_user")
self.assertEqual(config_parser.get("options", "db_password"), "db_password")
self.assertEqual(config_parser.get("options", "db_name"), "db_name")
self.assertEqual(config_parser.get("options", "max_cron_threads"), "3")
self.assertEqual(config_parser.get("options", "load_language"), "fr_FR,en")
self.assertEqual(config_parser.get("options", "data_dir"), "/var/lib/odoo")
self.assertEqual(config_parser.get("options", "db_host"), "db_host")
self.assertEqual(
config_parser.get("options", "addons_path"), "/mnt/extra-addons"
)
self.assertEqual(
config_parser.get("options", "something_else"), "something_else"
)
self.assertTrue(config_parser.has_section("anothersection"))
self.assertEqual(
config_parser.get("anothersection", "anothersection_key"), "value"
)
os.remove(new_conf)
def test_target_exists(self):
"""Test when the target file exists"""
with tempfile.TemporaryDirectory() as tmpdirname:
new_conf = os.path.join(tmpdirname, "odoo.conf")
with open(new_conf, "w", encoding="UTF-8") as file:
file.write("\n")
with self.assertRaises(Exception):
inject_env_var(self.conf_path, new_conf)
def test_no_options_section(self):
"""Test when the source file does not have the options section"""
self.addCleanup(clean_up_env)
with tempfile.TemporaryDirectory() as tmpdirname:
empty_conf = os.path.join(tmpdirname, "odoo-empty.conf")
with open(empty_conf, "w", encoding="UTF-8") as file:
file.write("\n")
new_conf = os.path.join(tmpdirname, "odoo.conf")
os.environ["ODOO_OPTIONS_DB_USER"] = "user"
inject_env_var(empty_conf, new_conf)
config_parser = ConfigParser()
config_parser.read(new_conf)
self.assertTrue(config_parser.has_section("options"))
self.assertEqual(config_parser.get("options", "db_user"), "user")
def test_env_var(self):
"""Test when there is no configuration file"""
self.addCleanup(clean_up_env)
with tempfile.TemporaryDirectory() as tmpdirname:
new_conf = os.path.join(tmpdirname, "odoo.conf")
os.environ["ODOO_OPTIONS_DB_USER"] = "another"
os.environ["ODOO_OPTIONS_DB_PASSWORD"] = "password"
os.environ["ODOO_OPTIONS_DB_NAME"] = "name"
os.environ["ODOO_OPTIONS_DB_HOST"] = "host"
os.environ["ODOO_OPTIONS_MAX_CRON_THREADS"] = "2"
os.environ["ODOO_OPTIONS_DATA_DIR"] = "/mnt"
os.environ["ODOO_OPTIONS_LOAD_LANGUAGE"] = "en_US"
os.environ["ODOO_OPTIONS_ADDONS_PATH"] = "/addons"
# use main rather than inject_env_var to also test its code
main(["--source", self.conf_path, "--target", new_conf])
config_parser = ConfigParser()
config_parser.read(new_conf)
self.assertTrue(config_parser.has_section("options"))
self.assertEqual(config_parser.get("options", "db_user"), "another")
self.assertEqual(config_parser.get("options", "db_password"), "password")
self.assertEqual(config_parser.get("options", "db_name"), "name")
self.assertEqual(config_parser.get("options", "max_cron_threads"), "2")
self.assertEqual(config_parser.get("options", "load_language"), "en_US")
self.assertEqual(config_parser.get("options", "data_dir"), "/mnt")
self.assertEqual(config_parser.get("options", "db_host"), "host")
self.assertEqual(config_parser.get("options", "addons_path"), "/addons")
self.assertEqual(
config_parser.get("options", "something_else"), "something_else"
)
self.assertTrue(config_parser.has_section("anothersection"))
self.assertEqual(
config_parser.get("anothersection", "anothersection_key"), "value"
)
os.remove(new_conf)
if __name__ == "__main__":
unittest.main()