forked from metal3-io/metal3-dev-env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_test.sh
executable file
·217 lines (181 loc) · 5.7 KB
/
05_test.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
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
#!/usr/bin/env bash
set -u
# shellcheck disable=SC1091
source lib/logging.sh
# shellcheck disable=SC1091
source lib/common.sh
FAILS=0
SKIP_RETRIES_USER="${SKIP_RETRIES:-false}"
MACHINES_LIST="centos-0 centos-1"
#
# Tries to run a command over ssh in the machine
#
# Inputs:
# - the ssh user
# - the server ip or domain
# - The baremetal host name
#
ssh_to_machine() {
local USER SERVER MACHINE_NAME
USER="${1:?}"
SERVER="${2:?}"
MACHINE_NAME="${3:?}"
RESULT_STR="${MACHINE_NAME} baremetal host reachable by ssh"
ssh -o ConnectTimeout=2 \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
"${USER}"@"${SERVER}" echo "SSH to host is up" > /dev/null 2>&1
process_status "$?"
return "$?"
}
#
# Checks the provisioning status of the baremetal host
#
# Inputs:
# - machine name
# - Baremetal host name
# - Expected state
#
check_provisioning_status(){
local MACHINE_NAME="${1}"
local BMH_NAME="${2}"
local FAILS_CHECK
local EXPECTED_STATE="${3}"
FAILS_CHECK="${FAILS}"
BMH="$(kubectl get bmh -n metal3 -o json "${BMH_NAME}")"
RESULT_STR="${MACHINE_NAME} baremetal host in correct state : ${EXPECTED_STATE}"
equals "$(echo "${BMH}" | jq -r '.status.provisioning.state')" \
"${EXPECTED_STATE}"
return "$((FAILS-FAILS_CHECK))"
}
#
# Check that the machine and baremetal host CRs are cross-referencing
#
# Inputs:
# - Machine name
# - Baremetal host name
#
check_bmh_association(){
local MACHINE_NAME="${1}"
local BMH_NAME
local FAILS_CHECK
FAILS_CHECK="${FAILS}"
MACHINE="$(kubectl get machine -n metal3 -o json "${MACHINE_NAME}")"
BMH_NAME="$(echo "${MACHINE}" | \
jq -r '.metadata.annotations["metal3.io/BareMetalHost"]' | \
tr '/' ' ' | awk '{print $2}')"
RESULT_STR="${MACHINE_NAME} Baremetalhost associated"
is_in "${BMH_NAME}" "master-0 worker-0"
# Fail fast if they are not associated as we cannot get the BMH name
if [[ "${FAILS_CHECK}" != "${FAILS}" ]]; then
return 1
fi
# Verify the existence of the bmh
RESULT_STR="${BMH_NAME} baremetal host CR exist"
BMH="$(kubectl get bmh -n metal3 -o json "${BMH_NAME}")"
process_status $?
# Check the consumer ref
RESULT_STR="${MACHINE_NAME} Baremetal host correct consumer ref"
equals "$(echo "${BMH}" | jq -r '.spec.consumerRef.name')" \
"${MACHINE_NAME}"
return "$((FAILS-FAILS_CHECK))"
}
#
# Checks the status fields of a baremetal host
#
# Inputs:
# - machine name
# - baremetal host name
# - the expected content of the image field (URL)
# - the expected content of the provisioned status
#
check_bmh_status(){
local MACHINE_NAME="${1}"
local BMH_NAME="${2}"
local IMAGE_NAME="${3}"
local PROVISIONED_STATUS="${4}"
local FAILS_CHECK
FAILS_CHECK="${FAILS}"
# Verify the provisioning state of the BMH
iterate check_provisioning_status "${MACHINE_NAME}" "${BMH_NAME}" \
"${PROVISIONED_STATUS}"
BMH="$(kubectl get bmh -n metal3 -o json "${BMH_NAME}")"
#Check the image
RESULT_STR="${MACHINE_NAME} Baremetal host correct image"
equals "$(echo "${BMH}" | jq -r '.spec.image.url')" \
"${IMAGE_NAME}"
# Check the error message and operational status
RESULT_STR="${MACHINE_NAME} Baremetal host no error message"
equals "$(echo "${BMH}" | jq -r '.status.errorMessage')" ""
RESULT_STR="${MACHINE_NAME} Baremetal host operational status ok"
equals "$(echo "${BMH}" | jq -r '.status.operationalStatus')" "OK"
return "$((FAILS-FAILS_CHECK))"
}
#
# Get the IP of a vm from the dhcp leases
#
# Inputs:
# - baremetal host name
#
get_vm_ip(){
local BMH_NAME="${1}"
sudo virsh net-dhcp-leases baremetal | grep "${BMH_NAME}" | awk '{print $5}' \
| cut -d '/' -f1
}
# provision the machines
for name in $MACHINES_LIST; do
# Create the machines
RESULT_STR="${name} machine CR created"
./create_machine.sh "${name}" > /dev/null
process_status "$?" || SKIP_RETRIES=true
done
# Test provisioning
for name in $MACHINES_LIST; do
# Verify the machine CR exists
RESULT_STR="${name} machine CR exist"
kubectl get machine -n metal3 -o json "${name}" > /dev/null
process_status "$?" || SKIP_RETRIES=true
#Verify that the machine has a bmh associated
iterate check_bmh_association "${name}" || SKIP_RETRIES=true
# Get the machine and BMH
MACHINE="$(kubectl get machine -n metal3 -o json "${name}")"
BMH_NAME="$(echo "${MACHINE}" | \
jq -r '.metadata.annotations["metal3.io/BareMetalHost"]' | \
tr '/' ' ' | awk '{print $2}')"
BMH="$(kubectl get bmh -n metal3 -o json "${BMH_NAME}")"
# shellcheck disable=SC2181
[[ "$?" == 0 ]] || SKIP_RETRIES=true
# Check the baremetal hosts status fields
check_bmh_status "${name}" "${BMH_NAME}" "$(echo "${MACHINE}" | \
jq -r '.spec.providerSpec.value.image.url')" "provisioned"
# Get the IP of the BMH
RESULT_STR="${name} get baremetal host IP"
VM_IP="$(get_vm_ip "${BMH_NAME}")"
process_status "$?" || SKIP_RETRIES=true
# Check ssh connection to BMH
if iterate ssh_to_machine "centos" "${VM_IP}" "${name}"; then
SKIP_RETRIES=true
fi
done
SKIP_RETRIES="${SKIP_RETRIES_USER}"
#Test deprovisioning
for name in $MACHINES_LIST; do
# Get the machine and BMH
MACHINE="$(kubectl get machine -n metal3 -o json "${name}")"
BMH_NAME="$(echo "${MACHINE}" | \
jq -r '.metadata.annotations["metal3.io/BareMetalHost"]' | \
tr '/' ' ' | awk '{print $2}')"
# shellcheck disable=SC2181
[[ "$?" == 0 ]] || SKIP_RETRIES=true
# Deprovision the machine
# shellcheck disable=SC2034
RESULT_STR="${name} machine CR deleted"
kubectl delete machine -n metal3 "${name}" > /dev/null
process_status $?
# Check the status fields of the BMH previously associated
if check_bmh_status "${name}" "${BMH_NAME}" "null" "ready"; then
SKIP_RETRIES=true
fi
done
echo -e "\nNumber of failures : $FAILS"
exit "${FAILS}"