Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that shell closes the query result
This is required in order to log the query.
- Loading branch information
Showing
5 changed files
with
211 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
enterprise/query-logging/src/test/java/org/neo4j/kernel/impl/query/ShellQueryLoggingIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
* 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 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.kernel.impl.query; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TestRule; | ||
import org.junit.runners.model.Statement; | ||
|
||
import org.neo4j.graphdb.Result; | ||
import org.neo4j.graphdb.Transaction; | ||
import org.neo4j.graphdb.factory.GraphDatabaseBuilder; | ||
import org.neo4j.graphdb.factory.GraphDatabaseFactory; | ||
import org.neo4j.graphdb.factory.GraphDatabaseSettings; | ||
import org.neo4j.kernel.configuration.Settings; | ||
import org.neo4j.shell.ShellClient; | ||
import org.neo4j.shell.ShellException; | ||
import org.neo4j.shell.ShellLobby; | ||
import org.neo4j.shell.impl.SystemOutput; | ||
import org.neo4j.shell.kernel.GraphDatabaseShellServer; | ||
import org.neo4j.test.DatabaseRule; | ||
import org.neo4j.test.EphemeralFileSystemRule; | ||
import org.neo4j.test.ImpermanentDatabaseRule; | ||
import org.neo4j.test.TargetDirectory; | ||
import org.neo4j.test.TestGraphDatabaseFactory; | ||
|
||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.rules.RuleChain.outerRule; | ||
import static org.neo4j.kernel.impl.query.QueryLoggerIT.readAllLines; | ||
import static org.neo4j.test.TargetDirectory.testDirForTest; | ||
|
||
public class ShellQueryLoggingIT | ||
{ | ||
private final EphemeralFileSystemRule fs = new EphemeralFileSystemRule(); | ||
private final TargetDirectory.TestDirectory dir = testDirForTest( getClass() ); | ||
private final DatabaseRule db = new ImpermanentDatabaseRule() | ||
{ | ||
@Override | ||
protected void configure( GraphDatabaseFactory factory ) | ||
{ | ||
((TestGraphDatabaseFactory) factory).setFileSystem( fs.get() ); | ||
} | ||
|
||
@Override | ||
protected void configure( GraphDatabaseBuilder builder ) | ||
{ | ||
builder.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE ); | ||
builder.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory().getPath() ); | ||
} | ||
}; | ||
@Rule | ||
public final TestRule order = outerRule( dir ).around( fs ).around( db ) | ||
.around( ( base, description ) -> new Statement() | ||
{ | ||
@Override | ||
public void evaluate() throws Throwable | ||
{ | ||
try | ||
{ | ||
base.evaluate(); | ||
} | ||
catch ( Throwable e ) | ||
{ | ||
System.err.println( "<Shell Output>" ); | ||
System.err.print( out ); | ||
System.err.println( "</Shell Output>" ); | ||
throw e; | ||
} | ||
} | ||
} ); | ||
private final StringWriter out = new StringWriter(); | ||
private GraphDatabaseShellServer server; | ||
private ShellClient client; | ||
|
||
@Before | ||
public void setup() throws Exception | ||
{ | ||
server = new GraphDatabaseShellServer( db.getGraphDatabaseAPI() ); | ||
SystemOutput output = new SystemOutput( new PrintWriter( out ) ); | ||
client = ShellLobby.newClient( server, new HashMap<>(), output, action -> () -> | ||
{ | ||
// we don't need any handling of CTRL+C | ||
} ); | ||
out.getBuffer().setLength( 0 ); // clear the output (remove the welcome message) | ||
} | ||
|
||
@After | ||
public void shutdown() throws Exception | ||
{ | ||
client.shutdown(); | ||
server.shutdown(); | ||
} | ||
|
||
@Test | ||
public void shouldLogQueryWhenExecutingDirectly() throws Exception | ||
{ | ||
// given | ||
String query = "CREATE (foo:Foo{bar:'baz'}) RETURN foo.bar"; | ||
|
||
// when | ||
try ( Result result = db.execute( query ); | ||
PrintWriter out = new PrintWriter( this.out ) ) | ||
{ | ||
result.writeAsStringTo( out ); | ||
} | ||
|
||
// then | ||
assertThat( out.toString(), allOf( | ||
containsString( "Nodes created: 1" ), | ||
containsString( "Properties set: 1" ), | ||
containsString( "Labels added: 1" ) ) ); | ||
assertThat( queryLog(), contains( containsString( query ) ) ); | ||
} | ||
|
||
@Test | ||
public void shouldLogReadQuery() throws Exception | ||
{ | ||
// given | ||
try ( Transaction tx = db.beginTx() ) | ||
{ | ||
db.createNode().setProperty( "hello", "world" ); | ||
|
||
tx.success(); | ||
} | ||
|
||
// then | ||
shouldLogQuery( "MATCH (n) RETURN n" ); | ||
} | ||
|
||
@Test | ||
public void shouldLogWriteQuery() throws Exception | ||
{ | ||
shouldLogQuery( "CREATE (:Foo{bar:'baz'})" ); | ||
} | ||
|
||
private void shouldLogQuery( String query ) throws ShellException, IOException | ||
{ | ||
// when | ||
client.evaluate( query + ';' ); | ||
|
||
// then | ||
assertThat( queryLog(), contains( containsString( query ) ) ); | ||
} | ||
|
||
private List<String> queryLog() throws IOException | ||
{ | ||
return readAllLines( fs.get(), queryLogFile() ); | ||
} | ||
|
||
private File queryLogFile() | ||
{ | ||
return new File( logsDirectory(), "query.log" ); | ||
} | ||
|
||
private File logsDirectory() | ||
{ | ||
File logsDir = new File( dir.graphDbDir(), "logs" ); | ||
fs.get().mkdirs( logsDir ); | ||
return logsDir; | ||
} | ||
} |