Esempio n. 1
0
def test_module_add_v2_module_to_v2_module(tmpdir: py.path.local, monkeypatch, modules_v2_dir: str) -> None:
    """
    Add a V2 module to a V2 module using the `inmanta module add` command.
    """
    # Create module to execute `inmanta module add` command on
    module_dir = os.path.join(tmpdir, "test")
    module_from_template(source_dir=os.path.join(modules_v2_dir, "elaboratev2module"), dest_dir=module_dir)
    monkeypatch.chdir(module_dir)

    def _assert_module_requirements(expected_requirements: List[str]) -> None:
        module_v2 = ModuleV2(project=None, path=module_dir)
        assert sorted(module_v2.metadata.install_requires) == sorted(expected_requirements)
        assert not os.path.exists(os.path.join(module_dir, "requirements.txt"))

    _assert_module_requirements(expected_requirements=[])
    installed_packages = process_env.get_installed_packages()

    name_dependent_module = "a_module"
    ModuleTool().add(module_req="a_module", v2=True, override=False)
    _assert_module_requirements(expected_requirements=[ModuleV2Source.get_package_name_for(name_dependent_module)])

    with pytest.raises(CLIException, match="A dependency on the given module was already defined"):
        ModuleTool().add(module_req=f"{name_dependent_module}==1.1.1", v2=True, override=False)
    _assert_module_requirements(expected_requirements=[ModuleV2Source.get_package_name_for(name_dependent_module)])

    ModuleTool().add(module_req=f"{name_dependent_module}==1.1.1", v2=True, override=True)
    _assert_module_requirements(expected_requirements=[f"{ModuleV2Source.get_package_name_for(name_dependent_module)}==1.1.1"])

    # Ensure no new packages were installed as a side-effect of `inmanta modules add`
    assert process_env.get_installed_packages() == installed_packages
Esempio n. 2
0
def test_module_install_version(
    tmpdir: py.path.local,
    snippetcompiler_clean,
    modules_v2_dir: str,
    dev: bool,
) -> None:
    """
    Make sure that the module install results in a module instance with the appropriate version information.
    :param dev: whether to add a dev tag to the version
    """
    module_name: str = "minimalv2module"
    module_path: str = os.path.join(str(tmpdir), module_name)
    module_version: version.Version = version.Version(
        "1.2.3") if not dev else version.Version("1.2.3.dev0")

    # set up simple project and activate the snippetcompiler venv
    project: module.Project = snippetcompiler_clean.setup_for_snippet("")

    # install module
    module_from_template(
        os.path.join(modules_v2_dir, module_name),
        module_path,
        new_version=module_version,
    )
    os.chdir(project.path)
    ModuleTool().install(editable=True, path=module_path)

    # check version
    mod: module.Module = ModuleTool().get_module(module_name)
    assert mod.version == module_version
Esempio n. 3
0
def test_complex_checkout(git_modules_dir, modules_repo):
    coroot = os.path.join(git_modules_dir, "testproject")
    subprocess.check_output([
        "git", "clone",
        os.path.join(git_modules_dir, "repos", "testproject")
    ],
                            cwd=git_modules_dir,
                            stderr=subprocess.STDOUT)
    os.chdir(coroot)
    Config.load_config()

    ProjectTool().execute("install", [])
    expected = ["mod1", "mod2", "mod3", "mod6", "mod7"]
    for i in expected:
        dirname = os.path.join(coroot, "libs", i)
        assert os.path.exists(os.path.join(dirname, "signal"))
        assert not os.path.exists(os.path.join(dirname, "badsignal"))

    assert not os.path.exists(os.path.join(coroot, "libs", "mod5"))

    # test all tools, perhaps isolate to other test case
    ModuleTool().execute("list", [])
    ModuleTool().execute("update", [])
    ModuleTool().execute("status", [])
    ModuleTool().execute("push", [])
