Skip to content
Snippets Groups Projects
test_odoo_conf_inject_env_var.py 6.04 KiB
Newer Older
"""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()