-
Notifications
You must be signed in to change notification settings - Fork 14.5k
/
test_migrate_database_job.py
363 lines (322 loc) · 13.6 KB
/
test_migrate_database_job.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
from __future__ import annotations
import jmespath
import pytest
from tests.charts.helm_template_generator import render_chart
class TestMigrateDatabaseJob:
"""Tests migrate DB job."""
def test_should_run_by_default(self):
docs = render_chart(show_only=["templates/jobs/migrate-database-job.yaml"])
assert "Job" == docs[0]["kind"]
assert "run-airflow-migrations" == jmespath.search("spec.template.spec.containers[0].name", docs[0])
assert 50000 == jmespath.search("spec.template.spec.securityContext.runAsUser", docs[0])
@pytest.mark.parametrize(
"migrate_database_job_enabled,created",
[
(False, False),
(True, True),
],
)
def test_enable_migrate_database_job(self, migrate_database_job_enabled, created):
docs = render_chart(
values={
"migrateDatabaseJob": {"enabled": migrate_database_job_enabled},
},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert bool(docs) is created
def test_should_support_annotations(self):
docs = render_chart(
values={"migrateDatabaseJob": {"annotations": {"foo": "bar"}, "jobAnnotations": {"fiz": "fuz"}}},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
annotations = jmespath.search("spec.template.metadata.annotations", docs[0])
assert "foo" in annotations
assert "bar" == annotations["foo"]
job_annotations = jmespath.search("metadata.annotations", docs[0])
assert "fiz" in job_annotations
assert "fuz" == job_annotations["fiz"]
def test_should_create_valid_affinity_tolerations_and_node_selector(self):
docs = render_chart(
values={
"migrateDatabaseJob": {
"affinity": {
"nodeAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": {
"nodeSelectorTerms": [
{
"matchExpressions": [
{"key": "foo", "operator": "In", "values": ["true"]},
]
}
]
}
}
},
"tolerations": [
{"key": "dynamic-pods", "operator": "Equal", "value": "true", "effect": "NoSchedule"}
],
"nodeSelector": {"diskType": "ssd"},
}
},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert "Job" == jmespath.search("kind", docs[0])
assert "foo" == jmespath.search(
"spec.template.spec.affinity.nodeAffinity."
"requiredDuringSchedulingIgnoredDuringExecution."
"nodeSelectorTerms[0]."
"matchExpressions[0]."
"key",
docs[0],
)
assert "ssd" == jmespath.search(
"spec.template.spec.nodeSelector.diskType",
docs[0],
)
assert "dynamic-pods" == jmespath.search(
"spec.template.spec.tolerations[0].key",
docs[0],
)
def test_scheduler_name(self):
docs = render_chart(
values={"schedulerName": "airflow-scheduler"},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert "airflow-scheduler" == jmespath.search(
"spec.template.spec.schedulerName",
docs[0],
)
@pytest.mark.parametrize(
"use_default_image,expected_image",
[
(True, "apache/airflow:2.1.0"),
(False, "apache/airflow:user-image"),
],
)
def test_should_use_correct_image(self, use_default_image, expected_image):
docs = render_chart(
values={
"defaultAirflowRepository": "apache/airflow",
"defaultAirflowTag": "2.1.0",
"images": {
"airflow": {
"repository": "apache/airflow",
"tag": "user-image",
},
"useDefaultImageForMigration": use_default_image,
},
},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert expected_image == jmespath.search("spec.template.spec.containers[0].image", docs[0])
def test_should_add_extra_containers(self):
docs = render_chart(
values={
"migrateDatabaseJob": {
"extraContainers": [
{"name": "test-container", "image": "test-registry/test-repo:test-tag"}
],
},
},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert {
"name": "test-container",
"image": "test-registry/test-repo:test-tag",
} == jmespath.search("spec.template.spec.containers[-1]", docs[0])
def test_set_resources(self):
docs = render_chart(
values={
"migrateDatabaseJob": {
"resources": {
"requests": {
"cpu": "1000mi",
"memory": "512Mi",
},
"limits": {
"cpu": "1000mi",
"memory": "512Mi",
},
},
},
},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert {
"requests": {
"cpu": "1000mi",
"memory": "512Mi",
},
"limits": {
"cpu": "1000mi",
"memory": "512Mi",
},
} == jmespath.search("spec.template.spec.containers[0].resources", docs[0])
def test_should_disable_default_helm_hooks(self):
docs = render_chart(
values={"migrateDatabaseJob": {"useHelmHooks": False}},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
annotations = jmespath.search("metadata.annotations", docs[0])
assert annotations is None
def test_should_set_correct_helm_hooks_weight(self):
docs = render_chart(
show_only=[
"templates/jobs/migrate-database-job.yaml",
],
)
annotations = jmespath.search("metadata.annotations", docs[0])
assert annotations["helm.sh/hook-weight"] == "1"
def test_should_add_extra_volumes(self):
docs = render_chart(
values={
"migrateDatabaseJob": {
"extraVolumes": [{"name": "myvolume-{{ .Chart.Name }}", "emptyDir": {}}],
},
},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert {"name": "myvolume-airflow", "emptyDir": {}} == jmespath.search(
"spec.template.spec.volumes[-1]", docs[0]
)
def test_should_add_extra_volume_mounts(self):
docs = render_chart(
values={
"migrateDatabaseJob": {
"extraVolumeMounts": [{"name": "foobar-{{ .Chart.Name }}", "mountPath": "foo/bar"}],
},
},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert {"name": "foobar-airflow", "mountPath": "foo/bar"} == jmespath.search(
"spec.template.spec.containers[0].volumeMounts[-1]", docs[0]
)
def test_should_add_global_volume_and_global_volume_mount(self):
docs = render_chart(
values={
"volumes": [{"name": "myvolume", "emptyDir": {}}],
"volumeMounts": [{"name": "foobar", "mountPath": "foo/bar"}],
},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert {"name": "myvolume", "emptyDir": {}} == jmespath.search(
"spec.template.spec.volumes[-1]", docs[0]
)
assert {"name": "foobar", "mountPath": "foo/bar"} == jmespath.search(
"spec.template.spec.containers[0].volumeMounts[-1]", docs[0]
)
def test_job_ttl_after_finished(self):
docs = render_chart(
values={"migrateDatabaseJob": {"ttlSecondsAfterFinished": 1}},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
ttl = jmespath.search("spec.ttlSecondsAfterFinished", docs[0])
assert ttl == 1
def test_job_ttl_after_finished_zero(self):
docs = render_chart(
values={"migrateDatabaseJob": {"ttlSecondsAfterFinished": 0}},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
ttl = jmespath.search("spec.ttlSecondsAfterFinished", docs[0])
assert ttl == 0
def test_job_ttl_after_finished_nil(self):
docs = render_chart(
values={"migrateDatabaseJob": {"ttlSecondsAfterFinished": None}},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
spec = jmespath.search("spec", docs[0])
assert "ttlSecondsAfterFinished" not in spec
@pytest.mark.parametrize(
"airflow_version, expected_arg",
[
("1.10.14", "airflow upgradedb"),
("2.0.2", "airflow db upgrade"),
("2.7.1", "airflow db migrate"),
],
)
def test_default_command_and_args_airflow_version(self, airflow_version, expected_arg):
docs = render_chart(
values={
"airflowVersion": airflow_version,
},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].command", docs[0]) is None
assert [
"bash",
"-c",
f"exec \\\n{expected_arg}",
] == jmespath.search("spec.template.spec.containers[0].args", docs[0])
@pytest.mark.parametrize("command", [None, ["custom", "command"]])
@pytest.mark.parametrize("args", [None, ["custom", "args"]])
def test_command_and_args_overrides(self, command, args):
docs = render_chart(
values={"migrateDatabaseJob": {"command": command, "args": args}},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert command == jmespath.search("spec.template.spec.containers[0].command", docs[0])
assert args == jmespath.search("spec.template.spec.containers[0].args", docs[0])
def test_command_and_args_overrides_are_templated(self):
docs = render_chart(
values={
"migrateDatabaseJob": {"command": ["{{ .Release.Name }}"], "args": ["{{ .Release.Service }}"]}
},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert ["release-name"] == jmespath.search("spec.template.spec.containers[0].command", docs[0])
assert ["Helm"] == jmespath.search("spec.template.spec.containers[0].args", docs[0])
def test_no_airflow_local_settings(self):
docs = render_chart(
values={"airflowLocalSettings": None}, show_only=["templates/jobs/migrate-database-job.yaml"]
)
volume_mounts = jmespath.search("spec.template.spec.containers[0].volumeMounts", docs[0])
assert "airflow_local_settings.py" not in str(volume_mounts)
def test_airflow_local_settings(self):
docs = render_chart(
values={"airflowLocalSettings": "# Well hello!"},
show_only=["templates/jobs/migrate-database-job.yaml"],
)
assert {
"name": "config",
"mountPath": "/opt/airflow/config/airflow_local_settings.py",
"subPath": "airflow_local_settings.py",
"readOnly": True,
} in jmespath.search("spec.template.spec.containers[0].volumeMounts", docs[0])
class TestMigrateDatabaseJobServiceAccount:
"""Tests migrate database job service account."""
def test_default_automount_service_account_token(self):
docs = render_chart(
values={
"migrateDatabaseJob": {
"serviceAccount": {"create": True},
},
},
show_only=["templates/jobs/migrate-database-job-serviceaccount.yaml"],
)
assert jmespath.search("automountServiceAccountToken", docs[0]) is True
def test_overridden_automount_service_account_token(self):
docs = render_chart(
values={
"migrateDatabaseJob": {
"serviceAccount": {"create": True, "automountServiceAccountToken": False},
},
},
show_only=["templates/jobs/migrate-database-job-serviceaccount.yaml"],
)
assert jmespath.search("automountServiceAccountToken", docs[0]) is False