blob: d236b2b79f68f788b939bd35a131af86b81b2625 [file] [log] [blame]
Mike Frysingera4507852014-12-11 23:01:13 -05001#!/bin/bash
2# Copyright 2010 The Chromium OS Authors. All rights reserved.
Will Drewry80fbc6c2010-08-30 10:13:34 -05003# 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
9set -u
10
11warn () {
12 echo "WARN: $@" 1>&2
13}
14
15error () {
16 echo "ERROR: $@" 1>&2
17 exit 1
18}
19
20PASS_COUNT=0
21pass () {
22 echo "PASS:$1" 1>&2
23 PASS_COUNT=$((PASS_COUNT + 1))
24 return 0
25}
26
27FAIL_COUNT=0
28fail () {
29 echo "FAIL:$1" 1>&2
30 FAIL_COUNT=$((FAIL_COUNT + 1))
31 return 0
32}
33
34WORKDIR=
35cleanup () {
36 if [ -n "$WORKDIR" ]; then
37 rm -rf "$WORKDIR"
38 fi
39 trap - EXIT
40}
41
42setup () {
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
50run_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
63expect () {
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 Frysingera4507852014-12-11 23:01:13 -050073ROOTDEV=${1:-./rootdev}
74if [[ ! -e ${ROOTDEV} ]]; then
75 error "could not find rootdev '${ROOTDEV}'"
Will Drewry80fbc6c2010-08-30 10:13:34 -050076fi
77
Mike Frysingera4507852014-12-11 23:01:13 -050078if [ "${USER:-}" != "root" ]; then
79 error "Must be run as root to use mknod (${USER:-})"
Will Drewry80fbc6c2010-08-30 10:13:34 -050080fi
81
82t00_bad_sys_dir () {
Mike Frysingera4507852014-12-11 23:01:13 -050083 out=$("${ROOTDEV}" --block $WORKDIR 2>/dev/null)
Will Drewry80fbc6c2010-08-30 10:13:34 -050084 expect "$? -ne 0" || return 1
85 expect "-z '$out'" || return 1
86}
87run_test t00_bad_sys_dir
88
89h00_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
104t01_sys_dev_match () {
105 local block=$WORKDIR/sys/block
106 local dev=$WORKDIR/dev
107 h00_setup_sda_tree $block $dev
108
Mike Frysingera4507852014-12-11 23:01:13 -0500109 out=$("${ROOTDEV}" --dev $dev --block $block --major 10 --minor 1 2>/dev/null)
Will Drewry80fbc6c2010-08-30 10:13:34 -0500110 expect "$? -eq 0" || return 1
111 expect "'$dev/sda1' = '$out'" || return 1
112}
113run_test t01_sys_dev_match
114
115t02_sys_dev_match_block () {
116 local block=$WORKDIR/sys/block
117 local dev=$WORKDIR/dev
118 h00_setup_sda_tree $block $dev
119
Mike Frysingera4507852014-12-11 23:01:13 -0500120 out=$("${ROOTDEV}" --dev $dev --block $block --major 10 --minor 0 2>/dev/null)
Will Drewry80fbc6c2010-08-30 10:13:34 -0500121 expect "$? -eq 0" || return 1
122 expect "'$dev/sda' = '$out'" || return 1
123}
124run_test t02_sys_dev_match_block
125
126t03_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 Frysingera4507852014-12-11 23:01:13 -0500132 out=$("${ROOTDEV}" --dev $dev --block $block --major 10 --minor 0 2>/dev/null)
Will Drewry80fbc6c2010-08-30 10:13:34 -0500133 expect "$? -eq 1" || return 1
134 expect "'$dev/sda' = '$out'" || return 1
135}
136run_test t03_sys_dev_match_block_no_dev
137
138t04_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 Frysingera4507852014-12-11 23:01:13 -0500144 out=$("${ROOTDEV}" -i --dev $dev --block $block --major 10 --minor 0 2>/dev/null)
Will Drewry80fbc6c2010-08-30 10:13:34 -0500145 expect "$? -eq 0" || return 1
146 expect "'$dev/sda' = '$out'" || return 1
147}
148run_test t04_sys_dev_match_block_no_dev_ignore
149
150
151h01_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
163t05_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 Frysingera4507852014-12-11 23:01:13 -0500169 out=$("${ROOTDEV}" --dev $dev --block $block --major 254 --minor 0 \
Will Drewry80fbc6c2010-08-30 10:13:34 -0500170 2>/dev/null)
171 expect "$? -eq 0" || return 1
172 expect "'$dev/dm-0' = '$out'" || return 1
173}
174run_test t05_match_dm
175
176t06_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 Frysingera4507852014-12-11 23:01:13 -0500182 out=$("${ROOTDEV}" -s --dev $dev --block $block --major 254 --minor 0 \
Will Drewry80fbc6c2010-08-30 10:13:34 -0500183 2>/dev/null)
184 expect "$? -eq 0" || return 1
185 expect "'$dev/sda1' = '$out'" || return 1
186}
187run_test t06_match_dm_slave
188
189t07_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 Frysingera4507852014-12-11 23:01:13 -0500195 out=$("${ROOTDEV}" -s --dev $dev --block $block --major 10 --minor 1 \
Will Drewry80fbc6c2010-08-30 10:13:34 -0500196 2>/dev/null)
197 expect "$? -eq 0" || return 1
198 expect "'$dev/sda1' = '$out'" || return 1
199}
200run_test t07_safe_fail_on_no_slave
201
202t08_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 Frysingera4507852014-12-11 23:01:13 -0500211 out=$("${ROOTDEV}" -s --dev $dev --block $block --major 254 --minor 0 \
Will Drewry80fbc6c2010-08-30 10:13:34 -0500212 2>/dev/null)
213 expect "$? -eq 1" || return 1
214 expect "'$dev/sda1' = '$out'" || return 1
215}
216run_test t08_safe_fail_on_no_slave_dev
217
218t09_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 Frysingera4507852014-12-11 23:01:13 -0500227 out=$("${ROOTDEV}" -i -s --dev $dev --block $block --major 254 --minor 0 \
Will Drewry80fbc6c2010-08-30 10:13:34 -0500228 2>/dev/null)
229 expect "$? -eq 0" || return 1
230 expect "'$dev/sda1' = '$out'" || return 1
231}
232run_test t09_safe_fail_on_no_slave_dev_ignore
233
234h02_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
249t10_mmcdev () {
250 local block=$WORKDIR/sys/block
251 local dev=$WORKDIR/dev
252 h02_setup_mmc_tree $block $dev
Mike Frysingera4507852014-12-11 23:01:13 -0500253 out=$("${ROOTDEV}" --dev $dev --block $block --major 11 --minor 2 \
Will Drewry80fbc6c2010-08-30 10:13:34 -0500254 2>/dev/null)
255 expect "$? -eq 0" || return 1
256 expect "'$dev/mmcblk0p2' = '$out'" || return 1
257}
258run_test t10_mmcdev
259
260t11_mmcdev_strip () {
261 local block=$WORKDIR/sys/block
262 local dev=$WORKDIR/dev
263 h02_setup_mmc_tree $block $dev
Mike Frysingera4507852014-12-11 23:01:13 -0500264 out=$("${ROOTDEV}" -d --dev $dev --block $block --major 11 --minor 2 \
Will Drewry80fbc6c2010-08-30 10:13:34 -0500265 2>/dev/null)
266 expect "$? -eq 0" || return 1
267 expect "'$dev/mmcblk0' = '$out'" || return 1
268}
269run_test t11_mmcdev_strip
270
271t12_sda_strip () {
272 local block=$WORKDIR/sys/block
273 local dev=$WORKDIR/dev
274 h00_setup_sda_tree $block $dev
Mike Frysingera4507852014-12-11 23:01:13 -0500275 out=$("${ROOTDEV}" -d --dev $dev --block $block --major 10 --minor 2 \
Will Drewry80fbc6c2010-08-30 10:13:34 -0500276 2>/dev/null)
277 expect "$? -eq 0" || return 1
278 expect "'$dev/sda' = '$out'" || return 1
279}
280run_test t12_sda_strip
281
282# TODO(wad) add node creation tests
283
284TEST_COUNT=$((PASS_COUNT + FAIL_COUNT))
285
286echo "----"
287echo "Test passed: $PASS_COUNT / $TEST_COUNT"
288echo "Test failed: $FAIL_COUNT / $TEST_COUNT"
289
290if [ $FAIL_COUNT -ne 0 ]; then
291 exit 1
292fi