async def test_version_argument(modules_repo):
    make_module_simple(modules_repo, "mod-version", [], "1.2")
    module_path = os.path.join(modules_repo, "mod-version")

    mod = module.ModuleV1(None, module_path)
    assert mod.version == version.Version("1.2")

    args = [
        sys.executable, "-m", "inmanta.app", "module", "commit", "-m", "msg",
        "-v", "1.3.1", "-r"
    ]
    process = await asyncio.subprocess.create_subprocess_exec(
        *args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=module_path)
    try:
        await asyncio.wait_for(process.communicate(), timeout=30)
    except asyncio.TimeoutError as e:
        process.kill()
        await process.communicate()
        raise e

    # Make sure exitcode is zero
    assert process.returncode == 0

    # Verify changes
    assert mod._get_metadata_from_disk().version == "1.3.1"
Exemple #2
0
def test_get_requirements(
    modules_dir: str,
    modules_v2_dir: str,
    v1_module: bool,
    all_python_requirements: List[str],
    strict_python_requirements: List[str],
    module_requirements: List[str],
    module_v2_requirements: List[str],
) -> None:
    """
    Test the different methods to get the requirements of a module.
    """
    module_name = "many_dependencies"

    if v1_module:
        module_dir = os.path.join(modules_dir, module_name)
        mod = module.ModuleV1(module.DummyProject(autostd=False), module_dir)
    else:
        module_dir = os.path.join(modules_v2_dir, module_name)
        mod = module.ModuleV2(module.DummyProject(autostd=False), module_dir)

    assert set(mod.get_all_python_requirements_as_list()) == set(
        all_python_requirements)
    assert set(mod.get_strict_python_requirements_as_list()) == set(
        strict_python_requirements)
    assert set(mod.get_module_requirements()) == set(module_requirements)
    assert set(mod.get_module_v2_requirements()) == set(module_v2_requirements)
    assert set(mod.requires()) == set(
        module.InmantaModuleRequirement.parse(req)
        for req in module_requirements)
Exemple #3
0
    def test_wrong_name(self):
        mod_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                               "data", "modules", "mod3")
        module.ModuleV1(project=mock.Mock(), path=mod_dir)

        self.handler.flush()
        assert "The name in the module file (mod1) does not match the directory name (mod3)" in self.stream.getvalue(
        ).strip()
def test_module_requires_single(inmanta_module_v1):
    inmanta_module_v1.write_metadata_file("""
name: mod
license: ASL
version: 1.0.0
requires: std > 1.0.0
        """)
    mod: module.Module = module.ModuleV1(
        None, inmanta_module_v1.get_root_dir_of_module())
    assert mod.requires() == [InmantaModuleRequirement.parse("std > 1.0.0")]
def test_invalid_yaml_syntax_in_module_yml(inmanta_module_v1):
    inmanta_module_v1.write_metadata_file("""
test:
 - first
 second
""")
    with pytest.raises(InvalidModuleException) as e:
        module.ModuleV1(None, inmanta_module_v1.get_root_dir_of_module())

    cause = e.value.__cause__
    assert isinstance(cause, InvalidMetadata)
    assert f"Invalid yaml syntax in {inmanta_module_v1.get_metadata_file_path()}" in cause.msg
def test_module_version_non_pep440_complient(inmanta_module_v1):
    inmanta_module_v1.write_metadata_file("""
name: mod
license: ASL
version: non_pep440_value
compiler_version: 2017.2
    """)
    with pytest.raises(InvalidModuleException) as e:
        module.ModuleV1(None, inmanta_module_v1.get_root_dir_of_module())

    cause = e.value.__cause__
    assert isinstance(cause, InvalidMetadata)
    assert "Version non_pep440_value is not PEP440 compliant" in cause.msg
def test_module_requires_legacy_invalid(inmanta_module_v1):
    inmanta_module_v1.write_metadata_file("""
name: mod
license: ASL
version: 1.0.0
requires:
    std: ip
        """)
    with pytest.raises(InvalidModuleException) as e:
        module.ModuleV1(None, inmanta_module_v1.get_root_dir_of_module())

    cause = e.value.__cause__
    assert isinstance(cause, InvalidMetadata)
    assert "Invalid legacy requires" in cause.msg
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)
def test_module_requires_legacy(inmanta_module_v1):
    inmanta_module_v1.write_metadata_file("""
name: mod
license: ASL
version: 1.0.0
requires:
    std: std
    ip: ip > 1.0.0
        """)
    mod: module.Module
    with warnings.catch_warnings(record=True) as w:
        mod = module.ModuleV1(None, inmanta_module_v1.get_root_dir_of_module())
        assert len(w) == 1
        warning = w[0]
        assert issubclass(warning.category, MetadataDeprecationWarning)
        assert "yaml dictionary syntax for specifying module requirements has been deprecated" in str(
            warning.message)
    assert mod.requires() == [
        InmantaModuleRequirement.parse("std"),
        InmantaModuleRequirement.parse("ip > 1.0.0")
    ]
Exemple #10
0
def test_project_install_modules_cache_invalid(
    caplog,
    local_module_package_index: str,
    snippetcompiler_clean,
    tmpdir: py.path.local,
    modules_dir: str,
    modules_v2_dir: str,
) -> None:
    """
    Verify that introducing invalidities in the modules cache results in the appropriate exception and warnings.

    - preinstall old (v1 or v2) version of {dependency_module}
    - install project with {main_module} that depends on {dependency_module}>={v2_version}
    """
    main_module: str = "main_module"
    dependency_module: str = "minimalv1module"
    fq_mod_name: str = "inmanta_plugins.minimalv1module"
    index: PipIndex = PipIndex(
        artifact_dir=os.path.join(str(tmpdir), ".custom-index"))
    libs_dir: str = os.path.join(str(tmpdir), "libs")
    os.makedirs(libs_dir)

    assert env.process_env.get_module_file(fq_mod_name) is None

    # prepare most recent v2 module
    v2_template_path: str = os.path.join(str(tmpdir), dependency_module)
    v1: module.ModuleV1 = module.ModuleV1(project=DummyProject(autostd=False),
                                          path=os.path.join(
                                              modules_dir, dependency_module))
    v2_version: version.Version = version.Version(
        str(v1.version.major + 1) + ".0.0")
    ModuleConverter(v1).convert(output_directory=v2_template_path)
    module_from_template(v2_template_path,
                         os.path.join(str(tmpdir), dependency_module,
                                      "stable"),
                         new_version=v2_version,
                         publish_index=index)

    # prepare main module that depends on stable v2 version of first module
    module_from_template(
        v2_template_path,
        os.path.join(str(tmpdir), main_module),
        new_name=main_module,
        new_content_init_cf=f"import {dependency_module}",
        # requires stable version, not currently installed dev version
        new_requirements=[
            module.InmantaModuleRequirement.parse(
                f"{dependency_module}>={v2_version}")
        ],
        install=False,
        publish_index=index,
    )

    # preinstall module
    # set up project, including activation of venv before installing the module
    snippetcompiler_clean.setup_for_snippet("", install_project=False)
    # install older v2 module
    module_from_template(
        v2_template_path,
        os.path.join(str(tmpdir), dependency_module, "dev"),
        new_version=version.Version(f"{v2_version}.dev0"),
        install=True,
    )

    # set up project for installation
    project: module.Project = snippetcompiler_clean.setup_for_snippet(
        f"import {main_module}",
        autostd=False,
        install_project=False,
        add_to_module_path=[libs_dir],
        python_package_sources=[index.url, local_module_package_index],
        # make sure main module gets installed, pulling in newest version of dependency module
        python_requires=[
            Requirement.parse(
                module.ModuleV2Source.get_package_name_for(main_module))
        ],
    )

    # populate project.modules[dependency_module] to force the error conditions in this simplified example
    project.get_module(dependency_module, allow_v1=True)

    os.chdir(project.path)
    with pytest.raises(
            CompilerException,
            match=
        ("Not all modules were loaded correctly as a result of transitive dependencies."
         " A recompile should load them correctly."),
    ):
        ProjectTool().execute("install", [])

    message: str = (
        f"Compiler has loaded module {dependency_module}=={v2_version}.dev0 but {dependency_module}=={v2_version} has"
        " later been installed as a side effect.")

    assert message in (rec.message for rec in caplog.records)
Exemple #11
0
def test_bad_module():
    bad_mod_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                               "data", "modules", "mod2")
    with pytest.raises(module.ModuleMetadataFileNotFound):
        module.ModuleV1(project=mock.Mock(), path=bad_mod_dir)
Exemple #12
0
def test_module():
    good_mod_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                "data", "modules", "mod1")
    module.ModuleV1(project=mock.Mock(), path=good_mod_dir)