-
Notifications
You must be signed in to change notification settings - Fork 13.7k
/
test_flower.py
296 lines (266 loc) · 11.3 KB
/
test_flower.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
# 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.
import jmespath
import pytest
from tests.helm_template_generator import render_chart
class TestFlowerDeployment:
@pytest.mark.parametrize(
"executor,flower_enabled,created",
[
("CeleryExecutor", False, False),
("CeleryKubernetesExecutor", False, False),
("KubernetesExecutor", False, False),
("CeleryExecutor", True, True),
("CeleryKubernetesExecutor", True, True),
("KubernetesExecutor", True, False),
],
)
def test_create_flower(self, executor, flower_enabled, created):
docs = render_chart(
values={"executor": executor, "flower": {"enabled": flower_enabled}},
show_only=["templates/flower/flower-deployment.yaml"],
)
assert bool(docs) is created
if created:
assert "RELEASE-NAME-flower" == jmespath.search("metadata.name", docs[0])
assert "flower" == jmespath.search("spec.template.spec.containers[0].name", docs[0])
@pytest.mark.parametrize(
"airflow_version, expected_arg",
[
("2.0.2", "airflow celery flower"),
("1.10.14", "airflow flower"),
("1.9.0", "airflow flower"),
("2.1.0", "airflow celery flower"),
],
)
def test_args_with_airflow_version(self, airflow_version, expected_arg):
docs = render_chart(
values={
"executor": "CeleryExecutor",
"flower": {"enabled": True},
"airflowVersion": airflow_version,
},
show_only=["templates/flower/flower-deployment.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].args", docs[0]) == [
"bash",
"-c",
f"exec \\\n{expected_arg}",
]
@pytest.mark.parametrize(
"command, args",
[
(None, None),
(None, ["custom", "args"]),
(["custom", "command"], None),
(["custom", "command"], ["custom", "args"]),
],
)
def test_command_and_args_overrides(self, command, args):
docs = render_chart(
values={"flower": {"command": command, "args": args}},
show_only=["templates/flower/flower-deployment.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={"flower": {"command": ["{{ .Release.Name }}"], "args": ["{{ .Release.Service }}"]}},
show_only=["templates/flower/flower-deployment.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_should_create_flower_deployment_with_authorization(self):
docs = render_chart(
values={
"executor": "CeleryExecutor",
"flower": {"username": "flower", "password": "fl0w3r"},
"ports": {"flowerUI": 7777},
},
show_only=["templates/flower/flower-deployment.yaml"],
)
assert "AIRFLOW__CELERY__FLOWER_BASIC_AUTH" == jmespath.search(
"spec.template.spec.containers[0].env[0].name", docs[0]
)
assert ['curl', '--user', '$AIRFLOW__CELERY__FLOWER_BASIC_AUTH', 'localhost:7777'] == jmespath.search(
"spec.template.spec.containers[0].livenessProbe.exec.command", docs[0]
)
assert ['curl', '--user', '$AIRFLOW__CELERY__FLOWER_BASIC_AUTH', 'localhost:7777'] == jmespath.search(
"spec.template.spec.containers[0].readinessProbe.exec.command", docs[0]
)
def test_should_create_flower_deployment_without_authorization(self):
docs = render_chart(
values={
"executor": "CeleryExecutor",
"ports": {"flowerUI": 7777},
},
show_only=["templates/flower/flower-deployment.yaml"],
)
assert "AIRFLOW__CORE__FERNET_KEY" == jmespath.search(
"spec.template.spec.containers[0].env[0].name", docs[0]
)
assert ['curl', 'localhost:7777'] == jmespath.search(
"spec.template.spec.containers[0].livenessProbe.exec.command", docs[0]
)
assert ['curl', 'localhost:7777'] == jmespath.search(
"spec.template.spec.containers[0].readinessProbe.exec.command", docs[0]
)
def test_should_create_valid_affinity_tolerations_and_node_selector(self):
docs = render_chart(
values={
"executor": "CeleryExecutor",
"flower": {
"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/flower/flower-deployment.yaml"],
)
assert "Deployment" == 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_flower_resources_are_configurable(self):
docs = render_chart(
values={
"flower": {
"resources": {
"limits": {"cpu": "200m", 'memory': "128Mi"},
"requests": {"cpu": "300m", 'memory': "169Mi"},
}
},
},
show_only=["templates/flower/flower-deployment.yaml"],
)
assert "128Mi" == jmespath.search("spec.template.spec.containers[0].resources.limits.memory", docs[0])
assert "169Mi" == jmespath.search(
"spec.template.spec.containers[0].resources.requests.memory", docs[0]
)
assert "300m" == jmespath.search("spec.template.spec.containers[0].resources.requests.cpu", docs[0])
def test_flower_resources_are_not_added_by_default(self):
docs = render_chart(
show_only=["templates/flower/flower-deployment.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].resources", docs[0]) == {}
def test_should_add_extra_containers(self):
docs = render_chart(
values={
"flower": {
"extraContainers": [
{"name": "test-container", "image": "test-registry/test-repo:test-tag"}
],
},
},
show_only=["templates/flower/flower-deployment.yaml"],
)
assert {
"name": "test-container",
"image": "test-registry/test-repo:test-tag",
} == jmespath.search("spec.template.spec.containers[-1]", docs[0])
def test_should_add_extra_volumes(self):
docs = render_chart(
values={
"flower": {
"extraVolumes": [{"name": "myvolume", "emptyDir": {}}],
},
},
show_only=["templates/flower/flower-deployment.yaml"],
)
assert {"name": "myvolume", "emptyDir": {}} == jmespath.search(
"spec.template.spec.volumes[-1]", docs[0]
)
class TestFlowerService:
@pytest.mark.parametrize(
"executor,flower_enabled,created",
[
("CeleryExecutor", False, False),
("CeleryKubernetesExecutor", False, False),
("KubernetesExecutor", False, False),
("CeleryExecutor", True, True),
("CeleryKubernetesExecutor", True, True),
("KubernetesExecutor", True, False),
],
)
def test_create_flower(self, executor, flower_enabled, created):
docs = render_chart(
values={"executor": executor, "flower": {"enabled": flower_enabled}},
show_only=["templates/flower/flower-service.yaml"],
)
assert bool(docs) is created
if created:
assert "RELEASE-NAME-flower" == jmespath.search("metadata.name", docs[0])
def test_default_service(self):
docs = render_chart(
show_only=["templates/flower/flower-service.yaml"],
)
assert "RELEASE-NAME-flower" == jmespath.search("metadata.name", docs[0])
assert jmespath.search("metadata.annotations", docs[0]) is None
assert {"tier": "airflow", "component": "flower", "release": "RELEASE-NAME"} == jmespath.search(
"spec.selector", docs[0]
)
assert "ClusterIP" == jmespath.search("spec.type", docs[0])
assert {"name": "flower-ui", "protocol": "TCP", "port": 5555} in jmespath.search(
"spec.ports", docs[0]
)
def test_overrides(self):
docs = render_chart(
values={
"ports": {"flowerUI": 9000},
"flower": {
"service": {
"type": "LoadBalancer",
"loadBalancerIP": "127.0.0.1",
"annotations": {"foo": "bar"},
}
},
},
show_only=["templates/flower/flower-service.yaml"],
)
assert {"foo": "bar"} == jmespath.search("metadata.annotations", docs[0])
assert "LoadBalancer" == jmespath.search("spec.type", docs[0])
assert {"name": "flower-ui", "protocol": "TCP", "port": 9000} in jmespath.search(
"spec.ports", docs[0]
)
assert "127.0.0.1" == jmespath.search("spec.loadBalancerIP", docs[0])