File: journal-state

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (162 lines) | stat: -rwxr-xr-x 3,897 bytes parent folder | download | duplicates (4)
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
#!/bin/bash

# This file contains all the cursors used for the different tests
JOURNALCTL_CURSOR_FILE="$TESTSTMP"/runtime-state/journalctl_cursor

show_help() {
    echo "usage: journal-state start-new-log"
    echo "       journal-state check-log-started"
    echo "       journal-state get-last-cursor"
    echo "       journal-state get-test-cursor"
    echo "       journal-state match-log <EXPRESSION> [--attempts|-n PARAM] [--wait PARAM] [--unit|-u PARAM] [-b]"
    echo "       journal-state get-log"
    echo "       journal-state get-log-from-cursor"
}

get_last_cursor(){
    journalctl --output=export -n1 | grep --binary-files=text -o '__CURSOR=.*' | sed -e 's/^__CURSOR=//'
}

start_new_log(){
    _sync_log
    echo "New test starts here - $SPREAD_JOB" | systemd-cat -t snapd-test
    cursor=$(get_last_cursor)
    if [ -z "$cursor" ]; then
        echo "Empty journalctl cursor, exiting..."
        exit 1
    else
        echo "$SPREAD_JOB " >> "$JOURNALCTL_CURSOR_FILE"
        echo "$cursor" >> "$JOURNALCTL_CURSOR_FILE"
    fi
}

get_test_cursor(){
    cursor="$(tail -n 1 "$JOURNALCTL_CURSOR_FILE")"
    if [ -z "$cursor" ]; then
        echo "Cursor for test not found"
        exit 1
    fi
    echo "$cursor"
}

check_log_started(){
    marker="test-${RANDOM}${RANDOM}"
    echo "Running test: $marker" | systemd-cat -t snapd-test
    if match_log "$marker"; then
        return 0
    fi
    echo "Test id not found in journalctl, exiting..."
    exit 1
}

match_log(){
    local wait=1
    local attempts=10
    local params=""
    local expression

    while [ $# -gt 0 ]; do
        case "$1" in
            --wait)
                wait=$2
                shift 2
                ;;
            -n|--attempts)
                attempts=$2
                shift 2
                ;;
            -u|--unit)
                params="$params -u $2"
                shift 2
                ;;
            -b)
                params="$params -b"
                shift
                ;;
            *)
                expression=$1
                shift
                ;;
        esac
    done

    for _ in $(seq "$attempts"); do
        # forcibly silence this particular bit because it produces GOBS of 
        # output and we really don't need to see all the output when we are 
        # checking the output for an expression, as if it is missing we want to 
        # check the journal once at the end of this loop, likely in the debug 
        # section
        set +x
        local log
        if [ -z "$params" ]; then
            log="$(get_log)"
        else
            #shellcheck disable=SC2086
            log="$(get_log $params)"
        fi
        if echo "$log" | grep -q -E "$expression"; then
            return 0
        fi
        set -x
        echo "Match for \"$expression\" failed, retrying"
        sleep "$wait"
    done
    return 1
}

get_log(){
    cursor=""
    if [ -f "$JOURNALCTL_CURSOR_FILE" ]; then
        cursor=$(tail -n1 "$JOURNALCTL_CURSOR_FILE")
    fi
    _sync_log
    get_log_from_cursor "$cursor" "$@"
}

get_log_from_cursor(){
    cursor=$1
    shift
    if [ -z "$cursor" ]; then
        journalctl "$@"
    else
        journalctl "$@" --cursor "$cursor"
    fi
}

_sync_log(){
    journalctl --flush || true
    journalctl --sync || true
}

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

    subcommand=$1
    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 "journal-state: no such command $subcommand" >&2
        show_help
        exit 1
    fi

    "$action" "$@"
}

main "$@"