How to use copy_into_container method in localstack

Best Python code snippet using localstack_python

docker.py

Source:docker.py Github

copy

Full Screen

...25 return client26 except Exception as e:27 logging.debug(e)28 raise DockerFailureException()29def copy_into_container(30 paths: Mapping[Path, str], container: "Container", destination_path: PurePath31) -> None:32 """Copy local ``paths`` to ``destination_path`` within ``container``.33 The ``container`` is assumed to be running.34 If ``destination_path`` does not exist, it will be created.35 """36 tar_buffer = BytesIO()37 with tarfile.open(mode="w", fileobj=tar_buffer) as archive:38 for p, loc in paths.items():39 archive.add(str(p), arcname=loc)40 tar_buffer.seek(0)41 tar_bytes = tar_buffer.read()42 logging.info(f"sending {len(tar_bytes)} bytes to {container}")43 container.put_archive(str(destination_path), tar_bytes)44class DockerTool(Generic[R], Tool[R]):45 UUID = str(uuid.uuid4())[:8]46 @property47 @abstractmethod48 def docker_image(self) -> str:49 """50 Returns the name of the docker image51 """52 pass53 @property54 @abstractmethod55 def docker_command(self) -> List[str]:56 """57 Returns the command to run in the docker image58 """59 pass60 @property61 def use_remote_docker(self) -> bool:62 """Return whether the Docker daemon is remote.63 We need to know because volume mounting doesn't work with remote daemons.64 """65 return (66 os.getenv("BENTO_REMOTE_DOCKER", "0") == "1"67 or os.getenv("R2C_USE_REMOTE_DOCKER", "0") == "1"68 )69 @property70 def additional_file_targets(self) -> Mapping[Path, str]:71 """72 Additional files, beyond the files to check, that should be copied to the Docker container73 For example, this might include configuration files.74 Format is mapping of local path objects to remote path strings.75 """76 return {}77 @property78 @abstractmethod79 def remote_code_path(self) -> str:80 """81 Where to locate remote code within the docker container82 """83 pass84 @property85 def container_name(self) -> str:86 """87 The name of the docker container88 This is used to separate the docker instances used by each instance of Bento and89 each instance of this class within Bento.90 """91 # UUID separates instances of Bento92 # Tool ID separates instances of the tool93 return f"bento-daemon-{self.UUID}-{self.tool_id()}"94 @property95 def local_volume_mapping(self) -> Mapping[str, Mapping[str, str]]:96 """The volumes to bind when Docker is running locally"""97 return {str(self.base_path): {"bind": self.remote_code_path, "mode": "ro"}}98 def is_allowed_returncode(self, returncode: int) -> bool:99 """Returns true iff the Docker container's return code indicates no error"""100 return returncode == 0101 def assemble_full_command(self, targets: Iterable[str]) -> List[str]:102 """Creates the full command to send to the docker container from file targets"""103 return self.docker_command + list(targets)104 def _prepull_image(self) -> None:105 """106 Pulls the docker image from Docker hub107 """108 client = get_docker_client()109 if not any(i for i in client.images.list() if self.docker_image in i.tags):110 client.images.pull(self.docker_image)111 logging.info(f"Pre-pulled {self.tool_id()} image")112 def _create_container(self, targets: Iterable[str]) -> "Container":113 """114 Creates and returns the Docker container115 """116 client = get_docker_client()117 our_containers = client.containers.list(118 filters={"name": self.container_name, "status": "running"}119 )120 if our_containers:121 logging.info(f"using existing {self.tool_id()} container")122 return our_containers[0]123 vols = {} if self.use_remote_docker else self.local_volume_mapping124 full_command = self.assemble_full_command(targets)125 logging.info(f"starting new {self.tool_id()} container")126 container: "Container" = client.containers.create(127 self.docker_image,128 command=full_command,129 auto_remove=True,130 name=self.container_name,131 volumes=vols,132 detach=True,133 working_dir=self.remote_code_path,134 )135 logging.info(f"started container: {container!r}")136 return container137 def _setup_remote_docker(138 self, container: "Container", expanded: Mapping[Path, str]139 ) -> None:140 """Copies files into the remote Docker container"""141 copy_into_container(expanded, container, PurePath(self.remote_code_path))142 copy_into_container(143 self.additional_file_targets, container, PurePath(self.remote_code_path)144 )145 def run_container(self, files: Iterable[str]) -> subprocess.CompletedProcess:146 """147 Run the Docker command148 """149 targets: Iterable[Path] = [Path(p) for p in files]150 command = self.docker_command151 is_remote = self.use_remote_docker152 expanded = {t: str(t.relative_to(self.base_path)) for t in targets}153 container = self._create_container(expanded.values())154 if is_remote:155 self._setup_remote_docker(container, expanded)156 # Python docker does not allow -a, so use subprocess.run...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful