File: test_disk.sh

package info (click to toggle)
liquidprompt 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,572 kB
  • sloc: sh: 3,621; python: 33; makefile: 15
file content (132 lines) | stat: -rw-r--r-- 3,963 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

# Error on unset variables
set -u

if [ -n "${ZSH_VERSION-}" ]; then
  SHUNIT_PARENT="$0"
  setopt shwordsplit ksh_arrays
fi

. ../liquidprompt --no-activate

LP_ENABLE_DISK=1
LP_DISK_PRECISION=2

typeset -a names outputs values values_human

names+=("Simple Linux")
outputs+=(
'Filesystem     1024-blocks      Used Available Capacity Mounted on
/dev/nvme0n1p2   479608528 425188220  30011332      94% /'
)
values+=(30011332)
values_human+=("28.62 GiB")

names+=("Cygwin with space on FS")
outputs+=(
'Filesystem                1024-blocks      Used  Available Capacity Mounted on
C:/Program Files/cygwin64  1999659004 450860152 1548798852      23% /'
)
values+=(1548798852)
values_human+=("1.44 TiB")

names+=("Cygwin with space on mount point")
outputs+=(
'Filesystem                1024-blocks      Used  Available Capacity Mounted on
C:/Program Files/cygwin64  1999659004 450860152 1548798852      23% C:/Program Files/cygwin64/drive_d'
)
values+=(1548798852)
values_human+=("1.44 TiB")

names+=("FreeBSD small FS, but over 1MB")
outputs+=(
'Filesystem      1024-blocks Used Avail Capacity  Mounted on
/dev/gpt/efiesp       32764  646 32117     2%    /boot/efi'
)
values+=(32117)
values_human+=("31.36 MiB")

names+=("FreeBSD special FS")
outputs+=(
'Filesystem 1024-blocks Used Avail Capacity  Mounted on
devfs                1    0     1     0%    /dev'
)
values+=("")
values_human+=("")

names+=("Linux special FS")
outputs+=(
'Filesystem     1024-blocks  Used Available Capacity Mounted on
sysfs                    0     0         0        - /sys'
)
values+=("")
values_human+=("")

function test_disk {
    for (( i=0; i < ${#outputs[@]}; i++ )); do
        df() {
            printf '%s\n' "${outputs[i]}"
        }

        LP_DISK_THRESHOLD_PERC=99
        LP_DISK_THRESHOLD=10000000000
        if [ -n "${values[i]}" ]; then
            _lp_disk
            assertEquals "Parsing of \"${names[i]}\" without error" "0" "$?"
            assertEquals "Correct parsing of \"${names[i]}\" in KiB" "${values[i]}" "$lp_disk"
            assertEquals "Correct parsing of \"${names[i]}\" for human" "${values_human[i]}" "$lp_disk_human $lp_disk_human_units"
        else
            _lp_disk
            assertFalse "Parsing of \"${names[i]}\" skipped" "$?"
        fi

        # Note: percent threshold should be below available space in the least
        #       available space case ("Simple Linux"), but absolute threshold
        #       should be slightly larger than "FreeBSD small FS" test case,
        #       so we can fully test the "total >= LP_DISK_THRESHOLD" condition.
        LP_DISK_THRESHOLD_PERC=5
        LP_DISK_THRESHOLD=100000
        _lp_disk
        assertFalse "Above threshold for \"${names[i]}\"" "$?"
    done
}

function test_bytes_to_human {
    LP_DISK_PRECISION=0

    local -a units
    units=("B" "KiB" "MiB" "GiB" "TiB" "PiB" "EiB" "YiB" "ZiB")

    for (( i=1; i < 7; i++ )); do
        __lp_bytes_to_human $((1024**i)) "$LP_DISK_PRECISION"
        assertEquals "Human readable 1024**$i" "1024 ${units[i-1]}" "$lp_bytes $lp_bytes_units"
    done

    for (( i=1; i < 7; i++ )); do
        __lp_bytes_to_human $((1025**i)) "$LP_DISK_PRECISION"
        assertEquals "Human readable 1025**$i" "1 ${units[i]}" "$lp_bytes $lp_bytes_units"
    done

    for (( i=1; i < 7; i++ )); do
        __lp_bytes_to_human $((2*1024**i)) "$LP_DISK_PRECISION"
        assertEquals "Human readable 2*1024**$i" "2 ${units[i]}" "$lp_bytes $lp_bytes_units"
    done

    LP_DISK_PRECISION=1

    for (( i=1; i < 7; i++ )); do
        __lp_bytes_to_human $((1024**i)) "$LP_DISK_PRECISION"
        assertEquals "Human readable 1024**$i" "1024.0 ${units[i-1]}" "$lp_bytes $lp_bytes_units"
    done

    LP_DISK_PRECISION=2

    for (( i=1; i < 7; i++ )); do
        __lp_bytes_to_human $((1024**i)) "$LP_DISK_PRECISION"
        assertEquals "Human readable 1024**$i" "1024.00 ${units[i-1]}" "$lp_bytes $lp_bytes_units"
    done

}

. ./shunit2