forked from GoogleCloudPlatform/python-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_dockerfile_test.py
More file actions
executable file
·274 lines (236 loc) · 8.89 KB
/
gen_dockerfile_test.py
File metadata and controls
executable file
·274 lines (236 loc) · 8.89 KB
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
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python3
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit test for gen_dockerfile.py"""
import argparse
import filecmp
import os
import shutil
import subprocess
import unittest.mock
import pytest
import yaml
import gen_dockerfile
# Expected list of files generated
EXPECTED_OUTPUT_FILES = set(('Dockerfile', '.dockerignore'))
@pytest.fixture
def testdata_dir():
testdata_dir = os.path.join(os.path.dirname(__file__), 'testdata')
assert os.path.isdir(testdata_dir), (
'Could not run test: testdata directory not found')
return testdata_dir
def compare_file(filename, dir1, dir2):
"""Compare identically named files in two different directories"""
assert filecmp.cmp(
os.path.join(dir1, filename), os.path.join(dir2, filename))
@pytest.mark.parametrize('app_yaml, expected', [
# Basic app.yaml
('env: flex', {
'base_image': 'some_image_name',
'dockerfile_python_version': '',
'has_requirements_txt': False,
'entrypoint': '',
'is_python_compat': False,
}),
('env: flex\nruntime: python-compat', {
'base_image': None,
'dockerfile_python_version': None,
'has_requirements_txt': None,
'entrypoint': None,
'is_python_compat': True,
}),
# All supported python versions
('runtime_config:\n python_version:', {
'dockerfile_python_version': '',
}),
('runtime_config:\n python_version: 2', {
'dockerfile_python_version': '',
}),
('runtime_config:\n python_version: 3', {
'dockerfile_python_version': '3.6',
}),
('runtime_config:\n python_version: 3.4', {
'dockerfile_python_version': '3.4',
}),
('runtime_config:\n python_version: 3.5', {
'dockerfile_python_version': '3.5',
}),
('runtime_config:\n python_version: 3.6', {
'dockerfile_python_version': '3.6',
}),
('runtime_config:\n python_version: 3.7', {
'dockerfile_python_version': '3.7',
}),
# entrypoint present
('entrypoint: my entrypoint', {
'entrypoint': 'exec my entrypoint',
}),
])
def test_get_app_config_valid(app_yaml, expected):
config_file = 'some_config_file'
base_image = 'some_image_name'
source_dir = 'some_source_dir'
raw_app_config = yaml.safe_load(app_yaml)
actual = gen_dockerfile.get_app_config(
raw_app_config, base_image, config_file,
source_dir)
for key, value in expected.items():
assert getattr(actual, key) == value
def test_get_app_config_requirements_txt():
"""requirements.txt file present"""
app_yaml = 'env: flex'
expected = {
'has_requirements_txt': True,
}
with unittest.mock.patch.object(os.path, 'isfile', return_value=True):
test_get_app_config_valid(app_yaml, expected)
@pytest.mark.parametrize('app_yaml', [
# Empty app.yaml
'',
# Invalid entrypoint
'entrypoint: "bad \\n entrypoint"',
# Invalid python version
'runtime_config:\n python_version: 1',
'runtime_config:\n python_version: python2',
])
def test_get_app_config_invalid(app_yaml):
config_file = 'some_config_file'
base_image = 'some_image_name'
source_dir = 'some_source_dir'
raw_app_config = yaml.safe_load(app_yaml)
with pytest.raises(ValueError):
gen_dockerfile.get_app_config(
raw_app_config, base_image, config_file, source_dir)
# Basic AppConfig used below
_BASE_APP_CONFIG = gen_dockerfile.AppConfig(
base_image='',
dockerfile_python_version='',
entrypoint='',
has_requirements_txt=False,
is_python_compat=False,
)
@pytest.mark.parametrize('app_config, should_find, test_string', [
# Requirements.txt
(_BASE_APP_CONFIG, False, 'ADD requirements.txt'),
(_BASE_APP_CONFIG._replace(has_requirements_txt=True), True,
'ADD requirements.txt'),
# Entrypoint
(_BASE_APP_CONFIG, False, 'CMD'),
(_BASE_APP_CONFIG._replace(entrypoint='my entrypoint'), True,
'CMD my entrypoint'),
(_BASE_APP_CONFIG._replace(entrypoint='exec my entrypoint'), True,
'CMD exec my entrypoint'),
# Base runtime image
(_BASE_APP_CONFIG._replace(base_image='my_base_runtime_image'), True,
'FROM my_base_runtime_image'),
# Python version
(_BASE_APP_CONFIG._replace(dockerfile_python_version='_my_version'), True,
'python_version=python_my_version'),
# python-compat runtime
(_BASE_APP_CONFIG._replace(is_python_compat=True), True,
'FROM gcr.io/google_appengine/python-compat-multicore'),
])
def test_generate_files(app_config, should_find, test_string):
result = gen_dockerfile.generate_files(app_config)
assert set(result.keys()) == EXPECTED_OUTPUT_FILES
dockerfile = result['Dockerfile']
if should_find:
assert test_string in dockerfile
else:
assert test_string not in dockerfile
def compare_against_golden_files(app, config_dir, testdata_dir):
golden_dir = os.path.join(testdata_dir, app + '_golden')
for filename in EXPECTED_OUTPUT_FILES:
compare_file(filename, config_dir, golden_dir)
@pytest.mark.parametrize('app', [
# Sampled from https://github.com/GoogleCloudPlatform/python-docs-samples
'hello_world',
# From an internal source.
'hello_world_compat'])
def test_generate_dockerfile_command(tmpdir, testdata_dir, app):
"""Generates output and compares against a set of golden files."""
app_dir = os.path.join(testdata_dir, app)
# Copy sample app to writable temp dir, and generate Dockerfile.
config_dir = os.path.join(str(tmpdir), 'config')
shutil.copytree(app_dir, config_dir)
gen_dockerfile.generate_dockerfile_command(
base_image='gcr.io/google-appengine/python',
config_file=os.path.join(config_dir, 'app.yaml'),
source_dir=config_dir)
compare_against_golden_files(app, config_dir, testdata_dir)
@pytest.mark.parametrize('app', [
# Sampled from https://github.com/GoogleCloudPlatform/python-docs-samples
'hello_world',
# From an internal source.
'hello_world_compat'])
@pytest.mark.xfail(not shutil.which('gcloud'),
reason='Google Cloud SDK is not installed')
def test_generate_dockerfile_golden(tmpdir, testdata_dir, app):
"""Validate our golden files against gcloud app gen-config"""
app_dir = os.path.join(testdata_dir, app)
# Copy sample app to writable temp dir, and generate Dockerfile.
gen_config_dir = os.path.join(str(tmpdir), 'gen_config')
shutil.copytree(app_dir, gen_config_dir)
app_yaml = os.path.join(gen_config_dir, 'app.yaml')
gcloud_args = [
'gcloud', '--quiet', 'beta', 'app', 'gen-config',
gen_config_dir, '--custom', '--config={}'.format(app_yaml)
]
print('Invoking gcloud as {}'.format(gcloud_args))
subprocess.check_call(gcloud_args)
compare_against_golden_files(app, gen_config_dir, testdata_dir)
@pytest.mark.parametrize('argv', [
[],
['argv0', '--base-image=nocolon'],
['argv0', '--base-image=name:andcolon'],
['argv0', '--base-image=name@sha256:digest'],
])
def test_parse_args_valid(argv):
args = gen_dockerfile.parse_args(argv)
assert args is not None
@pytest.mark.parametrize('argv', [
['argv0', '--base-image='],
['argv0', '--base-image=:'],
['argv0', '--base-image=:noname'],
])
def test_parse_args_invalid(argv):
def mock_error(*args):
"""Prevent argparse from calling sys.exit()"""
raise AssertionError(*args)
error_patch = unittest.mock.patch.object(
argparse.ArgumentParser, 'error', mock_error)
with error_patch:
with pytest.raises(AssertionError):
gen_dockerfile.parse_args(argv)
@pytest.mark.parametrize('argv, env, expected', [
# Explicit flag wins
(['argv0', '--config=flag/path'], 'env/path', 'flag/path'),
(['argv0', '--config=flag/path'], '', 'flag/path'),
(['argv0', '--config=flag/path'], None, 'flag/path'),
# Otherwise env var wins
(['argv0'], 'env/path', 'env/path'),
# Otherwise use default name
(['argv0'], '', 'app.yaml'),
(['argv0'], None, 'app.yaml'),
])
def test_parse_args_config(argv, env, expected):
if env is None:
mock_environ = {}
else:
mock_environ = {gen_dockerfile.GAE_APPLICATION_YAML_PATH: env}
with unittest.mock.patch.dict('os.environ', mock_environ, clear=True):
args = gen_dockerfile.parse_args(argv)
assert args.config == expected
if __name__ == '__main__':
pytest.main([__file__])