master
sh 318 lines 9.54 KB
Raw
1 #!/usr/bin/env bash
2 #
3 # Copyright (c) 2014 Jeromy Johnson
4 # MIT Licensed; see the LICENSE file in this repository.
5 #
6
7 test_description="Test ipfs repo operations"
8
9 . lib/test-lib.sh
10
11 test_init_ipfs --empty-repo=false
12 test_launch_ipfs_daemon_without_network
13
14 test_expect_success "'ipfs repo gc' succeeds" '
15 ipfs repo gc >gc_out_actual
16 '
17
18 test_expect_success "'ipfs add afile' succeeds" '
19 echo "some text" >afile &&
20 HASH=`ipfs add -q afile`
21 '
22
23 test_expect_success "added file was pinned" '
24 ipfs pin ls --type=recursive >actual &&
25 grep "$HASH" actual
26 '
27
28 test_expect_success "'ipfs repo gc' succeeds" '
29 ipfs repo gc >gc_out_actual
30 '
31
32 test_expect_success "'ipfs repo gc' looks good (patch root)" '
33 test_should_not_contain "removed $HASH" gc_out_actual
34 '
35
36 test_expect_success "'ipfs repo gc' doesn't remove file" '
37 ipfs cat "$HASH" >out &&
38 test_cmp out afile
39 '
40
41 test_expect_success "'ipfs pin rm' succeeds" '
42 ipfs pin rm -r "$HASH" >actual1
43 '
44
45 test_expect_success "'ipfs pin rm' output looks good" '
46 echo "unpinned $HASH" >expected1 &&
47 test_cmp expected1 actual1
48 '
49
50 test_expect_success "ipfs repo gc fully reverse ipfs add (part 1)" '
51 ipfs repo gc &&
52 random-data -size=100000 -seed=41 >gcfile &&
53 find "$IPFS_PATH/blocks" -type f -name "*.data" | sort -u > expected_blocks &&
54 hash=$(ipfs add -q gcfile) &&
55 ipfs pin rm -r $hash &&
56 ipfs repo gc
57 '
58 test_expect_success "'ipfs repo gc --silent' succeeds (no output)" '
59 echo "should be empty" >bfile &&
60 HASH2=`ipfs add -q bfile` &&
61 ipfs cat "$HASH2" >expected11 &&
62 test_cmp expected11 bfile &&
63 ipfs pin rm -r "$HASH2" &&
64 ipfs repo gc --silent >gc_out_empty &&
65 test_cmp /dev/null gc_out_empty &&
66 test_must_fail ipfs cat "$HASH2" 2>err_expected1 &&
67 grep "Error: block was not found locally (offline): ipld: could not find $HASH2" err_expected1
68 '
69
70 test_kill_ipfs_daemon
71
72 test_expect_success "ipfs repo gc fully reverse ipfs add (part 2)" '
73 find "$IPFS_PATH/blocks" -type f -name "*.data" | sort -u > actual_blocks &&
74 test_cmp expected_blocks actual_blocks
75 '
76
77 test_launch_ipfs_daemon_without_network
78
79 test_expect_success "file no longer pinned" '
80 ipfs pin ls --type=recursive --quiet >actual2 &&
81 test_expect_code 1 grep $HASH actual2
82 '
83
84 test_expect_success "recursively pin afile(default action)" '
85 HASH=`ipfs add -q afile` &&
86 ipfs pin add "$HASH"
87 '
88
89 test_expect_success "recursively pin rm afile (default action)" '
90 ipfs pin rm "$HASH"
91 '
92
93 test_expect_success "recursively pin afile" '
94 ipfs pin add -r "$HASH"
95 '
96
97 test_expect_success "pinning directly should fail now" '
98 echo "Error: pin: $HASH already pinned recursively" >expected3 &&
99 test_must_fail ipfs pin add -r=false "$HASH" 2>actual3 &&
100 test_cmp expected3 actual3
101 '
102
103 test_expect_success "'ipfs pin rm -r=false <hash>' should fail" '
104 echo "Error: $HASH is pinned recursively" >expected4
105 test_must_fail ipfs pin rm -r=false "$HASH" 2>actual4 &&
106 test_cmp expected4 actual4
107 '
108
109 test_expect_success "remove recursive pin, add direct" '
110 echo "unpinned $HASH" >expected5 &&
111 ipfs pin rm -r "$HASH" >actual5 &&
112 test_cmp expected5 actual5 &&
113 ipfs pin add -r=false "$HASH"
114 '
115
116 test_expect_success "remove direct pin" '
117 echo "unpinned $HASH" >expected6 &&
118 ipfs pin rm "$HASH" >actual6 &&
119 test_cmp expected6 actual6
120 '
121
122 test_expect_success "'ipfs repo gc' removes file" '
123 ipfs block stat $HASH &&
124 ipfs repo gc &&
125 test_must_fail ipfs block stat $HASH
126 '
127
128 # Convert all to a base32-multihash as refs local outputs cidv1 raw
129 # Technically converting refs local output would suffice, but this is more
130 # future proof if we ever switch to adding the files with cid-version 1.
131 test_expect_success "'ipfs refs local' no longer shows file" '
132 EMPTY_DIR=QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn &&
133 HASH_MH=`cid-fmt -b base32 "%M" "$HASH"` &&
134 HARDCODED_HASH_MH=`cid-fmt -b base32 "%M" "QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y"` &&
135 EMPTY_DIR_MH=`cid-fmt -b base32 "%M" "$EMPTY_DIR"` &&
136 HASH_WELCOME_DOCS_MH=`cid-fmt -b base32 "%M" "$HASH_WELCOME_DOCS"` &&
137 ipfs refs local | cid-fmt -b base32 --filter "%M" >actual8 &&
138 grep "$HARDCODED_HASH_MH" actual8 &&
139 grep "$EMPTY_DIR_MH" actual8 &&
140 grep "$HASH_WELCOME_DOCS_MH" actual8 &&
141 test_must_fail grep "$HASH_MH" actual8
142 '
143
144 test_expect_success "adding multiblock random file succeeds" '
145 random-data -size=1000000 >multiblock &&
146 MBLOCKHASH=`ipfs add -q multiblock`
147 '
148
149 test_expect_success "'ipfs pin ls --type=indirect' is correct" '
150 ipfs refs "$MBLOCKHASH" >refsout &&
151 ipfs refs -r "$HASH_WELCOME_DOCS" >>refsout &&
152 sed -i"~" "s/\(.*\)/\1 indirect/g" refsout &&
153 ipfs pin ls --type=indirect >indirectpins &&
154 test_sort_cmp refsout indirectpins
155 '
156
157 test_expect_success "pin something directly" '
158 echo "ipfs is so awesome" >awesome &&
159 DIRECTPIN=`ipfs add -q awesome` &&
160 echo "unpinned $DIRECTPIN" >expected9 &&
161 ipfs pin rm -r "$DIRECTPIN" >actual9 &&
162 test_cmp expected9 actual9 &&
163
164 echo "pinned $DIRECTPIN directly" >expected10 &&
165 ipfs pin add -r=false "$DIRECTPIN" >actual10 &&
166 test_cmp expected10 actual10
167 '
168
169 test_expect_success "'ipfs pin ls --type=direct' is correct" '
170 echo "$DIRECTPIN direct" >directpinexpected &&
171 ipfs pin ls --type=direct >directpinout &&
172 test_sort_cmp directpinexpected directpinout
173 '
174
175 test_expect_success "'ipfs pin ls --type=recursive' is correct" '
176 echo "$MBLOCKHASH" >rp_expected &&
177 echo "$HASH_WELCOME_DOCS" >>rp_expected &&
178 echo "$EMPTY_DIR" >>rp_expected &&
179 sed -i"~" "s/\(.*\)/\1 recursive/g" rp_expected &&
180 ipfs pin ls --type=recursive >rp_actual &&
181 test_sort_cmp rp_expected rp_actual
182 '
183
184 test_expect_success "'ipfs pin ls --type=all --quiet' is correct" '
185 cat directpinout >allpins &&
186 cat rp_actual >>allpins &&
187 cat indirectpins >>allpins &&
188 cut -f1 -d " " allpins | sort | uniq >> allpins_uniq_hashes &&
189 ipfs pin ls --type=all --quiet >actual_allpins &&
190 test_sort_cmp allpins_uniq_hashes actual_allpins
191 '
192
193 test_expect_success "'ipfs refs --unique' is correct" '
194 mkdir -p uniques &&
195 echo "content1" > uniques/file1 &&
196 echo "content1" > uniques/file2 &&
197 ROOT=$(ipfs add -r -Q uniques) &&
198 ipfs refs --unique $ROOT >expected &&
199 ipfs add -q uniques/file1 >unique_hash &&
200 test_cmp expected unique_hash
201 '
202
203 test_expect_success "'ipfs refs --unique --recursive' is correct" '
204 mkdir -p a/b/c &&
205 echo "c1" > a/f1 &&
206 echo "c1" > a/b/f1 &&
207 echo "c1" > a/b/c/f1 &&
208 echo "c2" > a/b/c/f2 &&
209 ROOT=$(ipfs add -r -Q a) &&
210 ipfs refs --unique --recursive $ROOT >refs_output &&
211 wc -l refs_output | sed "s/^ *//g" >line_count &&
212 echo "4 refs_output" >expected &&
213 test_cmp expected line_count || test_fsh cat refs_output
214 '
215
216 test_expect_success "'ipfs refs --recursive (bigger)'" '
217 mkdir -p b/c/d/e &&
218 echo "content1" >b/f &&
219 echo "content1" >b/c/f1 &&
220 echo "content1" >b/c/d/f2 &&
221 echo "content2" >b/c/f2 &&
222 echo "content2" >b/c/d/f1 &&
223 echo "content2" >b/c/d/e/f &&
224 cp -r b b2 && mv b2 b/b2 &&
225 cp -r b b3 && mv b3 b/b3 &&
226 cp -r b b4 && mv b4 b/b4 &&
227 hash=$(ipfs add -r -Q b) &&
228 ipfs refs -r "$hash" >refs_output &&
229 wc -l refs_output | sed "s/^ *//g" >actual &&
230 echo "79 refs_output" >expected &&
231 test_cmp expected actual || test_fsh cat refs_output
232 '
233
234 test_expect_success "'ipfs refs --unique --recursive (bigger)'" '
235 ipfs refs -r "$hash" >refs_output &&
236 sort refs_output | uniq >expected &&
237 ipfs refs -r -u "$hash" >actual &&
238 test_sort_cmp expected actual || test_fsh cat refs_output
239 '
240
241 get_field_num() {
242 field=$1
243 file=$2
244 num=$(grep "$field" "$file" | awk '{ print $2 }')
245 echo $num
246 }
247
248 test_expect_success "'ipfs repo stat' succeeds" '
249 ipfs repo stat > repo-stats
250 '
251
252 test_expect_success "repo stats came out correct" '
253 grep "RepoPath" repo-stats &&
254 grep "RepoSize" repo-stats &&
255 grep "NumObjects" repo-stats &&
256 grep "Version" repo-stats &&
257 grep "StorageMax" repo-stats
258 '
259
260 test_expect_success "'ipfs repo stat --human' succeeds" '
261 ipfs repo stat --human > repo-stats-human
262 '
263
264 test_expect_success "repo stats --human came out correct" '
265 grep "RepoPath" repo-stats-human &&
266 grep -E "RepoSize:\s*([0-9]*[.])?[0-9]+\s+?(B|kB|MB|GB|TB|PB|EB)" repo-stats-human &&
267 grep "NumObjects" repo-stats-human &&
268 grep "Version" repo-stats-human &&
269 grep -E "StorageMax:\s*([0-9]*[.])?[0-9]+\s+?(B|kB|MB|GB|TB|PB|EB)" repo-stats-human ||
270 test_fsh cat repo-stats-human
271 '
272
273 test_expect_success "'ipfs repo stat' after adding a file" '
274 ipfs add repo-stats &&
275 ipfs repo stat > repo-stats-2
276 '
277
278 test_expect_success "repo stats are updated correctly" '
279 test $(get_field_num "RepoSize" repo-stats-2) -ge $(get_field_num "RepoSize" repo-stats)
280 '
281
282 test_expect_success "'ipfs repo stat --size-only' succeeds" '
283 ipfs repo stat --size-only > repo-stats-size-only
284 '
285
286 test_expect_success "repo stats came out correct for --size-only" '
287 test_should_contain "RepoSize" repo-stats-size-only &&
288 test_should_contain "StorageMax" repo-stats-size-only &&
289 test_should_not_contain "RepoPath" repo-stats-size-only &&
290 test_should_not_contain "NumObjects" repo-stats-size-only &&
291 test_should_not_contain "Version" repo-stats-size-only
292 '
293
294 test_expect_success "'ipfs repo version' succeeds" '
295 ipfs repo version > repo-version
296 '
297
298 test_expect_success "repo version came out correct" '
299 egrep "^ipfs repo version fs-repo@[0-9]+" repo-version >/dev/null
300 '
301
302 test_expect_success "'ipfs repo version -q' succeeds" '
303 ipfs repo version -q > repo-version-q
304 '
305 test_expect_success "repo version came out correct" '
306 egrep "^fs-repo@[0-9]+" repo-version-q >/dev/null
307 '
308
309 test_kill_ipfs_daemon
310
311 test_expect_success "remove Datastore.StorageMax from config" '
312 ipfs config Datastore.StorageMax ""
313 '
314 test_expect_success "'ipfs repo stat' still succeeds" '
315 ipfs repo stat > repo-stats
316 '
317
318 test_done