Skip to content

Commit 1a24e53

Browse files
committed
tests/integration: Add integration tests for create_host_path implementation
Signed-off-by: Monika Kairaityte <[email protected]>
1 parent 0adc0b4 commit 1a24e53

File tree

6 files changed

+225
-0
lines changed

6 files changed

+225
-0
lines changed

tests/integration/vol/long_syntax/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: "3"
2+
services:
3+
create_host_path_default_true: # default is to always create
4+
image: docker.io/library/debian
5+
command: "echo test1"
6+
volumes:
7+
- type: bind
8+
source: ./test_dir
9+
target: /data
10+
create_host_path_true:
11+
image: docker.io/library/debian
12+
command: "echo test2"
13+
volumes:
14+
- type: bind
15+
source: ./test_dir
16+
target: /data
17+
bind:
18+
create_host_path: true
19+
create_host_path_false:
20+
image: docker.io/library/debian
21+
command: "echo test3"
22+
volumes:
23+
- type: bind
24+
source: ./test_dir
25+
target: /data
26+
bind:
27+
create_host_path: false
28+
29+
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
import os
4+
import shutil
5+
import unittest
6+
7+
from parameterized import parameterized
8+
9+
from tests.integration.test_utils import RunSubprocessMixin
10+
from tests.integration.test_utils import podman_compose_path
11+
from tests.integration.test_utils import test_path
12+
13+
14+
def compose_yaml_path(test_ref_folder: str) -> str:
15+
return os.path.join(test_path(), "vol", test_ref_folder, "docker-compose.yml")
16+
17+
18+
class TestComposeVolLongSyntax(unittest.TestCase, RunSubprocessMixin):
19+
@parameterized.expand([
20+
(True, "create_host_path_default_true", "test1\n"),
21+
(True, "create_host_path_true", "test2\n"),
22+
(True, "create_host_path_false", "test3\n"),
23+
(False, "create_host_path_default_true", "test1\n"),
24+
(False, "create_host_path_true", "test2\n"),
25+
])
26+
def test_source_host_dir(
27+
self, source_dir_exists: bool, service_name: str, log_output: str
28+
) -> None:
29+
project_dir = os.path.join(test_path(), "vol", "long_syntax")
30+
if source_dir_exists:
31+
# create host source directory for volume
32+
os.mkdir(os.path.join(project_dir, "test_dir"))
33+
34+
initial_dirs = set(os.listdir(project_dir))
35+
try:
36+
output, _, return_code = self.run_subprocess([
37+
podman_compose_path(),
38+
"-f",
39+
compose_yaml_path("long_syntax"),
40+
"up",
41+
"-d",
42+
f"{service_name}",
43+
])
44+
self.assertEqual(return_code, 0)
45+
46+
output, _, return_code = self.run_subprocess([
47+
podman_compose_path(),
48+
"-f",
49+
compose_yaml_path("long_syntax"),
50+
"logs",
51+
f"{service_name}",
52+
])
53+
self.assertEqual(return_code, 0)
54+
self.assertEqual(f"{log_output}", output.decode('utf-8'))
55+
56+
if source_dir_exists:
57+
# new directories were not created as source host directory already exists
58+
final_dirs = set(os.listdir(project_dir))
59+
self.assertEqual(initial_dirs, final_dirs)
60+
else:
61+
# new directory was created as source host directory did not exist
62+
final_dirs = set(os.listdir(project_dir))
63+
new_dir = final_dirs - initial_dirs
64+
self.assertIn("test_dir", new_dir)
65+
finally:
66+
self.run_subprocess_assert_returncode([
67+
podman_compose_path(),
68+
"-f",
69+
compose_yaml_path("long_syntax"),
70+
"down",
71+
"-t",
72+
"0",
73+
])
74+
shutil.rmtree(os.path.join(project_dir, "test_dir"))
75+
76+
def test_no_source_host_dir_create_host_path_false(self) -> None:
77+
project_dir = os.path.join(test_path(), "vol", "long_syntax")
78+
initial_dirs = set(os.listdir(project_dir))
79+
try:
80+
_, error, return_code = self.run_subprocess([
81+
podman_compose_path(),
82+
"-f",
83+
compose_yaml_path("long_syntax"),
84+
"up",
85+
"-d",
86+
"create_host_path_false",
87+
])
88+
self.assertEqual(return_code, 1)
89+
self.assertIn(
90+
b"invalid mount config for type 'bind': bind source path does not exist:", error
91+
)
92+
93+
# new directories were not created
94+
final_dirs = set(os.listdir(project_dir))
95+
self.assertEqual(initial_dirs, final_dirs)
96+
97+
finally:
98+
self.run_subprocess_assert_returncode([
99+
podman_compose_path(),
100+
"-f",
101+
compose_yaml_path("long_syntax"),
102+
"down",
103+
"-t",
104+
"0",
105+
])