Esempio n. 4
0
def test_module_add_v2_module_to_v1_module(tmpdir: py.path.local, modules_dir: str, monkeypatch) -> None:
    """
    Add a V2 module to a V1 module using the `inmanta module add` command.
    """
    original_module_dir = os.path.join(modules_dir, "mod1")
    module_dir = os.path.join(tmpdir, "mod1")
    shutil.copytree(original_module_dir, module_dir)
    requirements_txt_file = os.path.join(module_dir, "requirements.txt")
    monkeypatch.chdir(module_dir)

    def _assert_module_requirements(expected_requirement: str) -> None:
        module_v1 = ModuleV1(project=None, path=module_dir)
        assert module_v1.metadata.requires == []
        with open(requirements_txt_file, "r", encoding="utf-8") as fd:
            assert fd.read().strip() == expected_requirement

    assert not os.path.exists(requirements_txt_file)
    installed_packages = process_env.get_installed_packages()

    name_dependent_module = "a_module"
    ModuleTool().add(module_req=name_dependent_module, v2=True, override=False)
    _assert_module_requirements(expected_requirement=ModuleV2Source.get_package_name_for(name_dependent_module))

    with pytest.raises(CLIException, match="A dependency on the given module was already defined"):
        ModuleTool().add(module_req=f"{name_dependent_module}==1.1.1", v2=True, override=False)
    _assert_module_requirements(expected_requirement=ModuleV2Source.get_package_name_for(name_dependent_module))

    ModuleTool().add(module_req=f"{name_dependent_module}==1.1.1", v2=True, override=True)
    _assert_module_requirements(expected_requirement=f"{ModuleV2Source.get_package_name_for(name_dependent_module)}==1.1.1")

    # Ensure no new packages were installed as a side-effect of `inmanta modules add`
    assert process_env.get_installed_packages() == installed_packages
Esempio n. 5
0
def test_module_add_v1_module_to_v1_module(tmpdir: py.path.local, modules_dir: str, monkeypatch) -> None:
    """
    Add a V1 module to a V1 module using the `inmanta module add` command.
    """
    original_module_dir = os.path.join(modules_dir, "mod1")
    module_dir = os.path.join(tmpdir, "mod1")
    shutil.copytree(original_module_dir, module_dir)

    def _assert_module_requirements(expected_requirements: List[str]) -> None:
        module_v1 = ModuleV1(project=None, path=module_dir)
        assert sorted(module_v1.metadata.requires) == sorted(expected_requirements)
        assert not os.path.exists(os.path.join(module_dir, "requirements.txt"))

    monkeypatch.chdir(module_dir)
    _assert_module_requirements(expected_requirements=[])

    ModuleTool().add(module_req="mod3", v1=True, override=False)
    _assert_module_requirements(expected_requirements=["mod3"])

    with pytest.raises(CLIException, match="A dependency on the given module was already defined"):
        ModuleTool().add(module_req="mod3==1.0.1", v1=True, override=False)
    _assert_module_requirements(expected_requirements=["mod3"])

    ModuleTool().add(module_req="mod3==1.0.1", v1=True, override=True)
    _assert_module_requirements(expected_requirements=["mod3==1.0.1"])
Esempio n. 6
0
def test_module_add_v1_module_to_project(snippetcompiler_clean) -> None:
    """
    Add a V1 module to an inmanta project using the `inmanta module add` command.
    """
    project: Project = snippetcompiler_clean.setup_for_snippet(snippet="", autostd=False)
    requirements_txt_file = os.path.join(project.path, "requirements.txt")

    def _get_content_requirements_txt_file() -> str:
        with open(requirements_txt_file, "r", encoding="utf-8") as fd:
            return fd.read()

    def _assert_project_state(constraint: str) -> None:
        with open(project.get_metadata_file_path(), "r", encoding="utf-8") as fd:
            project_metadata = ProjectMetadata.parse(fd)
            assert constraint in project_metadata.requires
        with open(os.path.join(snippetcompiler_clean.libs, "std", ModuleV1.MODULE_FILE), "r", encoding="utf-8") as fd:
            module_metadata = ModuleV1Metadata.parse(fd)
            assert module_metadata.version == constraint.split("==")[1]
        assert os.listdir(snippetcompiler_clean.libs) == ["std"]
        assert not _get_content_requirements_txt_file()

    assert not os.listdir(snippetcompiler_clean.libs)
    assert not _get_content_requirements_txt_file()

    version_constraint = "std==3.0.2"
    ModuleTool().add(module_req=version_constraint, v1=True, override=False)
    _assert_project_state(version_constraint)

    new_version_constraint = "std==3.0.1"
    with pytest.raises(CLIException, match="A dependency on the given module was already defined"):
        ModuleTool().add(module_req=new_version_constraint, v1=True, override=False)
    _assert_project_state(version_constraint)

    ModuleTool().add(module_req=new_version_constraint, v1=True, override=True)
    _assert_project_state(new_version_constraint)
