-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_dockerized.py
265 lines (219 loc) · 9.98 KB
/
test_dockerized.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import re
import subprocess
import os
import unittest
from concurrent.futures.thread import ThreadPoolExecutor
from dockerized.test import ProjectAwareTestCase
PRINT_COMMANDS = False
class AbstractEndToEndTest(ProjectAwareTestCase):
def tearDown(self) -> None:
for path in self.project_dirs:
self.run_dockerized('clean', project_dir=path)
super().tearDown()
def run_dockerized(self, cmd_line, working_dir=None, project_dir=None, env=None):
safe_project_dir = project_dir or self.project_dirs[0]
this_file_path = os.path.dirname(os.path.realpath(__file__))
dockerized = f"PYTHONPATH={this_file_path} python -m dockerized.ui.cli"
cwd = safe_project_dir if working_dir is None else f"{safe_project_dir}/{working_dir}"
if PRINT_COMMANDS:
print(f"> {cwd}: {dockerized} {cmd_line}")
process = subprocess.run(f"{dockerized} {cmd_line}", cwd=cwd, shell=True, capture_output=True, text=True, env=env)
if PRINT_COMMANDS:
print(f"EXIT CODE: {process.returncode}")
print(f"STDOUT: {process.stdout}")
print(f"STDERR: {process.stderr}")
return process.returncode, process.stdout, process.stderr
def assert_dockerized(self, command, expected_exit_code=None, fixture_name=None, working_dir=None, project_dir=None,
expected_stderr_regex=None, expected_stdout_regex=None, env=None):
safe_project_dir = project_dir or self.project_dirs[0]
if fixture_name is not None:
if fixture_name == '_init':
self.run_dockerized('init')
else:
self.setup_project_dir(fixture_name, safe_project_dir)
exit_code, stdout, stderr = self.run_dockerized(command, working_dir, env=env)
self.assertRegex(stderr, re.compile(expected_stderr_regex, re.MULTILINE))
self.assertRegex(stdout, re.compile(expected_stdout_regex, re.MULTILINE))
self.assertEqual(expected_exit_code, exit_code)
class EndToEndTest(AbstractEndToEndTest):
def test_init_succeeds(self):
self.assert_dockerized(
command='init',
expected_exit_code=0,
expected_stdout_regex=r'^created',
expected_stderr_regex=r'.*'
)
def test_init_from(self):
self.assert_dockerized(
command='init --from https://github.com/benzaita/dockerized-example-golang.git',
expected_exit_code=0,
expected_stdout_regex=r'^initialized .dockerized/ from https://github.com/benzaita/dockerized-example-golang.git',
expected_stderr_regex=r'^$'
)
def test_init_fails(self):
self.run_dockerized('init')
self.assert_dockerized(
command='init',
expected_exit_code=1,
expected_stdout_regex=r'.*',
expected_stderr_regex=r'^Refusing to overwrite .dockerized'
)
def test_compose_delegates_to_docker_compose(self):
self.assert_dockerized(
fixture_name='_init',
command='compose version',
expected_exit_code=0,
# Output is different on different versions of docker-compose
expected_stdout_regex=r'^[Dd]ocker[- ][Cc]ompose version',
expected_stderr_regex=r'.*'
)
def test_compose_exit_code(self):
self.assert_dockerized(
fixture_name='_init',
command='compose kill foo',
expected_exit_code=1,
expected_stdout_regex=r'.*',
expected_stderr_regex=r'^[Nn]o such service: foo'
)
def test_exec_exit_code(self):
self.assert_dockerized(
fixture_name='_init',
command='exec exit 42',
expected_exit_code=42,
expected_stdout_regex=r'.*',
expected_stderr_regex=r'.*',
)
def test_exec_pipes_stdout(self):
self.assert_dockerized(
fixture_name='_init',
command='exec echo something out',
expected_exit_code=0,
expected_stdout_regex=r'^something out',
expected_stderr_regex=r'.*',
)
def test_exec_pipes_stderr(self):
self.assert_dockerized(
fixture_name='_init',
command='exec echo something err >&2',
expected_exit_code=0,
expected_stdout_regex=r'.*',
expected_stderr_regex=r'something err',
)
def test_exec_takes_env_vars_from_docker_compose_file(self):
self.assert_dockerized(
fixture_name='with_foo_env_var',
command='exec echo FOO=\\$FOO',
expected_exit_code=0,
expected_stdout_regex=r'^FOO=1',
expected_stderr_regex=r'.*',
)
def test_exec_passes_the_command_line_verbatim(self):
self.assert_dockerized(
fixture_name='with_foo_env_var',
command='exec \'env FOO=2 | grep FOO\'',
expected_exit_code=0,
expected_stdout_regex=r'^FOO=2',
expected_stderr_regex=r'.*',
)
def test_exec_binds_project_dir(self):
self.assert_dockerized(
fixture_name='with_files',
command='exec cat dir/file.txt',
expected_exit_code=0,
expected_stdout_regex=r'^Hello world!',
expected_stderr_regex=r'.*',
)
def test_exec_runs_from_sub_dir(self):
self.assert_dockerized(
fixture_name='with_files',
working_dir='dir',
command='exec cat file.txt',
expected_exit_code=0,
expected_stdout_regex=r'^Hello world!',
expected_stderr_regex=r'.*',
)
def test_exec_makes_the_entire_project_dir_available_in_the_container(self):
self.assert_dockerized(
fixture_name='with_files',
working_dir='dir',
command='exec cat ../file_in_project_root.txt',
expected_exit_code=0,
expected_stdout_regex=r'^Hello from project root',
expected_stderr_regex=r'.*',
)
def test_exec_fails_when_not_in_project_sub_dir(self):
self.assert_dockerized(
fixture_name='with_no_project',
command='exec true',
expected_exit_code=1,
expected_stdout_regex=r'.*',
expected_stderr_regex=r'^Not inside a Dockerized project directory. Did you run \'dockerized init\'\?',
)
def test_exec_takes_command_with_args(self):
self.assert_dockerized(
fixture_name='with_files',
command='exec id -u',
expected_exit_code=0,
expected_stdout_regex=r'^0',
expected_stderr_regex=r'.*',
)
def test_exec_in_two_dirs_does_not_conflict(self):
foo_dir = self.add_project_dir()
bar_dir = self.add_project_dir()
self.setup_project_dir('with_foo_in_dockerfile', foo_dir)
self.setup_project_dir('with_bar_in_dockerfile', bar_dir)
self.run_dockerized('exec true', project_dir=foo_dir)
self.run_dockerized('exec true', project_dir=bar_dir)
_, stdout_foo, _ = self.run_dockerized('exec \'echo $TEST_VAR\'', project_dir=foo_dir)
_, stdout_bar, _ = self.run_dockerized('exec \'echo $TEST_VAR\'', project_dir=bar_dir)
self.assertRegex(stdout_foo, re.compile(r'^foo', re.MULTILINE))
self.assertRegex(stdout_bar, re.compile(r'^bar', re.MULTILINE))
def test_exec_parallel_invocations_prepare_only_once(self):
project_dir = self.add_project_dir()
self.setup_project_dir('with_foo_in_dockerfile', project_dir=project_dir)
num_of_parallel_invocations = 10
futures = [None] * num_of_parallel_invocations
print(f"Running {num_of_parallel_invocations} exec commands in parallel")
with ThreadPoolExecutor(max_workers=num_of_parallel_invocations) as executor:
for i in range(num_of_parallel_invocations):
futures[i] = executor.submit(lambda: self.run_dockerized('exec true', project_dir=project_dir))
results = [f.result() for f in futures]
exit_codes = [r[0] for r in results]
stdouts = [r[1] for r in results]
stderrs = [r[2] for r in results]
non_zero_exit_codes = [c for c in exit_codes if c != 0]
non_empty_stdouts = [s for s in stdouts if len(s) > 0]
non_empty_stderrs = [s for s in stderrs if re.search(r'(Creating network|Network .* Creating)', s) is not None]
self.assertTrue(len(non_empty_stderrs) == 1,
f"Expected exactly one STDERR to be not empty, got {len(non_empty_stderrs)}:\n" +
'\n---\n'.join(non_empty_stderrs))
self.assertTrue(
# Output depends on the version of docker-compose
len(non_empty_stdouts) == 1 or len(non_empty_stdouts) == 0,
f"Expected exactly one STDOUT to be not empty, got {len(non_empty_stdouts)}:\n" +
'\n---\n'.join(non_empty_stdouts))
self.assertTrue(len(non_zero_exit_codes) == 0,
f"Expected all exit codes to be zero, got: {','.join(map(str, non_zero_exit_codes))}")
# TODO why does this test fail on CI but succeed locally?
# def test_exec_uses_build_cache(self):
# subprocess.run('docker rmi docker.pkg.github.com/benzaita/dockerized-cli/fixture-with_build_cache:latest', shell=True)
# self.assert_dockerized(
# fixture_name='with_build_cache',
# command='--loglevel INFO exec true',
# expected_exit_code=0,
# expected_stdout_regex=''.join([
# r'Step 2/2 : RUN echo "long operation"\n',
# r' ---> Using cache\n',
# ]),
# expected_stderr_regex=r'Pulling dockerized',
# )
def test_allows_config_file(self):
self.assert_dockerized(
fixture_name='with_config',
command='exec true',
expected_exit_code=1,
expected_stdout_regex=r'.*',
expected_stderr_regex=r'.*',
)
if __name__ == '__main__':
unittest.main()