-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extend docker compose integration to support native execution
- Loading branch information
Showing
7 changed files
with
191 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import logging | ||
from ipaddress import IPv4Address | ||
from pathlib import Path | ||
from unittest.mock import MagicMock, patch | ||
|
||
from docker import DockerComposeWrapper, DockerInDockerComposeRenderer, LocalComposeRenderer | ||
|
||
OPENVPN_COMPOSE_TEMPLATE: str = (Path(__file__).parent.parent / "openvpn" / "compose.yaml").read_text() | ||
|
||
|
||
@patch("docker.in_docker_container") | ||
def test_docker_compose_env_ok(in_docker_container: MagicMock) -> None: | ||
in_docker_container.return_value = True | ||
docker_compose_wrapper = DockerComposeWrapper(OPENVPN_COMPOSE_TEMPLATE, {}) | ||
logging.info(f"Current Docker Compose Env Dir: {docker_compose_wrapper.cwd}") | ||
|
||
|
||
@patch("docker.primary_host_ip") | ||
def test_docker_compose_renderer_local(primary_host_ip: MagicMock) -> None: | ||
primary_host_ip.return_value = IPv4Address("1.2.3.4") | ||
|
||
renderer = LocalComposeRenderer(OPENVPN_COMPOSE_TEMPLATE) | ||
assert not renderer.port_mappings["tcp"] | ||
assert len(renderer.port_mappings["udp"]) == 1 # openvpn port | ||
assert renderer.map_service("openvpn-server") == "1.2.3.4" | ||
|
||
|
||
@patch("docker.resolve") | ||
def test_docker_compose_renderer_dind(resolve: MagicMock) -> None: | ||
resolve.return_value = "mapped-service-name" | ||
|
||
renderer = DockerInDockerComposeRenderer(OPENVPN_COMPOSE_TEMPLATE) | ||
assert renderer.port_mappings["udp"]["openvpn"] == 1194 | ||
assert renderer.map_service("openvpn-server") == "mapped-service-name" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import logging | ||
import random | ||
import string | ||
import tempfile | ||
from pathlib import Path | ||
|
||
|
||
def create_temp_dir() -> Path: | ||
while True: | ||
# we don't need a super-secure cryptographic PRNG | ||
random_name = "".join(random.choices(string.ascii_letters + string.digits, k=12)) # noqa: S311 | ||
temp_path = Path(tempfile.gettempdir()) / random_name | ||
logging.info(f"Creating temporary directory {temp_path}.") | ||
try: | ||
temp_path.mkdir(exist_ok=False) | ||
return temp_path | ||
except FileExistsError: | ||
continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters