def git_update(ignore_submodule: List[str]) -> None: for name, gitobj in Application.gits.items(): if name in ignore_submodule: log.debug("Skipping update on {}", name) continue if gitobj and not git.can_be_updated(name, gitobj): print_and_exit("Can't continue with updates") controller_is_updated = False for name, gitobj in Application.gits.items(): if name in ignore_submodule: continue if name == "do": controller_is_updated = True if gitobj: git.update(name, gitobj) if controller_is_updated: installation_path = Packages.get_installation_path("rapydo") # Can't be tested on GA since rapydo is alway installed from a folder if not installation_path: # pragma: no cover log.warning("Controller is not installed in editable mode, " "rapydo is unable to update it") elif Application.gits["do"].working_dir: do_dir = Path(Application.gits["do"].working_dir) if do_dir.is_symlink(): do_dir = do_dir.resolve() # This can be used starting from py39 # do_dir = do_dir.readlink() if do_dir == installation_path: log.info("Controller installed from {} and updated", installation_path) else: log.warning( "Controller not updated because it is installed outside this " "project. Installation path is {}, the current folder is {}", installation_path, do_dir, ) else: # pragma: no cover log.warning("Controller submodule folder can't be found")
def test( test: str = typer.Argument(None, help="Name of the test to be executed"), swarm_mode: bool = typer.Option( False, "--swarm", help="Execute the test in swarm mode", show_default=False, ), no_remove: bool = typer.Option( False, "--no-rm", help="Do not remove the container", show_default=False, ), # I have no need to test a command to locally execute tests # and I would like to preventany recursive test execution! ) -> None: # pragma: no cover Application.print_command( Application.serialize_parameter("--swarm", swarm_mode, IF=swarm_mode), Application.serialize_parameter("--no-rm", no_remove, IF=no_remove), Application.serialize_parameter("", test), ) controller_path = Packages.get_installation_path("rapydo") # Can't really happen... if not controller_path: # pragma: no cover print_and_exit("Controller path not found") if not test: log.info("Choose a test to be executed:") for f in sorted(controller_path.joinpath("tests").glob("test_*.py")): test_name = f.with_suffix("").name.replace("test_", "") print(f" - {test_name}") return None test_file = Path("tests", f"test_{test}.py") if not controller_path.joinpath(test_file).exists(): print_and_exit("Invalid test name {}", test) image_name = f"rapydo/controller:{__version__}" container_name = "controller" docker.image.pull(image_name) if docker.container.exists(container_name): docker.container.remove(container_name, force=True, volumes=True) docker.container.run( image_name, detach=True, privileged=True, remove=True, volumes=[(controller_path, "/code")], name=container_name, envs={ "TESTING": "1", "SWARM_MODE": "1" if swarm_mode else "0", }, ) docker.container.execute( container_name, command="syslogd", interactive=False, tty=False, stream=False, detach=True, ) # Wait few seconds to let the docker daemon to start log.info("Waiting for docker daemon to start...") time.sleep(3) command = ["py.test", "-s", "-x", f"/code/{test_file}"] log.info("Executing command: {}", " ".join(command)) try: docker.container.execute( container_name, command=command, workdir="/tmp", interactive=True, tty=True, stream=False, detach=False, ) except DockerException as e: log.error(e) # Do not remove the container to let for some debugging if not no_remove: docker.container.remove(container_name, force=True, volumes=True) log.info("Test container ({}) removed", container_name)
def test_packages(faker: Faker) -> None: assert Packages.get_bin_version("invalid") is None v = Packages.get_bin_version("git") assert v is not None # Something like 2.25.1 assert len(str(Version(v)).split(".")) == 3 # Check docker client version v = Packages.get_bin_version("docker") assert v is not None # Something like 19.03.8 or 18.06.0-ce assert len(str(Version(v)).split(".")) >= 3 # Check docker engine version v = Packages.get_bin_version( "docker", option=["version", "--format", "'{{.Server.Version}}'"] ) assert v is not None assert len(str(Version(v)).split(".")) >= 3 with pytest.raises(SystemExit): Packages.check_program("invalid") v = Packages.check_program("docker") assert v is not None with pytest.raises(SystemExit): Packages.check_program("docker", min_version="99999.99") with pytest.raises(SystemExit): Packages.check_program("docker", max_version="0.0") v = Packages.check_program("docker", min_version="0.0") assert v is not None v = Packages.check_program("docker", max_version="99999.99") assert v is not None v = Packages.check_program("docker", min_version="0.0", max_version="99999.99") assert v is not None v = Packages.check_program( "docker", min_version="0.0", max_version="99999.99", min_recommended_version="99999.99", ) assert v is not None assert Packages.get_installation_path("invalid") is None assert Packages.get_installation_path("rapydo") is not None assert Packages.get_installation_path("pip") is None assert Packages.convert_bin_to_win32("test") == "test" assert Packages.convert_bin_to_win32("compose") == "compose" assert Packages.convert_bin_to_win32("buildx") == "buildx" assert Packages.convert_bin_to_win32("git") == "git" rand_str = faker.pystr() assert Packages.convert_bin_to_win32(rand_str) == rand_str assert Packages.convert_bin_to_win32("docker") == "docker.exe"