1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2016 Mike Pfister
4
+# MIT Licensed; see the LICENSE file in this repository.
5
+#
6
+
7
+test_description="Test ipfs repo fsck operations"
8
+
9
+. lib/test-lib.sh
10
+
11
+test_init_ipfs
12
+
13
+#############################
14
+# Test without daemon running
15
+#############################
16
+# Note: if api file isn't present we can assume the daemon isn't running
17
+
18
+# Try with all lock files present: repo.lock, api, and datastore/LOCK with
19
+# repo.lock and datastore/LOCK being empty
20
+test_expect_success "'ipfs repo fsck' succeeds with no daemon running empty
21
+repo.lock" '
22
+ mkdir -p $IPFS_PATH &&
23
+ mkdir -p $IPFS_PATH/datastore &&
24
+ touch $IPFS_PATH/datastore/LOCK &&
25
+ touch $IPFS_PATH/repo.lock &&
26
+ printf "/ip4/127.0.0.1/tcp/5001" > $IPFS_PATH/api &&
27
+ ipfs repo fsck > fsck_out_actual1
28
+'
29
+test_expect_success "'ipfs repo fsck' output looks good with no daemon" '
30
+ grep "Lockfiles have been removed." fsck_out_actual1
31
+'
32
+
33
+# Make sure the files are actually removed
34
+test_expect_success "'ipfs repo fsck' confirm file deletion" '
35
+ test ! -e "$IPFS_PATH/repo.lock" &&
36
+ test ! -e "$IPFS_PATH/datastore/LOCK" &&
37
+ test ! -e "$IPFS_PATH/api"
38
+'
39
+
40
+# Try with all lock files present: repo.lock, api, and datastore/LOCK with
41
+# repo.lock is non-zero TODO: this test is broken until we find consensus on the
42
+# non-zero repo.lock issue
43
+test_expect_success "'ipfs repo fsck' succeeds with no daemon running non-zero
44
+repo.lock" '
45
+ mkdir -p $IPFS_PATH &&
46
+ printf ":D" > $IPFS_PATH/repo.lock &&
47
+ touch $IPFS_PATH/datastore/LOCK &&
48
+ ipfs repo fsck > fsck_out_actual1b
49
+'
50
+test_expect_success "'ipfs repo fsck' output looks good with no daemon" '
51
+ grep "Lockfiles have been removed." fsck_out_actual1b
52
+'
53
+
54
+# Make sure the files are actually removed
55
+test_expect_success "'ipfs repo fsck' confirm file deletion" '
56
+ test ! -e "$IPFS_PATH/repo.lock" &&
57
+ test ! -e "$IPFS_PATH/datastore/LOCK" &&
58
+ test ! -e "$IPFS_PATH/api"
59
+'
60
+
61
+########################
62
+# Test for partial locks
63
+########################
64
+
65
+# Try with locks api and datastore/LOCK
66
+test_expect_success "'ipfs repo fsck' succeeds partial lock" '
67
+ printf "/ip4/127.0.0.1/tcp/5001" > $IPFS_PATH/api &&
68
+ touch $IPFS_PATH/datastore/LOCK &&
69
+ ipfs repo fsck > fsck_out_actual2
70
+'
71
+
72
+test_expect_success "'ipfs repo fsck' output looks good with no daemon" '
73
+ grep "Lockfiles have been removed." fsck_out_actual2
74
+'
75
+
76
+# Make sure the files are actually removed
77
+test_expect_success "'ipfs repo fsck' confirm file deletion" '
78
+ test ! -e "$IPFS_PATH/repo.lock" &&
79
+ test ! -e "$IPFS_PATH/datastore/LOCK" &&
80
+ test ! -e "$IPFS_PATH/api"
81
+'
82
+
83
+# Try with locks api and repo.lock
84
+test_expect_success "'ipfs repo fsck' succeeds partial lock" '
85
+ printf "/ip4/127.0.0.1/tcp/5001" > $IPFS_PATH/api &&
86
+ touch $IPFS_PATH/repo.lock &&
87
+ ipfs repo fsck > fsck_out_actual3
88
+'
89
+
90
+test_expect_success "'ipfs repo fsck' output looks good with no daemon" '
91
+ grep "Lockfiles have been removed." fsck_out_actual3
92
+'
93
+
94
+# Make sure the files are actually removed
95
+test_expect_success "'ipfs repo fsck' confirm file deletion" '
96
+ test ! -e "$IPFS_PATH/repo.lock" &&
97
+ test ! -e "$IPFS_PATH/datastore/LOCK" &&
98
+ test ! -e "$IPFS_PATH/api"
99
+'
100
+
101
+# Try with locks repo.lock and datastore
102
+test_expect_success "'ipfs repo fsck' succeeds partial lock" '
103
+ touch $IPFS_PATH/repo.lock &&
104
+ touch $IPFS_PATH/datastore/LOCK &&
105
+ ipfs repo fsck > fsck_out_actual4
106
+'
107
+
108
+test_expect_success "'ipfs repo fsck' output looks good with no daemon" '
109
+ grep "Lockfiles have been removed." fsck_out_actual4
110
+'
111
+
112
+# Make sure the files are actually removed
113
+test_expect_success "'ipfs repo fsck' confirm file deletion" '
114
+ test ! -e "$IPFS_PATH/repo.lock" &&
115
+ test ! -e "$IPFS_PATH/datastore/LOCK" &&
116
+ test ! -e "$IPFS_PATH/api"
117
+'
118
+
119
+#######################
120
+# Test for single locks
121
+#######################
122
+
123
+# Try with single locks repo.lock
124
+test_expect_success "'ipfs repo fsck' succeeds partial lock" '
125
+ touch $IPFS_PATH/repo.lock &&
126
+ ipfs repo fsck > fsck_out_actual5
127
+'
128
+test_expect_success "'ipfs repo fsck' output looks good with no daemon" '
129
+ grep "Lockfiles have been removed." fsck_out_actual5
130
+'
131
+
132
+# Make sure the files are actually removed
133
+test_expect_success "'ipfs repo fsck' confirm file deletion" '
134
+ test ! -e "$IPFS_PATH/repo.lock" &&
135
+ test ! -e "$IPFS_PATH/datastore/LOCK" &&
136
+ test ! -e "$IPFS_PATH/api"
137
+'
138
+
139
+# Try with single locks datastore/LOCK
140
+test_expect_success "'ipfs repo fsck' succeeds partial lock" '
141
+ touch $IPFS_PATH/datastore/LOCK &&
142
+ ipfs repo fsck > fsck_out_actual6
143
+'
144
+test_expect_success "'ipfs repo fsck' output looks good with no daemon" '
145
+ grep "Lockfiles have been removed." fsck_out_actual6
146
+'
147
+
148
+# Make sure the files are actually removed
149
+test_expect_success "'ipfs repo fsck' confirm file deletion" '
150
+ test ! -e "$IPFS_PATH/repo.lock" &&
151
+ test ! -e "$IPFS_PATH/datastore/LOCK" &&
152
+ test ! -e "$IPFS_PATH/api"
153
+'
154
+
155
+# Try with single lock api
156
+test_expect_success "'ipfs repo fsck' succeeds partial lock" '
157
+ printf "/ip4/127.0.0.1/tcp/5001" > $IPFS_PATH/api &&
158
+ ipfs repo fsck > fsck_out_actual7
159
+'
160
+
161
+test_expect_success "'ipfs repo fsck' output looks good with no daemon" '
162
+ grep "Lockfiles have been removed." fsck_out_actual7
163
+'
164
+
165
+# Make sure the files are actually removed
166
+test_expect_success "'ipfs repo fsck' confirm file deletion" '
167
+ test ! -e "$IPFS_PATH/repo.lock" &&
168
+ test ! -e "$IPFS_PATH/datastore/LOCK" &&
169
+ test ! -e "$IPFS_PATH/api"
170
+'
171
+
172
+##########################
173
+# Test with daemon running
174
+##########################
175
+
176
+test_launch_ipfs_daemon
177
+
178
+# Daemon is running -> command doesn't run
179
+test_expect_success "'ipfs repo fsck' fails with daemon running" '
180
+ ! (ipfs repo fsck 2>fsck_out_actual8 )
181
+
182
+'
183
+
184
+test_expect_success "'ipfs repo fsck' output looks good with daemon" '
185
+ grep "Error: ipfs daemon is running" fsck_out_actual8
186
+'
187
+
188
+test_kill_ipfs_daemon
189
+
190
+test_done