Esempio n. 7
0
def test_for_git_failures(git_modules_dir, modules_repo):
    coroot = os.path.join(git_modules_dir, "testproject2")
    subprocess.check_output(
        [
            "git", "clone",
            os.path.join(git_modules_dir, "repos", "testproject"),
            "testproject2"
        ],
        cwd=git_modules_dir,
        stderr=subprocess.STDOUT,
    )
    os.chdir(coroot)
    Config.load_config()

    ProjectTool().execute("install", [])

    gp = module.gitprovider
    module.gitprovider = BadModProvider(gp,
                                        os.path.join(coroot, "libs", "mod6"))
    try:
        # test all tools, perhaps isolate to other test case
        ProjectTool().execute("install", [])
        ModuleTool().execute("list", [])
        ModuleTool().execute("update", [])
        ModuleTool().execute("status", [])
        ModuleTool().execute("push", [])
    finally:
        module.gitprovider = gp
Esempio n. 8
0
def test_code_manager():
    """Verify the code manager"""
    project_dir: str = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    "data", "plugins_project")
    project: Project = Project(project_dir)
    Project.set(project)
    project.load()

    ModuleTool().install("single_plugin_file")
    ModuleTool().install("multiple_plugin_files")
    import inmanta_plugins.multiple_plugin_files.handlers as multi
    import inmanta_plugins.single_plugin_file as single

    mgr = loader.CodeManager()
    mgr.register_code("std::File", single.MyHandler)
    mgr.register_code("std::Directory", multi.MyHandler)

    def assert_content(source_info: SourceInfo, handler) -> str:
        filename = inspect.getsourcefile(handler)
        content: str
        with open(filename, "r", encoding="utf-8") as fd:
            content = fd.read()
            assert source_info.content == content
            assert len(source_info.hash) > 0
            return content

    # get types
    types = dict(mgr.get_types())
    assert "std::File" in types
    assert "std::Directory" in types

    single_type_list: List[SourceInfo] = types["std::File"]
    multi_type_list: List[SourceInfo] = types["std::Directory"]

    assert len(single_type_list) == 1
    single_content: str = assert_content(single_type_list[0], single.MyHandler)

    assert len(multi_type_list) == 3
    multi_content: str = assert_content(
        next(s for s in multi_type_list if s.module_name ==
             "inmanta_plugins.multiple_plugin_files.handlers"),
        multi.MyHandler)

    # get_file_hashes
    mgr_contents: Set[str] = {
        mgr.get_file_content(hash)
        for hash in mgr.get_file_hashes()
    }
    assert single_content in mgr_contents
    assert multi_content in mgr_contents

    with pytest.raises(KeyError):
        mgr.get_file_content("test")

    # register type without source
    with pytest.raises(loader.SourceNotFoundException):
        mgr.register_code("test2", str)
def test_get_module_v1(tmp_working_dir: py.path.local):
    metadata_file: str = tmp_working_dir.join("module.yml")
    metadata_file.write("""
name: mod
license: ASL
version: 1.2.3
compiler_version: 2017.2
        """.strip())

    mt = ModuleTool()
    mod: module.Module = mt.get_module()
    assert mod.GENERATION == module.ModuleGeneration.V1
