Skip to content

Commit 0bdee8d

Browse files
author
Zhen Li
authored
Merge pull request #338 from zhenlineo/1.2-download-server-if-not-exists
Only download server/cluster if the home/cluster path does not exsit
2 parents e74e618 + 8d8936b commit 0bdee8d

File tree

7 files changed

+60
-29
lines changed

7 files changed

+60
-29
lines changed

driver/src/test/java/org/neo4j/driver/v1/tck/DriverComplianceIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
public class DriverComplianceIT
4444
{
4545
@Rule
46-
TemporaryFolder folder = new TemporaryFolder( );
46+
TemporaryFolder folder = new TemporaryFolder();
4747

4848
@ClassRule
4949
public static TestNeo4j neo4j = new TestNeo4j();

driver/src/test/java/org/neo4j/driver/v1/tck/DriverSecurityComplianceSteps.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import static org.neo4j.driver.v1.Config.TrustStrategy.trustOnFirstUse;
5050
import static org.neo4j.driver.v1.tck.DriverComplianceIT.neo4j;
5151
import static org.neo4j.driver.v1.util.CertificateToolTest.generateSelfSignedCertificate;
52-
import static org.neo4j.driver.v1.util.Neo4jRunner.NEO4J_HOME;
52+
import static org.neo4j.driver.v1.util.Neo4jRunner.HOME_DIR;
5353
import static org.neo4j.driver.v1.util.Neo4jSettings.DEFAULT_TLS_CERT_PATH;
5454

5555
public class DriverSecurityComplianceSteps
@@ -228,7 +228,7 @@ public void aRunningNeo4jDatabaseUsingThatExactTrustedCertificate()
228228
Neo4jRunner.DEFAULT_URI,
229229
Config.build().withEncryptionLevel( EncryptionLevel.REQUIRED )
230230
.withTrustStrategy( trustCustomCertificateSignedBy(
231-
new File( NEO4J_HOME, DEFAULT_TLS_CERT_PATH ) ) )
231+
new File( HOME_DIR, DEFAULT_TLS_CERT_PATH ) ) )
232232
.toConfig() );
233233
}
234234

driver/src/test/java/org/neo4j/driver/v1/util/Neo4jRunner.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static java.util.Arrays.asList;
3535
import static org.junit.Assume.assumeTrue;
3636
import static org.neo4j.driver.v1.ConfigTest.deleteDefaultKnownCertFileIfExists;
37+
import static org.neo4j.driver.v1.util.FileTools.moveFile;
3738
import static org.neo4j.driver.v1.util.FileTools.updateProperties;
3839
import static org.neo4j.driver.v1.util.cc.CommandLineUtil.boltKitAvailable;
3940
import static org.neo4j.driver.v1.util.cc.CommandLineUtil.executeCommand;
@@ -56,8 +57,8 @@ public class Neo4jRunner
5657
private Neo4jSettings currentSettings = Neo4jSettings.TEST_SETTINGS;
5758

5859
public static final String TARGET_DIR = new File( "../target" ).getAbsolutePath();
59-
private static final String NEO4J_DIR = new File( TARGET_DIR, "neo4j" ).getAbsolutePath();
60-
public static String NEO4J_HOME;
60+
private static final String NEO4J_DIR = new File( TARGET_DIR, "test-server" ).getAbsolutePath();
61+
public static final String HOME_DIR = new File( NEO4J_DIR, "neo4jHome" ).getAbsolutePath();
6162

