1+ #! /bin/bash
2+ set -o nounset
3+ set -o errexit
4+
5+ DEVFILE_PATH=${DEVFILE_PATH:- " $( pwd) /devfile.yaml" }
6+
7+ REGISTRY_PATH=${REGISTRY_PATH:- " ../registry" }
8+
9+ BIN_NAME=${BIN_NAME:- " flatten-parent" }
10+ NON_TERMINATING_MODULE_BIN=" ${REGISTRY_PATH} /tests/check_non_terminating/$BIN_NAME "
11+
12+ replaceVariables () {
13+ image=$1
14+ VAR_KEYS=(liberty-version)
15+ VAR_VALUES=(22.0.0.1)
16+
17+ for i in " ${! VAR_KEYS[@]} " ; do
18+ key=' {{'
19+ key+=${VAR_KEYS[i]}
20+ key+=' }}'
21+ value=${VAR_VALUES[i]}
22+ image=${image/ ${key} / ${value} }
23+ done
24+ echo " $image "
25+ }
26+
27+ getContainerComponentsNum () {
28+ devfilePath=$1
29+ component_num=$( $YQ_PATH eval ' [ .components[] | select(has("container")) ] | length' " $devfilePath " -r)
30+ echo " ${component_num} "
31+ }
32+
33+ getName () {
34+ devfilePath=$1
35+ name=$( $YQ_PATH eval ' .metadata.name' " $devfilePath " -r)
36+ echo " ${name} "
37+ }
38+
39+ getFirstContainerComponentImage () {
40+ devfilePath=$1
41+
42+ image_original=$( $YQ_PATH eval ' [ .components[] | select(has("container")) ] | .[0].container.image' " $devfilePath " -r)
43+ image_processed=$( replaceVariables " ${image_original} " )
44+ echo " ${image_processed} "
45+ }
46+
47+ getFirstContainerComponentCommand () {
48+ devfilePath=$1
49+ local _gfccc_command=()
50+ local _gfccc_command_string=()
51+
52+ IFS=" " read -r -a _gfccc_command_string <<< " $($YQ_PATH eval '[ .components[] | select(has(" container" )) ] | .[0].container.command[]? + " " ' " $devfilePath " -r | paste -s -d '\0' -)"
53+ if (( ${# _gfccc_command_string[@]} == 0 )) ; then
54+ echo " "
55+ else
56+ for command_word in " ${_gfccc_command_string[@]} " ; do
57+ _gfccc_command+=(" ${command_word} " )
58+ done
59+ echo " ${_gfccc_command[@]} "
60+ fi
61+ }
62+
63+ getFirstContainerComponentArgs () {
64+ devfilePath=$1
65+ local _gfcca_args=()
66+ local _gfcca_args_string=()
67+
68+ IFS=" " read -r -a _gfcca_args_string <<< " $($YQ_PATH eval '[ .components[] | select(has(" container" )) ] | .[0].container.args[]? + " " ' " $devfilePath " -r | paste -s -d '\0' -)"
69+ if (( ${# _gfcca_args_string[@]} == 0 )) ; then
70+ echo " "
71+ else
72+ for arg in " ${_gfcca_args_string[@]} " ; do
73+ _gfcca_args+=(" ${arg} " )
74+ done
75+ echo " ${_gfcca_args[@]} "
76+ fi
77+ }
78+
79+ isNonTerminating () {
80+ _int_image=$1
81+ _int_command=(" $2 " )
82+ _int_command_args=(" $3 " )
83+
84+ timeout_in_sec=240 # <== includes image pulling
85+
86+ # workaround: cri-dockerd v0.2.6+ fixes a timeout issue where large images are not being pulled
87+ # this can be removed when actions-setup-minikube updates cri-dockerd
88+ if [ " $ENV " = " minikube" ]; then
89+ echo " COMMAND: minikube ssh docker pull $_int_image "
90+ minikube ssh docker pull $_int_image > /dev/null 2>&1
91+ fi
92+
93+ echo " PARAMS: image --> $_int_image , command --> ${_int_command[*]} , args --> ${_int_command_args[*]} "
94+
95+ if [ " ${_int_command[*]} " == " null" ] && [ " ${_int_command_args[*]} " == " null" ]; then
96+ echo " COMMAND: \" kubectl run test-terminating -n ${TEST_NAMESPACE} --attach=false --restart=Never --image=$_int_image \" "
97+ kubectl run test-terminating -n " ${TEST_NAMESPACE} " --attach=false --restart=Never --image=" $_int_image " > /dev/null 2>&1
98+ elif [ " ${_int_command[*]} " == " null" ]; then
99+ echo " COMMAND: \" kubectl run test-terminating -n ${TEST_NAMESPACE} --attach=false --restart=Never --image=$_int_image -- ${_int_command_args[*]} \" "
100+ kubectl run test-terminating -n " ${TEST_NAMESPACE} " --attach=false --restart=Never --image=" $_int_image " -- ${_int_command_args[*]} > /dev/null 2>&1
101+ elif [ " ${_int_command_args[*]} " == " null" ]; then
102+ echo " COMMAND: \" kubectl run test-terminating -n ${TEST_NAMESPACE} --attach=false --restart=Never --image=$_int_image --command -- ${_int_command[*]} \" "
103+ kubectl run test-terminating -n " ${TEST_NAMESPACE} " --attach=false --restart=Never --image=" $_int_image " --command=true -- ${_int_command[*]} > /dev/null 2>&1
104+ else
105+ echo " COMMAND: \" kubectl run test-terminating -n ${TEST_NAMESPACE} --attach=false --restart=Never --image=$_int_image --command -- ${_int_command[*]} ${_int_command_args[*]} \" "
106+ kubectl run test-terminating -n " ${TEST_NAMESPACE} " --attach=false --restart=Never --image=" $_int_image " --command=true -- ${_int_command[*]} ${_int_command_args[*]} > /dev/null 2>&1
107+ fi
108+
109+ if kubectl wait pods -n " ${TEST_NAMESPACE} " test-terminating --for condition=Ready --timeout=${timeout_in_sec} s > /dev/null 2>&1 ; then
110+ echo " SUCCESS: The container started successfully and didn't terminate"
111+ kubectl delete pod test-terminating -n " ${TEST_NAMESPACE} " > /dev/null 2>&1
112+
113+ return 0
114+ else
115+ echo " ERROR: Failed to reach \" Ready\" condition after $timeout_in_sec seconds"
116+ echo " ↓↓↓↓↓↓↓↓↓ Pod description ↓↓↓↓↓↓↓↓"
117+ echo " "
118+ kubectl describe pod -n " ${TEST_NAMESPACE} " test-terminating
119+ echo " "
120+ echo " ↑↑↑↑↑↑↑↑↑ Pod description ↑↑↑↑↑↑↑↑"
121+ kubectl delete pod test-terminating -n " ${TEST_NAMESPACE} " > /dev/null 2>&1
122+ return 1
123+ fi
124+ }
125+
126+ YQ_PATH=${YQ_PATH:- yq}
127+ TEST_NAMESPACE=${TEST_NAMESPACE:- default}
128+
129+ if [ -z " ${ENV:- } " ]; then
130+ ENV=minikube
131+ fi
132+
133+ if [ " $ENV " != " minikube" ] && [ " $ENV " != " openshift" ]; then
134+ echo " ERROR:: Allowed values for ENV are either \" minikube\" (default) or \" openshift\" ."
135+ exit 1
136+ fi
137+
138+ if [ ! -f " $NON_TERMINATING_MODULE_BIN " ]; then
139+ echo " ERROR: Go binary not found at $NON_TERMINATING_MODULE_BIN "
140+ echo " Please ensure the devfile/registry repository is cloned and the binary is built."
141+ exit 1
142+ fi
143+
144+ if [ ! -f " $DEVFILE_PATH " ]; then
145+ echo " ERROR: Devfile not found at path $DEVFILE_PATH "
146+ exit 1
147+ fi
148+
149+ echo " ======================="
150+ echo " Testing single sample: ${DEVFILE_PATH} "
151+
152+ # if devfile in path has a parent flatten it
153+ if $YQ_PATH eval ' has("parent")' " $DEVFILE_PATH " -r | grep -q " true" ; then
154+ echo " INFO:: Found parent for $DEVFILE_PATH "
155+ " $NON_TERMINATING_MODULE_BIN " " $DEVFILE_PATH "
156+ fi
157+
158+ IFS=" " read -r -a components_num <<< " $(getContainerComponentsNum " $DEVFILE_PATH " )"
159+
160+ # if there are zero components of type container skip
161+ if (( components_num == 0 )) ; then
162+ echo " WARNING: Devfile with no container component found (" " $DEVFILE_PATH " " ). Skipping."
163+ echo " ======================="
164+ exit 0
165+ fi
166+
167+ # if there is more than one component of type container skip (we may want to cover this case in the future)
168+ if (( components_num > 1 )) ; then
169+ echo " WARNING: Devfile with more than one container component found (" " $DEVFILE_PATH " " ). Skipping."
170+ echo " ======================="
171+ exit 0
172+ fi
173+
174+ name=$( getName " $DEVFILE_PATH " )
175+ image=$( getFirstContainerComponentImage " $DEVFILE_PATH " )
176+
177+ declare -a command=()
178+ IFS=" " read -r -a command <<< " $(getFirstContainerComponentCommand " $DEVFILE_PATH " )"
179+
180+ declare -a command_args=()
181+ IFS=" " read -r -a command_args <<< " $(getFirstContainerComponentArgs " $DEVFILE_PATH " )"
182+
183+ if (( ${# command[@]} > 0 )) ; then
184+ command_string=" ${command[*]} "
185+ else
186+ command_string=" null"
187+ fi
188+
189+ if (( ${# command_args[@]} > 0 )) ; then
190+ command_args_string=" ${command_args[*]} "
191+ else
192+ command_args_string=" null"
193+ fi
194+
195+ echo " Sample: $name "
196+ echo " Image: $image "
197+ echo " Command: $command_string "
198+ echo " Args: $command_args_string "
199+
200+ isNonTerminating " ${image} " " ${command_string} " " ${command_args_string} "
201+
202+ # remove image to save space
203+ if [ " $ENV " = " minikube" ]; then
204+ echo " COMMAND: \" minikube ssh -- docker image rm ${image} --force\" "
205+ minikube ssh -- docker image rm ${image} --force > /dev/null 2>&1
206+ fi
207+
208+ echo " ======================="
209+ echo " Non-terminating test completed successfully!"
210+
211+ exit 0
0 commit comments