Esempio n. 10
0
def test_module_add_v2_module_to_project(
    tmpdir: py.path.local, snippetcompiler_clean, local_module_package_index: str, modules_v2_dir: str
) -> None:
    """
    Add a V2 module to an inmanta project using the `inmanta module add` command.
    """
    # Ensure that versions 1.1.1 and 1.2.0 exist in the pip index.
    # Version 1.2.3 is present in the pip index exported by local_module_package_index.
    pip_index = PipIndex(artifact_dir=os.path.join(str(tmpdir), "pip-index"))
    for version in ["1.1.1", "1.2.0"]:
        module_from_template(
            source_dir=os.path.join(modules_v2_dir, "elaboratev2module"),
            dest_dir=os.path.join(tmpdir, f"elaboratev2module-v{version}"),
            new_version=Version(version),
            publish_index=pip_index,
        )

    # Create project
    project: Project = snippetcompiler_clean.setup_for_snippet(
        snippet="", autostd=False, python_package_sources=[local_module_package_index, pip_index.url]
    )

    requirements_txt_file = os.path.join(project.path, "requirements.txt")

    def _assert_project_state(pkg_name: str, expected_version: Version, project_requires_constraint: str) -> None:
        installed_packages = process_env.get_installed_packages()
        assert pkg_name in installed_packages
        assert installed_packages[pkg_name] == expected_version
        with open(project.get_metadata_file_path(), "r", encoding="utf-8") as fd:
            project_metadata = ProjectMetadata.parse(fd)
            assert not project_metadata.requires
        with open(requirements_txt_file, "r", encoding="utf-8") as fd:
            assert fd.read().strip() == ModuleV2Source.get_package_name_for(project_requires_constraint)

    module_name = "elaboratev2module"
    pkg_name = "inmanta-module-elaboratev2module"
    assert pkg_name not in process_env.get_installed_packages().keys()

    version_constraint = f"{module_name}==1.2.3"
    ModuleTool().add(module_req=version_constraint, v2=True, override=False)
    _assert_project_state(pkg_name=pkg_name, expected_version=Version("1.2.3"), project_requires_constraint=version_constraint)

    new_version_constraint = f"{module_name}==1.2.0"
    with pytest.raises(CLIException, match="A dependency on the given module was already defined"):
        ModuleTool().add(module_req=new_version_constraint, v2=True, override=False)
    _assert_project_state(pkg_name=pkg_name, expected_version=Version("1.2.3"), project_requires_constraint=version_constraint)

    ModuleTool().add(module_req=new_version_constraint, v2=True, override=True)
    _assert_project_state(
        pkg_name=pkg_name, expected_version=Version("1.2.0"), project_requires_constraint=new_version_constraint
    )
Esempio n. 11
0
def test_module_add_preinstalled(tmpdir: py.path.local, modules_v2_dir: str, snippetcompiler_clean, caplog) -> None:
    """
    Verify that `inmanta module add` respects preinstalled modules when they're compatible and logs a warning when they're
    not.
    """
    module_name: str = "mymodule"
    pip_index = PipIndex(artifact_dir=str(tmpdir.join("pip-index")))
    snippetcompiler_clean.setup_for_snippet(snippet="", autostd=False, python_package_sources=[pip_index.url])

    # preinstall 1.0.0, don't publish to index
    module_from_template(
        os.path.join(modules_v2_dir, "minimalv2module"),
        str(tmpdir.join(module_name, "1.0.0")),
        new_name=module_name,
        new_version=Version("1.0.0"),
        install=True,
    )
    # publish 1.1.0 and 2.0.0 to index
    module_from_template(
        os.path.join(modules_v2_dir, "minimalv2module"),
        str(tmpdir.join(module_name, "1.1.0")),
        new_name=module_name,
        new_version=Version("1.1.0"),
        install=False,
        publish_index=pip_index,
    )
    module_from_template(
        os.path.join(modules_v2_dir, "minimalv2module"),
        str(tmpdir.join(module_name, "2.0.0")),
        new_name=module_name,
        new_version=Version("2.0.0"),
        install=False,
        publish_index=pip_index,
    )

    # verify that compatible constraint does not reinstall or update
    ModuleTool().add(module_req=f"{module_name}~=1.0", v2=True, override=True)
    caplog.clear()
    with caplog.at_level(logging.WARNING):
        assert ModuleTool().get_module(module_name).version == Version("1.0.0")
        assert "does not match constraint" not in caplog.text

    # verify that incompatible constraint does reinstall and logs a warning
    with caplog.at_level(logging.WARNING):
        ModuleTool().add(module_req=f"{module_name}~=2.0", v2=True, override=True)
        assert (
            f"Currently installed {module_name}-1.0.0 does not match constraint ~=2.0: updating to compatible version."
            in caplog.messages
        )
    assert ModuleTool().get_module(module_name).version == Version("2.0.0")
