File: tests.nested

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (488 lines) | stat: -rwxr-xr-x 12,826 bytes parent folder | download
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
#!/bin/bash -e

show_help() {
    echo "usage: prepare"
    echo "       restore"
    echo "       build-image IMAGE-TYPE"
    echo "       create-vm IMAGE-TYPE [--param-cdrom PARAM] [--param-cpus PARAM] [--param-mem PARAM]"
    echo "       is-nested [core|classic|uc16|uc18|uc20|uc22|uc24]"
    echo "       show [ATTRIBUTE]"
    echo "       vm <ACTION>"
    echo "       get <ARTIFACT>"
    echo "       download <REMOTE_FILE>"
    echo "       wait-for <EVENT>"
    echo "       boot-id"
    echo "       is-enabled <tpm|kvm|secboot>"
    echo "       secboot-sign <file|gadget> <PATH> <KEY> <CERT>"
    echo "       secboot-remove-signature <PATH>"
    echo "       transition <recovery-system> <system-mode>"
    echo "       snap-rev <snap-name> [channel]"
    echo "       build-seed <seed-dir> <seed-name> <label>"
    echo "       nested-system"
    echo ""
    echo "Available options:"
    echo "  -h --help   show this help message."
    echo ""
    echo "COMMANDS:"
    echo "  prepare:                    creates all the directories needed to run a nested test"
    echo "  restore:                    removes all the directories and data used by nested tests"
    echo "  build-image:                creates an image using ubuntu image tool"
    echo "  create-vm:                  creates new virtual machine and leave it running"
    echo "  is-nested:                  indicates if the execution is using a nested vm"
    echo "  vm:                         runs an action for the current vm"
    echo "  exec:                       executes a command in the vm"
    echo "  copy:                       copies a file to the vm"
    echo "  get:                        retrieves an artifact"
    echo "  download:                   download an artifact"
    echo "  wait-for:                   waits for the specified event"
    echo "  boot-id:                    prints the boot id"
    echo "  is-enabled:                 indicated if the feature is enabled"
    echo "  secboot-sign:               signs an artifact"
    echo "  secboot-remove-signature:   removes a signature"
    echo "  transition:                 transition to system mode (just available for uc20)"
    echo "  snap-rev:                   get the snap rev in the vm (prints the installed rev if no channel provided)"
    echo "  build-seed                  build a seed that can be used to boot a vm"
    echo "  prepare-essential-snaps     download and repack essential snaps (gadget, kernel, snapd) to include current snapd, and debug command lines"
    echo "  configure-default-user      add either cloudinit user or a system-user assertion that will be auto-imported"
    echo "  nested-system               retrieves the nested system used for spread nested tests"
    echo ""
    echo "IMAGE-TYPES:"
    echo "  core:    work with a core image"
    echo "  classic: work with a classic image"
    echo ""
    echo "ATTRIBUTE:"
    echo "  version: the version of the system i.e. 16, 18, 20, 22"
    echo ""
    echo "ACTION:"
    echo "  start:  starts a stopped vm"
    echo "  stop:   shutdowns a running vm"
    echo "  remove: removes a vm"
    echo "  status: retrieves status for a running vm"
    echo ""
    echo "ARTIFACT:"
    echo "  serial-log:       serial log for the nested vm"
    echo "  model-authority:  authority id for the current model"
    echo "  image-name:       then name of the image used for the test"
    echo "  current-name:     then name of the image being executed"
    echo "  extra-snaps-path: paths used to store extra snaps"
    echo "  assets-path:      path used to store vm assets"
    echo "  images-path:      path used to store vm images"
    echo ""
    echo "REMOTE_FILE:"
    echo "  snakeoil-key: snakeoil key"
    echo "  spread:       the spread binary"
    echo ""
    echo "EVENT:"
    echo "  ssh:                it is possible to ssh to the vm"
    echo "  no-ssh:             is not possible to ssh to the vm"
    echo "  snap-command:       snap command is available in the vm"
    echo "  reboot:             reboot is done in the vm"
    echo "  device-initialized: the device is initialized and has a serial"
    echo ""
}

prepare() {
    nested_prepare_env
}