6263
/** Global runner controlling a single server, used to avoid having to restart the server between tests */
6364
public static synchronized Neo4jRunner getOrCreateGlobalRunner() throws IOException
@@ -111,25 +112,36 @@ public Driver driver()
111112
private void installNeo4j() throws IOException
112113
{
113114
// this is required for windows as python scripts cannot delete the file when it is used by driver tests
114-
deleteDefaultKnownCertFileIfExists();
115+
deleteDefaultKnownCertFileIfExists(); // Remove this once TrustOnFirstUse is removed.
115116

116-
List<String> commands = new ArrayList<>();
117-
commands.add( "neoctrl-install" );
118-
String[] split = NEOCTRL_ARGS.trim().split( "\\s+" );
119-
commands.addAll( asList( split ) );
120-
commands.add( NEO4J_DIR );
117+
File targetHomeFile = new File( HOME_DIR );
118+
if( targetHomeFile.exists() )
119+
{
120+
debug( "Found and using server installed at `%s`. ", HOME_DIR );
121+
}
122+
else
123+
{
124+
List<String> commands = new ArrayList<>();
125+
commands.add( "neoctrl-install" );
126+
String[] split = NEOCTRL_ARGS.trim().split( "\\s+" );
127+
commands.addAll( asList( split ) );
128+
commands.add( NEO4J_DIR );
121129

122-
NEO4J_HOME = executeCommand( commands ).trim();
123-
updateServerSettingsFile();
130+
String tempHomeDir = executeCommand( commands ).trim();
131+
debug( "Downloaded server at `%s`, now renaming to `%s`.", tempHomeDir, HOME_DIR );
132+
133+
moveFile( new File( tempHomeDir ), targetHomeFile );
134+
debug( "Installed server at `%s`.", HOME_DIR );
135+
}
124136

125-
debug( "Installed server at `%s`.", NEO4J_HOME );
137+
updateServerSettingsFile();
126138
}
127139

128140
private void startNeo4j() throws IOException
129141
{
130142
debug( "Starting server..." );
131-
executeCommand( "neoctrl-create-user", NEO4J_HOME, "neo4j", "neo4j" );
132-
executeCommand( "neoctrl-start", NEO4J_HOME );
143+
executeCommand( "neoctrl-create-user", HOME_DIR, "neo4j", "neo4j" );
144+
executeCommand( "neoctrl-start", HOME_DIR );
133145
debug( "Server started." );
134146
}
135147

@@ -146,7 +158,7 @@ public synchronized void stopNeo4j() throws IOException
146158
}
147159

148160
debug( "Stopping server..." );
149-
executeCommand( "neoctrl-stop", NEO4J_HOME );
161+
executeCommand( "neoctrl-stop", HOME_DIR );
150162
debug( "Server stopped." );
151163
}
152164

@@ -226,7 +238,7 @@ private void updateServerSettingsFile()
226238
return;
227239
}
228240

229-
File oldFile = new File( NEO4J_HOME, "conf/neo4j.conf" );
241+
File oldFile = new File( HOME_DIR, "conf/neo4j.conf" );
230242
try
231243
{
232244
debug( "Changing server properties file (for next start): %s", oldFile.getCanonicalPath() );
@@ -266,7 +278,7 @@ public void run()
266278
} ) );
267279
}
268280