Esempio n. 12
0
def test_3322_module_install_deep_data_files(tmpdir: py.path.local,
                                             snippetcompiler_clean,
                                             modules_v2_dir: str) -> None:
    """
    Verify that module installation includes data files regardless of depth in the directory structure.
    """
    # set up module directory
    module_name: str = "minimalv2module"
    module_path: str = str(tmpdir.join(module_name))
    module_from_template(
        os.path.join(modules_v2_dir, module_name),
        module_path,
    )
    deep_model_file_rel: str = os.path.join(
        "model",
        *(str(i) for i in range(10)),
        "mymod.cf",
    )
    os.makedirs(os.path.join(module_path,
                             os.path.dirname(deep_model_file_rel)))
    open(os.path.join(module_path, deep_model_file_rel), "w").close()

    # set up simple project and activate snippetcompiler venv
    snippetcompiler_clean.setup_for_snippet("")

    # install module: non-editable mode
    ModuleTool().install(editable=False, path=module_path)

    assert os.path.exists(
        os.path.join(
            env.process_env.site_packages_dir,
            const.PLUGINS_PACKAGE,
            module_name,
            deep_model_file_rel,
        ))
Esempio n. 13
0
def test_module_add_v2_wrong_name_error(tmpdir: py.path.local, monkeypatch, modules_v2_dir: str) -> None:
    """
    Test the error messages of v2 modules when adding with a wrong name. (issue #3556)
    """
    # Create module to execute `inmanta module add` command on
    module_dir = os.path.join(tmpdir, "test")
    module_from_template(source_dir=os.path.join(modules_v2_dir, "elaboratev2module"), dest_dir=module_dir)
    monkeypatch.chdir(module_dir)

    with pytest.raises(ValueError, match="Invalid Inmanta module requirement: Inmanta module names use '_', not '-'."):
        ModuleTool().add(module_req="a-module", v2=True, override=False)

    with pytest.raises(
        ValueError, match="Invalid Inmanta module requirement: Use the Inmanta module name instead of the Python package name"
    ):
        ModuleTool().add(module_req="inmanta-module-a-module", v2=True, override=False)
 def verify_v2_message(command: str,
                       args: Optional[argparse.Namespace] = None) -> None:
     caplog.clear()
     with caplog.at_level(logging.WARNING):
         ModuleTool().execute(
             command, args if args is not None else argparse.Namespace())
         assert "Skipping module minimalv2module: v2 modules do not support this operation." in caplog.messages
Esempio n. 15
0
def test_module_update_syntax_error_in_project(tmpdir: py.path.local,
                                               modules_v2_dir: str,
                                               snippetcompiler_clean) -> None:
    snippetcompiler_clean.setup_for_snippet(snippet="entity",
                                            autostd=False,
                                            install_project=False)
    with pytest.raises(ParserException):
        ModuleTool().update()
Esempio n. 16
0
def test_master_checkout(modules_dir, modules_repo):
    coroot = install_project(modules_dir, "masterproject")

    ModuleTool().execute("install", [])

    dirname = os.path.join(coroot, "libs", "mod8")
    assert os.path.exists(os.path.join(dirname, "devsignal"))
    assert os.path.exists(os.path.join(dirname, "mastersignal"))
