Skip to content

Commit

Permalink
Improve crate test cluster
Browse files Browse the repository at this point in the history
* port ranges can be defined in builder
* leave default behavior as it is
* improve builder pattern
* Add unit test checking port ranges
  • Loading branch information
mackerl authored and seut committed Dec 20, 2024
1 parent 9079969 commit 212fee7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changes for Crate-Java-Testing
==============================

Unreleased
==========

- Added support for custom port ranges for httpPort, transportPort, and psqlPort on CrateTestCluster Builder.

2023-10-04 0.11.0
=================

Expand Down
82 changes: 59 additions & 23 deletions src/main/java/io/crate/testing/CrateTestCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,36 @@ public class CrateTestCluster extends ExternalResource {
private final String crateVersion;
private final Map<String, Object> commandLineArguments;

private final int transportPortsFrom;
private final int transportPortsTo;

private final int httpPortsFrom;
private final int httpPortsTo;

private final int psqlPortsFrom;
private final int psqlPortsTo;

private volatile CrateTestServer[] servers;

private CrateTestCluster(int numberOfNodes,
String clusterName,
Path workingDir,
DownloadSource downloadSource,
Map<String, Object> settings,
String hostAddress,
boolean keepWorkingDir,
String crateVersion,
Map<String, Object> commandLineArguments) {
this.numberOfNodes = numberOfNodes;
this.clusterName = clusterName;
this.workingDir = workingDir;
this.downloadSource = downloadSource;
this.settings = settings;
this.hostAddress = hostAddress;
this.keepWorkingDir = keepWorkingDir;
this.crateVersion = crateVersion;
this.commandLineArguments = commandLineArguments;
private CrateTestCluster(Builder builder) {
this.numberOfNodes = builder.numberOfNodes;
this.clusterName = builder.clusterName;
this.workingDir = builder.workingDir;
this.downloadSource = builder.downloadSource;
this.settings = builder.settings;
this.hostAddress = builder.hostAddress;
this.keepWorkingDir = builder.keepWorkingDir;
this.crateVersion = builder.crateVersion;
this.commandLineArguments = builder.commandLineArguments;

this.transportPortsFrom=builder.transportPortsFrom;
this.transportPortsTo=builder.transportPortsTo;

this.httpPortsFrom=builder.httpPortsFrom;
this.httpPortsTo=builder.httpPortsTo;

this.psqlPortsFrom=builder.psqlPortsFrom;
this.psqlPortsTo=builder.psqlPortsTo;
}

public static class Builder {
Expand All @@ -103,6 +113,15 @@ public static class Builder {
private String crateVersion;
private Map<String, Object> commandLineArguments;

private int transportPortsFrom=4200;
private int transportPortsTo=4400;

private int httpPortsFrom=4500;
private int httpPortsTo=4600;

private int psqlPortsFrom=5432;
private int psqlPortsTo=5532;

private Builder(DownloadSource downloadSource) {
if (downloadSource == null) {
throw new IllegalArgumentException("No download source given (version, git-ref, url, file)");
Expand Down Expand Up @@ -156,6 +175,24 @@ public Builder settings(Map<String, Object> settings) {
return this;
}

public Builder httpPort(int from, int to) {
this.httpPortsFrom=from;
this.httpPortsTo=to;
return this;
}

public Builder transportPort(int from, int to) {
this.transportPortsFrom=from;
this.transportPortsTo=to;
return this;
}

public Builder psqlPort(int from, int to) {
this.psqlPortsFrom=from;
this.psqlPortsTo=to;
return this;
}

public Builder numberOfNodes(int numberOfNodes) {
if (numberOfNodes <= 0) {
throw new IllegalArgumentException(String.format("invalid number of nodes: %d", numberOfNodes));
Expand Down Expand Up @@ -189,8 +226,7 @@ public Builder commandLineArguments(Map<String, Object> commandLineArguments) {
}

public CrateTestCluster build() {
return new CrateTestCluster(numberOfNodes, clusterName, workingDir, downloadSource,
settings, hostAddress, keepWorkingDir, crateVersion, commandLineArguments);
return new CrateTestCluster(this);
}
}

Expand Down Expand Up @@ -225,9 +261,9 @@ private CrateTestServer[] buildServers() {
int httpPorts[] = new int[numberOfNodes];
int psqlPorts[] = new int[numberOfNodes];
for (int i = 0; i < numberOfNodes; i++) {
transportPorts[i] = Utils.randomAvailablePort(4200, 4400);
httpPorts[i] = Utils.randomAvailablePort(4500, 4600);
psqlPorts[i] = Utils.randomAvailablePort(5432, 5532);
transportPorts[i] = Utils.randomAvailablePort(transportPortsFrom, transportPortsTo);
httpPorts[i] = Utils.randomAvailablePort(httpPortsFrom, httpPortsTo);
psqlPorts[i] = Utils.randomAvailablePort(psqlPortsFrom, psqlPortsTo);
}
CrateTestServer[] servers = new CrateTestServer[numberOfNodes];

Expand Down
30 changes: 30 additions & 0 deletions src/test/java/io/crate/testing/ClusterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

import static io.crate.testing.Constants.CRATE_VERSION_FOR_TESTS;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.core.Is.is;

public class ClusterTest extends BaseTest {
Expand Down Expand Up @@ -67,6 +69,34 @@ public void testClusterBuilder() throws Throwable {
}
}

@Test
public void testClusterBuilderCustomPort() throws Throwable {
CrateTestCluster cluster = CrateTestCluster.fromVersion(VERSION)
.httpPort(14010, 14011)
.transportPort(14012, 14013)
.psqlPort(14014, 14015)
.build();

try {
cluster.before();
prepare(cluster);
Collection<CrateTestServer> servers = cluster.servers();
CrateTestServer server = servers.iterator().next();

assertThat("HTTP port should be in the range 14010-14011", server.httpPort(), greaterThanOrEqualTo(14010));
assertThat("HTTP port should be in the range 14010-14011", server.httpPort(), lessThanOrEqualTo(14011));

assertThat("Transport port should be in the range 14012-14013", server.transportPort(), greaterThanOrEqualTo(14012));
assertThat("Transport port should be in the range 14012-14013", server.transportPort(), lessThanOrEqualTo(14013));

assertThat("PSQL port should be in the range 14014-14015", server.psqlPort(), greaterThanOrEqualTo(14014));
assertThat("PSQL port should be in the range 14014-14015", server.psqlPort(), lessThanOrEqualTo(14015));

} finally {
cluster.after();
}
}

@Test
public void testBuilderKeepWorkingDir() throws Throwable {
CrateTestCluster testCluster = CrateTestCluster
Expand Down

0 comments on commit 212fee7

Please sign in to comment.