-
Notifications
You must be signed in to change notification settings - Fork 9
/
ghaf-parallel-hw-test.groovy
302 lines (292 loc) · 11.8 KB
/
ghaf-parallel-hw-test.groovy
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
#!/usr/bin/env groovy
// SPDX-FileCopyrightText: 2022-2024 TII (SSRC) and the Ghaf contributors
// SPDX-License-Identifier: Apache-2.0
////////////////////////////////////////////////////////////////////////////////
def REPO_URL = 'https://github.com/tiiuae/ci-test-automation/'
def DEF_LABEL = 'testagent'
def TMP_IMG_DIR = 'image'
def TMP_SIG_DIR = 'signature'
def CONF_FILE_PATH = '/etc/jenkins/test_config.json'
////////////////////////////////////////////////////////////////////////////////
def run_cmd(String cmd) {
// Run cmd returning stdout
return sh(script: cmd, returnStdout:true).trim()
}
def get_test_conf_property(String file_path, String device, String property) {
// Get the requested device property data from test_config.json file
def device_data = readJSON file: file_path
property_data = "${device_data['addresses'][device][property]}"
println "Got device '${device}' property '${property}' value: '${property_data}'"
return property_data
}
def ghaf_robot_test(String testname='boot') {
if (!env.DEVICE_TAG) {
sh "echo 'DEVICE_TAG not set'; exit 1"
}
if (!env.DEVICE_NAME) {
sh "echo 'DEVICE_NAME not set'; exit 1"
}
if (testname == 'turnoff') {
env.INCLUDE_TEST_TAGS = "${testname}"
} else {
env.INCLUDE_TEST_TAGS = "${testname}AND${env.DEVICE_TAG}"
}
// TODO: do we really need credentials to access the target devices?
// Target devices are connected to the testagent, which itself is
// only available over a private network. What is the risk
// we are protecting against by having additional authentication
// for the test devices?
// The current configuration requires additional manual configuration
// on the jenkins UI to add the following secrets:
withCredentials([
string(credentialsId: 'testagent-dut-pass', variable: 'DUT_PASS'),
string(credentialsId: 'testagent-plug-pass', variable: 'PLUG_PASS'),
string(credentialsId: 'testagent-switch-token', variable: 'SW_TOKEN'),
string(credentialsId: 'testagent-switch-secret', variable: 'SW_SECRET'),
string(credentialsId: 'testagent-wifi-ssid', variable: 'WIFI_SSID'),
string(credentialsId: 'testagent-wifi-password', variable: 'WIFI_PSWD'),
]) {
dir("Robot-Framework/test-suites") {
sh 'rm -f *.png output.xml report.html log.html'
// On failure, continue the pipeline execution
try {
// Pass the secrets to the shell as environment variables, as we
// don't want Groovy to interpolate them. Similary, we pass
// other variables as environment variables to shell.
// Ref: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation
sh '''
nix run .#ghaf-robot -- \
-v DEVICE:$DEVICE_NAME \
-v DEVICE_TYPE:$DEVICE_TAG \
-v LOGIN:ghaf \
-v PASSWORD:$DUT_PASS \
-v PLUG_USERNAME:ghaftester@gmail.com \
-v PLUG_PASSWORD:$PLUG_PASS \
-v SWITCH_TOKEN:$SW_TOKEN \
-v SWITCH_SECRET:$SW_SECRET \
-v BUILD_ID:${BUILD_NUMBER} \
-v TEST_WIFI_SSID:${WIFI_SSID} \
-v TEST_WIFI_PSWD:${WIFI_PSWD} \
-i $INCLUDE_TEST_TAGS .
'''
if (testname == 'boot') {
// Set an environment variable to indicate boot test passed
env.BOOT_PASSED = 'true'
}
} catch (Exception e) {
currentBuild.result = "FAILURE"
unstable("FAILED '${testname}': ${e.toString()}")
} finally {
// Move the test output (if any) to a subdirectory
sh """
rm -fr $testname; mkdir -p $testname
mv -f *.png output.xml report.html log.html $testname/ || true
"""
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
pipeline {
agent { label "${params.getOrDefault('LABEL', DEF_LABEL)}" }
options { timestamps () }
stages {
stage('Checkout') {
steps {
checkout scmGit(
branches: [[name: 'main']],
extensions: [cleanBeforeCheckout()],
userRemoteConfigs: [[url: REPO_URL]]
)
}
}
stage('Setup') {
steps {
script {
env.TEST_CONFIG_DIR = 'Robot-Framework/config'
if(!params.getOrDefault('TARGET', null)) {
println "Missing TARGET parameter"
sh "exit 1"
}
println "Using TARGET: ${params.TARGET}"
sh """
mkdir -p ${TEST_CONFIG_DIR}
rm -f ${TEST_CONFIG_DIR}/*.json
ln -sv ${CONF_FILE_PATH} ${TEST_CONFIG_DIR}
echo { \\\"Job\\\": \\\"${params.TARGET}\\\" } > ${TEST_CONFIG_DIR}/${BUILD_NUMBER}.json
ls -la ${TEST_CONFIG_DIR}
"""
if(!params.containsKey('DESC')) {
println "Missing DESC parameter, skip setting description"
} else {
currentBuild.description = "${params.DESC}"
}
env.TESTSET = params.getOrDefault('TESTSET', '_boot_')
println "Using TESTSET: ${env.TESTSET}"
}
}
}
stage('Image download') {
steps {
script {
if(!params.containsKey('IMG_URL')) {
println "Missing IMG_URL parameter"
sh "exit 1"
}
sh "rm -fr ${TMP_IMG_DIR}"
// Wget occasionally fails due to a failure in name lookup. Below is a
// hack to force re-try a few times before aborting. Wget options, such
// as --tries, --waitretry, --retry-connrefused, etc. do not help in case
// the failure is due to an issue in name resolution which is considered
// a fatal error. Therefore, we need to add the below retry loop.
// TODO: remove the below re-try loop when test network DNS works
// reliably.
sh """
retry=1
max_retry=3
while ! wget -nv --show-progress --progress=dot:giga -P ${TMP_IMG_DIR} ${params.IMG_URL};
do
if (( \$retry >= \$max_retry )); then
echo "wget failed after \$retry retries"
exit 1
fi
retry=\$(( \$retry + 1 ))
sleep 5
done
"""
img_relpath = run_cmd("find ${TMP_IMG_DIR} -type f -print -quit | grep .")
println "Downloaded image to workspace: ${img_relpath}"
// Verify signature using the tooling from: https://github.com/tiiuae/ci-yubi
sh "wget -nv -P ${TMP_SIG_DIR} ${params.IMG_URL}.sig"
sig_relpath = run_cmd("find ${TMP_SIG_DIR} -type f -print -quit | grep .")
println "Downloaded signature to workspace: ${sig_relpath}"
sh "nix run github:tiiuae/ci-yubi/bdb2dbf#verify -- --path ${img_relpath} --sigfile ${sig_relpath} --cert INT-Ghaf-Devenv-Image"
// Uncompress, keeping only the decompressed image file
if(img_relpath.endsWith("zst")) {
sh "zstd -dfv ${img_relpath} && rm ${img_relpath}"
}
sh "ls -la ${TMP_IMG_DIR}"
}
}
}
stage('Flash') {
steps {
script {
if(!params.getOrDefault('DEVICE_CONFIG_NAME', null)) {
println "Missing DEVICE_CONFIG_NAME parameter"
sh "exit 1"
}
// Determine the device name
if(params.DEVICE_CONFIG_NAME == "orin-agx") {
env.DEVICE_NAME = 'OrinAGX1'
} else if(params.DEVICE_CONFIG_NAME == "orin-nx") {
env.DEVICE_NAME = 'OrinNX1'
} else if(params.DEVICE_CONFIG_NAME == "lenovo-x1") {
env.DEVICE_NAME = 'LenovoX1-1'
} else if(params.DEVICE_CONFIG_NAME == "nuc") {
env.DEVICE_NAME = 'NUC1'
} else if(params.DEVICE_CONFIG_NAME == "riscv") {
env.DEVICE_NAME = 'Polarfire1'
} else {
println "Error: unsupported device config '${params.DEVICE_CONFIG_NAME}'"
sh "exit 1"
}
// Determine mount commands
if(params.DEVICE_CONFIG_NAME == "riscv") {
muxport = get_test_conf_property(CONF_FILE_PATH, env.DEVICE_NAME, 'usb_sd_mux_port')
dgrep = 'sdmux'
mount_cmd = "/run/wrappers/bin/sudo usbsdmux ${muxport} host; sleep 10"
unmount_cmd = "/run/wrappers/bin/sudo usbsdmux ${muxport} dut"
} else {
serial = get_test_conf_property(CONF_FILE_PATH, env.DEVICE_NAME, 'usbhub_serial')
mount_cmd = "/run/wrappers/bin/sudo AcronameHubCLI -u 0 -s ${serial}; sleep 10"
unmount_cmd = "/run/wrappers/bin/sudo AcronameHubCLI -u 1 -s ${serial}"
}
env.DEVICE_TAG = params.DEVICE_CONFIG_NAME
// Mount the target disk
sh "${mount_cmd}"
// Read the device name
dev = get_test_conf_property(CONF_FILE_PATH, env.DEVICE_NAME, 'ext_drive_by-id')
println "Using device '$dev'"
// Wipe possible ZFS leftovers, more details here:
// https://github.com/tiiuae/ghaf/blob/454b18bc/packages/installer/ghaf-installer.sh#L75
// TODO: use ghaf flashing scripts or installers?
if(params.DEVICE_CONFIG_NAME == "lenovo-x1") {
echo "Wiping filesystem..."
SECTOR = 512
MIB_TO_SECTORS = 20480
// Disk size in 512-byte sectors
SECTORS = sh(script: "/run/wrappers/bin/sudo blockdev --getsz /dev/disk/by-id/${dev}", returnStdout: true).trim()
// Unmount possible mounted filesystems
sh "sync; /run/wrappers/bin/sudo umount -q /dev/disk/by-id/${dev}* || true"
// Wipe first 10MiB of disk
sh "/run/wrappers/bin/sudo dd if=/dev/zero of=/dev/disk/by-id/${dev} bs=${SECTOR} count=${MIB_TO_SECTORS} conv=fsync status=none"
// Wipe last 10MiB of disk
sh "/run/wrappers/bin/sudo dd if=/dev/zero of=/dev/disk/by-id/${dev} bs=${SECTOR} count=${MIB_TO_SECTORS} seek=\$(( ${SECTORS} - ${MIB_TO_SECTORS} )) conv=fsync status=none"
}
// Write the image
img_relpath = run_cmd("find ${TMP_IMG_DIR} -type f -print -quit | grep .")
println "Using image '$img_relpath'"
sh "/run/wrappers/bin/sudo dd if=${img_relpath} of=/dev/disk/by-id/${dev} bs=1M status=progress conv=fsync"
// Unmount
sh "${unmount_cmd}"
}
}
}
stage('Boot test') {
when { expression { env.TESTSET.contains('_boot_')} }
steps {
script {
env.BOOT_PASSED = 'false'
ghaf_robot_test('boot')
println "Boot test passed: ${env.BOOT_PASSED}"
}
}
}
stage('Bat test') {
when { expression { env.BOOT_PASSED == 'true' && env.TESTSET.contains('_bat_')} }
steps {
script {
ghaf_robot_test('bat')
}
}
}
stage('Perf test') {
when { expression { env.BOOT_PASSED == 'true' && env.TESTSET.contains('_perf_')} }
steps {
script {
ghaf_robot_test('performance')
}
}
}
stage('Turn off') {
steps {
script {
ghaf_robot_test('turnoff')
}
}
}
}
post {
always {
// Archive Robot-Framework results as artifacts
archiveArtifacts allowEmptyArchive: true, artifacts: 'Robot-Framework/test-suites/**/*.html, Robot-Framework/test-suites/**/*.xml, Robot-Framework/test-suites/**/*.png'
// Publish all results under Robot-Framework/test-suites subfolders
step(
[$class: 'RobotPublisher',
archiveDirName: 'robot-plugin',
outputPath: 'Robot-Framework/test-suites',
outputFileName: '**/output.xml',
otherFiles: '**/*.png',
disableArchiveOutput: false,
reportFileName: '**/report.html',
logFileName: '**/log.html',
passThreshold: 0,
unstableThreshold: 0,
onlyCritical: true,
]
)
}
}
}
////////////////////////////////////////////////////////////////////////////////