-
Notifications
You must be signed in to change notification settings - Fork 25
/
Jenkinsfile
1381 lines (1267 loc) · 44.1 KB
/
Jenkinsfile
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
def skipNativeTest = params.SKIP_NATIVE ?: false
def skipH2Test = params.SKIP_H2 ?: false
podTemplate(activeDeadlineSeconds: 7200, idleMinutes: 1, containers: [
containerTemplate(
name: "jnlp",
image: "jenkins/inbound-agent:4.13-2-alpine-jdk11",
runAsUser: '0',
resourceLimitCpu: '900m',
resourceLimitMemory: '1Gi',
resourceRequestCpu: '900m',
resourceRequestMemory: '1Gi'
),
containerTemplate(
name: "kubectl",
image: "bitnami/kubectl:1.19.4",
command: 'cat',
runAsUser: '0',
ttyEnabled: true,
),
]) {
node(POD_LABEL) {
stage ("create environment") {
container ("kubectl") {
withKubeConfig([credentialsId: '6a647093-716e-4e8f-90bd-a8007be37f0e',
serverUrl: 'https://10.100.1.42:6443',
contextName: 'jenkins',
clusterName: 'kubernetes',
namespace: 'jenkins'
]) {
try {
sh """#!/bin/bash
#timestamp="\$(date +%s)-${JOB_NAME}-${BUILD_NUMBER}"
timestamp="${JOB_NAME}-${BUILD_NUMBER}"
echo \${timestamp} >timestamp
mkdir logs-\${timestamp}
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: docker-reg-\${timestamp}
namespace: jenkins
spec:
ports:
- name: registry
protocol: TCP
port: 5000
targetPort: 5000
selector:
app: docker-reg-\${timestamp}
type: ClusterIP
sessionAffinity: None
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: docker-registry-\${timestamp}
namespace: jenkins
annotations:
kubernetes.io/ingress.class: haproxy
spec:
tls:
- hosts:
- registry-\${timestamp}.lab.evolveum.com
secretName: cert-lab-evolveum
rules:
- host: registry-\${timestamp}.lab.evolveum.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: docker-reg-\${timestamp}
port:
number: 5000
---
apiVersion: v1
kind: ConfigMap
metadata:
name: docker-reg-\${timestamp}-config
namespace: jenkins
data:
config.yml: |-
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3
http:
addr: :5000
headers:
X-Content-Type-Options:
- nosniff
log:
fields:
service: registry
storage:
delete:
enabled: true
cache:
blobdescriptor: inmemory
version: 0.1
---
apiVersion: v1
kind: Secret
metadata:
name: docker-reg-\${timestamp}-secret
namespace: jenkins
data:
haSharedSecret: Z2d5bnBzUWdwcXFWOXB2dw==
type: Opaque
---
apiVersion: v1
kind: Pod
metadata:
name: docker-reg-\${timestamp}
namespace: jenkins
labels:
app: docker-reg-\${timestamp}
spec:
volumes:
- name: data
emptyDir: {}
- name: docker-reg-\${timestamp}-config
configMap:
name: docker-reg-\${timestamp}-config
defaultMode: 420
containers:
- name: docker-registry
image: registry:2.8.1
command:
- /bin/registry
- serve
- /etc/docker/registry/config.yml
ports:
- containerPort: 5000
protocol: TCP
env:
- name: REGISTRY_HTTP_SECRET
valueFrom:
secretKeyRef:
name: docker-reg-\${timestamp}-secret
key: haSharedSecret
- name: REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY
value: /var/lib/registry
volumeMounts:
- name: data
mountPath: /var/lib/registry/
- name: docker-reg-\${timestamp}-config
mountPath: /etc/docker/registry
livenessProbe:
httpGet:
path: /
port: 5000
scheme: HTTP
timeoutSeconds: 1
periodSeconds: 10
successThreshold: 1
failureThreshold: 3
readinessProbe:
httpGet:
path: /
port: 5000
scheme: HTTP
timeoutSeconds: 1
periodSeconds: 10
successThreshold: 1
failureThreshold: 3
imagePullPolicy: IfNotPresent
securityContext:
runAsUser: 1000
fsGroup: 1000
EOF
echo "Waiting to get registry ready..."
http=\$(curl -I -L -s https://registry-\${timestamp}.lab.evolveum.com/v2/_catalog | head -n 1 | cut -d " " -f 2 )
while [ \${http} -eq 404 -o \${http} -eq 503 ]
do
sleep 2
http=\$(curl -I -L -s https://registry-\${timestamp}.lab.evolveum.com/v2/_catalog | head -n 1 | cut -d " " -f 2 )
done
echo "Registry is ready... (\${http})"
curl -s https://registry-\${timestamp}.lab.evolveum.com/v2/_catalog
"""
} catch (err) {
echo "Caught: ${err}"
unstable 'Error during envvironment initialization'
}
}
}
}
stage ("build image") {
container ("kubectl") {
withKubeConfig([credentialsId: '6a647093-716e-4e8f-90bd-a8007be37f0e',
serverUrl: 'https://10.100.1.42:6443',
contextName: 'jenkins',
clusterName: 'kubernetes',
namespace: 'jenkins'
]) {
try {
sh """#!/bin/bash
timestamp="\$(cat timestamp)"
#mkdir midpoint-docker
#curl -s -L https://github.com/Evolveum/midpoint-docker/tarball/master | tar -xzC ${WORKSPACE}/midpoint-docker --strip-components=1
osID=\$(echo "${IMAGEOS}" | cut -d "-" -f 1)
osVer=\$(echo "${IMAGEOS}" | cut -d "-" -f 2)
case \${osID} in
alpine)
javaPath="/usr/lib/jvm/default-jvm"
;;
*)
javaPath="/usr/lib/jvm/java-${JAVAVER}-openjdk-amd64"
;;
esac
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: kaniko-\${timestamp}
namespace: jenkins
spec:
containers:
- name: kaniko
image: gcr.io/kaniko-project/executor:latest
args:
- "--dockerfile=Dockerfile"
- "--context=git://github.com/Evolveum/midpoint-docker.git#refs/heads/master"
- "--destination=registry-\${timestamp}.lab.evolveum.com/midpoint:build-${DOCKERTAG}-${IMAGEOS}-\${timestamp}"
- --build-arg
- base_image=\${osID}
- --build-arg
- base_image_tag=\${osVer}
- --build-arg
- java_home=\${javaPath}
- --build-arg
- MP_VERSION=${DOCKERTAG}
- --build-arg
- JAVA_VERSION=${JAVAVER}
restartPolicy: Never
EOF
echo "Waiting to finish the process of the image build..."
status=\$(kubectl get -n jenkins pod/kaniko-\${timestamp} -o=jsonpath="{.status.phase}")
while [ "\${status}" == "Pending" -o "\${status}" == "Running" ]
do
sleep 15
status=\$(kubectl get -n jenkins pod/kaniko-\${timestamp} -o=jsonpath="{.status.phase}")
echo "Log contain \$(kubectl logs -n jenkins kaniko-\${timestamp} | wc -l) lines..."
done
echo " - - - - partial log from the kaniko container - - - -"
kubectl logs -n jenkins kaniko-\${timestamp} |tee logs-\${timestamp}/kaniko.log | grep -B 1 "Applying\\|Downloading midPoint\\|Pushed"
echo " - - - - end of container's partial log - - - -"
echo -e "\\tFull log is available to download in the job build's artifact..."
kubectl delete -n jenkins pod/kaniko-\${timestamp}
"""
} catch (err) {
echo "Caught: ${err}"
unstable 'Error during build phase'
}
}
}
}
stage ("test-H2") {
container ("kubectl") {
try {
if (skipH2Test) {
sh """#!/bin/bash
timestamp="\$(cat timestamp)"
echo 0 > logs-\${timestamp}/test-result-h2
"""
echo "H2 tests are not required"
return
} else {
echo "Processing the H2 test..."
}
sh """#!/bin/bash
timestamp="\$(cat timestamp)"
#In each cycle there is sleep time set to 5 seconds
waitCycle=120
mkdir logs-\${timestamp}/h2
function createPVC {
cat <<EOF | kubectl apply -f - | grep created | sed "s|\\([^[:space:]]*\\) created|\\1|"
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: \${2}
namespace: \${1}
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: \${3:-5}\${4:-G}i
volumeMode: Filesystem
EOF
}
function createMPPod {
if [ "\${4}" == "" ]
then
volDef="emptyDir: {}"
else
volDef="persistentVolumeClaim:
claimName: \${4}"
fi
cat <<EOF | kubectl apply -f - | grep created | sed "s|\\([^[:space:]]*\\) created|\\1|"
apiVersion: v1
kind: Pod
metadata:
name: \${2}-\${3}
namespace: \${1}
labels:
app: \${2}-\${3}
type: test
spec:
volumes:
- name: mpdata
\${volDef}
containers:
- name: mp
image: 'registry-\${3}.lab.evolveum.com/midpoint:build-${DOCKERTAG}-${IMAGEOS}-\${3}'
ports:
- name: gui
containerPort: 8080
protocol: TCP
env:
- name: MP_SET_midpoint_administrator_initialPassword
value: Test5ecr3t
volumeMounts:
- name: mpdata
mountPath: /opt/midpoint/var
imagePullPolicy: IfNotPresent
restartPolicy: Always
EOF
}
function checkApp {
iteration=0
logItemFound=0
while [ \${iteration} -le \${waitCycle} -a \${logItemFound} -eq 0 ]
do
sleep 5
kubectl logs -n \${1} \${2} > \${3}/pod-mp.log 2>\${3}/pod-mp.errlog
[ \$(grep -c "INFO (com.evolveum.midpoint.web.boot.MidPointSpringApplication): Started MidPointSpringApplication in" \${3}/pod-mp.log) -gt 0 ] && logItemFound=1
[ \$(grep -c "is waiting to start: image can't be pulled" \${3}/pod-mp.errlog) -gt 0 ] && logItemFound=2
[ \$(grep -c "midPoint to start" \${3}/pod-mp.log) -gt 0 ] && logItemFound=3
[ \$(grep -c "midPoint did not start" \${3}/pod-mp.log) -gt 0 ] && logItemFound=4
iteration=\$(( \${iteration} + 1 ))
done
if [ -s \${3}/pod-mp.errlog ]
then
echo " - - - - error log - - - -"
if [ \$(cat \${3}/pod-mp.errlog | wc -l) -gt 20 ]
then
head \${3}/pod-mp.errlog
echo ". . ."
tail \${3}/pod-mp.errlog
else
cat \${3}/pod-mp.errlog
fi
fi
if [ -s \${3}/pod-mp.log ]
then
echo " - - - - log - - - -"
if [ \$(cat \${3}/pod-mp.log | wc -l) -gt 20 ]
then
head \${3}/pod-mp.log
echo ". . ."
tail \${3}/pod-mp.log
else
cat \${3}/pod-mp.log
fi
fi
echo " - - - -"
case \${logItemFound} in
0)
echo "-- : Time out happen..."
return 1
;;
1)
echo "OK : Application is UP"
;;
2)
echo "ER : The image can't be pulled"
return 1
;;
*)
echo "ER : Something is wrong..."
return 1
;;
esac
return 0
}
function checkGenPass {
if [ \$( grep -c "Please change administrator password after first login." \${1} ) -gt 0 ]
then
mppw="\$(grep "Administrator initial password" \${1} | sed 's/[^"]*"\\(.*\\)"[^"]*/\\1/')"
if [ -z "\${mppw}" ]
then
mppw="\${2:-Test5ecr3t}"
fi
else
mppw="5ecr3t"
fi
echo "\${mppw}"
return 0
}
function healthCheck {
iteration=0
status="\$(curl -s -f http://\${1}:\${2}/midpoint/actuator/health | tr -d '[:space:]' | sed "s|{\\"status\\":\\"\\([^\\"]*\\)\\"}|\\1|")"
while [ \${iteration} -lt \${waitCycle} -a "\${status}" != "UP" ]
do
sleep 5
status="\$(curl -s -f http://\${1}:\${2}/midpoint/actuator/health | tr -d '[:space:]' | sed "s|{\\"status\\":\\"\\([^\\"]*\\)\\"}|\\1|")"
iteration=\$(( \${iteration} + 1 ))
done
if [ "\${status}" == "UP" ]
then
echo "OK : Health: \${status}"
else
echo "ER : Health: \${status}"
return 1
fi
return 0
}
function addUser {
curl -s --user "administrator:\${4:-5ecr3t}" -H "Content-Type: application/xml" -X POST -d "<user><name>\${3}</name></user>" "http://\${1}:\${2}/midpoint/ws/rest/users"
}
function checkUserExists {
suffix="-\$(ls -1 \${3}/pod-users*.lst 2>/dev/null | wc -l)"
curl -s --user "administrator:\${7:-5ecr3t}" -H "Content-Type: application/xml" -X GET "http://\${1}:\${2}/midpoint/ws/rest/users" | grep "<apti\\|<name>" | paste - - | sed "s|.*oid=\\"\\([^\\"]*\\)\\".*<name>\\([^<]*\\)</name.*|\\1:\\2:|" > \${3}/pod-users\${suffix}.lst
case \${4} in
oid)
if [ \$(grep "^\${5}:" \${3}/pod-users\${suffix}.lst | wc -l) -gt 0 ]
then
if [ "\${6:-}" == "" ]
then
echo "OK : User with OID \${5} exists... (\$(grep "^\${5}:" \${3}/pod-users\${suffix}.lst | cut -d ":" -f 2))"
else
if [ \$(grep "^\${5}:\${6}" \${3}/pod-users\${suffix}.lst | wc -l) -gt 0 ]
then
echo "OK : User with OID \${5} and name \${6} exists... (\$(grep "^\${5}:\${6}" \${3}/pod-users\${suffix}.lst | tr ":" " "))"
else
echo "ER : User with OID \${5} and name \${6} does not exist..."
return 1
fi
fi
else
echo "ER : User with OID \${5} does not exist..."
return 1
fi
;;
name)
if [ \$(grep ":\${5}:" \${3}/pod-users\${suffix}.lst | wc -l) -gt 0 ]
then
echo "OK : User \${5} exists... (\$(grep ":\${5}:" \${3}/pod-users\${suffix}.lst | cut -d ":" -f 1))"
else
echo "ER : User \${5} does not exist..."
return 1
fi
;;
esac
return 0
}
echo "1" > logs-\${timestamp}/test-result-h2
error=0
phase=1
mkdir logs-\${timestamp}/h2/\${phase}
echo
createPVC jenkins test-mp-\${timestamp} 1 | tee logs-\${timestamp}/h2/pvc
createMPPod jenkins test-mp \${timestamp} test-mp-\${timestamp} | tee logs-\${timestamp}/h2/\${phase}/pod
pvcname="\$(cat logs-\${timestamp}/h2/pvc)"
podname="\$(cat logs-\${timestamp}/h2/\${phase}/pod)"
podIPs="\$(kubectl get -n jenkins \${podname} -o=jsonpath="{.status.podIP}{':'}{.status.hostIP}")"
while [ "\${podIPs:0:1}" == ":" ]
do
sleep 5
podIPs="\$(kubectl get -n jenkins \${podname} -o=jsonpath="{.status.podIP}{':'}{.status.hostIP}")"
done
podIP="\$(echo -n \${podIPs} | cut -d : -f 1)"
hostIP="\$(echo -n \${podIPs} | cut -d : -f 2)"
echo
kubectl get -n jenkins \${podname} -o=jsonpath="{range .status.conditions[*]}{.type}{': '}{.status}{'\\n'}{end}"
echo -e "\\nPod IP: \${podIP}\\nHost IP: \${hostIP}\\n"
echo -e "\\nWait to Application get up..."
if [ \${error} -ne 0 ]
then
echo -e "\\tSkipped due to the previous error in the tests..."
else
checkApp jenkins \${podname} logs-\${timestamp}/h2/\${phase}
error=\$?
fi
echo -e "\\nHealth Check Test..."
if [ \${error} -ne 0 ]
then
echo -e "\\tSkipped due to the previous error in the tests..."
else
healthCheck \${podIP} 8080
error=\$?
fi
mppw="\$(checkGenPass logs-\${timestamp}/h2/\${phase}/pod-mp.log Test5ecr3t)"
echo "Administrator Password: \\"\${mppw}\\""
echo -e "\\nGet 'administrator' Test..."
if [ \${error} -ne 0 ]
then
echo -e "\\tSkipped due to the previous error in the tests..."
else
checkUserExists \${podIP} 8080 logs-\${timestamp}/h2/\${phase}/ oid 00000000-0000-0000-0000-000000000002 administrator "\${mppw}"
error=\$?
fi
echo -e "\\nAdd and test 'test110' user Test..."
if [ \${error} -ne 0 ]
then
echo -e "\\tSkipped due to the previous error in the tests..."
else
addUser \${podIP} 8080 test110 \${mppw}
checkUserExists \${podIP} 8080 logs-\${timestamp}/h2/\${phase}/ name test110 - "\${mppw}"
error=\$?
fi
echo " = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="
echo -e "\\nCheck repository preserved between restarts..."
if [ \${error} -ne 0 ]
then
echo -e "\\tSkipped due to the previous error in the tests..."
else
echo
kubectl logs -n jenkins \${podname} > logs-\${timestamp}/h2/\${phase}/pod-full.log
kubectl exec -n jenkins \${podname} -- ls -lah /opt/midpoint/var/midpoint.mv.db
kubectl delete -n jenkins \${podname}
phase=\$(( \${phase} + 1 ))
mkdir logs-\${timestamp}/h2/\${phase}
createMPPod jenkins test-mp \${timestamp} test-mp-\${timestamp} | tee logs-\${timestamp}/h2/\${phase}/pod
podIPs="\$(kubectl get -n jenkins \${podname} -o=jsonpath="{.status.podIP}{':'}{.status.hostIP}")"
while [ "\${podIPs:0:1}" == ":" ]
do
sleep 5
podIPs="\$(kubectl get -n jenkins \${podname} -o=jsonpath="{.status.podIP}{':'}{.status.hostIP}")"
done
podIP="\$(echo -n \${podIPs} | cut -d : -f 1)"
hostIP="\$(echo -n \${podIPs} | cut -d : -f 2)"
echo
kubectl get -n jenkins \${podname} -o=jsonpath="{range .status.conditions[*]}{.type}{': '}{.status}{'\\n'}{end}"
echo -e "\\nPod IP: \${podIP}\\nHost IP: \${hostIP}\\n"
echo -e "\\nWait to Application get up..."
if [ \${error} -ne 0 ]
then
echo -e "\\tSkipped due to the previous error in the tests..."
else
checkApp jenkins \${podname} logs-\${timestamp}/h2/\${phase}
error=\$?
fi
kubectl exec -n jenkins \${podname} -- ls -lah /opt/midpoint/var/midpoint.mv.db
echo -e "\\nHealth Check Test..."
if [ \${error} -ne 0 ]
then
echo -e "\\tSkipped due to the previous error in the tests..."
else
healthCheck \${podIP} 8080
error=\$?
fi
echo -e "\\n'test110' user Test..."
if [ \${error} -ne 0 ]
then
echo -e "\\tSkipped due to the previous error in the tests..."
else
checkUserExists \${podIP} 8080 logs-\${timestamp}/h2/\${phase}/ name test110 - "\${mppw}"
error=\$?
fi
fi
echo " = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="
[ \${error} -eq 0 ] && echo "OK : repository preserved between restarts"
if [ \${error} -eq 0 ]
then
echo -e "\\n\\tALL tests were OK...\n"
else
echo -e "\\n\\tThere were error during the test...\n"
fi
echo \${error} > logs-\${timestamp}/test-result-h2
kubectl logs -n jenkins \${podname} > logs-\${timestamp}/h2/\${phase}/pod-full.log
kubectl delete -n jenkins \${podname} \${pvcname}
grep -B 8 -A 4 "^ Version" logs-\${timestamp}/h2/\${phase}/pod-full.log
[ \${error} -ne 0 ] && exit 1
exit 0
"""
} catch (err) {
echo "Caught: ${err}"
unstable 'Error during H2 tests'
}
}
}
stage ("test-native") {
container ("kubectl") {
try {
if (skipNativeTest) {
sh """#!/bin/bash
timestamp="\$(cat timestamp)"
echo 0 > logs-\${timestamp}/test-result-native
"""
echo "Native tests are not required"
return
} else {
echo "Processing the Native test..."
}
sh """#!/bin/bash
#legacy check
docTag='${DOCKERTAG}'
if [ "\${docTag:0:3}" == "4.0" ]
then
echo "Native test is not relevant in case of 4.0 branch..."
exit 0
fi
timestamp="\$(cat timestamp)"
#In each cycle there is sleep time set to 5 seconds
waitCycle=120
echo "1" > logs-\${timestamp}/test-result-native
error=0
mkdir logs-\${timestamp}/native
phase=1
mkdir logs-\${timestamp}/native/\${phase}
function createPVC {
cat <<EOF | kubectl apply -f - | grep created | sed "s|\\([^[:space:]]*\\) created|\\1|"
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: \${2}
namespace: \${1}
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: \${3:-5}\${4:-G}i
volumeMode: Filesystem
EOF
}
function createDBPod {
cat <<EOF | kubectl apply -f - | grep created | sed "s|\\([^[:space:]]*\\) created|\\1|"
apiVersion: v1
kind: Pod
metadata:
name: \${2}-\${3}
namespace: \${1}
labels:
app: \${2}-\${3}
type: test
spec:
volumes:
- name: pvc
persistentVolumeClaim:
claimName: \${4}
containers:
- name: postgresql
image: 'postgres:\${5:-13}-alpine'
ports:
- name: db
containerPort: 5432
protocol: TCP
env:
- name: POSTGRES_INITDB_ARGS
value: '--lc-collate=en_US.utf8 --lc-ctype=en_US.utf8'
- name: POSTGRES_USER
value: midpoint
- name: POSTGRES_PASSWORD
value: SuperSecretPassword007
volumeMounts:
- name: pvc
mountPath: /var/lib/postgresql/data
subPath: db-data
imagePullPolicy: IfNotPresent
restartPolicy: Always
EOF
}
function createMPPod {
if [ "\${5}" == "" ]
then
volDef="emptyDir: {}"
else
volDef="persistentVolumeClaim:
claimName: \${5}"
fi
cat <<EOF | kubectl apply -f - | grep created | sed "s|\\([^[:space:]]*\\) created|\\1|"
apiVersion: v1
kind: Pod
metadata:
name: \${2}-\${3}
namespace: \${1}
labels:
app: \${2}-\${3}
type: test
spec:
volumes:
- name: mpdata
\${volDef}
initContainers:
- name: mp-config-init
image: 'registry-\${3}.lab.evolveum.com/midpoint:build-${DOCKERTAG}-${IMAGEOS}-\${3}'
command: [ "/bin/bash", "-c" ]
args:
- cd /opt/midpoint ;
bin/midpoint.sh init-native ;
echo ' - - - < 4.8 ninja sh does not apply env var - - - ' ;
sed -i "/jdbcUrl/c\\<jdbcUrl>jdbc:postgresql://\${4}:5432/midpoint</jdbcUrl>" /opt/midpoint/var/config.xml ;
sed -i "/jdbcUsername/c\\<jdbcUsername>midpoint</jdbcUsername>" /opt/midpoint/var/config.xml ;
sed -i "/jdbcPassword/c\\<jdbcPassword>SuperSecretPassword007</jdbcPassword>" /opt/midpoint/var/config.xml ;
cat /opt/midpoint/var/config.xml ;
echo ' - - - - - - ' ;
bin/ninja.sh -B info >/dev/null 2>/tmp/ninja.log ;
grep -q "ERROR" /tmp/ninja.log && (
bin/ninja.sh -B run-sql --create --mode REPOSITORY ;
bin/ninja.sh -B run-sql --create --mode AUDIT
) ||
echo -e '\\n Repository init is not needed...' ;
env:
- name: MP_INIT_CFG
value: /opt/midpoint/var
- name: MP_SET_midpoint_repository_database
value: postgresql
- name: MP_SET_midpoint_repository_jdbcUsername
value: midpoint
- name: MP_SET_midpoint_repository_jdbcPassword
value: SuperSecretPassword007
- name: MP_SET_midpoint_repository_jdbcUrl
value: jdbc:postgresql://\${4}:5432/midpoint
volumeMounts:
- name: mpdata
mountPath: /opt/midpoint/var
imagePullPolicy: IfNotPresent
containers:
- name: mp
image: 'registry-\${3}.lab.evolveum.com/midpoint:build-${DOCKERTAG}-${IMAGEOS}-\${3}'
ports:
- name: gui
containerPort: 8080
protocol: TCP
env:
- name: MP_SET_midpoint_repository_database
value: postgresql
- name: MP_SET_midpoint_repository_jdbcUsername
value: midpoint
- name: MP_SET_midpoint_repository_jdbcPassword
value: SuperSecretPassword007
- name: MP_SET_midpoint_repository_jdbcUrl
value: jdbc:postgresql://\${4}:5432/midpoint
- name: MP_SET_midpoint_administrator_initialPassword
value: Test5ecr3t
- name: MP_UNSET_midpoint_repository_hibernateHbm2ddl
value: "1"
- name: MP_NO_ENV_COMPAT
value: "1"
volumeMounts:
- name: mpdata
mountPath: /opt/midpoint/var
imagePullPolicy: IfNotPresent
restartPolicy: Always
EOF
}
function checkApp {
iteration=0
logItemFound=0
while [ \${iteration} -le \${waitCycle} -a \${logItemFound} -eq 0 ]
do
sleep 5
kubectl logs -n \${1} \${2} > \${3}/pod-mp.log 2>\${3}/pod-mp.errlog
[ \$(grep -c "INFO (com.evolveum.midpoint.web.boot.MidPointSpringApplication): Started MidPointSpringApplication in" \${3}/pod-mp.log) -gt 0 ] && logItemFound=1
[ \$(grep -c "is waiting to start: image can't be pulled" \${3}/pod-mp.errlog) -gt 0 ] && logItemFound=2
[ \$(grep -c "midPoint to start" \${3}/pod-mp.log) -gt 0 ] && logItemFound=3
[ \$(grep -c "midPoint did not start" \${3}/pod-mp.log) -gt 0 ] && logItemFound=4
iteration=\$(( \${iteration} + 1 ))
done
if [ -s \${3}/pod-mp.errlog ]
then
echo " - - - - error log - - - -"
if [ \$(cat \${3}/pod-mp.errlog | wc -l) -gt 20 ]
then
head \${3}/pod-mp.errlog
echo ". . ."
tail \${3}/pod-mp.errlog
else
cat \${3}/pod-mp.errlog
fi
fi
if [ -s \${3}/pod-mp.errlog ]
then
echo " - - - - log - - - -"
if [ \$(cat \${3}/pod-mp.log | wc -l) -gt 20 ]
then
head \${3}/pod-mp.log
echo ". . ."
tail \${3}/pod-mp.log
else
cat \${3}/pod-mp.log
fi
fi
echo " - - - -"
case \${logItemFound} in
0)
echo "-- : Time out happen..."
return 1
;;
1)
echo "OK : Application is UP"
;;
2)
echo "ER : The image can't be pulled"
return 1
;;
*)
echo "ER : Something is wrong..."
return 1
;;
esac
return 0
}
function checkGenPass {
if [ \$( grep -c "Please change administrator password after first login." \${1} ) -gt 0 ]
then
mppw="\$(grep "Administrator initial password" \${1} | sed 's/[^"]*"\\(.*\\)"[^"]*/\\1/')"
if [ -z "\${mppw}" ]
then
mppw="\${2:-Test5ecr3t}"
fi
else
mppw="5ecr3t"
fi
echo "\${mppw}"
return 0
}
function checkDB {
iteration=0
logItemFound=0
echo -n "Processed : 0 lines of log..."
while [ \${iteration} -le \${waitCycle} -a \${logItemFound} -eq 0 ]
do
sleep 5
kubectl logs -n \${1} \${2} > \${3}/pod-db.log 2>\${3}/pod-db.errlog
echo -n -e "\rProcessed : \$(cat \${3}/pod-db.log | wc -l ) lines of log..."
[ \$(grep -c "LOG: database system is ready to accept connections" \${3}/pod-db.log) -gt \${4} ] && logItemFound=1
iteration=\$(( \${iteration} + 1 ))
done
echo
case \${logItemFound} in
0)
echo "-- : Time out happen..."
return 1
;;
1)
echo "OK : repository is UP"
if [ \${4} -gt 0 ]
then
echo " stats:"
grep "psql:/docker-entrypoint-initdb.d" \${3}/pod-db.log | cut -d ":" -f 4 | sort | uniq -c
fi
;;
*)
echo "ER : Something is wrong..."
return 1
;;
esac
return 0
}
function healthCheck {
iteration=0
status="\$(curl -s -f http://\${1}:\${2}/midpoint/actuator/health | tr -d '[:space:]' | sed "s|{\\"status\\":\\"\\([^\\"]*\\)\\"}|\\1|")"
while [ \${iteration} -lt \${waitCycle} -a "\${status}" != "UP" ]
do
sleep 5
status="\$(curl -s -f http://\${1}:\${2}/midpoint/actuator/health | tr -d '[:space:]' | sed "s|{\\"status\\":\\"\\([^\\"]*\\)\\"}|\\1|")"
iteration=\$(( \${iteration} + 1 ))
done
if [ "\${status}" == "UP" ]
then
echo "OK : Health: \${status}"
else
echo "ER : Health: \${status}"
return 1
fi
return 0
}
function addUser {
curl -s --user "administrator:\${4:-5ecr3t}" -H "Content-Type: application/xml" -X POST -d "<user><name>\${3}</name></user>" "http://\${1}:\${2}/midpoint/ws/rest/users"
}
function checkUserExists {
suffix="-\$(ls -1 \${3}/pod-users*.lst 2>/dev/null | wc -l)"
curl -s --user "administrator:\${7:-5ecr3t}" -H "Content-Type: application/xml" -X GET "http://\${1}:\${2}/midpoint/ws/rest/users" | grep "<apti\\|<name>" | paste - - | sed "s|.*oid=\\"\\([^\\"]*\\)\\".*<name>\\([^<]*\\)</name.*|\\1:\\2:|" > \${3}/pod-users\${suffix}.lst
case \${4} in
oid)
if [ \$(grep "^\${5}:" \${3}/pod-users\${suffix}.lst | wc -l) -gt 0 ]
then
if [ "\${6:-}" == "" ]
then
echo "OK : User with OID \${5} exists... (\$(grep "^\${5}:" \${3}/pod-users\${suffix}.lst | cut -d ":" -f 2))"
else
if [ \$(grep "^\${5}:\${6}" \${3}/pod-users\${suffix}.lst | wc -l) -gt 0 ]
then
echo "OK : User with OID \${5} and name \${6} exists... (\$(grep "^\${5}:\${6}" \${3}/pod-users\${suffix}.lst | tr ":" " "))"
else
echo "ER : User with OID \${5} and name \${6} does not exist..."
return 1
fi
fi
else
echo "ER : User with OID \${5} does not exist..."
return 1
fi
;;
name)
if [ \$(grep ":\${5}:" \${3}/pod-users\${suffix}.lst | wc -l) -gt 0 ]
then
echo "OK : User \${5} exists... (\$(grep ":\${5}:" \${3}/pod-users\${suffix}.lst | cut -d ":" -f 1))"
else
echo "ER : User \${5} does not exist..."
return 1
fi
;;
esac
return 0
}
echo
createPVC jenkins test-db-\${timestamp} 5 | tee logs-\${timestamp}/native/dbpvc
createDBPod jenkins test-db \${timestamp} test-db-\${timestamp} 13 | tee logs-\${timestamp}/native/\${phase}/dbpod
pvcdbname="\$(cat logs-\${timestamp}/native/dbpvc)"
poddbname="\$(cat logs-\${timestamp}/native/\${phase}/dbpod)"
podIPs="\$(kubectl get -n jenkins \${poddbname} -o=jsonpath="{.status.podIP}{':'}{.status.hostIP}")"
while [ "\${podIPs:0:1}" == ":" ]
do
sleep 5
podIPs="\$(kubectl get -n jenkins \${poddbname} -o=jsonpath="{.status.podIP}{':'}{.status.hostIP}")"
done
poddbIP="\$(echo -n \${podIPs} | cut -d : -f 1)"
hostdbIP="\$(echo -n \${podIPs} | cut -d : -f 2)"
echo
kubectl get -n jenkins \${poddbname} -o=jsonpath="{'initContainer exitCode: '}{.status.initContainerStatuses[0].state.terminated.exitCode}{'\\n\\n'}{range .status.conditions[*]}{.type}{': '}{.status}{'\\n'}{end}"
echo -e "\\nPod IP: \${poddbIP}\\nHost IP: \${hostdbIP}\\n"
echo -e "\\nWait to DB repository get up..."
if [ \${error} -ne 0 ]
then
echo -e "\\tSkipped due to the previous error in the tests..."