Ejemplo n.º 1
0
def test_requirements_from_source_info(tmpdir):
    """Test the code path used by the exporter"""
    common.makeproject(tmpdir,
                       "test-project",
                       deps=[("mod1", "")],
                       imports=["mod1"])
    project_dir = os.path.join(tmpdir, "test-project")
    libs_dir = os.path.join(project_dir, "libs")
    common.makemodule(libs_dir, "mod1", project=False)
    mod1 = os.path.join(libs_dir, "mod1")
    mod1_req_txt = """# I'm a comment
pytest\
>=\
1.5


iplib>=0.0.1

        """
    common.add_file(mod1,
                    "requirements.txt",
                    mod1_req_txt,
                    msg="initial commit")
    project = Project(project_dir)
    Project.set(project)
    project.load_module("mod1")
    requirements = SourceInfo(mod1, "inmanta_plugins.mod1").requires
    assert sorted(requirements) == sorted(["pytest>=1.5", "iplib>=0.0.1"])
    project.virtualenv.use_virtual_env()
    # This would fail if the comments weren't filtered out
    project.virtualenv.install_from_list(requirements)
Ejemplo n.º 2
0
 def load_project(venv_path: Optional[str]) -> None:
     if venv_path is None:
         project: Project = Project(project_dir)
     else:
         project: Project = Project(project_dir, venv_path=venv_path)
     Project.set(project)
     project.load()
Ejemplo n.º 3
0
 def setUp(self):
     project = Project(self.project_dir, autostd=False)
     if self.mainfile is not None:
         project.main_file = self.mainfile
     Project.set(project)
     self.state_dir = tempfile.mkdtemp()
     config.Config.set("config", "state-dir", self.state_dir)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
def test_plugin_loading_on_project_load(tmpdir, capsys):
    """
    Load all plugins via the Project.load() method call and verify that no
    module is loaded twice when an import statement is used.
    """
    main_cf = tmpdir.join("main.cf")
    main_cf.write("import submodule")

    project_yml = tmpdir.join("project.yml")
    project_yml.write(
        """
name: test
modulepath: libs
downloadpath: libs
repo: https://github.com/inmanta/inmanta.git
install_mode: master
    """
    )

    tmpdir.mkdir("libs")
    origin_mod_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "modules", "submodule")
    mod_dir = tmpdir.join("libs", os.path.basename(origin_mod_dir))
    shutil.copytree(origin_mod_dir, mod_dir)

    project = Project(tmpdir, autostd=False, venv_path=os.path.join(tmpdir, ".env"))
    Project.set(project)
    project.load()

    (stdout, stderr) = capsys.readouterr()
    assert stdout.count("#loading inmanta_plugins.submodule#") == 1
    assert stdout.count("#loading inmanta_plugins.submodule.submod#") == 1
    assert stdout.count("#loading inmanta_plugins.submodule.pkg#") == 1
    assert stdout.count("#loading inmanta_plugins.submodule.pkg.submod2#") == 1

    from inmanta_plugins.submodule import test

    assert test() == "test"
    (stdout, stderr) = capsys.readouterr()
    assert "#loading" not in stdout

    from inmanta_plugins.submodule.submod import test_submod

    assert test_submod() == "test_submod"
    (stdout, stderr) = capsys.readouterr()
    assert "#loading" not in stdout

    from inmanta_plugins.submodule.pkg import test_pkg

    assert test_pkg() == "test_pkg -- test_submod2"
    (stdout, stderr) = capsys.readouterr()
    assert "#loading" not in stdout
