Beispiel #1
0
def test_code_loader(tmp_path):
    """Test loading a new module"""
    cl = loader.CodeLoader(tmp_path)

    def deploy(code: str) -> None:
        sha1sum = hashlib.new("sha1")
        sha1sum.update(code.encode())
        hv: str = sha1sum.hexdigest()
        cl.deploy_version([ModuleSource("inmanta_plugins.inmanta_unit_test", code, hv)])

    with pytest.raises(ImportError):
        import inmanta_plugins.inmanta_unit_test  # NOQA

    deploy(
        """
def test():
    return 10
        """
    )

    import inmanta_plugins.inmanta_unit_test  # NOQA

    assert inmanta_plugins.inmanta_unit_test.test() == 10

    # deploy new version
    deploy(
        """
def test():
    return 20
        """
    )

    assert inmanta_plugins.inmanta_unit_test.test() == 20
Beispiel #2
0
def test_plugin_loading_old_format(tmpdir, capsys):
    """
    Ensure the code loader ignores code formatted in the old on disk format (pre Inmanta 2020.4).
    (See issue: #2162)
    """
    # Create directory structure code dir
    code_dir = tmpdir
    modules_dir = tmpdir.join(loader.MODULE_DIR)
    modules_dir.mkdir()

    # Create source files using pre Inmanta 2020.4 format
    old_format_source_file = modules_dir.join("inmanta_plugins.old_format.py")
    old_format_source_file.write("")

    # Assert code using the pre inmanta 2020.4 format is ignored
    loader.CodeLoader(code_dir)
    with pytest.raises(ImportError):
        import inmanta_plugins.old_format  # NOQA

    # Add newly formatted code next the pre Inmanta 2020.4 format
    new_format_mod_dir = modules_dir.join("new_format")
    new_format_mod_dir.mkdir()
    new_format_plugins_dir = new_format_mod_dir.join("plugins")
    new_format_plugins_dir.mkdir()
    new_format_source_file = new_format_plugins_dir.join("__init__.py")
    new_format_source_file.write(
        """
def test():
    return 10
    """
    )

    # Assert newly formatted code is loaded and code using the pre inmanta 2020.4 format is ignored
    loader.CodeLoader(code_dir)
    import inmanta_plugins.new_format as mod  # NOQA

    assert mod.test() == 10
    with pytest.raises(ImportError):
        import inmanta_plugins.old_format  # NOQA
Beispiel #3
0
def test_2312_code_loader_missing_init(tmp_path) -> None:
    cl = loader.CodeLoader(tmp_path)

    code: str = """
def test():
    return 10
        """
    sha1sum = hashlib.new("sha1")
    sha1sum.update(code.encode())
    hv: str = sha1sum.hexdigest()
    cl.deploy_version([ModuleSource("inmanta_plugins.my_module.my_sub_mod", code, hv)])

    import inmanta_plugins.my_module.my_sub_mod as sm

    assert sm.test() == 10
Beispiel #4
0
def test_code_loader_import_error(tmp_path, caplog):
    """Test loading code with an import error"""
    cl = loader.CodeLoader(tmp_path)
    code = """
import badimmport
def test():
    return 10
    """

    sha1sum = hashlib.new("sha1")
    sha1sum.update(code.encode())
    hv = sha1sum.hexdigest()

    with pytest.raises(ImportError):
        import inmanta_bad_unit_test  # NOQA

    cl.deploy_version([ModuleSource("inmanta_plugins.inmanta_bad_unit_test", code, hv)])

    assert "ModuleNotFoundError: No module named 'badimmport'" in caplog.text
Beispiel #5
0
def test_code_loader_dependency(tmp_path, caplog):
    """Test loading two modules with a dependency between them"""
    cl = loader.CodeLoader(tmp_path)

    def get_module_source(module: str, code: str) -> ModuleSource:
        sha1sum = hashlib.new("sha1")
        sha1sum.update(code.encode())
        hv: str = sha1sum.hexdigest()
        return ModuleSource(module, code, hv)

    source_init: ModuleSource = get_module_source(
        "inmanta_plugins.inmanta_unit_test_modular",
        """
        """,
    )

    source_tests: ModuleSource = get_module_source(
        "inmanta_plugins.inmanta_unit_test_modular.tests",
        """
from inmanta_plugins.inmanta_unit_test_modular.helpers import helper

def test():
    return 10 + helper()
        """,
    )

    source_helpers: ModuleSource = get_module_source(
        "inmanta_plugins.inmanta_unit_test_modular.helpers",
        """
def helper():
    return 1
        """,
    )

    cl.deploy_version([source_tests, source_helpers, source_init])

    import inmanta_plugins.inmanta_unit_test_modular.tests  # NOQA

    assert inmanta_plugins.inmanta_unit_test_modular.tests.test() == 11
    assert "ModuleNotFoundError: No module named" not in caplog.text