-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
OnlineBackupCommandTest.java
324 lines (269 loc) · 13.4 KB
/
OnlineBackupCommandTest.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
/*
* 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 Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.backup;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;
import org.neo4j.commandline.admin.CommandFailed;
import org.neo4j.commandline.admin.CommandLocator;
import org.neo4j.commandline.admin.IncorrectUsage;
import org.neo4j.consistency.ConsistencyCheckService;
import org.neo4j.commandline.admin.Usage;
import org.neo4j.consistency.ConsistencyCheckSettings;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.HostnamePort;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.test.rule.TestDirectory;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.stub;
import static org.mockito.Mockito.verify;
import static org.neo4j.consistency.ConsistencyCheckService.Result.success;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.cypher_planner;
public class OnlineBackupCommandTest
{
@Rule
public TestDirectory testDirectory = TestDirectory.testDirectory();
private final BackupTool tool = mock( BackupTool.class );
private Path configDir;
private ConsistencyCheckService consistencyCheckService;
@Before
public void setUp() throws Exception
{
configDir = testDirectory.directory( "config-dir" ).toPath();
}
@Test
public void shouldNotRequestForensics() throws CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
execute( "--to=/" );
verify( tool )
.executeBackup( any(), any(), any(), any(), anyLong(), eq( false ) );
}
@Test
public void shouldDefaultFromToDefaultBackupAddress()
throws CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
execute( "--to=/" );
verify( tool ).executeBackup(
eq( new HostnamePort( "localhost", 6362 ) ), any(), any(), any(), anyLong(), anyBoolean() );
}
@Test
public void shouldDefaultPortAndPassHost() throws CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
execute( "--from=foo.bar.server", "--to=/" );
verify( tool ).executeBackup(
eq( new HostnamePort( "foo.bar.server", 6362 ) ), any(), any(), any(), anyLong(), anyBoolean() );
}
@Test
public void shouldAcceptAHostWithATrailingColon() throws CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
execute( "--from=foo.bar.server:", "--to=/" );
verify( tool ).executeBackup(
eq( new HostnamePort( "foo.bar.server", 6362 ) ), any(), any(), any(), anyLong(), anyBoolean() );
}
@Test
public void shouldDefaultHostAndPassPort() throws CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
execute( "--from=:1234", "--to=/" );
verify( tool ).executeBackup(
eq( new HostnamePort( "localhost", 1234 ) ), any(), any(), any(), anyLong(), anyBoolean() );
}
@Test
public void shouldPassHostAndPort() throws CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
execute( "--from=foo.bar.server:1234", "--to=/" );
verify( tool ).executeBackup(
eq( new HostnamePort( "foo.bar.server", 1234 ) ), any(), any(), any(), anyLong(), anyBoolean() );
}
@Test
public void shouldPassDestination() throws CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
execute( "--to=/some/path/or/other" );
verify( tool ).executeBackup(
any(), eq( new File( "/some/path/or/other" ) ), any(), any(), anyLong(), anyBoolean() );
}
@Test
public void shouldTreatToArgumentAsMandatory() throws CommandFailed
{
try
{
execute();
fail( "exception expected" );
}
catch ( IncorrectUsage incorrectUsage )
{
assertThat( incorrectUsage.getMessage(), containsString( "to" ) );
}
}
@Test
public void shouldNotAskForConsistencyCheckIfNotSpecified()
throws CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
execute( "--check-consistency=false", "--to=/" );
verify( tool ).executeBackup( any(), any(), eq( ConsistencyCheck.NONE ), any(), anyLong(), anyBoolean() );
}
@Test
public void shouldAskForConsistencyCheckIfSpecified() throws Exception
{
consistencyCheckService = mock( ConsistencyCheckService.class );
stub( consistencyCheckService.runFullConsistencyCheck( any(), any(), any(), any(), any(), any(), anyBoolean(),
any() ) ).toReturn( success( null ) );
ArgumentCaptor<ConsistencyCheck> captor = ArgumentCaptor.forClass( ConsistencyCheck.class );
stub( tool.executeBackup( any(), any(), captor.capture(), any(), anyLong(), anyBoolean() ) ).toReturn( null );
execute( "--check-consistency", "--to=/" );
captor.getValue().runFull( null, null, null, null, null, null, false );
verify( consistencyCheckService ).runFullConsistencyCheck( any(), any(), any(), any(), any(), any(),
anyBoolean(), eq( new File(".").getCanonicalFile()) );
}
@Test
public void shouldSpecifyReportDirIfSpecified() throws Exception
{
consistencyCheckService = mock( ConsistencyCheckService.class );
stub( consistencyCheckService.runFullConsistencyCheck( any(), any(), any(), any(), any(), any(), anyBoolean(),
any() ) ).toReturn( success( null ) );
ArgumentCaptor<ConsistencyCheck> captor = ArgumentCaptor.forClass( ConsistencyCheck.class );
stub( tool.executeBackup( any(), any(), captor.capture(), any(), anyLong(), anyBoolean() ) ).toReturn( null );
execute( "--check-consistency", "--to=/", "--cc-report-dir=/some/dir" );
captor.getValue().runFull( null, null, null, null, null, null, false );
verify( consistencyCheckService ).runFullConsistencyCheck( any(), any(), any(), any(), any(), any(),
anyBoolean(), eq( new File("/some/dir") ) );
}
@Test
public void shouldIncludeGraphDatabaseSettings()
throws IOException, CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
ArgumentCaptor<Config> config = ArgumentCaptor.forClass( Config.class );
execute( "--to=/" );
verify( tool ).executeBackup( any(), any(), any(), config.capture(), anyLong(), anyBoolean() );
assertThat( config.getValue().getSettingsClasses(), hasItem( GraphDatabaseSettings.class ) );
}
@Test
public void shouldIncludeConsistencyCheckSettings()
throws IOException, CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
ArgumentCaptor<Config> config = ArgumentCaptor.forClass( Config.class );
execute( "--to=/" );
verify( tool ).executeBackup( any(), any(), any(), config.capture(), anyLong(), anyBoolean() );
assertThat( config.getValue().getSettingsClasses(), hasItem( ConsistencyCheckSettings.class ) );
}
@Test
public void shouldReadStandardConfig() throws IOException, CommandFailed, IncorrectUsage, BackupTool
.ToolFailureException
{
Files.write( configDir.resolve( "neo4j.conf" ), asList( cypher_planner.name() + "=RULE" ) );
ArgumentCaptor<Config> config = ArgumentCaptor.forClass( Config.class );
execute( "--to=/" );
verify( tool ).executeBackup( any(), any(), any(), config.capture(), anyLong(), anyBoolean() );
assertThat( config.getValue().get( cypher_planner ), is( "RULE" ) );
}
@Test
public void shouldAugmentConfig()
throws IOException, CommandFailed, IncorrectUsage, BackupTool.ToolFailureException
{
Path extraConf = testDirectory.directory( "someOtherDir" ).toPath().resolve( "extra.conf" );
Files.write( extraConf, asList( cypher_planner.name() + "=RULE" ) );
ArgumentCaptor<Config> config = ArgumentCaptor.forClass( Config.class );
execute( "--additional-config=" + extraConf, "--to=/" );
verify( tool ).executeBackup( any(), any(), any(), config.capture(), anyLong(), anyBoolean() );
assertThat( config.getValue().get( cypher_planner ), is( "RULE" ) );
}
@Test
public void shouldDefaultTimeoutToTwentyMinutes()
throws BackupTool.ToolFailureException, CommandFailed, IncorrectUsage
{
execute( "--to=/" );
verify( tool ).executeBackup( any(), any(), any(), any(), eq( MINUTES.toMillis( 20 ) ), anyBoolean() );
}
@Test
public void shouldInterpretAUnitlessTimeoutAsSeconds()
throws BackupTool.ToolFailureException, CommandFailed, IncorrectUsage
{
execute( "--timeout=10", "--to=/" );
verify( tool ).executeBackup( any(), any(), any(), any(), eq( SECONDS.toMillis( 10 ) ), anyBoolean() );
}
@Test
public void shouldParseATimeoutWithUnits()
throws BackupTool.ToolFailureException, CommandFailed, IncorrectUsage
{
execute( "--timeout=10h", "--to=/" );
verify( tool ).executeBackup( any(), any(), any(), any(), eq( HOURS.toMillis( 10 ) ), anyBoolean() );
}
@Test
public void shouldPrintNiceHelp() throws Throwable
{
Usage usage = new Usage( "neo4j-admin", mock( CommandLocator.class ) );
Consumer<String> out = mock( Consumer.class );
usage.printUsageForCommand( new OnlineBackupCommand.Provider(), out );
verify( out ).accept( "usage: neo4j-admin backup [--from=<address>] --to=<backup-path>\n" +
" [--check-consistency[=<true|false>]]\n" +
" [--cc-report-dir=<directory>]\n" +
" [--additional-config=<config-file-path>]\n" +
" [--timeout=<timeout>]" );
verify( out ).accept( "" );
verify( out ).accept( "Perform a backup, over the network, from a running Neo4j server into a local\n" +
"copy of the database store (the backup). Neo4j Server must be configured to run\n" +
"a backup service. See http://neo4j.com/docs/operations-manual/current/backup/\n" +
"for more details.\n" +
"\n" +
"options:\n" +
" --from=<address> Host and port of Neo4j.\n" +
" [default:localhost:6362]\n" +
" --to=<backup-path> Directory where the backup will be\n" +
" made; if there is already a backup\n" +
" present an incremental backup will be\n" +
" attempted.\n" +
" --check-consistency=<true|false> If a consistency check should be\n" +
" made. [default:true]\n" +
" --cc-report-dir=<directory> Directory where consistency report\n" +
" will be written. [default:.]\n" +
" --additional-config=<config-file-path> Configuration file to supply\n" +
" additional configuration in.\n" +
" [default:]\n" +
" --timeout=<timeout> Timeout in the form <time>[ms|s|m|h],\n" +
" where the default unit is seconds.\n" +
" [default:20m]" );
verifyNoMoreInteractions( out );
}
private void execute( String... args ) throws IncorrectUsage, CommandFailed
{
new OnlineBackupCommand( tool, Paths.get( "/some/path" ), configDir, consistencyCheckService )
.execute( args );
}
}