|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | +from typing import Any, Dict, List, Optional, Tuple |
| 14 | + |
| 15 | +from testcontainers.core.container import DockerContainer |
| 16 | + |
| 17 | + |
| 18 | +class CustomContainer(DockerContainer): |
| 19 | + """ |
| 20 | + Custom container exposing settings through the class. |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + image: |
| 25 | + The name of the image to start. |
| 26 | + command: |
| 27 | + Optional execution command for the container. |
| 28 | + name: |
| 29 | + Optional name for the container. |
| 30 | + ports: |
| 31 | + Ports to be exposed by the container. The port number will be |
| 32 | + automatically assigned on the host, use :code:`get_exposed_port(PORT)` |
| 33 | + method to get the port number on the host. |
| 34 | + volumes: |
| 35 | + Volumes to mount into the container. Each entry should be a tuple with |
| 36 | + three values: |
| 37 | + #. host path |
| 38 | + #. container path |
| 39 | + #. mode (default 'ro'). |
| 40 | +
|
| 41 | + Example |
| 42 | + ------- |
| 43 | + The example will spin up a container with port 8000 exposed |
| 44 | + :: |
| 45 | +
|
| 46 | + with CustomContainer( |
| 47 | + image="python3", env={"ENV_VAR": "ENV_VALUE"}, ports=[8000] |
| 48 | + ) as container: |
| 49 | + # Do something for example requesting an api endpoint |
| 50 | + # r = requests.get( |
| 51 | + # f"{container.get_container_host_ip()}:{container.get_exposed_port(8000)}" |
| 52 | + # ) |
| 53 | +
|
| 54 | + """ |
| 55 | + |
| 56 | + def __init__( |
| 57 | + self, |
| 58 | + image: str, |
| 59 | + command: Optional[str] = None, |
| 60 | + env: Optional[Dict[str, str]] = None, |
| 61 | + name: Optional[str] = None, |
| 62 | + ports: Optional[List[int]] = None, |
| 63 | + volumes: Optional[List[Tuple[str, str, str]]] = None, |
| 64 | + **kwargs: Any, |
| 65 | + ) -> None: |
| 66 | + super().__init__(image, **kwargs) |
| 67 | + |
| 68 | + if command: |
| 69 | + self.with_command(command) |
| 70 | + if env: |
| 71 | + for variable, value in env.items(): |
| 72 | + self.with_env(variable, value) |
| 73 | + if name: |
| 74 | + self.with_name(name) |
| 75 | + if ports: |
| 76 | + self.with_exposed_ports(*ports) |
| 77 | + if volumes: |
| 78 | + for vol in volumes: |
| 79 | + self.with_volume_mapping(*vol) |
0 commit comments