Esempio n. 17
0
def test_3322_module_install_preinstall_cleanup(tmpdir: py.path.local,
                                                snippetcompiler_clean,
                                                modules_v2_dir: str) -> None:
    """
    Verify that installing a module from source cleans up any old installation's data files.
    """
    # set up module directory
    module_name: str = "minimalv2module"
    module_path: str = str(tmpdir.join(module_name))
    module_from_template(
        os.path.join(modules_v2_dir, module_name),
        module_path,
        new_version=version.Version("1.0.0"),
    )
    model_file_rel: str = os.path.join("model", "mymod.cf")
    model_file_source_path: str = os.path.join(module_path, model_file_rel)
    assert not os.path.exists(model_file_source_path)
    open(model_file_source_path, "w").close()

    def model_file_installed() -> bool:
        return os.path.exists(
            os.path.join(
                env.process_env.site_packages_dir,
                const.PLUGINS_PACKAGE,
                module_name,
                model_file_rel,
            ))

    # set up simple project and activate snippetcompiler venv
    snippetcompiler_clean.setup_for_snippet("")

    # install module: non-editable mode
    ModuleTool().install(editable=False, path=module_path)
    assert model_file_installed()

    # remove model file and reinstall
    os.remove(model_file_source_path)
    module_from_template(
        module_path,
        new_version=version.Version("2.0.0"),
        in_place=True,
    )
    ModuleTool().install(editable=False, path=module_path)
    assert not model_file_installed()
Esempio n. 18
0
def test_module_install_reinstall(
    tmpdir: py.path.local,
    snippetcompiler_clean,
    modules_v2_dir,
) -> None:
    """
    Verify that reinstalling a module from source installs any changes to model and Python files if the version is bumped.
    """
    module_name: str = "minimalv2module"
    module_path: str = str(tmpdir.join(module_name))
    module_from_template(
        os.path.join(modules_v2_dir, module_name),
        dest_dir=module_path,
        new_version=version.Version("1.0.0"),
    )

    # set up simple project and activate snippetcompiler venv
    snippetcompiler_clean.setup_for_snippet("")

    def new_files_exist() -> Iterator[bool]:
        return (
            os.path.exists(
                os.path.join(env.process_env.site_packages_dir,
                             const.PLUGINS_PACKAGE, module_name, rel_path))
            for rel_path in [os.path.join("model", "newmod.cf"), "newmod.py"])

    # install module
    ModuleTool().install(editable=False, path=module_path)

    assert not any(new_files_exist())

    # make some changes to the source and install again
    model_dir: str = os.path.join(module_path, "model")
    os.makedirs(model_dir, exist_ok=True)
    open(os.path.join(model_dir, "newmod.cf"), "w").close()
    open(
        os.path.join(module_path, const.PLUGINS_PACKAGE, module_name,
                     "newmod.py"), "w").close()
    module_from_template(module_path,
                         new_version=version.Version("2.0.0"),
                         in_place=True)
    ModuleTool().install(editable=False, path=module_path)

    assert all(new_files_exist())
def test_moduletool_create_v2(tmp_working_dir: py.path.local) -> None:
    """
    Verify that `inmanta module create` creates a valid v2 module with expected parameters.
    """
    ModuleTool().execute(
        "create", argparse.Namespace(name="my_module", v1=False,
                                     no_input=True))
    mod: module.ModuleV2 = module.ModuleV2(
        project=None, path=str(tmp_working_dir.join("my-module")))
    assert mod.name == "my_module"
Esempio n. 20
0
def add_file(modpath, file, content, msg, version=None, dev=False, tag=True):
    with open(os.path.join(modpath, file), "w", encoding="utf-8") as projectfile:
        projectfile.write(content)

    if version is None:
        return commitmodule(modpath, msg)
    else:
        ocd = os.curdir
        os.curdir = modpath
        subprocess.check_output(["git", "add", "*"], cwd=modpath, stderr=subprocess.STDOUT)
        ModuleTool().commit(msg, version=version, dev=dev, commit_all=True, tag=tag)
        os.curdir = ocd
Esempio n. 21
0
def test_freeze_basic(modules_dir, modules_repo):
    install_project(modules_dir, "projectA")
    modtool = ModuleTool()
    cmod = modtool.get_module("modC")
    assert cmod.get_freeze("modC", recursive=False, mode="==") == {
        "std": "== 3.2",
        "modE": "== 3.2",
        "modF": "== 3.2"
    }
    assert cmod.get_freeze("modC", recursive=True, mode="==") == {
        "std": "== 3.2",
        "modE": "== 3.2",
        "modF": "== 3.2",
        "modH": "== 3.2",
        "modJ": "== 3.2",
    }

    assert cmod.get_freeze("modC::a", recursive=False, mode="==") == {
        "std": "== 3.2",
        "modI": "== 3.2"
    }
Esempio n. 22
0
def test_project_freeze_basic(modules_dir, modules_repo):
    install_project(modules_dir, "projectA")
    modtool = ModuleTool()
    proj = modtool.get_project()
    assert proj.get_freeze(recursive=False, mode="==") == {
        "std": "== 3.2",
        "modB": "== 3.2",
        "modC": "== 3.2",
        "modD": "== 3.2",
    }
    assert proj.get_freeze(recursive=True, mode="==") == {
        "std": "== 3.2",
        "modB": "== 3.2",
        "modC": "== 3.2",
        "modD": "== 3.2",
        "modE": "== 3.2",
        "modF": "== 3.2",
        "modG": "== 3.2",
        "modH": "== 3.2",
        "modJ": "== 3.2",
    }
Esempio n. 23
0
def test_for_repo_without_versions(modules_dir, modules_repo):
    coroot = os.path.join(modules_dir, "noverproject")
    subprocess.check_output(
        ["git", "clone",
         os.path.join(modules_dir, "repos", "noverproject")],
        cwd=modules_dir,
        stderr=subprocess.STDOUT)
    os.chdir(coroot)
    os.curdir = coroot
    Config.load_config()

    ModuleTool().execute("install", [])
Esempio n. 24
0
def test_module_add_preinstalled_v1(snippetcompiler_clean, caplog) -> None:
    """
    Verify that `inmanta module add` respects preinstalled v1 modules when they're compatible and logs a warning when they're
    not.
    """
    module_name: str = "std"
    snippetcompiler_clean.setup_for_snippet(snippet="", autostd=False)

    # preinstall 2.0.0
    ModuleTool().add(module_req=f"{module_name}==2.0.0", v1=True)
    assert ModuleTool().get_module(module_name).version == Version("2.0.0")

    # verify that compatible constraint does not reinstall or update
    ModuleTool().add(module_req=f"{module_name}~=2.0", v1=True, override=True)
    caplog.clear()
    with caplog.at_level(logging.WARNING):
        assert ModuleTool().get_module(module_name).version == Version("2.0.0")
        assert not caplog.messages

    # verify that incompatible constraint does reinstall and logs a warning
    with caplog.at_level(logging.WARNING):
        ModuleTool().add(module_req=f"{module_name}~=2.1.0", v1=True, override=True)
        assert (
            f"Currently installed {module_name}-2.0.0 does not match constraint ~=2.1.0: updating to compatible version."
            in caplog.messages
        )
    assert Version("2.1.11") <= ModuleTool().get_module(module_name).version < Version("2.2")
Esempio n. 25
0
def test_bad_checkout(modules_dir, modules_repo):
    coroot = os.path.join(modules_dir, "badproject")
    subprocess.check_output(
        ["git", "clone",
         os.path.join(modules_dir, "repos", "badproject")],
        cwd=modules_dir,
        stderr=subprocess.STDOUT)
    os.chdir(coroot)
    os.curdir = coroot
    Config.load_config()

    with pytest.raises(ModuleNotFoundException):
        ModuleTool().execute("install", [])
