def test_basic_install(tmpdir): env_dir1 = tmpdir.mkdir("env1").strpath with pytest.raises(ImportError): import lorem # NOQA venv1 = env.VirtualEnv(env_dir1) venv1.use_virtual_env() venv1._install(["lorem"]) import lorem # NOQA lorem.sentence() with pytest.raises(ImportError): import yummy # NOQA venv1 = env.VirtualEnv(env_dir1) venv1.use_virtual_env() venv1.install_from_list(["dummy-yummy"]) import yummy # NOQA with pytest.raises(ImportError): import iplib # NOQA venv1 = env.VirtualEnv(env_dir1) venv1.use_virtual_env() try: venv1.install_from_list( ["lorem == 0.1.1", "dummy-yummy", "iplib@git+https://github.com/bartv/python3-iplib", "lorem", "iplib >=0.0.1"]) except CalledProcessError as ep: print(ep.stdout) raise import iplib # NOQA
def test_install_fails(tmpdir, caplog): venv = env.VirtualEnv(tmpdir) venv.use_virtual_env() caplog.clear() package_name = "non-existing-pkg-inmanta" with pytest.raises(Exception): venv.install_from_list([package_name]) log_sequence = LogSequence(caplog) log_sequence.contains("inmanta.env", logging.ERROR, f"requirements: {package_name}")
def __init__(self, io_loop, hostname=None, agent_map=None, code_loader=True, environment=None, poolsize=1, cricital_pool_size=5): super().__init__("agent", io_loop, timeout=cfg.server_timeout.get(), reconnect_delay=cfg.agent_reconnect_delay.get()) self.poolsize = poolsize self.ratelimiter = locks.Semaphore(poolsize) self.critical_ratelimiter = locks.Semaphore(cricital_pool_size) self._sched = Scheduler(io_loop=self._io_loop) self.thread_pool = ThreadPoolExecutor(poolsize) if agent_map is None: agent_map = cfg.agent_map.get() self.agent_map = agent_map self._storage = self.check_storage() if environment is None: environment = cfg.environment.get() if environment is None: raise Exception("The agent requires an environment to be set.") self.set_environment(environment) self._instances = {} if code_loader: self._env = env.VirtualEnv(self._storage["env"]) self._env.use_virtual_env() self._loader = CodeLoader(self._storage["code"]) else: self._loader = None if hostname is not None: self.add_end_point_name(hostname) else: # load agent names from the config file agent_names = cfg.agent_names.get() if agent_names is not None: names = [x.strip() for x in agent_names.split(",")] for name in names: if "$" in name: name = name.replace("$node-name", self.node_name) self.add_end_point_name(name)
def test_run_pythonpath(io, tmpdir): """Test to see if the python path of the venv is removed for the subprocess See issue #2676 """ venv = env.VirtualEnv(tmpdir) venv.use_virtual_env() result = io.run("env") assert "PYTHONPATH" not in result[0] if not hasattr(io, "run_as") or not io.run_as: # This does not work as expected when using the run_as root feature with bash and sudo because by default # PYTHONPATH is a "bad" variable in sudo https://fossies.org/linux/sudo/plugins/sudoers/env.c#l_190 result = io.run("env", env={"PYTHONPATH": "test"}) assert "PYTHONPATH" in result[0]
def test_req_parser(tmpdir): url = "git+https://github.com/bartv/python3-iplib" at_url = "iplib@" + url egg_url = url + "#egg=iplib" e = env.VirtualEnv(tmpdir) name, u = e._parse_line(url) assert name is None assert u == url name, u = e._parse_line(at_url) assert name == "iplib" assert u == egg_url e._parse_line(egg_url) assert name == "iplib" assert u == egg_url
def test_pip_binary_when_venv_path_contains_double_quote(tmpdir) -> None: """ Test whether the pip binary generated by the VirtualEnv class works correctly when the pip binary contains a double quote. """ venv_dir = os.path.join(tmpdir, 'tes t"test') venv = env.VirtualEnv(venv_dir) venv.use_virtual_env() assert any('"' in path and " " in path for path in sys.path) pip_binary = os.path.join(os.path.dirname(venv.python_path), "pip") # Ensure that the pip command doesn't raise an exception result = subprocess.check_output([pip_binary, "list", "--format", "json"], timeout=10, encoding="utf-8") parsed_output = json.loads(result) # Ensure inheritance works correctly assert "inmanta-core" in [elem["name"] for elem in parsed_output]
def test_install_package_already_installed_in_parent_env(tmpdir): """Test using and installing a package that is already present in the parent virtual environment.""" # get all packages in the parent parent_installed = list(env.process_env.get_installed_packages().keys()) # create a venv and list all packages available in the venv venv = env.VirtualEnv(tmpdir) venv.use_virtual_env() installed_packages = list( env.PythonEnvironment( python_path=venv._parent_python).get_installed_packages().keys()) # verify that the venv sees all parent packages assert not set(parent_installed) - set(installed_packages) # site dir should be empty site_dir = os.path.join(venv.env_path, "lib/python*/site-packages") dirs = glob.glob(site_dir) assert len(dirs) == 1 site_dir = dirs[0] def _list_dir(path: str, ignore: List[str]) -> List[str]: return [d for d in os.listdir(site_dir) if d not in ignore] # site_dir should only contain a sitecustomize.py file that sets up inheritance from the parent venv assert not _list_dir( site_dir, ignore=["inmanta-inherit-from-parent-venv.pth", "__pycache__"]) # test installing a package that is already present in the parent venv random_package = parent_installed[0] venv.install_from_list([random_package]) # site_dir should only contain a sitecustomize.py file that sets up inheritance from the parent venv assert not _list_dir( site_dir, ignore=["inmanta-inherit-from-parent-venv.pth", "__pycache__"]) # report json subprocess.check_output([os.path.join(venv.env_path, "bin/pip"), "list"])
def test_gen_req_file(tmpdir): e = env.VirtualEnv(tmpdir) req = [ "lorem == 0.1.1", "lorem > 0.1", "dummy-yummy", "iplib@git+https://github.com/bartv/python3-iplib", "lorem", # verify support for environment markers as described in PEP 508 "lorem;python_version<'3.7'", "lorem;platform_machine == 'x86_64' and platform_system == 'Linux'", ] req_lines = [ x for x in e._gen_content_requirements_file(req).split("\n") if len(x) > 0 ] assert len(req_lines) == 3 assert ( 'lorem == 0.1.1, > 0.1 ; python_version < "3.7" and platform_machine == "x86_64" and platform_system == "Linux"' in req_lines)
def test_gen_req_file(tmpdir): e = env.VirtualEnv(tmpdir) req = ["lorem == 0.1.1", "lorem > 0.1", "dummy-yummy", "iplib@git+https://github.com/bartv/python3-iplib", "lorem"] req_lines = [x for x in e._gen_requirements_file(req).split("\n") if len(x) > 0] assert(len(req_lines) == 3)