summaryrefslogtreecommitdiffstats
path: root/test/TEST-98-GETARG/test.sh
blob: e7337d98f18c44eb7ddbce81e34837bcdb75f679 (plain)
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
#!/bin/bash

# This file is part of dracut.
# SPDX-License-Identifier: GPL-2.0-or-later

# shellcheck disable=SC2034
TEST_DESCRIPTION="dracut getarg command"

test_check() {
    if ! [[ -x "$PKGLIBDIR/dracut-util" ]]; then
        echo "Test needs dracut-util... Skipping"
        return 1
    fi
}

test_setup() {
    ln -sfnr "$PKGLIBDIR"/dracut-util "$TESTDIR"/dracut-getarg
    ln -sfnr "$PKGLIBDIR"/dracut-util "$TESTDIR"/dracut-getargs
    ln -sfnr "$PKGLIBDIR"/modules.d/99base/dracut-lib.sh "$TESTDIR"/dracut-lib.sh
    ln -sfnr "$PKGLIBDIR"/modules.d/99base/dracut-dev-lib.sh "$TESTDIR"/dracut-dev-lib.sh
    return 0
}

test_run() {
    set -x
    (
        cd "$TESTDIR" || exit 1
        export CMDLINE='key1=0 key2=val key2=val2 key3="  val  3  " "  key 4  ="val4 "key  5=val  5" "key 6=""val  6" key7="foo"bar" baz="end "  key8  =  val 8  "
"key 9"="val 9"'

        ret=0

        unset TEST
        declare -A TEST
        TEST=(
            ["key1"]="0"
            ["key2"]="val2"
            ["key3"]="  val  3  "
            ["  key 4  "]="val4"
            ["key  5"]="val  5"
            ["key 6"]='"val  6'
            ["key7"]='foo"bar" baz="end'
            ["  key8  "]="  val 8  "
            ['key 9"']="val 9"
        )
        for key in "${!TEST[@]}"; do
            if ! val=$(./dracut-getarg "${key}="); then
                echo "'$key' == '${TEST[$key]}', but not found" >&2
                ret=$((ret + 1))
            else
                if [[ $val != "${TEST[$key]}" ]]; then
                    echo "'$key' != '${TEST[$key]}' but '$val'" >&2
                    ret=$((ret + 1))
                fi
            fi
        done

        declare -a INVALIDKEYS

        INVALIDKEYS=("key" "4" "5" "6" "key8" "9" '"' "baz")
        for key in "${INVALIDKEYS[@]}"; do
            val=$(./dracut-getarg "$key")
            # shellcheck disable=SC2181
            if (($? == 0)); then
                echo "key '$key' should not be found"
                ret=$((ret + 1))
            fi
            # must have no output
            [[ $val ]] && ret=$((ret + 1))
        done

        RESULT=("val" "val2")
        readarray -t args < <(./dracut-getargs "key2=")
        ((${#RESULT[@]} == ${#args[@]})) || ret=$((ret + 1))
        for ((i = 0; i < ${#RESULT[@]}; i++)); do
            [[ ${args[$i]} == "${RESULT[$i]}" ]] || ret=$((ret + 1))
        done

        val=$(./dracut-getarg "key1") || ret=$((ret + 1))
        [[ $val == "0" ]] || ret=$((ret + 1))

        val=$(./dracut-getarg "key2=val") && ret=$((ret + 1))
        # must have no output
        [[ $val ]] && ret=$((ret + 1))
        val=$(./dracut-getarg "key2=val2") || ret=$((ret + 1))
        # must have no output
        [[ $val ]] && ret=$((ret + 1))

        export PATH=".:$PATH"

        . dracut-dev-lib.sh
        . dracut-lib.sh

        # shellcheck disable=SC2317  # overwrites debug_off from dracut-lib.sh
        debug_off() {
            :
        }

        # shellcheck disable=SC2317  # overwrites debug_on from dracut-lib.sh
        debug_on() {
            :
        }

        # shellcheck disable=SC2317  # called later by getarg in dracut-lib.sh
        getcmdline() {
            echo "rdbreak=cmdline rd.lvm rd.auto=0 rd.auto rd.retry=10"
        }
        RDRETRY=$(getarg rd.retry -d 'rd_retry=')
        [[ $RDRETRY == "10" ]] || ret=$((ret + 1))
        getarg rd.break=cmdline -d rdbreak=cmdline || ret=$((ret + 1))
        getargbool 1 rd.lvm -d -n rd_NO_LVM || ret=$((ret + 1))
        getargbool 0 rd.auto || ret=$((ret + 1))

        # shellcheck disable=SC2317  # called later by getarg in dracut-lib.sh
        getcmdline() {
            echo "rd.break=cmdlined rd.lvm=0 rd.auto rd.auto=1 rd.auto=0"
        }
        getarg rd.break=cmdline -d rdbreak=cmdline && ret=$((ret + 1))
        getargbool 1 rd.lvm -d -n rd_NO_LVM && ret=$((ret + 1))
        getargbool 0 rd.auto && ret=$((ret + 1))

        # shellcheck disable=SC2317  # called later by getarg in dracut-lib.sh
        getcmdline() {
            echo "ip=a ip=b ip=dhcp6"
        }
        getargs "ip=dhcp6" &> /dev/null || ret=$((ret + 1))
        readarray -t args < <(getargs "ip=")
        RESULT=("a" "b" "dhcp6")
        ((${#RESULT[@]} || ${#args[@]})) || ret=$((ret + 1))
        for ((i = 0; i < ${#RESULT[@]}; i++)); do
            [[ ${args[$i]} == "${RESULT[$i]}" ]] || ret=$((ret + 1))
        done

        # shellcheck disable=SC2317  # called later by getarg in dracut-lib.sh
        getcmdline() {
            echo "bridge bridge=val"
        }
        readarray -t args < <(getargs bridge=)
        RESULT=("bridge" "val")
        ((${#RESULT[@]} == ${#args[@]})) || ret=$((ret + 1))
        for ((i = 0; i < ${#RESULT[@]}; i++)); do
            [[ ${args[$i]} == "${RESULT[$i]}" ]] || ret=$((ret + 1))
        done

        # shellcheck disable=SC2317  # called later by getarg in dracut-lib.sh
        getcmdline() {
            echo "rd.break rd.md.uuid=bf96e457:230c9ad4:1f3e59d6:745cf942 rd.md.uuid=bf96e457:230c9ad4:1f3e59d6:745cf943 rd.shell"
        }
        readarray -t args < <(getargs rd.md.uuid -d rd_MD_UUID=)
        RESULT=("bf96e457:230c9ad4:1f3e59d6:745cf942" "bf96e457:230c9ad4:1f3e59d6:745cf943")
        ((${#RESULT[@]} == ${#args[@]})) || ret=$((ret + 1))
        for ((i = 0; i < ${#RESULT[@]}; i++)); do
            [[ ${args[$i]} == "${RESULT[$i]}" ]] || ret=$((ret + 1))
        done

        return $ret
    )
}

test_cleanup() {
    rm -fr -- "$TESTDIR"/*.rpm
    return 0
}

# shellcheck disable=SC1090
. "$testdir"/test-functions