tests/integration/vol/short_syntax/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: "3"
2+
services:
3+
test1:
4+
image: docker.io/library/debian
5+
command: "echo test1"
6+
volumes:
7+
- ./test_dir:/cont/data
8+
test2:
9+
image: docker.io/library/debian
10+
command: "echo test2"
11+
volumes:
12+
- ./not_exists:/cont/data
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
import os
4+
import shutil
5+
import unittest
6+
7+
from tests.integration.test_utils import RunSubprocessMixin
8+
from tests.integration.test_utils import podman_compose_path
9+
from tests.integration.test_utils import test_path
10+
11+
12+
def compose_yaml_path(test_ref_folder: str) -> str:
13+
return os.path.join(test_path(), "vol", test_ref_folder, "docker-compose.yml")
14+
15+
16+
class TestComposeVolShortSyntax(unittest.TestCase, RunSubprocessMixin):
17+
def test_source_host_dir(self) -> None:
18+
project_dir = os.path.join(test_path(), "vol", "short_syntax")
19+
initial_dirs = set(os.listdir(project_dir))
20+
try:
21+
output, _, return_code = self.run_subprocess([
22+
podman_compose_path(),
23+
"-f",
24+
compose_yaml_path("short_syntax"),
25+
"up",
26+
"-d",
27+
"test1",
28+
])
29+
self.assertEqual(return_code, 0)
30+
31+
output, _, return_code = self.run_subprocess([
32+
podman_compose_path(),
33+
"-f",
34+
compose_yaml_path("short_syntax"),
35+
"logs",
36+
"test1",
37+
])
38+
self.assertEqual(return_code, 0)
39+
self.assertEqual(output, b'test1\n')
40+
41+
final_dirs = set(os.listdir(project_dir))
42+
# new directories were not created as source host directory already exists
43+
self.assertEqual(initial_dirs, final_dirs)
44+
45+
output, _, return_code = self.run_subprocess([
46+
podman_compose_path(),
47+
"-f",
48+
compose_yaml_path("short_syntax"),
49+
"up",
50+
"-d",
51+
"test2",
52+
])
53+
self.assertEqual(return_code, 0)
54+
55+
output, _, return_code = self.run_subprocess([
56+
podman_compose_path(),
57+
"-f",
58+
compose_yaml_path("short_syntax"),
59+
"logs",
60+
"test2",
61+
])
62+
self.assertEqual(return_code, 0)
63+
self.assertEqual(output, b'test2\n')
64+
65+
# new directory was created as source host directory did not exist
66+
final_dirs = set(os.listdir(project_dir))
67+
new_dir = final_dirs - initial_dirs
68+
self.assertIn("not_exists", new_dir)
69+
finally:
70+
self.run_subprocess_assert_returncode([
71+
podman_compose_path(),
72+
"-f",
73+
compose_yaml_path("short_syntax"),
74+
"down",
75+
"-t",
76+
"0",
77+
])
78+
dir_path = os.path.join(project_dir, "not_exists")
79+
shutil.rmtree(dir_path)

0 commit comments

Comments
 (0)