|
1 |
| -from unittest import TestCase |
| 1 | +import pytest |
2 | 2 |
|
3 | 3 | from orderly_web.docker_helpers import *
|
4 | 4 |
|
5 | 5 |
|
6 |
| -class TestDockerHelpers(TestCase): |
| 6 | +def test_exec_safely_throws_on_failure(): |
| 7 | + with docker_client() as cl: |
| 8 | + container = cl.containers.run("alpine", ["sleep", "10"], |
| 9 | + detach=True, auto_remove=True) |
| 10 | + with pytest.raises(Exception): |
| 11 | + exec_safely(container, "missing_command") |
| 12 | + container.kill() |
7 | 13 |
|
8 |
| - def test_exec_safely_throws_on_failure(self): |
9 |
| - with docker_client() as cl: |
10 |
| - container = cl.containers.run("alpine", ["sleep", "10"], |
11 |
| - detach=True, auto_remove=True) |
12 |
| - with self.assertRaises(Exception): |
13 |
| - exec_safely(container, "missing_command") |
14 |
| - container.kill() |
| 14 | +def test_stop_and_remove_container_works(): |
| 15 | + with docker_client() as cl: |
| 16 | + container = cl.containers.run("alpine", ["sleep", "10"], |
| 17 | + detach=True) |
| 18 | + name = container.name |
| 19 | + assert container_exists(cl, name) |
| 20 | + stop_and_remove_container(cl, name, False, 1) |
| 21 | + assert not container_exists(cl, name) |
15 | 22 |
|
16 |
| - def test_stop_and_remove_container_works(self): |
17 |
| - with docker_client() as cl: |
18 |
| - container = cl.containers.run("alpine", ["sleep", "10"], |
| 23 | +def test_string_into_container(): |
| 24 | + with docker_client() as cl: |
| 25 | + container = cl.containers.run("alpine", ["sleep", "20"], |
| 26 | + detach=True, auto_remove=True) |
| 27 | + text = "a\nb\nc\n" |
| 28 | + string_into_container(container, text, "/test") |
| 29 | + out = container.exec_run(["cat", "/test"]) |
| 30 | + assert out[0] == 0 |
| 31 | + assert out[1].decode("UTF-8") == text |
| 32 | + container.kill() |
| 33 | + |
| 34 | + |
| 35 | +def test_container_wait_running_detects_start_failure(): |
| 36 | + with docker_client() as cl: |
| 37 | + container = cl.containers.create("alpine") |
| 38 | + with pytest.raises(Exception) as e: |
| 39 | + container_wait_running(container, 0.1, 0.1) |
| 40 | + assert "is not running (created)" in str(e) |
| 41 | + |
| 42 | + |
| 43 | +def test_container_wait_running_detects_slow_failure(): |
| 44 | + with docker_client() as cl: |
| 45 | + with pytest.raises(Exception) as e: |
| 46 | + container = cl.containers.run("alpine", ["sleep", "1"], |
19 | 47 | detach=True)
|
20 |
| - name = container.name |
21 |
| - self.assertTrue(container_exists(cl, name)) |
22 |
| - stop_and_remove_container(cl, name, False, 1) |
23 |
| - self.assertFalse(container_exists(cl, name)) |
24 |
| - |
25 |
| - def test_string_into_container(self): |
26 |
| - with docker_client() as cl: |
27 |
| - container = cl.containers.run("alpine", ["sleep", "20"], |
28 |
| - detach=True, auto_remove=True) |
29 |
| - text = "a\nb\nc\n" |
30 |
| - string_into_container(container, text, "/test") |
31 |
| - out = container.exec_run(["cat", "/test"]) |
32 |
| - self.assertEqual(out[0], 0) |
33 |
| - self.assertEqual(out[1].decode("UTF-8"), text) |
34 |
| - container.kill() |
| 48 | + container_wait_running(container, 0.1, 1.2) |
| 49 | + assert "is no longer running (exited)" in str(e) |
0 commit comments