wip docker module
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import unittest
|
||||
import docker
|
||||
from docker.models.containers import Container
|
||||
import time
|
||||
from sia.docker_module import DockerModule
|
||||
|
||||
class DockerModuleTest(unittest.TestCase):
|
||||
@@ -14,7 +13,7 @@ class DockerModuleTest(unittest.TestCase):
|
||||
docker_module = DockerModule()
|
||||
|
||||
# Test parameters
|
||||
image = "sia:latest"
|
||||
image = "busybox:latest"
|
||||
timeout = 1000
|
||||
command = "echo"
|
||||
arguments = ["Hello World"]
|
||||
@@ -33,8 +32,8 @@ class DockerModuleTest(unittest.TestCase):
|
||||
"""Test starting a long-running container."""
|
||||
docker_module = DockerModule()
|
||||
|
||||
name = "test-container"
|
||||
image = "sia:latest"
|
||||
name = "test_start_container_long_running"
|
||||
image = "busybox:latest"
|
||||
|
||||
container_name = docker_module.start_container(
|
||||
image=image,
|
||||
@@ -44,23 +43,24 @@ class DockerModuleTest(unittest.TestCase):
|
||||
# Verify container was started and stored
|
||||
self.assertEqual(container_name, name)
|
||||
self.assertIn(name, docker_module.containers)
|
||||
self.assertEqual(docker_module.containers[name], self.mock_container)
|
||||
|
||||
def test_start_container_with_volumes(self):
|
||||
"""Test starting a container with volume mappings."""
|
||||
docker_module = DockerModule()
|
||||
|
||||
volumes = {"./pdf": "/pdf"}
|
||||
|
||||
arguments = ["sh", "-c", "echo 'Hello World' > /write_here/test_start_container_with_volumes"]
|
||||
volumes = {"/tmp": "/write_here"}
|
||||
|
||||
output = docker_module.start_container(
|
||||
image="sia:latest",
|
||||
image="busybox:latest",
|
||||
timeout=1000,
|
||||
arguments=arguments,
|
||||
volumes=volumes
|
||||
)
|
||||
|
||||
# Verify volume bindings were configured correctly
|
||||
expected_binds = {
|
||||
"./pdf": {"bind": "/pdf", "mode": "rw"}
|
||||
}
|
||||
print(f"output: {output}", flush=True)
|
||||
# Verify output was written to volume
|
||||
with open("/tmp/test_start_container_with_volumes", "r") as f:
|
||||
self.assertEqual(f.read(), "Hello World\n")
|
||||
|
||||
def test_invalid_container_start(self):
|
||||
"""Test error handling for invalid container start."""
|
||||
@@ -68,16 +68,16 @@ class DockerModuleTest(unittest.TestCase):
|
||||
|
||||
# Test missing required parameters
|
||||
with self.assertRaises(ValueError):
|
||||
docker_module.start_container(image="test:latest")
|
||||
docker_module.start_container(image="busybox:latest")
|
||||
|
||||
def test_stop_container(self):
|
||||
"""Test stopping a container."""
|
||||
docker_module = DockerModule()
|
||||
|
||||
# Start a container first
|
||||
name = "test-container"
|
||||
name = "test_stop_container"
|
||||
docker_module.start_container(
|
||||
image="test:latest",
|
||||
image="busybox:latest",
|
||||
name=name
|
||||
)
|
||||
|
||||
@@ -85,8 +85,6 @@ class DockerModuleTest(unittest.TestCase):
|
||||
docker_module.stop_container(name)
|
||||
|
||||
# Verify container was stopped and removed
|
||||
self.mock_container.stop.assert_called_once()
|
||||
self.mock_container.remove.assert_called_once()
|
||||
self.assertNotIn(name, docker_module.containers)
|
||||
|
||||
def test_stop_nonexistent_container(self):
|
||||
@@ -99,88 +97,83 @@ class DockerModuleTest(unittest.TestCase):
|
||||
def test_container_io_operations(self):
|
||||
"""Test container I/O operations."""
|
||||
docker_module = DockerModule()
|
||||
|
||||
arguments = [
|
||||
"sh",
|
||||
"-c",
|
||||
"read input; echo \"$input\" | tr '[:lower:]' '[:upper:]'; echo \"$input\" | tr '[:upper:]' '[:lower:]' >&2"
|
||||
]
|
||||
|
||||
# Start a container
|
||||
name = "test-container"
|
||||
name = "test_container_io_operations"
|
||||
docker_module.start_container(
|
||||
image="test:latest",
|
||||
name=name
|
||||
image="busybox:latest",
|
||||
name=name,
|
||||
arguments=arguments
|
||||
)
|
||||
|
||||
# Test stdin write
|
||||
test_input = "test input"
|
||||
mock_socket = Mock()
|
||||
mock_socket._sock = Mock()
|
||||
self.mock_container.attach_socket.return_value = mock_socket
|
||||
|
||||
test_input = "Test Input\n"
|
||||
docker_module.write_container_stdin(name, test_input)
|
||||
mock_socket._sock.send.assert_called_once_with(test_input.encode('utf-8'))
|
||||
time.sleep(1) # for processing
|
||||
|
||||
# Test stdout read
|
||||
stdout = docker_module.read_container_stdout(name)
|
||||
self.mock_container.logs.assert_called_with(
|
||||
stdout=True,
|
||||
stderr=False,
|
||||
tail='all'
|
||||
)
|
||||
self.assertEqual(stdout, "Test output")
|
||||
print(f"stdout: {stdout}", flush=True)
|
||||
self.assertEqual(stdout, "test input\n")
|
||||
|
||||
# Test stderr read
|
||||
stderr = docker_module.read_container_stderr(name)
|
||||
self.mock_container.logs.assert_called_with(
|
||||
stdout=False,
|
||||
stderr=True,
|
||||
tail='all'
|
||||
)
|
||||
self.assertEqual(stderr, "Test output")
|
||||
print(f"stderr: {stderr}", flush=True)
|
||||
self.assertEqual(stderr, "TEST INPUT\n")
|
||||
|
||||
def test_wait_container(self):
|
||||
"""Test waiting for container completion."""
|
||||
docker_module = DockerModule()
|
||||
|
||||
# Start a container
|
||||
name = "test-container"
|
||||
docker_module.start_container(
|
||||
image="test:latest",
|
||||
name=name
|
||||
)
|
||||
|
||||
# Test wait operation
|
||||
exit_code, output = docker_module.wait_container(name, timeout=1000)
|
||||
|
||||
# Verify wait was called with correct timeout
|
||||
self.mock_container.wait.assert_called_once_with(timeout=1.0)
|
||||
self.assertEqual(exit_code, 0)
|
||||
self.assertEqual(output, "Test output")
|
||||
|
||||
def test_get_container_status(self):
|
||||
"""Test getting container status information."""
|
||||
docker_module = DockerModule()
|
||||
|
||||
# Start a container
|
||||
name = "test-container"
|
||||
docker_module.start_container(
|
||||
image="test:latest",
|
||||
name=name
|
||||
)
|
||||
|
||||
# Configure mock logs sizes
|
||||
self.mock_container.logs.side_effect = [b"stdout", b"stderr"]
|
||||
|
||||
# Get status
|
||||
status = docker_module.get_container_status(name)
|
||||
|
||||
# Verify status information
|
||||
self.assertEqual(status['name'], name)
|
||||
self.assertEqual(status['status'], "running")
|
||||
self.assertEqual(status['started_at'], "2024-10-23T10:00:00Z")
|
||||
self.assertEqual(status['exit_code'], 0)
|
||||
self.assertEqual(status['error'], "")
|
||||
self.assertEqual(status['stdout_size'], 6) # len(b"stdout")
|
||||
self.assertEqual(status['stderr_size'], 6) # len(b"stderr")
|
||||
|
||||
# Verify container was reloaded
|
||||
self.mock_container.reload.assert_called_once()
|
||||
# def test_wait_container(self):
|
||||
# """Test waiting for container completion."""
|
||||
# docker_module = DockerModule()
|
||||
#
|
||||
# # Start a container
|
||||
# name = "test-container"
|
||||
# docker_module.start_container(
|
||||
# image="test:latest",
|
||||
# name=name
|
||||
# )
|
||||
#
|
||||
# # Test wait operation
|
||||
# exit_code, output = docker_module.wait_container(name, timeout=1000)
|
||||
#
|
||||
# # Verify wait was called with correct timeout
|
||||
# self.mock_container.wait.assert_called_once_with(timeout=1.0)
|
||||
# self.assertEqual(exit_code, 0)
|
||||
# self.assertEqual(output, "Test output")
|
||||
#
|
||||
# def test_get_container_status(self):
|
||||
# """Test getting container status information."""
|
||||
# docker_module = DockerModule()
|
||||
#
|
||||
# # Start a container
|
||||
# name = "test-container"
|
||||
# docker_module.start_container(
|
||||
# image="test:latest",
|
||||
# name=name
|
||||
# )
|
||||
#
|
||||
# # Configure mock logs sizes
|
||||
# self.mock_container.logs.side_effect = [b"stdout", b"stderr"]
|
||||
#
|
||||
# # Get status
|
||||
# status = docker_module.get_container_status(name)
|
||||
#
|
||||
# # Verify status information
|
||||
# self.assertEqual(status['name'], name)
|
||||
# self.assertEqual(status['status'], "running")
|
||||
# self.assertEqual(status['started_at'], "2024-10-23T10:00:00Z")
|
||||
# self.assertEqual(status['exit_code'], 0)
|
||||
# self.assertEqual(status['error'], "")
|
||||
# self.assertEqual(status['stdout_size'], 6) # len(b"stdout")
|
||||
# self.assertEqual(status['stderr_size'], 6) # len(b"stderr")
|
||||
#
|
||||
# # Verify container was reloaded
|
||||
# self.mock_container.reload.assert_called_once()
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user