Ejemplo n.º 6
0
def test_collect_python_requirements(tmpdir):
    # Create project
    common.makeproject(tmpdir,
                       "test-project",
                       deps=[("mod1", ""), ("mod2", "")],
                       imports=["mod1", "mod2"])
    project_dir = os.path.join(tmpdir, "test-project")
    libs_dir = os.path.join(project_dir, "libs")
    # Create mod1
    common.makemodule(libs_dir, "mod1", project=False)
    mod1 = os.path.join(libs_dir, "mod1")
    mod1_req_txt = """iplib@git+https://github.com/bartv/python3-iplib
pytest\
>=\
1.5


iplib>=0.0.1
    """
    common.add_file(mod1,
                    "requirements.txt",
                    mod1_req_txt,
                    msg="initial commit")
    # Create mod2
    common.makemodule(libs_dir, "mod2", project=False)
    mod2 = os.path.join(libs_dir, "mod2")
    mod2_req_txt = """# A comment
dummy-yummy # A comment
 # Another comment

    """
    common.add_file(mod2,
                    "requirements.txt",
                    mod2_req_txt,
                    msg="initial commit")

    project = Project(project_dir)
    Project.set(project)
    project.load_module("mod1")
    project.load_module("mod2")
    reqs = project.collect_python_requirements()
    expected_reqs = [
        "iplib@git+https://github.com/bartv/python3-iplib", "pytest>=1.5",
        "iplib>=0.0.1", "dummy-yummy"
    ]
    assert sorted(reqs) == sorted(expected_reqs)
Ejemplo n.º 7
0
    def run_snippet(self, snippet):
        project_dir = tempfile.mkdtemp()
        os.symlink(self.env, os.path.join(project_dir, ".env"))

        with open(os.path.join(project_dir, "project.yml"), "w") as cfg:
            cfg.write("""
            name: snippet test
            modulepath: %s
            downloadpath: %s
            version: 1.0
            repo: ['[email protected]:modules/', '[email protected]:config/']"""
                      % (self.libs, self.libs))

        with open(os.path.join(project_dir, "main.cf"), "w") as x:
            x.write(snippet)

        Project.set(Project(project_dir))
        compiler.do_compile()
Ejemplo n.º 8
0
    def setup_for_snippet(self, snippet, autostd=True):
        # init project
        self.project_dir = tempfile.mkdtemp()
        os.symlink(self.__class__.env, os.path.join(self.project_dir, ".env"))

        with open(os.path.join(self.project_dir, "project.yml"), "w") as cfg:
            cfg.write("""
            name: snippet test
            modulepath: [%s, %s]
            downloadpath: %s
            version: 1.0
            repo: ['https://github.com/inmanta/']""" %
                      (self.__class__.libs,
                       os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    "data", "modules"), self.__class__.libs))

        with open(os.path.join(self.project_dir, "main.cf"), "w") as x:
            x.write(snippet)

        Project.set(Project(self.project_dir, autostd=autostd))
Ejemplo n.º 9
0
    def run_project(self, root):
        project_dir = root
        env = os.path.join(project_dir, ".env")
        if os.path.exists(env):
            os.remove(env)
        os.symlink(self.env, env)

        project = os.path.join(project_dir, "project.yml")
        if os.path.exists(project):
            os.remove(project)
        with open(project, "w") as cfg:
            cfg.write("""
            name: snippet test
            modulepath: [libs,%s]
            downloadpath: %s
            version: 1.0
            repo: ['[email protected]:modules/', '[email protected]:config/']"""
                      % (self.libs, self.libs))

        Project.set(Project(project_dir))
        compiler.do_compile()
        os.remove(project)
Ejemplo n.º 10
0
 def reset(self):
     Project.set(Project(self.project_dir, autostd=Project.get().autostd))
     loader.unload_inmanta_plugins()
Ejemplo n.º 11
0
 def setup_for_snippet(self, snippet, autostd=True):
     self.setup_for_snippet_external(snippet)
     Project.set(Project(self.project_dir, autostd=autostd))
     loader.unload_inmanta_plugins()
Ejemplo n.º 12
0
 def load_project(venv_path: str) -> None:
     project: Project = Project(project_dir, venv_path=venv_path)
     Project.set(project)
     # don't load full project, only AST so we don't have to deal with module finder cleanup
     project.load_module_recursive(install=True)