restore() {
    nested_cleanup_env
}

build_image() {
    if [ $# -eq 0 ]; then
        show_help
        exit 1
    fi
    while [ $# -gt 0 ]; do
        case "$1" in
            classic)
                nested_create_classic_vm
                exit
                ;;
            core)
                nested_create_core_vm
                exit
                ;;
            *)
                echo "tests.nested: expected either classic or core as argument" >&2
                exit 1
                ;;
        esac
    done
}

create_vm() {
    if [ $# -eq 0 ]; then
        show_help
        exit 1
    fi
    local action=
    case "$1" in
        classic)
            shift 1
            action=nested_start_classic_vm
            ;;
        core)
            shift 1
            action=nested_start_core_vm
            ;;
        *)
            echo "tests.nested: unsupported parameter $1" >&2
            exit 1
            ;;
    esac

    local NESTED_PARAM_CD NESTED_CPUS NESTED_MEM NESTED_PARAM_EXTRA NESTED_KEEP_FIRMWARE_STATE NESTED_PASSPHRASE
    while [ $# -gt 0 ]; do
        case "$1" in
            --param-cdrom)
                NESTED_PARAM_CD="$2"
                shift 2
                ;;
            --param-mem)
                NESTED_MEM="$2"
                shift 2
                ;;
            --param-cpus)
                NESTED_CPUS="$2"
                shift 2
                ;;
            --extra-param)
                NESTED_PARAM_EXTRA="$2"
                shift 2
                ;;
            --keep-firmware-state)
                NESTED_KEEP_FIRMWARE_STATE=1
                shift
                ;;
            --passphrase)
                NESTED_PASSPHRASE="$2"
                shift 2
                ;;
            *)
                echo "tests.nested: unsupported parameter $1" >&2
                exit 1
                ;;
        esac
    done

    export NESTED_PARAM_CD NESTED_CPUS NESTED_MEM NESTED_PARAM_EXTRA NESTED_KEEP_FIRMWARE_STATE NESTED_PASSPHRASE
    "$action"
    unset NESTED_PARAM_CD NESTED_CPUS NESTED_MEM NESTED_PARAM_EXTRA NESTED_KEEP_FIRMWARE_STATE NESTED_PASSPHRASE
}

is_nested() {
    if [ $# -eq 0 ]; then
        nested_is_nested_system
    else
        case "$1" in
            core)
                nested_is_core_system
                ;;
            classic)
                nested_is_classic_system
                ;;
            uc16)
                nested_is_core_16_system
                ;;
            uc18)
                nested_is_core_18_system
                ;;
            uc20)
                nested_is_core_20_system
                ;;
            uc22)
                nested_is_core_22_system
                ;;
            uc24)
                nested_is_core_24_system
                ;;
            *)
                echo "tests.nested: parameter not supported: $1" >&2
                exit 1
                ;;
        esac
    fi
}

show() {
    if [ $# -eq 0 ]; then
        echo "tests.nested: attribute to show is required" >&2
        exit 1
    else
        case "$1" in
            version)
                nested_get_version
                ;;
            *)
                echo "tests.nested: attribute not supported: $1" >&2
                exit 1
                ;;
        esac
    fi
}

vm() {
    cmd="${1}"
    shift
    case "${cmd}" in
        start)
            nested_start
            ;;
        stop)
            nested_shutdown
            ;;
        remove)
            nested_destroy_vm
            ;;
        status)
            nested_status_vm
            ;;
        unclean-reboot)
            nested_force_restart_vm
            ;;
        clear-tpm)
            nested_vm_clear_tpm
            ;;
        clear-uefi)
            nested_vm_clear_uefi
            ;;
        set-passphrase)
            nested_vm_set_passphrase "$@"
            ;;
        set-recovery-key)
            nested_vm_set_recovery_key "$@"
            ;;
        *)
            echo "tests.nested: action not supported: $1" >&2
            exit 1
            ;;
    esac
}

