|
2 | 2 | import time
|
3 | 3 | from time import sleep
|
4 | 4 |
|
5 |
| -import docker |
| 5 | +import mock |
6 | 6 | import pytest
|
7 | 7 |
|
8 |
| -from node_cli.utils.docker_utils import save_container_logs, safe_rm |
| 8 | +from node_cli.utils.docker_utils import ( |
| 9 | + docker_cleanup, |
| 10 | + save_container_logs, |
| 11 | + safe_rm |
| 12 | +) |
9 | 13 | from node_cli.configs import REMOVED_CONTAINERS_FOLDER_PATH
|
10 | 14 |
|
11 | 15 |
|
12 |
| -client = docker.from_env() |
13 |
| - |
14 |
| - |
15 | 16 | @pytest.fixture
|
16 |
| -def simple_container(simple_image, docker_hc): |
| 17 | +def simple_container(dclient, simple_image, docker_hc): |
17 | 18 | name = 'simple-container'
|
18 | 19 | c = None
|
19 | 20 | try:
|
20 |
| - info = client.api.create_container( |
| 21 | + info = dclient.api.create_container( |
21 | 22 | simple_image,
|
22 | 23 | detach=True,
|
23 | 24 | name=name,
|
24 | 25 | host_config=docker_hc
|
25 | 26 | )
|
26 |
| - c = client.containers.get(info['Id']) |
| 27 | + c = dclient.containers.get(info['Id']) |
27 | 28 | c.restart()
|
28 | 29 | yield c
|
29 | 30 | finally:
|
@@ -83,3 +84,21 @@ def test_safe_rm(simple_container, removed_containers_folder):
|
83 | 84 | with open(log_path) as log_file:
|
84 | 85 | log_lines = log_file.readlines()
|
85 | 86 | assert log_lines[-1] == 'signal_handler completed, exiting...\n'
|
| 87 | + |
| 88 | + |
| 89 | +def test_docker_cleanup(dclient, simple_container): |
| 90 | + c = simple_container |
| 91 | + image = c.image |
| 92 | + docker_cleanup(dclient=dclient) |
| 93 | + assert image in dclient.images.list() |
| 94 | + |
| 95 | + c.stop() |
| 96 | + docker_cleanup(dclient=dclient) |
| 97 | + assert image in dclient.images.list() |
| 98 | + |
| 99 | + c.remove() |
| 100 | + docker_cleanup(dclient=dclient) |
| 101 | + assert image not in dclient.images.list() |
| 102 | + |
| 103 | + with mock.patch('node_cli.utils.docker_utils.run_cmd', side_effect=ValueError): |
| 104 | + docker_cleanup(dclient=dclient) |
0 commit comments