-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
DumpCommandTest.java
334 lines (298 loc) · 11.5 KB
/
DumpCommandTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.commandline.dbms;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Predicate;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.neo4j.commandline.admin.CommandFailed;
import org.neo4j.commandline.admin.IncorrectUsage;
import org.neo4j.dbms.archive.Dumper;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.kernel.internal.StoreLocker;
import org.neo4j.test.rule.TestDirectory;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.neo4j.dbms.DatabaseManagementSystemSettings.data_directory;
import static org.neo4j.dbms.archive.TestUtils.withPermissions;
public class DumpCommandTest
{
@Rule
public TestDirectory testDirectory = TestDirectory.testDirectory();
private Path homeDir;
private Path configDir;
private Path archive;
private Dumper dumper;
@Before
public void setUp() throws Exception
{
homeDir = testDirectory.directory( "home-dir" ).toPath();
configDir = testDirectory.directory( "config-dir" ).toPath();
archive = testDirectory.file( "some-archive.dump" ).toPath();
dumper = mock( Dumper.class );
}
@Test
public void shouldDumpTheDatabaseToTheArchive() throws CommandFailed, IncorrectUsage, IOException
{
execute( "foo.db" );
verify( dumper ).dump( eq( homeDir.resolve( "data/databases/foo.db" ) ), eq( archive ), any() );
}
@Test
public void shouldCalculateTheDatabaseDirectoryFromConfig() throws IOException, CommandFailed, IncorrectUsage
{
Path dataDir = testDirectory.directory( "some-other-path" ).toPath();
Path databaseDir = dataDir.resolve( "databases/foo.db" );
Files.createDirectories( databaseDir );
Files.write( configDir.resolve( "neo4j.conf" ),
asList( format( "%s=%s", data_directory.name(), dataDir.toString().replace( '\\', '/' ) ) ) );
execute( "foo.db" );
verify( dumper ).dump( eq( databaseDir ), any(), any() );
}
@Test
public void shouldCalculateTheArchiveNameIfPassedAnExistingDirectory()
throws CommandFailed, IncorrectUsage, IOException
{
File to = testDirectory.directory( "some-dir" );
new DumpCommand( homeDir, configDir, dumper ).execute( new String[]{"--database=" + "foo.db", "--to=" + to} );
verify( dumper ).dump( any( Path.class ), eq( to.toPath().resolve( "foo.db.dump" ) ), any() );
}
@Test
public void shouldNotCalculateTheArchiveNameIfPassedAnExistingFile()
throws CommandFailed, IncorrectUsage, IOException
{
Files.createFile( archive );
execute( "foo.db" );
verify( dumper ).dump( any(), eq( archive ), any() );
}
@Test
public void shouldRespectTheStoreLock() throws IOException, IncorrectUsage, CommandFailed
{
Path databaseDirectory = homeDir.resolve( "data/databases/foo.db" );
Files.createDirectories( databaseDirectory );
StoreLocker storeLocker = new StoreLocker( new DefaultFileSystemAbstraction() );
storeLocker.checkLock( databaseDirectory.toFile() );
try
{
execute( "foo.db" );
fail( "expected exception" );
}
catch ( CommandFailed e )
{
assertThat( e.getMessage(), equalTo( "the database is in use -- stop Neo4j and try again" ) );
}
finally
{
storeLocker.release();
}
}
@Test
public void shouldReleaseTheStoreLockAfterDumping() throws IOException, IncorrectUsage, CommandFailed
{
Path databaseDirectory = homeDir.resolve( "data/databases/foo.db" );
Files.createDirectories( databaseDirectory );
execute( "foo.db" );
assertCanLockStore( databaseDirectory );
}
@Test
public void shouldReleaseTheStoreLockEvenIfThereIsAnError() throws IOException, IncorrectUsage
{
doThrow( IOException.class ).when( dumper ).dump( any(), any(), any() );
Path databaseDirectory = homeDir.resolve( "data/databases/foo.db" );
Files.createDirectories( databaseDirectory );
try
{
execute( "foo.db" );
}
catch ( CommandFailed ignored )
{
}
assertCanLockStore( databaseDirectory );
}
@Test
public void shouldNotAccidentallyCreateTheDatabaseDirectoryAsASideEffectOfStoreLocking()
throws CommandFailed, IncorrectUsage, IOException
{
Path databaseDirectory = homeDir.resolve( "data/databases/foo.db" );
doAnswer( ignored ->
{
assertThat( Files.exists( databaseDirectory ), equalTo( false ) );
return null;
} ).when( dumper ).dump( any(), any(), any() );
execute( "foo.db" );
}
@Test
public void shouldReportAHelpfulErrorIfWeDontHaveWritePermissionsForLock()
throws IOException, IncorrectUsage
{
assumeFalse( "We haven't found a way to reliably tests permissions on Windows", SystemUtils.IS_OS_WINDOWS );
Path databaseDirectory = homeDir.resolve( "data/databases/foo.db" );
Files.createDirectories( databaseDirectory );
StoreLocker storeLocker = new StoreLocker( new DefaultFileSystemAbstraction() );
storeLocker.checkLock( databaseDirectory.toFile() );
try ( Closeable ignored =
withPermissions( databaseDirectory.resolve( StoreLocker.STORE_LOCK_FILENAME ), emptySet() ) )
{
execute( "foo.db" );
fail( "expected exception" );
}
catch ( CommandFailed e )
{
assertThat( e.getMessage(), equalTo( "you do not have permission to dump the database -- is Neo4j " +
"running as a different user?" ) );
}
finally
{
storeLocker.release();
}
}
@Test
public void shouldExcludeTheStoreLockFromTheArchiveToAvoidProblemsWithReadingLockedFilesOnWindows()
throws CommandFailed, IncorrectUsage, IOException
{
doAnswer( invocation ->
{
//noinspection unchecked
Predicate<Path> exclude = (Predicate<Path>) invocation.getArgumentAt( 2, Predicate.class );
assertThat( exclude.test( Paths.get( StoreLocker.STORE_LOCK_FILENAME ) ), is( true ) );
assertThat( exclude.test( Paths.get( "some-other-file" ) ), is( false ) );
return null;
} ).when( dumper ).dump( any(), any(), any() );
execute( "foo.db" );
}
@Test
public void shouldObjectIfTheDatabaseArgumentIsMissing() throws CommandFailed
{
try
{
new DumpCommand( null, null, null ).execute( new String[]{"--to=something"} );
fail( "expected exception" );
}
catch ( IncorrectUsage e )
{
assertThat( e.getMessage(), equalTo( "Missing argument 'database'" ) );
}
}
@Test
public void shouldObjectIfTheArchiveArgumentIsMissing() throws CommandFailed
{
try
{
new DumpCommand( homeDir, configDir, null ).execute( new String[]{"--database=something"} );
fail( "expected exception" );
}
catch ( IncorrectUsage e )
{
assertThat( e.getMessage(), equalTo( "Missing argument 'to'" ) );
}
}
@Test
public void shouldGiveAClearErrorIfTheArchiveAlreadyExists() throws IOException, IncorrectUsage
{
doThrow( new FileAlreadyExistsException( "the-archive-path" ) ).when( dumper ).dump( any(), any(), any() );
try
{
execute( null );
fail( "expected exception" );
}
catch ( CommandFailed e )
{
assertThat( e.getMessage(), equalTo( "archive already exists: the-archive-path" ) );
}
}
@Test
public void shouldGiveAClearMessageIfTheDatabaseDoesntExist() throws IOException, IncorrectUsage
{
doThrow( new NoSuchFileException( homeDir.resolve( "data/databases/foo.db" ).toString() ) )
.when( dumper ).dump( any(), any(), any() );
try
{
execute( "foo.db" );
fail( "expected exception" );
}
catch ( CommandFailed e )
{
assertThat( e.getMessage(), equalTo( "database does not exist: foo.db" ) );
}
}
@Test
public void shouldGiveAClearMessageIfTheArchivesParentDoesntExist() throws IOException, IncorrectUsage
{
doThrow( new NoSuchFileException( archive.getParent().toString() ) ).when( dumper ).dump( any(), any(), any() );
try
{
execute( "foo.db" );
fail( "expected exception" );
}
catch ( CommandFailed e )
{
assertThat( e.getMessage(),
equalTo( "unable to dump database: NoSuchFileException: " + archive.getParent() ) );
}
}
@Test
public void
shouldWrapIOExceptionsCarefulllyBecauseCriticalInformationIsOftenEncodedInTheirNameButMissingFromTheirMessage()
throws IOException, IncorrectUsage
{
doThrow( new IOException( "the-message" ) ).when( dumper ).dump( any(), any(), any() );
try
{
execute( null );
fail( "expected exception" );
}
catch ( CommandFailed e )
{
assertThat( e.getMessage(), equalTo( "unable to dump database: IOException: the-message" ) );
}
}
private void execute( final String database ) throws IncorrectUsage, CommandFailed, AccessDeniedException
{
new DumpCommand( homeDir, configDir, dumper )
.execute( new String[]{"--database=" + database, "--to=" + archive} );
}
private void assertCanLockStore( Path databaseDirectory ) throws IOException
{
StoreLocker storeLocker = new StoreLocker( new DefaultFileSystemAbstraction() );
storeLocker.checkLock( databaseDirectory.toFile() );
storeLocker.release();
}
}