get() {
    if [ $# -eq 0 ]; then
        show_help
        exit 1
    fi
    local action=
    case "$1" in
        serial-log)
            action=nested_print_serial_log
            shift
            ;;
        model-authority)
            action=nested_model_authority
            shift
            ;;
        image-name)
            action=nested_get_image_name
            shift
            ;;
        current-name)
            action=nested_get_current_image_name
            shift
            ;;
        extra-snaps-path)
            action=nested_get_extra_snaps_path
            shift
            ;;
        assets-path)
            action=nested_get_assets_path
            shift
            ;;
        images-path)
            action=nested_get_images_path
            shift
            ;;
        *)
            echo "tests.nested: unsupported parameter $1" >&2
            exit 1
            ;;
    esac

    "$action" "$@"
}

download() {
    if [ $# -eq 0 ]; then
        show_help
        exit 1
    fi
    local action=
    case "$1" in
        snakeoil-key)
            action=nested_get_snakeoil_key
            shift
            ;;
        spread)
            action=nested_fetch_spread
            shift
            ;;
        *)
            echo "tests.nested: unsupported parameter $1" >&2
            exit 1
            ;;
    esac

    "$action" "$@"
}

wait_for() {
    if [ $# -eq 0 ]; then
        show_help
        exit 1
    fi
    local action=
    case "$1" in
        ssh)
            action=nested_wait_for_ssh
            shift
            ;;
        no-ssh)
            action=nested_wait_for_no_ssh
            shift
            ;;
        snap-command)
            action=nested_wait_for_snap_command
            shift
            ;;
        reboot)
            action=nested_wait_for_reboot
            shift
            ;;
        device-initialized)
            action=nested_wait_for_device_initialized_change
            shift
            ;;
        *)
            echo "tests.nested: unsupported parameter $1" >&2
            exit 1
            ;;
    esac

    "$action" "$@"
}

boot_id() {
    nested_get_boot_id
}

is_enabled() {
    case "$1" in
        kvm)
            nested_is_kvm_enabled
            ;;
        tpm)
            nested_is_tpm_enabled
            ;;
        secboot)
            nested_is_secure_boot_enabled
            ;;
        *)
            echo "tests.nested: feature not supported: $1" >&2
            exit 1
            ;;
    esac
}

secboot_sign() {
    case "$1" in
        file)
            shift
            nested_secboot_sign_file "$@"
            ;;
        gadget)
            shift
            nested_secboot_sign_gadget "$@"
            ;;
        *)
            echo "tests.nested: action not supported: $1" >&2
            exit 1
            ;;
    esac
}

secboot_remove_signature() {
    nested_secboot_remove_signature "$@"
}

transition() {
    nested_uc20_transition_to_system_mode "$@"
}

snap_rev() {
    if [ $# -eq 0 ] || [ $# -gt 2 ]; then
        show_help
        exit 1
    elif [ $# -eq 1 ]; then
        nested_get_core_revision_installed "$@"
    else
        nested_get_core_revision_for_channel "$@"
    fi
}

build_seed() {
    nested_build_seed_cdrom "$@"
}

prepare_essential_snaps() {
    nested_prepare_essential_snaps
}

configure_default_user() {
    nested_configure_default_user
}

nested_system() {
    if os.query is-ubuntu 16.04; then
        echo ubuntu-core-16-64
    elif os.query is-ubuntu 18.04; then
        echo ubuntu-core-18-64
    elif os.query is-ubuntu 20.04; then
        echo ubuntu-core-20-64
    elif os.query is-ubuntu 22.04; then
        echo ubuntu-core-22-64
    elif os.query is-ubuntu 24.04; then
        echo ubuntu-core-24-64
    else
        echo "unsupported nested system"
        exit 1
    fi
}

main() {
    if [ $# -eq 0 ]; then
        show_help
        exit 0
    fi

    local subcommand="$1"
    local action=
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)
                show_help
                exit 0
                ;;
            *)
                action=$(echo "$subcommand" | tr '-' '_')
                shift
                break
                ;;
        esac
    done

    if [ -z "$(declare -f "$action")" ]; then
        echo "tests.nested: no such command: $subcommand"
        show_help
        exit 1
    fi

    #shellcheck source=tests/lib/nested.sh
    . "$TESTSLIB/nested.sh"
    "$action" "$@"
}

main "$@"