-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ConsistencyCheckToolTest.java
307 lines (270 loc) · 12.6 KB
/
ConsistencyCheckToolTest.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
/*
* Copyright (c) 2002-2017 "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.consistency;
import org.apache.commons.lang3.StringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.time.ZoneOffset;
import java.util.Properties;
import java.util.TimeZone;
import org.neo4j.consistency.ConsistencyCheckTool.ToolFailureException;
import org.neo4j.consistency.checking.full.ConsistencyFlags;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.helpers.progress.ProgressMonitorFactory;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.LogTimeZone;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.EphemeralFileSystemRule;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.logical_logs_location;
public class ConsistencyCheckToolTest
{
private final TestDirectory testDirectory = TestDirectory.testDirectory();
private final EphemeralFileSystemRule fs = new EphemeralFileSystemRule();
@Rule
public RuleChain ruleChain = RuleChain.outerRule( testDirectory ).around( fs );
@Test
public void runsConsistencyCheck() throws Exception
{
// given
File storeDir = testDirectory.directory();
String[] args = {storeDir.getPath()};
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
// when
runConsistencyCheckToolWith( service, args );
// then
verify( service ).runFullConsistencyCheck( eq( storeDir ), any( Config.class ),
any( ProgressMonitorFactory.class ), any( LogProvider.class ), any( FileSystemAbstraction.class ),
anyBoolean(), any( ConsistencyFlags.class ) );
}
@Test
public void consistencyCheckerLogUseSystemTimezoneIfConfigurable() throws Exception
{
TimeZone defaultTimeZone = TimeZone.getDefault();
try
{
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
Mockito.when( service.runFullConsistencyCheck( any( File.class ), any( Config.class ),
any( ProgressMonitorFactory.class ), any( LogProvider.class ), any( FileSystemAbstraction.class ),
eq( false ), any( ConsistencyFlags.class ) ) )
.then( invocationOnMock ->
{
LogProvider provider = invocationOnMock.getArgument( 3 );
provider.getLog( "test" ).info( "testMessage" );
return ConsistencyCheckService.Result.success( new File( StringUtils.EMPTY ) );
} );
File storeDir = testDirectory.directory();
File configFile = testDirectory.file( Config.DEFAULT_CONFIG_FILE_NAME );
Properties properties = new Properties();
properties.setProperty( GraphDatabaseSettings.log_timezone.name(), LogTimeZone.SYSTEM.name() );
properties.store( new FileWriter( configFile ), null );
String[] args = {storeDir.getPath(), "-config", configFile.getPath()};
checkLogRecordTimeZone( service, args, 5, "+0500" );
checkLogRecordTimeZone( service, args, -5, "-0500" );
}
finally
{
TimeZone.setDefault( defaultTimeZone );
}
}
@Test
public void appliesDefaultTuningConfigurationForConsistencyChecker() throws Exception
{
// given
File storeDir = testDirectory.directory();
String[] args = {storeDir.getPath()};
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
// when
runConsistencyCheckToolWith( service, args );
// then
ArgumentCaptor<Config> config = ArgumentCaptor.forClass( Config.class );
verify( service ).runFullConsistencyCheck( eq( storeDir ), config.capture(),
any( ProgressMonitorFactory.class ), any( LogProvider.class ), any( FileSystemAbstraction.class ),
anyBoolean(), any( ConsistencyFlags.class ) );
assertFalse( config.getValue().get( ConsistencyCheckSettings.consistency_check_property_owners ) );
}
@Test
public void passesOnConfigurationIfProvided() throws Exception
{
// given
File storeDir = testDirectory.directory();
File configFile = testDirectory.file( Config.DEFAULT_CONFIG_FILE_NAME );
Properties properties = new Properties();
properties.setProperty( ConsistencyCheckSettings.consistency_check_property_owners.name(), "true" );
properties.store( new FileWriter( configFile ), null );
String[] args = {storeDir.getPath(), "-config", configFile.getPath()};
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
// when
runConsistencyCheckToolWith( service, args );
// then
ArgumentCaptor<Config> config = ArgumentCaptor.forClass( Config.class );
verify( service ).runFullConsistencyCheck( eq( storeDir ), config.capture(),
any( ProgressMonitorFactory.class ), any( LogProvider.class ), any( FileSystemAbstraction.class ),
anyBoolean(), any(ConsistencyFlags.class) );
assertTrue( config.getValue().get( ConsistencyCheckSettings.consistency_check_property_owners ) );
}
@Test
public void exitWithFailureIndicatingCorrectUsageIfNoArgumentsSupplied() throws Exception
{
// given
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
String[] args = {};
try
{
// when
runConsistencyCheckToolWith( service, args );
fail( "should have thrown exception" );
}
catch ( ConsistencyCheckTool.ToolFailureException e )
{
// then
assertThat( e.getMessage(), containsString( "USAGE:" ) );
}
}
@Test
public void exitWithFailureIfConfigSpecifiedButConfigFileDoesNotExist() throws Exception
{
// given
File configFile = testDirectory.file( "nonexistent_file" );
String[] args = {testDirectory.directory().getPath(), "-config", configFile.getPath()};
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
try
{
// when
runConsistencyCheckToolWith( service, args );
fail( "should have thrown exception" );
}
catch ( ConsistencyCheckTool.ToolFailureException e )
{
// then
assertThat( e.getMessage(), containsString( "Could not read configuration file" ) );
assertThat( e.getCause(), instanceOf( IOException.class ) );
}
verifyZeroInteractions( service );
}
@Test( expected = ToolFailureException.class )
public void failWhenStoreWasNonCleanlyShutdown() throws Exception
{
createGraphDbAndKillIt( Config.defaults() );
runConsistencyCheckToolWith( fs.get(), testDirectory.graphDbDir().getAbsolutePath() );
}
@Test( expected = ToolFailureException.class )
public void failOnNotCleanlyShutdownStoreWithLogsInCustomRelativeLocation() throws Exception
{
File customConfigFile = testDirectory.file( "customConfig" );
Config customConfig = Config.defaults( logical_logs_location, "otherLocation" );
createGraphDbAndKillIt( customConfig );
MapUtil.store( customConfig.getRaw(), customConfigFile );
String[] args = {testDirectory.graphDbDir().getPath(), "-config", customConfigFile.getPath()};
runConsistencyCheckToolWith( fs.get(), args );
}
@Test( expected = ToolFailureException.class )
public void failOnNotCleanlyShutdownStoreWithLogsInCustomAbsoluteLocation() throws Exception
{
File customConfigFile = testDirectory.file( "customConfig" );
File otherLocation = testDirectory.directory( "otherLocation" );
Config customConfig = Config.defaults( logical_logs_location, otherLocation.getAbsolutePath() );
createGraphDbAndKillIt( customConfig );
MapUtil.store( customConfig.getRaw(), customConfigFile );
String[] args = {testDirectory.graphDbDir().getPath(), "-config", customConfigFile.getPath()};
runConsistencyCheckToolWith( fs.get(), args );
}
private void checkLogRecordTimeZone( ConsistencyCheckService service, String[] args, int hoursShift,
String timeZoneSuffix ) throws ToolFailureException, IOException
{
TimeZone.setDefault( TimeZone.getTimeZone( ZoneOffset.ofHours( hoursShift ) ) );
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream( outputStream );
runConsistencyCheckToolWith( service, printStream, args );
String logLine = readLogLine( outputStream );
assertTrue( logLine, logLine.contains( timeZoneSuffix ) );
}
private String readLogLine( ByteArrayOutputStream outputStream ) throws IOException
{
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( outputStream.toByteArray() );
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( byteArrayInputStream ) );
return bufferedReader.readLine();
}
private void createGraphDbAndKillIt( Config config ) throws Exception
{
final GraphDatabaseService db = new TestGraphDatabaseFactory()
.setFileSystem( fs.get() )
.newImpermanentDatabaseBuilder( testDirectory.graphDbDir() )
.setConfig( config.getRaw() )
.newGraphDatabase();
try ( Transaction tx = db.beginTx() )
{
db.createNode( label( "FOO" ) );
db.createNode( label( "BAR" ) );
tx.success();
}
fs.snapshot( db::shutdown );
}
private void runConsistencyCheckToolWith( FileSystemAbstraction fileSystem, String... args )
throws IOException, ToolFailureException
{
new ConsistencyCheckTool( mock( ConsistencyCheckService.class ), fileSystem, mock( PrintStream.class),
mock( PrintStream.class ) ).run( args );
}
private void runConsistencyCheckToolWith( ConsistencyCheckService
consistencyCheckService, String... args ) throws ToolFailureException, IOException
{
runConsistencyCheckToolWith( consistencyCheckService, mock( PrintStream.class ), args );
}
private void runConsistencyCheckToolWith( ConsistencyCheckService
consistencyCheckService, PrintStream printStream, String... args ) throws ToolFailureException, IOException
{
try ( FileSystemAbstraction fileSystemAbstraction = new DefaultFileSystemAbstraction() )
{
new ConsistencyCheckTool( consistencyCheckService, fileSystemAbstraction, printStream, printStream )
.run( args );
}
}
}