Skip to content

Commit

Permalink
Change create docker volume task return to volume name (#70)
Browse files Browse the repository at this point in the history
* Change create docker volume return to volume name

* Rename volume name in docker tasks
  • Loading branch information
jessicasyu authored Sep 25, 2024
1 parent ec8afd4 commit a595fea
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/container_collection/docker/create_docker_volume.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from docker import APIClient


def create_docker_volume(api_client: APIClient, path: str) -> dict:
def create_docker_volume(api_client: APIClient, path: str) -> str:
"""
Create a docker volume that copies content to specified path.
Expand All @@ -15,9 +15,9 @@ def create_docker_volume(api_client: APIClient, path: str) -> dict:
Returns
-------
:
Created volume reference object.
Created volume name.
"""

return api_client.create_volume(
driver="local", driver_opts={"type": "none", "device": path, "o": "bind"}
)
)["Name"]
6 changes: 3 additions & 3 deletions src/container_collection/docker/remove_docker_volume.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from docker import APIClient


def remove_docker_volume(api_client: APIClient, volume_name: str) -> None:
def remove_docker_volume(api_client: APIClient, volume: str) -> None:
"""
Remove docker volume.
Parameters
----------
api_client
Docker API client.
volume_name
volume
Name of the docker volume.
"""

api_client.remove_volume(volume_name)
api_client.remove_volume(volume)
6 changes: 3 additions & 3 deletions src/container_collection/docker/run_docker_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def run_docker_command(
client: DockerClient,
image: str,
command: list[str],
volume_name: str | None = None,
volume: str | None = None,
environment: list | None = None,
*,
detach: bool,
Expand All @@ -26,7 +26,7 @@ def run_docker_command(
Docker image.
command
Command list passed to container.
volume_name
volume
Name of the docker volume.
environment
List of environment variables as strings.
Expand All @@ -36,7 +36,7 @@ def run_docker_command(
"""

environment = [] if environment is None else environment
volumes = {} if volume_name is None else {volume_name: {"bind": "/mnt", "mode": "rw"}}
volumes = {} if volume is None else {volume: {"bind": "/mnt", "mode": "rw"}}

client.containers.run(
image,
Expand Down
6 changes: 3 additions & 3 deletions src/container_collection/docker/submit_docker_job.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from docker import APIClient


def submit_docker_job(api_client: APIClient, job_definition: dict, volume_name: str) -> str:
def submit_docker_job(api_client: APIClient, job_definition: dict, volume: str) -> str:
"""
Submit Docker job.
Expand All @@ -11,7 +11,7 @@ def submit_docker_job(api_client: APIClient, job_definition: dict, volume_name:
Docker API client.
job_definition
Docker job definition used to create job container.
volume_name
volume
Name of the docker volume.
Returns
Expand All @@ -20,7 +20,7 @@ def submit_docker_job(api_client: APIClient, job_definition: dict, volume_name:
Container ID.
"""

host_config = api_client.create_host_config(binds={volume_name: {"bind": "/mnt", "mode": "rw"}})
host_config = api_client.create_host_config(binds={volume: {"bind": "/mnt", "mode": "rw"}})

container = api_client.create_container(**job_definition, host_config=host_config)
container_id = container.get("Id")
Expand Down
25 changes: 6 additions & 19 deletions tests/container_collection/docker/test_create_docker_volume.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import datetime
import secrets
import unittest
from unittest import mock

Expand All @@ -8,30 +6,19 @@
from container_collection.docker.create_docker_volume import create_docker_volume


def mock_create_volume(**kwargs):
name = kwargs.get("name", secrets.token_hex(32))
return {
"CreatedAt": datetime.datetime.now().astimezone().replace(microsecond=0).isoformat(),
"Driver": kwargs.get("driver", "local"),
"Labels": kwargs.get("labels", {"com.docker.volume.anonymous": ""}),
"Mountpoint": f"/var/lib/docker/volumes/{name}/_data",
"Name": name,
"Options": kwargs.get("driver_opts", None),
"Scope": "local",
}


class TestCreateDockerVolume(unittest.TestCase):
def test_create_docker_volume(self):
expected_volume = "volume-name"
client = mock.MagicMock(spec=APIClient)
client.create_volume.side_effect = mock_create_volume
client.create_volume.return_value = {"Name": expected_volume}
path = "/docker/volume/path"

volume = create_docker_volume(client, path)

self.assertEqual(path, volume["Options"]["device"])
self.assertEqual("none", volume["Options"]["type"])
self.assertEqual("bind", volume["Options"]["o"])
client.create_volume.assert_called_with(
driver="local", driver_opts={"type": "none", "device": path, "o": "bind"}
)
self.assertEqual(expected_volume, volume)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import secrets
import unittest
from unittest import mock

Expand All @@ -10,11 +9,11 @@
class TestRemoveDockerVolume(unittest.TestCase):
def test_remove_docker_volume(self):
client = mock.MagicMock(spec=APIClient)
volume_name = secrets.token_hex(32)
volume = "volume-name"

remove_docker_volume(client, volume_name)
remove_docker_volume(client, volume)

client.remove_volume.assert_called_with(volume_name)
client.remove_volume.assert_called_with(volume)


if __name__ == "__main__":
Expand Down
7 changes: 3 additions & 4 deletions tests/container_collection/docker/test_run_docker_command.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import secrets
import unittest
from unittest import mock

Expand Down Expand Up @@ -27,18 +26,18 @@ def test_run_docker_command_with_optional_parameters(self):
"ENVIRONMENT_VARIABLE_A=X",
"ENVIRONMENT_VARIABLE_B=Y",
]
volume_name = secrets.token_hex(32)
volume = "volume-name"
detach = True

run_docker_command(
client, image, command, environment=environment, volume_name=volume_name, detach=detach
client, image, command, environment=environment, volume=volume, detach=detach
)

client.containers.run.assert_called_with(
image,
command,
environment=environment,
volumes={volume_name: {"bind": "/mnt", "mode": "rw"}},
volumes={volume: {"bind": "/mnt", "mode": "rw"}},
auto_remove=True,
detach=detach,
)
Expand Down
9 changes: 4 additions & 5 deletions tests/container_collection/docker/test_submit_docker_job.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import secrets
import unittest
from unittest import mock

Expand All @@ -24,16 +23,16 @@ def mock_create_host_config(**kwargs):

class TestSubmitDockerJob(unittest.TestCase):
def test_submit_docker_job(self):
container_id = secrets.token_hex(32)
container_id = "container-id"
client = mock.MagicMock(spec=APIClient)
client.create_host_config.side_effect = mock_create_host_config
client.create_container.return_value = {"Id": container_id, "Warnings": []}

name = "job-definition-name"
image = "jobimage:latest"
volume_name = secrets.token_hex(32)
volume = "volume-name"

host_config = {"NetworkMode": "default", "Binds": [f"{volume_name}:/mnt:rw"]}
host_config = {"NetworkMode": "default", "Binds": [f"{volume}:/mnt:rw"]}

job_definition = {
"image": image,
Expand All @@ -45,7 +44,7 @@ def test_submit_docker_job(self):
],
}

submit_docker_job(client, job_definition, volume_name)
submit_docker_job(client, job_definition, volume)

client.create_container.assert_called_with(**job_definition, host_config=host_config)
client.start.assert_called_with(container=container_id)
Expand Down

0 comments on commit a595fea

Please sign in to comment.