-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.sh
159 lines (113 loc) · 3.95 KB
/
pipeline.sh
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
#!/bin/bash
# Pull image from Docker Hub, line commented as image will be downloaded anyway with next command
# if there is no local copy
# docker pull myproject/qaauto:latest
# Missing port options, without port(-p) application will not be accessible,
# We don't know on which port (80,443 or some other port) application is expecting traffic
#Also I give name for running container as is easier to work with names then with hash values
docker run -d --name qaauto -v qalogs:/qaauto/logs myproject/qaauto:latest
# I assumed host has public ip address
# with cURL command we will fetch host's public ip address,
# in case of private ip address, we will use regularex to find output
# from ifconfig or ip add show
QUAATO_IP=curl ifconfig.me
#This command will give output of ip address from eth0 NIC,
#also we can use if our host use private ip address
#QUAATO_IP=$(ip -f inet -o addr show eth0 | cut -d\ -f 7 | cut -d/ -f 1)
# With docker exec we will run test automatisation inside of running container
docker exec -it qaauto /qhauto/runauto.sh
# We are checking file qhauto-$(date +%Y-%m-%d).log is created or not, as we use volumes,
#we will be able to access file from docker host via /var/lib/docker/volumes/galogs_data/ folder
##############Vars#############
#Location of log file which is used inside of running container
FILENAME=/var/lib/docker/volumes/qalogs/_data/qhauto-$(date +%Y-%m-%d).log
##############Vars#############
##########Functions############
#In this function we can implement return codes for exit, in that case we will know why script exit
function stopremove {
docker stop qaauto
docker container rm qaauto
docker rmi qaauto:latest
echo "Docker container is stopped, and image have been removed"
exit
}
##########Functions############
#this if statement is verifying existence of a log file, and is it from today
if [ -f "$FILENAME" ]
then
echo "Log file have been created, TEST PASSED"
else
echo "Log file not created, TEST FAILED!!!"
stopremove
fi
#Checking how many bytes log file has
#0 (zero) means, file is empty
#everything else, mean file is NOT empty
# FYI word FAIL has 5 bytes
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of log file: $FILENAME is $FILESIZE byte"
if [ "$FILESIZE" -eq "0" ]
then
echo "file is empty, TEST FAILED!!!, "
stopremove
else
echo "file is NOT empty"
fi
#Checking if string FAIL exists in log file
if grep -q "FAIL" "$FILENAME"
then
echo "Failure, TEST FAILED!!!"
stopremove
else
echo "Success"
fi
#END OF THE SCRIPT
stopremove
#Second part of the task
#=====================================================================================================
For this deployment I will use K8S workload CronJob, with Persistant Volume CLAME YAML files are below:
#nano qaauto-log-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: qaauto
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
# nano qaauto.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: qaauto
spec:
schedule: "* 2 * * *" ## Every night at 2AM
jobTemplate:
spec:
template:
spec:
containers:
- name: qaauto
image: myproject/qaauto:latest
imagePullPolicy: IfNotPresent
command: ["/qhauto/runauto.sh"]
volumeMounts:
- name: qaauto
mountPath: /qaauto/logs
restartPolicy: OnFailure #or maybe is better Never, as is for testing
volumes:
- name: qaauto
persistentVolumeClaim:
claimName: qaauto-log-pvc
# We will create CronJob with command from below
kubectl apply -f qaauto.yaml
#We will create PVC for container
kubectl apply -f qaauto-log-pvc.yaml
#To verify is it job created
kubectl get cronjob qaauto
#End when we want to remove this job, we can use command from below
kubectl delete pvc qaauto-log-pvc.yaml
kubectl delete cronjob qaauto
#END