def test_get_module_v2(tmp_working_dir: py.path.local):
    metadata_file: str = tmp_working_dir.join("setup.cfg")
    metadata_file.write("""
[metadata]
name = inmanta-module-mod
version = 1.2.3
license = ASL

[options]
install_requires =
  inmanta-modules-net ~=0.2.4
  inmanta-modules-std >1.0,<2.5

  cookiecutter~=1.7.0
  cryptography>1.0,<3.5
        """.strip())
    model_dir: py.path.local = tmp_working_dir.join("model")
    os.makedirs(str(model_dir))
    open(str(model_dir.join("_init.cf")), "w").close()

    mt = ModuleTool()
    mod: module.Module = mt.get_module()
    assert mod.GENERATION == module.ModuleGeneration.V2
Esempio n. 27
0
def run_module_install(module_path: str, editable: bool,
                       set_path_argument: bool) -> None:
    """
    Install the Inmanta module (v2) into the active environment using the `inmanta module install` command.

    :param module_path: Path to the inmanta module
    :param editable: Install the module in editable mode (pip install -e).
    :param set_path_argument: If true provide the module_path via the path argument, otherwise the module path is set via cwd.
    """
    if not set_path_argument:
        os.chdir(module_path)
    ModuleTool().execute(
        "install",
        argparse.Namespace(editable=editable,
                           path=module_path if set_path_argument else None))
Esempio n. 28
0
def test_dev_checkout(modules_dir, modules_repo):
    coroot = os.path.join(modules_dir, "devproject")
    subprocess.check_output(
        ["git", "clone",
         os.path.join(modules_dir, "repos", "devproject")],
        cwd=modules_dir,
        stderr=subprocess.STDOUT)
    os.chdir(coroot)
    os.curdir = coroot
    Config.load_config()

    ModuleTool().execute("install", [])

    dirname = os.path.join(coroot, "libs", "mod8")
    assert os.path.exists(os.path.join(dirname, "devsignal"))
    assert not os.path.exists(os.path.join(dirname, "mastersignal"))
def test_module_v2_incompatible_commands(caplog,
                                         local_module_package_index: str,
                                         snippetcompiler,
                                         modules_v2_dir: str) -> None:
    """
    Verify that module v2 incompatible commands are reported as such.
    """
    # set up project with a v1 and a v2 module
    snippetcompiler.setup_for_snippet(
        """
import minimalv1module
import minimalv2module
        """.strip(),
        python_package_sources=[local_module_package_index],
        python_requires=[
            module.ModuleV2Source.get_python_package_requirement(
                module.InmantaModuleRequirement.parse("minimalv2module"))
        ],
        autostd=False,
    )

    def verify_v2_message(command: str,
                          args: Optional[argparse.Namespace] = None) -> None:
        caplog.clear()
        with caplog.at_level(logging.WARNING):
            ModuleTool().execute(
                command, args if args is not None else argparse.Namespace())
            assert "Skipping module minimalv2module: v2 modules do not support this operation." in caplog.messages

    verify_v2_message("status")
    verify_v2_message("do",
                      argparse.Namespace(module=None, command="echo hello"))
    cwd = os.getcwd()
    try:
        os.chdir(os.path.join(modules_v2_dir, "minimalv2module"))
        with pytest.raises(
                CLIException,
                match=
                "minimalv2module is a v2 module and does not support this operation."
        ):
            ModuleTool().execute("commit",
                                 argparse.Namespace(message="message"))
    finally:
        os.chdir(cwd)
    verify_v2_message("push")
def test_moduletool_create_v1(snippetcompiler_clean) -> None:
    """
    Verify that `inmanta module create --v1` creates a valid v1 module with expected parameters.
    """
    project: module.Project = snippetcompiler_clean.setup_for_snippet(
        "", add_to_module_path=["libs"])
    os.mkdir(os.path.join(project.path, "libs"))
    cwd = os.getcwd()
    try:
        os.chdir(project.path)
        ModuleTool().execute("create",
                             argparse.Namespace(name="my_module", v1=True))
        mod: module.ModuleV1 = module.ModuleV1(project=None,
                                               path=os.path.join(
                                                   project.path, "libs",
                                                   "my_module"))
        assert mod.name == "my_module"
    finally:
        os.chdir(cwd)