This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
logstash_test.py
executable file
·687 lines (579 loc) · 18 KB
/
logstash_test.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
import os
import sys
sys.path.insert(1, os.path.join(sys.path[0], "../../helpers"))
from helpers import helm_template
import yaml
name = "release-name-logstash"
def test_defaults():
config = """
"""
r = helm_template(config)
# Statefulset
assert r["statefulset"][name]["spec"]["replicas"] == 1
assert r["statefulset"][name]["spec"]["updateStrategy"] == {"type": "RollingUpdate"}
assert r["statefulset"][name]["spec"]["podManagementPolicy"] == "Parallel"
assert r["statefulset"][name]["spec"]["template"]["spec"]["affinity"][
"podAntiAffinity"
]["requiredDuringSchedulingIgnoredDuringExecution"][0] == {
"labelSelector": {
"matchExpressions": [{"key": "app", "operator": "In", "values": [name]}]
},
"topologyKey": "kubernetes.io/hostname",
}
# Default environment variables
env_vars = [
{"name": "LS_JAVA_OPTS", "value": "-Xmx1g -Xms1g"},
]
c = r["statefulset"][name]["spec"]["template"]["spec"]["containers"][0]
for env in env_vars:
assert env in c["env"]
# Image
assert c["image"].startswith("docker.elastic.co/logstash/logstash:")
assert c["imagePullPolicy"] == "IfNotPresent"
assert c["name"] == "logstash"
# Ports
assert c["ports"][0] == {"name": "http", "containerPort": 9600}
# Health checks
assert c["livenessProbe"]["failureThreshold"] == 3
assert c["livenessProbe"]["initialDelaySeconds"] == 300
assert c["livenessProbe"]["periodSeconds"] == 10
assert c["livenessProbe"]["successThreshold"] == 1
assert c["livenessProbe"]["timeoutSeconds"] == 5
assert "/" in c["livenessProbe"]["httpGet"]["path"]
assert "http" in c["livenessProbe"]["httpGet"]["port"]
assert c["readinessProbe"]["failureThreshold"] == 3
assert c["readinessProbe"]["initialDelaySeconds"] == 60
assert c["readinessProbe"]["periodSeconds"] == 10
assert c["readinessProbe"]["successThreshold"] == 3
assert c["readinessProbe"]["timeoutSeconds"] == 5
assert "/" in c["readinessProbe"]["httpGet"]["path"]
assert "http" in c["readinessProbe"]["httpGet"]["port"]
# Resources
assert c["resources"] == {
"requests": {"cpu": "100m", "memory": "1536Mi"},
"limits": {"cpu": "1000m", "memory": "1536Mi"},
}
# Persistence
assert "volumeClaimTemplates" not in r["statefulset"][name]["spec"]
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["containers"][0][
"volumeMounts"
]
== None
)
# Service
assert "serviceName" not in r["statefulset"][name]["spec"]
assert "service" not in r
# Other
assert r["statefulset"][name]["spec"]["template"]["spec"]["securityContext"] == {
"fsGroup": 1000,
"runAsUser": 1000,
}
assert (
r["statefulset"][name]["spec"]["template"]["spec"][
"terminationGracePeriodSeconds"
]
== 120
)
# Pod disruption budget
assert r["poddisruptionbudget"][name + "-pdb"]["spec"]["maxUnavailable"] == 1
# Empty customizable defaults
assert "imagePullSecrets" not in r["statefulset"][name]["spec"]["template"]["spec"]
assert "tolerations" not in r["statefulset"][name]["spec"]["template"]["spec"]
assert "nodeSelector" not in r["statefulset"][name]["spec"]["template"]["spec"]
def test_increasing_the_replicas():
config = """
replicas: 5
"""
r = helm_template(config)
assert r["statefulset"][name]["spec"]["replicas"] == 5
def test_disabling_pod_disruption_budget():
config = """
maxUnavailable: false
"""
r = helm_template(config)
assert "poddisruptionbudget" not in r
def test_overriding_the_image_and_tag():
config = """
image: customImage
imageTag: 6.2.4
"""
r = helm_template(config)
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["containers"][0]["image"]
== "customImage:6.2.4"
)
def test_adding_extra_env_vars():
config = """
extraEnvs:
- name: hello
value: world
"""
r = helm_template(config)
env = r["statefulset"][name]["spec"]["template"]["spec"]["containers"][0]["env"]
assert {"name": "hello", "value": "world"} in env
def test_adding_a_extra_volume_with_volume_mount():
config = """
extraVolumes: |
- name: extras
emptyDir: {}
extraVolumeMounts: |
- name: extras
mountPath: /usr/share/extras
readOnly: true
"""
r = helm_template(config)
extraVolume = r["statefulset"][name]["spec"]["template"]["spec"]["volumes"]
assert {"name": "extras", "emptyDir": {}} in extraVolume
extraVolumeMounts = r["statefulset"][name]["spec"]["template"]["spec"][
"containers"
][0]["volumeMounts"]
assert {
"name": "extras",
"mountPath": "/usr/share/extras",
"readOnly": True,
} in extraVolumeMounts
def test_adding_a_extra_container():
config = """
extraContainers: |
- name: do-something
image: busybox
command: ['do', 'something']
"""
r = helm_template(config)
extraContainer = r["statefulset"][name]["spec"]["template"]["spec"]["containers"]
assert {
"name": "do-something",
"image": "busybox",
"command": ["do", "something"],
} in extraContainer
def test_adding_a_extra_init_container():
config = """
extraInitContainers: |
- name: do-something
image: busybox
command: ['do', 'something']
"""
r = helm_template(config)
extraInitContainer = r["statefulset"][name]["spec"]["template"]["spec"][
"initContainers"
]
assert {
"name": "do-something",
"image": "busybox",
"command": ["do", "something"],
} in extraInitContainer
def test_adding_persistence():
config = """
persistence:
enabled: true
"""
r = helm_template(config)
c = r["statefulset"][name]["spec"]["template"]["spec"]["containers"][0]
assert c["volumeMounts"][0]["mountPath"] == "/usr/share/logstash/data"
assert c["volumeMounts"][0]["name"] == name
v = r["statefulset"]["release-name-logstash"]["spec"]["volumeClaimTemplates"][0]
assert v["metadata"]["name"] == name
assert v["spec"]["accessModes"] == ["ReadWriteOnce"]
assert v["spec"]["resources"]["requests"]["storage"] == "1Gi"
def test_adding_storageclass_annotation_to_volumeclaimtemplate():
config = """
persistence:
enabled: true
annotations:
volume.beta.kubernetes.io/storage-class: id
"""
r = helm_template(config)
annotations = r["statefulset"][name]["spec"]["volumeClaimTemplates"][0]["metadata"][
"annotations"
]
assert annotations["volume.beta.kubernetes.io/storage-class"] == "id"
def test_adding_multiple_persistence_annotations():
config = """
persistence:
enabled: true
annotations:
hello: world
world: hello
"""
r = helm_template(config)
annotations = r["statefulset"][name]["spec"]["volumeClaimTemplates"][0]["metadata"][
"annotations"
]
assert annotations["hello"] == "world"
assert annotations["world"] == "hello"
def test_adding_a_secret_mount():
config = """
secretMounts:
- name: elastic-certificates
secretName: elastic-certs
path: /usr/share/logstash/config/certs
"""
r = helm_template(config)
s = r["statefulset"][name]["spec"]["template"]["spec"]
assert s["containers"][0]["volumeMounts"][-1] == {
"mountPath": "/usr/share/logstash/config/certs",
"name": "elastic-certificates",
}
assert s["volumes"] == [
{"name": "elastic-certificates", "secret": {"secretName": "elastic-certs"}}
]
def test_adding_a_secret_mount_with_subpath():
config = """
secretMounts:
- name: elastic-certificates
secretName: elastic-certs
path: /usr/share/logstash/config/certs
subPath: cert.crt
"""
r = helm_template(config)
s = r["statefulset"][name]["spec"]["template"]["spec"]
assert s["containers"][0]["volumeMounts"][-1] == {
"mountPath": "/usr/share/logstash/config/certs",
"subPath": "cert.crt",
"name": "elastic-certificates",
}
def test_adding_image_pull_secrets():
config = """
imagePullSecrets:
- name: test-registry
"""
r = helm_template(config)
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["imagePullSecrets"][0][
"name"
]
== "test-registry"
)
def test_adding_tolerations():
config = """
tolerations:
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoExecute"
tolerationSeconds: 3600
"""
r = helm_template(config)
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["tolerations"][0]["key"]
== "key1"
)
def test_adding_pod_annotations():
config = """
podAnnotations:
iam.amazonaws.com/role: logstash-role
"""
r = helm_template(config)
assert (
r["statefulset"][name]["spec"]["template"]["metadata"]["annotations"][
"iam.amazonaws.com/role"
]
== "logstash-role"
)
def test_adding_a_node_selector():
config = """
nodeSelector:
disktype: ssd
"""
r = helm_template(config)
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["nodeSelector"]["disktype"]
== "ssd"
)
def test_adding_a_node_affinity():
config = """
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: mylabel
operator: In
values:
- myvalue
"""
r = helm_template(config)
assert r["statefulset"]["release-name-logstash"]["spec"]["template"]["spec"][
"affinity"
]["nodeAffinity"] == {
"preferredDuringSchedulingIgnoredDuringExecution": [
{
"weight": 100,
"preference": {
"matchExpressions": [
{"key": "mylabel", "operator": "In", "values": ["myvalue"]}
]
},
}
]
}
def test_adding_in_logstash_config():
config = """
logstashConfig:
logstash.yml: |
key:
nestedkey: value
dot.notation: test
log4j2.properties: |
appender.rolling.name = rolling
"""
r = helm_template(config)
c = r["configmap"][name + "-config"]["data"]
assert "logstash.yml" in c
assert "log4j2.properties" in c
assert "nestedkey: value" in c["logstash.yml"]
assert "dot.notation: test" in c["logstash.yml"]
assert "appender.rolling.name = rolling" in c["log4j2.properties"]
s = r["statefulset"][name]["spec"]["template"]["spec"]
assert {
"configMap": {"name": "release-name-logstash-config"},
"name": "logstashconfig",
} in s["volumes"]
assert {
"mountPath": "/usr/share/logstash/config/logstash.yml",
"name": "logstashconfig",
"subPath": "logstash.yml",
} in s["containers"][0]["volumeMounts"]
assert {
"mountPath": "/usr/share/logstash/config/log4j2.properties",
"name": "logstashconfig",
"subPath": "log4j2.properties",
} in s["containers"][0]["volumeMounts"]
assert (
"configchecksum"
in r["statefulset"][name]["spec"]["template"]["metadata"]["annotations"]
)
def test_adding_in_pipeline():
config = """
logstashPipeline:
uptime.conf: |
input { stdin { } } }
output { stdout { { } } }
"""
r = helm_template(config)
c = r["configmap"][name + "-pipeline"]["data"]
assert "uptime.conf" in c
assert "input { stdin { } } }" in c["uptime.conf"]
assert "output { stdout { { } } }" in c["uptime.conf"]
assert (
"pipelinechecksum"
in r["statefulset"][name]["spec"]["template"]["metadata"]["annotations"]
)
def test_priority_class_name():
config = """
priorityClassName: ""
"""
r = helm_template(config)
spec = r["statefulset"][name]["spec"]["template"]["spec"]
assert "priorityClassName" not in spec
config = """
priorityClassName: "highest"
"""
r = helm_template(config)
priority_class_name = r["statefulset"][name]["spec"]["template"]["spec"][
"priorityClassName"
]
assert priority_class_name == "highest"
def test_scheduler_name():
r = helm_template("")
spec = r["statefulset"][name]["spec"]["template"]["spec"]
assert "schedulerName" not in spec
config = """
schedulerName: "stork"
"""
r = helm_template(config)
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["schedulerName"] == "stork"
)
def test_lifecycle_hooks():
config = ""
r = helm_template(config)
c = r["statefulset"][name]["spec"]["template"]["spec"]["containers"][0]
assert "lifecycle" not in c
config = """
lifecycle:
preStop:
exec:
command: ["/bin/bash","/preStop"]
"""
r = helm_template(config)
c = r["statefulset"][name]["spec"]["template"]["spec"]["containers"][0]
assert c["lifecycle"]["preStop"]["exec"]["command"] == ["/bin/bash", "/preStop"]
def test_set_pod_security_context():
config = ""
r = helm_template(config)
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["securityContext"]["fsGroup"]
== 1000
)
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["securityContext"][
"runAsUser"
]
== 1000
)
config = """
podSecurityContext:
fsGroup: 1001
other: test
"""
r = helm_template(config)
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["securityContext"]["fsGroup"]
== 1001
)
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["securityContext"]["other"]
== "test"
)
def test_set_container_security_context():
config = ""
r = helm_template(config)
c = r["statefulset"][name]["spec"]["template"]["spec"]["containers"][0]
assert c["securityContext"]["capabilities"]["drop"] == ["ALL"]
assert c["securityContext"]["runAsNonRoot"] == True
assert c["securityContext"]["runAsUser"] == 1000
config = """
securityContext:
runAsUser: 1001
other: test
"""
r = helm_template(config)
c = r["statefulset"][name]["spec"]["template"]["spec"]["containers"][0]
assert c["securityContext"]["capabilities"]["drop"] == ["ALL"]
assert c["securityContext"]["runAsNonRoot"] == True
assert c["securityContext"]["runAsUser"] == 1001
assert c["securityContext"]["other"] == "test"
def test_adding_pod_labels():
config = """
labels:
app.kubernetes.io/name: logstash
"""
r = helm_template(config)
assert (
r["statefulset"][name]["metadata"]["labels"]["app.kubernetes.io/name"]
== "logstash"
)
assert (
r["statefulset"][name]["spec"]["template"]["metadata"]["labels"][
"app.kubernetes.io/name"
]
== "logstash"
)
def test_pod_security_policy():
## Make sure the default config is not creating any resources
config = ""
resources = ("role", "rolebinding", "serviceaccount", "podsecuritypolicy")
r = helm_template(config)
for resource in resources:
assert resource not in r
assert (
"serviceAccountName" not in r["statefulset"][name]["spec"]["template"]["spec"]
)
## Make sure all the resources are created with default values
config = """
rbac:
create: true
serviceAccountName: ""
podSecurityPolicy:
create: true
name: ""
"""
r = helm_template(config)
for resource in resources:
assert resource in r
assert r["role"][name]["rules"][0] == {
"apiGroups": ["extensions"],
"verbs": ["use"],
"resources": ["podsecuritypolicies"],
"resourceNames": [name],
}
assert r["rolebinding"][name]["subjects"] == [
{"kind": "ServiceAccount", "namespace": "default", "name": name}
]
assert r["rolebinding"][name]["roleRef"] == {
"apiGroup": "rbac.authorization.k8s.io",
"kind": "Role",
"name": name,
}
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["serviceAccountName"] == name
)
psp_spec = r["podsecuritypolicy"][name]["spec"]
assert psp_spec["privileged"] is True
def test_external_pod_security_policy():
## Make sure we can use an externally defined pod security policy
config = """
rbac:
create: true
serviceAccountName: ""
podSecurityPolicy:
create: false
name: "customPodSecurityPolicy"
"""
resources = ("role", "rolebinding")
r = helm_template(config)
for resource in resources:
assert resource in r
assert r["role"][name]["rules"][0] == {
"apiGroups": ["extensions"],
"verbs": ["use"],
"resources": ["podsecuritypolicies"],
"resourceNames": ["customPodSecurityPolicy"],
}
def test_external_service_account():
## Make sure we can use an externally defined service account
config = """
rbac:
create: false
serviceAccountName: "customServiceAccountName"
podSecurityPolicy:
create: false
name: ""
"""
resources = ("role", "rolebinding", "serviceaccount")
r = helm_template(config)
assert (
r["statefulset"][name]["spec"]["template"]["spec"]["serviceAccountName"]
== "customServiceAccountName"
)
# When referencing an external service account we do not want any resources to be created.
for resource in resources:
assert resource not in r
def test_adding_a_service():
config = """
service:
annotations: {}
type: ClusterIP
ports:
- name: beats
port: 5044
protocol: TCP
targetPort: 5044
"""
r = helm_template(config)
s = r["service"][name]
assert s["metadata"]["name"] == name
assert s["metadata"]["annotations"] == {}
assert s["spec"]["type"] == "ClusterIP"
assert len(s["spec"]["ports"]) == 1
assert s["spec"]["ports"][0] == {
"name": "beats",
"port": 5044,
"protocol": "TCP",
"targetPort": 5044,
}
def test_setting_fullnameOverride():
config = """
fullnameOverride: 'logstash-custom'
"""
r = helm_template(config)
custom_name = "logstash-custom"
assert custom_name in r["statefulset"]
assert (
r["statefulset"][custom_name]["spec"]["template"]["spec"]["containers"][0][
"name"
]
== "logstash"
)