File: hotplug.sh

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 (110 lines) | stat: -rw-r--r-- 3,581 bytes parent folder | download | duplicates (2)
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
#!/bin/bash

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


hotplug_add_dev1() {
    nested_add_tty_chardev my-chardev1 /tmp/serialport1
    nested_add_usb_serial_device my-usb-serial my-chardev1 1234
}

hotplug_del_dev1() {
    nested_del_device my-usb-serial
    nested_remove_chardev my-chardev1
}

hotplug_add_dev2() {
    nested_add_tty_chardev my-chardev2 /tmp/serialport2
    nested_add_usb_serial_device my-usb-serial2 my-chardev2 5678
}

hotplug_del_dev2() {
    nested_del_device my-usb-serial2
    nested_remove_chardev my-chardev2
}

# Check that given slot is not present in 'snap connections' output
# (note, it can still be present in the state with hotplug-gone=true)
check_slot_not_present() {
    SLOT_NAME="$1"
    for _ in $(seq 10); do
        if ! remote.exec "snap connections system" | MATCH ":$SLOT_NAME"; then
            break
        fi
        sleep 1
    done
    if remote.exec "snap connections system" | MATCH ":$SLOT_NAME "; then
        echo "slot $SLOT_NAME shouldn't be present anymore"
        exit 1
    fi
}

# Check that given slot is present in 'snap connections' output (but is not connected)
check_slot_present() {
    SLOT_NAME="$1"
    for _ in $(seq 10); do
        if remote.exec "snap connections system" | MATCH "serial-port .* - .* :$SLOT_NAME"; then
            break
        fi
        sleep 1
    done
    remote.exec "snap connections system" | MATCH "serial-port .* - .* :$SLOT_NAME"
}

# Check that given slot has hotplug-gone=true, meaning the device was unplugged but there are connections remembered for it
check_slot_gone() {
    SLOT_NAME="$1"
    remote.exec 'sudo cat /var/lib/snapd/state.json' | gojq -r ".data[\"hotplug-slots\"][\"$SLOT_NAME\"][\"hotplug-gone\"]" | MATCH "true"
}

# Check that given slot has hotplug-gone=false, meaning the device is plugged
check_slot_not_gone() {
    SLOT_NAME="$1"
    remote.exec 'sudo  cat /var/lib/snapd/state.json' | gojq -r ".data[\"hotplug-slots\"][\"$SLOT_NAME\"][\"hotplug-gone\"]" | MATCH "false"
}

# Check that given slot has no record in "hotplug-slots" map in the state
check_slot_not_present_in_state() {
    SLOT_NAME="$1"
    remote.exec 'sudo  cat /var/lib/snapd/state.json' | gojq -r ".data[\"hotplug-slots\"][\"$SLOT_NAME\"] // \"missing\"" | MATCH "missing"
}

check_slot_device_path() {
    SLOT_NAME="$1"
    DEVICE_PATH="$2"
    remote.exec 'sudo  cat /var/lib/snapd/state.json' | gojq -r ".data[\"hotplug-slots\"][\"$SLOT_NAME\"][\"static-attrs\"].path" | MATCH "$DEVICE_PATH"
}

# Check that given slot is connected to the serial-port-hotplug snap, per 'snap connections' output
check_slot_connected() {
    SLOT_NAME="$1"
    for _ in $(seq 10); do
        if remote.exec "snap connections" | MATCH "serial-port .*serial-port-hotplug:serial-port .*$SLOT_NAME"; then
            break
        fi
        sleep 1
    done
    remote.exec "snap connections" | MATCH "serial-port .*serial-port-hotplug:serial-port .*$SLOT_NAME"
}

# Check that apparmor profile allows rw access to given device path.
verify_apparmor_profile() {
    DEVPATH=$1
    for _ in $(seq 10); do
        if remote.exec "cat /var/lib/snapd/apparmor/profiles/snap.serial-port-hotplug.consumer" | MATCH "$DEVPATH rwk,"; then
            break
        fi
        sleep 1
    done
    remote.exec "cat /var/lib/snapd/apparmor/profiles/snap.serial-port-hotplug.consumer" | MATCH "$DEVPATH rwk,"
}

wait_for_all_changes() {
    for _ in $(seq 10); do
        if ! remote.exec "snap changes" | MATCH "Doing"; then
            break
        fi
        sleep 1
    done
}