Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2010 The Chromium OS Authors. All rights reserved. |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | # |
| 6 | # Simple functional test harness for rootdev |
| 7 | # TODO(wad) rootdev should be rewritten in C++ and gmocked. |
| 8 | |
| 9 | set -u |
| 10 | |
| 11 | warn () { |
| 12 | echo "WARN: $@" 1>&2 |
| 13 | } |
| 14 | |
| 15 | error () { |
| 16 | echo "ERROR: $@" 1>&2 |
| 17 | exit 1 |
| 18 | } |
| 19 | |
| 20 | PASS_COUNT=0 |
| 21 | pass () { |
| 22 | echo "PASS:$1" 1>&2 |
| 23 | PASS_COUNT=$((PASS_COUNT + 1)) |
| 24 | return 0 |
| 25 | } |
| 26 | |
| 27 | FAIL_COUNT=0 |
| 28 | fail () { |
| 29 | echo "FAIL:$1" 1>&2 |
| 30 | FAIL_COUNT=$((FAIL_COUNT + 1)) |
| 31 | return 0 |
| 32 | } |
| 33 | |
| 34 | WORKDIR= |
| 35 | cleanup () { |
| 36 | if [ -n "$WORKDIR" ]; then |
| 37 | rm -rf "$WORKDIR" |
| 38 | fi |
| 39 | trap - EXIT |
| 40 | } |
| 41 | |
| 42 | setup () { |
| 43 | WORKDIR=$(mktemp -d rootdev_test.XXXXXXX) |
| 44 | if [ ! -d "$WORKDIR" ]; then |
| 45 | error "Failed to create temporary work dir" |
| 46 | fi |
| 47 | trap cleanup EXIT |
| 48 | } |
| 49 | |
| 50 | run_test () { |
| 51 | setup |
| 52 | echo "RUN:$1" 1>&2 |
| 53 | eval $1 |
| 54 | ret=$? |
| 55 | cleanup |
| 56 | if [ $ret -eq 0 ]; then |
| 57 | pass $1 |
| 58 | else |
| 59 | fail $1 |
| 60 | fi |
| 61 | } |
| 62 | |
| 63 | expect () { |
| 64 | cond="$1" |
| 65 | eval test $1 |
| 66 | if [ $? -ne 0 ]; then |
| 67 | warn "expect: $1" |
| 68 | return 1 |
| 69 | fi |
| 70 | return 0 |
| 71 | } |
| 72 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 73 | ROOTDEV=${1:-./rootdev} |
| 74 | if [[ ! -e ${ROOTDEV} ]]; then |
| 75 | error "could not find rootdev '${ROOTDEV}'" |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 76 | fi |
| 77 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 78 | if [ "${USER:-}" != "root" ]; then |
| 79 | error "Must be run as root to use mknod (${USER:-})" |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 80 | fi |
| 81 | |
| 82 | t00_bad_sys_dir () { |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 83 | out=$("${ROOTDEV}" --block $WORKDIR 2>/dev/null) |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 84 | expect "$? -ne 0" || return 1 |
| 85 | expect "-z '$out'" || return 1 |
| 86 | } |
| 87 | run_test t00_bad_sys_dir |
| 88 | |
| 89 | h00_setup_sda_tree() { |
| 90 | local block=$1 |
| 91 | local dev=$2 |
| 92 | mkdir -p $block |
| 93 | mkdir -p $dev |
| 94 | mkdir -p $block/sda/sda1 |
| 95 | mkdir -p $block/sda/sda2 |
| 96 | echo "10:0" > $block/sda/dev |
| 97 | echo "10:1" > $block/sda/sda1/dev |
| 98 | echo "10:2" > $block/sda/sda2/dev |
| 99 | mknod $dev/sda1 b 10 1 |
| 100 | mknod $dev/sda2 b 10 2 |
| 101 | mknod $dev/sda b 10 0 |
| 102 | } |
| 103 | |
| 104 | t01_sys_dev_match () { |
| 105 | local block=$WORKDIR/sys/block |
| 106 | local dev=$WORKDIR/dev |
| 107 | h00_setup_sda_tree $block $dev |
| 108 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 109 | out=$("${ROOTDEV}" --dev $dev --block $block --major 10 --minor 1 2>/dev/null) |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 110 | expect "$? -eq 0" || return 1 |
| 111 | expect "'$dev/sda1' = '$out'" || return 1 |
| 112 | } |
| 113 | run_test t01_sys_dev_match |
| 114 | |
| 115 | t02_sys_dev_match_block () { |
| 116 | local block=$WORKDIR/sys/block |
| 117 | local dev=$WORKDIR/dev |
| 118 | h00_setup_sda_tree $block $dev |
| 119 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 120 | out=$("${ROOTDEV}" --dev $dev --block $block --major 10 --minor 0 2>/dev/null) |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 121 | expect "$? -eq 0" || return 1 |
| 122 | expect "'$dev/sda' = '$out'" || return 1 |
| 123 | } |
| 124 | run_test t02_sys_dev_match_block |
| 125 | |
| 126 | t03_sys_dev_match_block_no_dev () { |
| 127 | local block=$WORKDIR/sys/block |
| 128 | local dev=$WORKDIR/dev |
| 129 | h00_setup_sda_tree $block $dev |
| 130 | rm $dev/sda |
| 131 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 132 | out=$("${ROOTDEV}" --dev $dev --block $block --major 10 --minor 0 2>/dev/null) |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 133 | expect "$? -eq 1" || return 1 |
| 134 | expect "'$dev/sda' = '$out'" || return 1 |
| 135 | } |
| 136 | run_test t03_sys_dev_match_block_no_dev |
| 137 | |
| 138 | t04_sys_dev_match_block_no_dev_ignore () { |
| 139 | local block=$WORKDIR/sys/block |
| 140 | local dev=$WORKDIR/dev |
| 141 | h00_setup_sda_tree $block $dev |
| 142 | rm $dev/sda |
| 143 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 144 | out=$("${ROOTDEV}" -i --dev $dev --block $block --major 10 --minor 0 2>/dev/null) |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 145 | expect "$? -eq 0" || return 1 |
| 146 | expect "'$dev/sda' = '$out'" || return 1 |
| 147 | } |
| 148 | run_test t04_sys_dev_match_block_no_dev_ignore |
| 149 | |
| 150 | |
| 151 | h01_setup_dm_tree() { |
| 152 | local block=$1 |
| 153 | local dev=$2 |
| 154 | mkdir -p $block |
| 155 | mkdir -p $dev |
| 156 | mkdir -p $block/dm-0 |
| 157 | mkdir -p $block/dm-0/slaves/sda1 |
| 158 | echo "254:0" > $block/dm-0/dev |
| 159 | echo "10:1" > $block/dm-0/slaves/sda1/dev |
| 160 | mknod $dev/dm-0 b 254 0 |
| 161 | } |
| 162 | |
| 163 | t05_match_dm () { |
| 164 | local block=$WORKDIR/sys/block |
| 165 | local dev=$WORKDIR/dev |
| 166 | h00_setup_sda_tree $block $dev |
| 167 | h01_setup_dm_tree $block $dev |
| 168 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 169 | out=$("${ROOTDEV}" --dev $dev --block $block --major 254 --minor 0 \ |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 170 | 2>/dev/null) |
| 171 | expect "$? -eq 0" || return 1 |
| 172 | expect "'$dev/dm-0' = '$out'" || return 1 |
| 173 | } |
| 174 | run_test t05_match_dm |
| 175 | |
| 176 | t06_match_dm_slave () { |
| 177 | local block=$WORKDIR/sys/block |
| 178 | local dev=$WORKDIR/dev |
| 179 | h00_setup_sda_tree $block $dev |
| 180 | h01_setup_dm_tree $block $dev |
| 181 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 182 | out=$("${ROOTDEV}" -s --dev $dev --block $block --major 254 --minor 0 \ |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 183 | 2>/dev/null) |
| 184 | expect "$? -eq 0" || return 1 |
| 185 | expect "'$dev/sda1' = '$out'" || return 1 |
| 186 | } |
| 187 | run_test t06_match_dm_slave |
| 188 | |
| 189 | t07_safe_fail_on_no_slave () { |
| 190 | local block=$WORKDIR/sys/block |
| 191 | local dev=$WORKDIR/dev |
| 192 | h00_setup_sda_tree $block $dev |
| 193 | h01_setup_dm_tree $block $dev |
| 194 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 195 | out=$("${ROOTDEV}" -s --dev $dev --block $block --major 10 --minor 1 \ |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 196 | 2>/dev/null) |
| 197 | expect "$? -eq 0" || return 1 |
| 198 | expect "'$dev/sda1' = '$out'" || return 1 |
| 199 | } |
| 200 | run_test t07_safe_fail_on_no_slave |
| 201 | |
| 202 | t08_safe_fail_on_no_slave_dev () { |
| 203 | local block=$WORKDIR/sys/block |
| 204 | local dev=$WORKDIR/dev |
| 205 | h00_setup_sda_tree $block $dev |
| 206 | h01_setup_dm_tree $block $dev |
| 207 | # If the matching dev node is missing, an error code will be returned |
| 208 | # but the path will still represent the slave. |
| 209 | rm $dev/sda1 |
| 210 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 211 | out=$("${ROOTDEV}" -s --dev $dev --block $block --major 254 --minor 0 \ |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 212 | 2>/dev/null) |
| 213 | expect "$? -eq 1" || return 1 |
| 214 | expect "'$dev/sda1' = '$out'" || return 1 |
| 215 | } |
| 216 | run_test t08_safe_fail_on_no_slave_dev |
| 217 | |
| 218 | t09_safe_fail_on_no_slave_dev_ignore () { |
| 219 | local block=$WORKDIR/sys/block |
| 220 | local dev=$WORKDIR/dev |
| 221 | h00_setup_sda_tree $block $dev |
| 222 | h01_setup_dm_tree $block $dev |
| 223 | # If the matching dev node is missing, an error code will be returned |
| 224 | # but the path will still represent the slave. |
| 225 | rm $dev/sda1 |
| 226 | |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 227 | out=$("${ROOTDEV}" -i -s --dev $dev --block $block --major 254 --minor 0 \ |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 228 | 2>/dev/null) |
| 229 | expect "$? -eq 0" || return 1 |
| 230 | expect "'$dev/sda1' = '$out'" || return 1 |
| 231 | } |
| 232 | run_test t09_safe_fail_on_no_slave_dev_ignore |
| 233 | |
| 234 | h02_setup_mmc_tree() { |
| 235 | local block=$1 |
| 236 | local dev=$2 |
| 237 | mkdir -p $block |
| 238 | mkdir -p $dev |
| 239 | mkdir -p $block/mmcblk0/mmcblk0p1 |
| 240 | mkdir -p $block/mmcblk0/mmcblk0p2 |
| 241 | echo "11:0" > $block/mmcblk0/dev |
| 242 | echo "11:1" > $block/mmcblk0/mmcblk0p1/dev |
| 243 | echo "11:2" > $block/mmcblk0/mmcblk0p2/dev |
| 244 | mknod $dev/mmcblk0 b 11 0 |
| 245 | mknod $dev/mmcblk0p1 b 11 1 |
| 246 | mknod $dev/mmcblk0p2 b 11 2 |
| 247 | } |
| 248 | |
| 249 | t10_mmcdev () { |
| 250 | local block=$WORKDIR/sys/block |
| 251 | local dev=$WORKDIR/dev |
| 252 | h02_setup_mmc_tree $block $dev |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 253 | out=$("${ROOTDEV}" --dev $dev --block $block --major 11 --minor 2 \ |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 254 | 2>/dev/null) |
| 255 | expect "$? -eq 0" || return 1 |
| 256 | expect "'$dev/mmcblk0p2' = '$out'" || return 1 |
| 257 | } |
| 258 | run_test t10_mmcdev |
| 259 | |
| 260 | t11_mmcdev_strip () { |
| 261 | local block=$WORKDIR/sys/block |
| 262 | local dev=$WORKDIR/dev |
| 263 | h02_setup_mmc_tree $block $dev |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 264 | out=$("${ROOTDEV}" -d --dev $dev --block $block --major 11 --minor 2 \ |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 265 | 2>/dev/null) |
| 266 | expect "$? -eq 0" || return 1 |
| 267 | expect "'$dev/mmcblk0' = '$out'" || return 1 |
| 268 | } |
| 269 | run_test t11_mmcdev_strip |
| 270 | |
| 271 | t12_sda_strip () { |
| 272 | local block=$WORKDIR/sys/block |
| 273 | local dev=$WORKDIR/dev |
| 274 | h00_setup_sda_tree $block $dev |
Mike Frysinger | a450785 | 2014-12-11 23:01:13 -0500 | [diff] [blame] | 275 | out=$("${ROOTDEV}" -d --dev $dev --block $block --major 10 --minor 2 \ |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 276 | 2>/dev/null) |
| 277 | expect "$? -eq 0" || return 1 |
| 278 | expect "'$dev/sda' = '$out'" || return 1 |
| 279 | } |
| 280 | run_test t12_sda_strip |
| 281 | |
| 282 | # TODO(wad) add node creation tests |
| 283 | |
| 284 | TEST_COUNT=$((PASS_COUNT + FAIL_COUNT)) |
| 285 | |
| 286 | echo "----" |
| 287 | echo "Test passed: $PASS_COUNT / $TEST_COUNT" |
| 288 | echo "Test failed: $FAIL_COUNT / $TEST_COUNT" |
| 289 | |
| 290 | if [ $FAIL_COUNT -ne 0 ]; then |
| 291 | exit 1 |
| 292 | fi |