]> git.ipfire.org Git - thirdparty/util-linux.git/blob - tests/ts/partx/partx
partx: document -d vs. --nr and fix test
[thirdparty/util-linux.git] / tests / ts / partx / partx
1 #!/bin/bash
2
3 #
4 # Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org>
5 #
6 # This file is part of util-linux.
7 #
8 # This file is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This file is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18
19 TS_TOPDIR="${0%/*}/../.."
20 TS_DESC="partitions probing"
21 PARTS=3
22
23 . $TS_TOPDIR/functions.sh
24 ts_init "$*"
25
26 ts_check_test_command "$TS_CMD_PARTX"
27 ts_check_test_command "$TS_CMD_ADDPART"
28 ts_check_test_command "$TS_CMD_DELPART"
29
30 ts_skip_nonroot
31
32 shopt -s nullglob
33
34 function check_partition_count
35 {
36 # this function needs shopt -s nullglob
37 local cnt_want=$1
38 local devname=$(basename $TS_DEVICE)
39 local parts=(/sys/block/${devname}/${devname}*)
40 local cnt_have=${#parts[@]}
41
42 if [ $cnt_have -eq $cnt_want ]; then
43 return 0
44 fi
45 echo "error: expected $cnt_want partitions, have $cnt_have" >&2
46 echo "${parts[@]}" >&2
47 return 1
48 }
49
50 # set global variable TS_DEVICE
51 ts_scsi_debug_init dev_size_mb=50
52
53 ts_init_subtest "addpart"
54 {
55 $TS_CMD_ADDPART ${TS_DEVICE} 1 0 1 &&
56 echo OK ||
57 echo "Unable to add partition"
58 check_partition_count 1
59 } >$TS_OUTPUT 2>&1
60 ts_finalize_subtest
61
62 udevadm settle
63
64 ts_init_subtest "delpart"
65 {
66 $TS_CMD_DELPART ${TS_DEVICE} 1 &&
67 echo OK ||
68 echo "Unable to remove partition"
69 check_partition_count 0
70 } >$TS_OUTPUT 2>&1
71 ts_finalize_subtest
72
73 ts_scsi_debug_rmmod
74
75 # set global variable TS_DEVICE
76 ts_scsi_debug_init dev_size_mb=50 num_parts=$PARTS
77
78 ts_init_subtest "detect-parts"
79 {
80 $TS_CMD_PARTX --show $TS_DEVICE &&
81 echo OK ||
82 echo "Unable to list partitions"
83 check_partition_count $PARTS
84 } >$TS_OUTPUT 2>&1
85 ts_finalize_subtest
86
87 udevadm settle
88
89 ts_init_subtest "delete-all"
90 #delete partinfo
91 {
92 $TS_CMD_PARTX --delete $TS_DEVICE &&
93 echo "partitions deleted" ||
94 echo "Unable to delete partitions on $TS_DEVICE"
95 check_partition_count 0
96 } >$TS_OUTPUT 2>&1
97 ts_finalize_subtest
98
99 udevadm settle
100
101 ts_init_subtest "add-all"
102 #read TS_DEVICE and restore the partinfo
103 {
104 $TS_CMD_PARTX --add $TS_DEVICE &&
105 echo "partitions added" ||
106 echo "Unable to add partitions for $TS_DEVICE"
107 check_partition_count $PARTS
108 } >$TS_OUTPUT 2>&1
109 ts_finalize_subtest
110
111 udevadm settle
112
113 ts_init_subtest "update-one"
114 #read TS_DEVICE and update second partition
115 {
116 $TS_CMD_PARTX --update ${TS_DEVICE}2 &&
117 echo "partitions updated" ||
118 echo "Unable to update 2nd partition for $TS_DEVICE"
119 check_partition_count $PARTS
120 } >$TS_OUTPUT 2>&1
121 ts_finalize_subtest
122
123 udevadm settle
124
125 ts_init_subtest "delete-one"
126 #remove last partition only
127 {
128 $TS_CMD_PARTX -d --nr -1 $TS_DEVICE &&
129 echo "last partition removed" ||
130 echo "Unable to remove a partition on $TS_DEVICE"
131 check_partition_count $((PARTS-1))
132 } >$TS_OUTPUT 2>&1
133 ts_finalize_subtest
134
135 udevadm settle
136
137 ts_init_subtest "delete-non-existent"
138 #attempt to remove it again
139 {
140 # remove non-existing partitions (ENXIO) is not error
141 #
142 # see ab025087f91b66ee8e23a16bc49eb0d9bd421d65 and
143 # 53ae7d60cfeacd4e87bfe6fcc015b58b78ef4555
144 #
145 $TS_CMD_PARTX -d --nr $PARTS $TS_DEVICE &&
146 echo "partx: OK" ||
147 echo "partx failed: removed non-existing partition"
148 } >$TS_OUTPUT 2>&1
149 ts_finalize_subtest
150
151 udevadm settle
152
153 ts_init_subtest "add-existing"
154 #try adding an existing partition
155 {
156 $TS_CMD_PARTX -a --nr 1 $TS_DEVICE 2>/dev/null &&
157 echo "partx failed: re-added an existing partition" ||
158 echo "partx: OK"
159 } >$TS_OUTPUT 2>&1
160 ts_finalize_subtest
161
162 udevadm settle
163
164 ts_init_subtest "change-by-range"
165 # {-a|-d} --nr 0 should handle all partitions
166 {
167 $TS_CMD_PARTX -d --nr 0 $TS_DEVICE &&
168 echo "partitions deleted" ||
169 echo "Unable to delete partitions on $TS_DEVICE"
170 check_partition_count 0
171 $TS_CMD_PARTX -a --nr 0 $TS_DEVICE &&
172 echo "partitions added" ||
173 echo "Failed to add $TS_DEVICE partitions"
174 check_partition_count $PARTS
175 } >$TS_OUTPUT 2>&1
176 ts_finalize_subtest
177
178 ts_finalize