269-
static void debug( String text, Object... args )
281+
public static void debug( String text, Object... args )
270282
{
271283
if ( debug )
272284
{

driver/src/test/java/org/neo4j/driver/v1/util/TestNeo4j.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,27 @@ public void updateEncryptionKeyAndCert( File key, File cert ) throws Exception
124124
{
125125
FileTools.copyFile( key, tlsKeyFile() );
126126
FileTools.copyFile( cert, tlsCertFile() );
127+
debug( "Updated neo4j key and certificate file." );
127128
runner.forceToRestart(); // needs to force to restart as no configuration changed
128129
}
129130

130131
public File tlsCertFile()
131132
{
132-
return new File( NEO4J_HOME, DEFAULT_TLS_CERT_PATH );
133+
return new File( HOME_DIR, DEFAULT_TLS_CERT_PATH );
133134
}
134135

135136
public File tlsKeyFile()
136137
{
137-
return new File( NEO4J_HOME, DEFAULT_TLS_KEY_PATH );
138+
return new File( HOME_DIR, DEFAULT_TLS_KEY_PATH );
138139
}
139140

140141
public void ensureProcedures( String jarName ) throws IOException
141142
{
142-
File procedureJar = new File( NEO4J_HOME, "plugins/" + jarName );
143+
File procedureJar = new File( HOME_DIR, "plugins/" + jarName );
143144
if( !procedureJar.exists() )
144145
{
145146
FileTools.copyFile( new File( TEST_RESOURCE_FOLDER_PATH, jarName ), procedureJar );
147+
debug( "Added a new procedure `%s`", jarName );
146148
runner.forceToRestart(); // needs to force to restart as no configuration changed
147149
}
148150
}

driver/src/test/java/org/neo4j/driver/v1/util/cc/ClusterControl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.neo4j.driver.v1.util.cc;
2020

2121
import java.nio.file.Path;
22+
23+
import static org.neo4j.driver.v1.util.Neo4jRunner.debug;
2224
import static org.neo4j.driver.v1.util.cc.CommandLineUtil.executeCommand;
2325

2426
final class ClusterControl

driver/src/test/java/org/neo4j/driver/v1/util/cc/ClusterRule.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ protected void before() throws Throwable
6464

6565
if ( !SharedCluster.exists() )
6666
{
67-
deleteClusterDir();
68-
6967
SharedCluster.install( parseNeo4jVersion(),
7068
CORE_COUNT, READ_REPLICA_COUNT, PASSWORD, INITIAL_PORT, CLUSTER_DIR );
7169

driver/src/test/java/org/neo4j/driver/v1/util/cc/SharedCluster.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Set;
2626

2727
import static java.lang.System.lineSeparator;
28+
import static org.neo4j.driver.v1.util.Neo4jRunner.debug;
2829

2930
final class SharedCluster
3031
{
@@ -54,7 +55,15 @@ static boolean exists()
5455
static void install( String neo4jVersion, int cores, int readReplicas, String password, int port, Path path )
5556
{
5657
assertClusterDoesNotExist();
57-
ClusterControl.installCluster( neo4jVersion, cores, readReplicas, password, port, path );
58+
if( path.toFile().exists() )
59+
{
60+
debug( "Found and using cluster installed at `%s`.", path );
61+
}
62+
else
63+
{
64+
ClusterControl.installCluster( neo4jVersion, cores, readReplicas, password, port, path );
65+
debug( "Downloaded cluster at `%s`.", path );
66+
}
5867
clusterInstance = new Cluster( path, password );
5968
}
6069

@@ -67,6 +76,7 @@ static void start() throws ClusterUnavailableException
6776
try
6877
{
6978
clusterInstance = clusterInstance.withMembers( members );
79+
debug( "Cluster started: %s.", members );
7080
}
7181
catch ( ClusterUnavailableException e )
7282
{
@@ -79,45 +89,52 @@ static void start( ClusterMember member )
7989
{
8090
assertClusterExists();
8191
ClusterControl.startClusterMember( member.getPath() );
92+
debug( "Cluster member at `%s` started.", member );
8293
}
8394

8495
static void stop()
8596
{
8697
assertClusterExists();
8798
ClusterControl.stopCluster( clusterInstance.getPath() );
99+
debug( "Cluster at `%s` stopped.", clusterInstance.getPath() );
88100
}
89101

90102
static void stop( ClusterMember member )
91103
{
92104
assertClusterExists();
93105
ClusterControl.stopClusterMember( member.getPath() );
106+
debug( "Cluster member at `%s` stopped.", member.getPath() );
94107
}
95108

96109
static void kill()
97110
{
98111
assertClusterExists();
99112
ClusterControl.killCluster( clusterInstance.getPath() );
113+
debug( "Cluster at `%s` killed.", clusterInstance.getPath() );
100114
}
101115

102116
static void kill( ClusterMember member )
103117
{
104118
assertClusterExists();
105119
ClusterControl.killClusterMember( member.getPath() );
120+
debug( "Cluster member at `%s` killed.", member.getPath() );
106121
}
107122

108123
private static Set<ClusterMember> parseStartCommandOutput( String output )
109124
{
110125
Set<ClusterMember> result = new HashSet<>();
111126

112127
String[] lines = output.split( lineSeparator() );
113-
for ( String line : lines )
128+
for ( int i = 0; i < lines.length; i++ )
114129
{
130+
String line = lines[i];
115131
String[] clusterMemberSplit = line.split( " " );
116132
if ( clusterMemberSplit.length != 3 )
117133
{
118-
throw new IllegalArgumentException(
119-
"Wrong start command output. " +
120-
"Expected to have 'http_uri bolt_uri path' in line '" + line + "'" );
134+
throw new IllegalArgumentException( String.format(
135+
"Wrong start command output found at line [%s]. " +
136+
"Expected to have 'http_uri bolt_uri path' on each line. " +
137+
"Command output:%n'%s'", i + 1, output ) );
121138
}
122139

123140
URI boltUri = URI.create( clusterMemberSplit[1] );

0 commit comments

Comments
 (0)