summaryrefslogtreecommitdiff
path: root/yarns/shell.lib
blob: 1f8432ed6d327ff0cb894dc52a386f5075770759 (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
# A shell library for yarn scenario step implementations.

# Copyright 2015  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


# Is running of scenarios of a given type requested? If nothing is
# requested ($TESTS is empty), then everything is.

test_requested()
{
    if [ "${TESTS:-}" = "" ]
    then
        return 0
    elif echo "${TESTS}" | tr , '\n' | grep -Fx "$1"
    then
        return 0
    else
        return 1
    fi
}

support_required()
{
    if [ ! -x /usr/bin/cmdtest ]; then
        echo "All tests need the cmdtest package."
        return 1
    fi
}


build_support_required()
{
    if [ ! -x /usr/bin/bc ]; then
        echo "Build tests need the bc package."
        return 1
    fi
}

# Convert a size with a unit, such a kB, to plain bytes.

size_in_bytes()
{
    local amount="$(echo "$1" | sed 's/[^0-9]*$//')"
    local unit="$(echo "$1" | sed 's/^[0-9]*//' | tr A-Z a-z)"
    local factor=1
    case "$unit" in
        k|kb) factor=1000 ;;
        m|mb) factor=1000000 ;;
        g|gb) factor=1000000000 ;;
        t|tb) factor=1000000000000 ;;
        ki|kib) factor=1024 ;;
        mi|mib) factor=1048576 ;;
        gi|gib) factor=1073741824 ;;
        ti|tib) factor=1099511627776 ;;
    esac
    echo "$amount * $factor" | bc -lq
}


# Make a partition in a disk image accessible as a block device.
# Return it's device file.

kpartx_image_partition()
{
    kpartx -sav "$1" | awk -v "n=$2" 'NR == n { print "/dev/mapper/" $3 }'
}


# Undo kpartx_image_partition.

unkpartx_image()
{
    dmsetup remove /dev/mapper/$(dmsetup ls|awk -F' ' '{print $1}')
    losetup -d $(losetup -a | grep "$1" | awk -F':' '{print $1}')
    kpartx -d "$1"
}


# Rembember and retrieve settings between scenario steps. This is
# implemented by a shell script snippet that gets sourced at the end
# of this shell library.

remember_setting()
{
    echo "$1=$2" >> "$DATADIR/settings.sh"
}

if [ -e "$DATADIR/settings.sh" ]
then
    . "$DATADIR/settings.sh"
fi


# Make sure the current working directory is $DATADIR, not $SRCDIR.
# This makes it a little simpler to write scenario step
# implementations by not having to be specifying $DATADIR explicitly
# everywhere.

cd "$DATADIR"