Skip to content

Commit

Permalink
fixup! WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tristantarrant committed Sep 9, 2021
1 parent e534cfc commit a3dabf6
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 42 deletions.
Expand Up @@ -47,6 +47,21 @@ public static void onlyOnOS(OS... oses) {
throw new AssumptionViolatedException("Skipping test on " + os);
}

public static void skipSinceJDK(int major) {
int version = getJDKVersion();
if (version >= major) {
throw new AssumptionViolatedException("Skipping test on JDK " + version);
}
}

private static int getJDKVersion() {
String[] parts = System.getProperty("java.version").replaceAll("[^0-9\\.]", "").split("\\.");
int version = Integer.parseInt(parts[0]);
if (version == 1)
version = Integer.parseInt(parts[1]);
return version;
}

private static class IgnoreStatement extends Statement {

private final Description method;
Expand Down
Expand Up @@ -425,5 +425,4 @@ public void pause(int server) {
public RemoteCacheManager createRemoteCacheManager(ConfigurationBuilder builder) {
return new RemoteCacheManager(builder.build());
}

}
Expand Up @@ -6,6 +6,8 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
Expand All @@ -31,6 +33,8 @@
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.infinispan.commons.logging.Log;
import org.infinispan.commons.test.CommonsTestingUtil;
import org.infinispan.commons.test.Exceptions;
Expand Down Expand Up @@ -249,7 +253,7 @@ protected void start(String name, File rootDir, File configurationFile) {
}

public InfinispanGenericContainer getContainer(int i) {
if(containers.length <= i) {
if (containers.length <= i) {
throw new IllegalStateException("Container " + i + " has not been initialized");
}
return containers[i];
Expand Down Expand Up @@ -282,17 +286,17 @@ private GenericContainer<?> createContainer(int i, Consumer<OutputFrame>... logC
}

GenericContainer<?> container = new GenericContainer<>(image)
.withCreateContainerCmdModifier(cmd -> {
cmd.getHostConfig().withMounts(
Arrays.asList(new Mount().withSource(this.volumes[i]).withTarget(serverPath()).withType(MountType.VOLUME))
);
if (IMAGE_MEMORY != null) {
cmd.getHostConfig().withMemory(IMAGE_MEMORY);
}
if (IMAGE_MEMORY_SWAP != null) {
cmd.getHostConfig().withMemorySwap(IMAGE_MEMORY_SWAP);
}
});
.withCreateContainerCmdModifier(cmd -> {
cmd.getHostConfig().withMounts(
Arrays.asList(new Mount().withSource(this.volumes[i]).withTarget(serverPath()).withType(MountType.VOLUME))
);
if (IMAGE_MEMORY != null) {
cmd.getHostConfig().withMemory(IMAGE_MEMORY);
}
if (IMAGE_MEMORY_SWAP != null) {
cmd.getHostConfig().withMemorySwap(IMAGE_MEMORY_SWAP);
}
});

// Replace 99 with index of server to debug
if (i == 99) {
Expand Down Expand Up @@ -423,4 +427,36 @@ private String serverPath() {
private String serverPathFrom(String path) {
return String.format("%s/%s", serverPath(), path);
}

@Override
public void syncFilesFromServer(int server, String path) {
try (InputStream is = DockerClientFactory.instance().client().copyArchiveFromContainerCmd(containers[server].getContainerId(), INFINISPAN_SERVER_HOME + "/server/" + path).exec()) {
TarArchiveInputStream tar = new TarArchiveInputStream(is);
Path basePath = getRootDir().toPath().resolve(Integer.toString(server));
Util.recursiveFileRemove(basePath.resolve(path));
for (TarArchiveEntry entry = tar.getNextTarEntry(); entry != null; entry = tar.getNextTarEntry()) {
Path entryPath = basePath.resolve(entry.getName());
if (entry.isDirectory()) {
entryPath.toFile().mkdirs();
} else {
OutputStream os = Files.newOutputStream(entryPath);
for (int b = tar.read(); b >= 0; b = tar.read()) {
os.write(b);
}
Util.close(os);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public String syncFilesToServer(int server, String path) {
Path local = Paths.get(path);
DockerClientFactory.instance().client().copyArchiveToContainerCmd(containers[server].getContainerId())
.withHostResource(path)
.withRemotePath("/tmp").exec();
return Paths.get("/tmp").resolve(local.getFileName()).toString();
}
}
Expand Up @@ -227,4 +227,15 @@ private static void failMissingMembers(List<Address> members, Address address, i

throw new IllegalStateException(sb.toString());
}

@Override
public void syncFilesFromServer(int server, String dir) {
//NO-OP
}

@Override
public String syncFilesToServer(int server, String path) {
//NO-OP
return path;
}
}
Expand Up @@ -242,4 +242,14 @@ private static <T> T sync(CompletionStage<T> stage) {
return Exceptions.unchecked(() -> stage.toCompletableFuture().get(SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS));
}

@Override
public void syncFilesFromServer(int server, String dir) {
//NO-OP
}

@Override
public String syncFilesToServer(int server, String path) {
//NO-OP
return path;
}
}
Expand Up @@ -25,6 +25,10 @@ public InfinispanGenericContainer(GenericContainer genericContainer) {
this.genericContainer = genericContainer;
}

public String getContainerId() {
return containerId;
}

public void pause() {
dockerClient().pauseContainerCmd(this.containerId).exec();
}
Expand Down
Expand Up @@ -137,4 +137,13 @@ public interface InfinispanServerDriver {
* @return the number of seconds after which a server start/stop is considered to timeout
*/
int getTimeout();

/**
* Synchronizes files from the server to the local filesystem
* @param server the server
* @param dir the path relative to the server root
*/
void syncFilesFromServer(int server, String dir);

String syncFilesToServer(int server, String path);
}
Expand Up @@ -16,6 +16,7 @@
import org.infinispan.commons.util.Util;
import org.infinispan.server.test.core.AeshTestConnection;
import org.infinispan.server.test.core.AeshTestShell;
import org.infinispan.server.test.core.Common;
import org.infinispan.server.test.core.ServerRunMode;
import org.infinispan.server.test.junit4.InfinispanServerRule;
import org.infinispan.server.test.junit4.InfinispanServerRuleBuilder;
Expand All @@ -35,10 +36,7 @@ public class CliIT {
@ClassRule
public static InfinispanServerRule SERVERS =
InfinispanServerRuleBuilder.config("configuration/ClusteredServerTest.xml")
.mavenArtifacts(
"org.openjdk.nashorn:nashorn-core:15.3",
"org.ow2.asm:asm:7.3.1",
"org.ow2.asm:asm-util:7.3.1")
.mavenArtifacts(Common.NASHORN_DEPS)
.runMode(ServerRunMode.CONTAINER)
.build();

Expand Down
Expand Up @@ -36,10 +36,12 @@
class AbstractMultiClusterIT {

protected final String config;
protected final String[] mavenArtifacts;
protected Cluster source, target;

public AbstractMultiClusterIT(String config) {
public AbstractMultiClusterIT(String config, String... mavenArtifacts) {
this.config = config;
this.mavenArtifacts = mavenArtifacts;
}

@After
Expand All @@ -49,23 +51,23 @@ public void cleanup() throws Exception {
}

protected void startSourceCluster() {
source = new Cluster(new ClusterConfiguration(config, 2, 0), getCredentials());
source.start("source");
source = new Cluster(new ClusterConfiguration(config, 2, 0, mavenArtifacts), getCredentials());
source.start(this.getClass().getName() + "-source");
}

protected void stopSourceCluster() throws Exception {
if (source != null)
source.stop("source");
source.stop(this.getClass().getName() + "-source");
}

protected void startTargetCluster() {
target = new Cluster(new ClusterConfiguration(config, 2, 1000), getCredentials());
target.start("target");
target = new Cluster(new ClusterConfiguration(config, 2, 1000, mavenArtifacts), getCredentials());
target.start(this.getClass().getName() + "-target");
}

protected void stopTargetCluster() throws Exception {
if (target != null)
target.stop("target");
target.stop(this.getClass().getName() + "-target");
}

protected int getCacheSize(String cacheName, RestClient restClient) {
Expand Down Expand Up @@ -93,8 +95,8 @@ protected KeyValuePair<String, String> getCredentials() {
}

protected static class ClusterConfiguration extends InfinispanServerTestConfiguration {
public ClusterConfiguration(String configurationFile, int numServers, int portOffset) {
super(configurationFile, numServers, ServerRunMode.EMBEDDED, new Properties(), null, null,
public ClusterConfiguration(String configurationFile, int numServers, int portOffset, String[] mavenArtifacts) {
super(configurationFile, numServers, mavenArtifacts != null ? ServerRunMode.CONTAINER : ServerRunMode.EMBEDDED, new Properties(), mavenArtifacts, null,
false, false, false, Collections.emptyList(), null, portOffset, new String[]{});
}
}
Expand All @@ -119,7 +121,7 @@ static class Cluster {
simpleConfiguration.properties().put(prop, sysProps.getProperty(prop));
}
}
this.driver = ServerRunMode.DEFAULT.newDriver(simpleConfiguration);
this.driver = simpleConfiguration.runMode().newDriver(simpleConfiguration);
}

void start(String name) {
Expand Down
Expand Up @@ -53,7 +53,7 @@ public class BackupManagerIT extends AbstractMultiClusterIT {
static final int NUM_ENTRIES = 10;

public BackupManagerIT() {
super("configuration/ClusteredServerTest.xml");
super("configuration/ClusteredServerTest.xml", Common.NASHORN_DEPS);
}

@BeforeClass
Expand Down Expand Up @@ -83,7 +83,8 @@ public void testManagerBackupUpload() throws Exception {
assertEquals(202, response.getStatus());
return awaitCreated(() -> cm.getRestore(name));
},
this::assertWildcardContent
this::assertWildcardContent,
false
);
}

Expand All @@ -105,7 +106,8 @@ public void testManagerBackupFromFile() throws Exception {
assertEquals(202, response.getStatus());
return awaitCreated(() -> cm.getRestore(name));
},
this::assertWildcardContent
this::assertWildcardContent,
true
);
}

Expand Down Expand Up @@ -140,7 +142,8 @@ public void testManagerBackupParameters() throws Exception {
assertEquals("[\"weak-volatile\"]", await(client.counters()).getBody());
assertEquals(404, await(client.schemas().get("schema.proto")).getStatus());
assertEquals("[]", await(client.tasks().list(RestTaskClient.ResultType.USER)).getBody());
}
},
false
);
}

Expand Down Expand Up @@ -207,7 +210,8 @@ public void testManagerRestoreParameters() throws Exception {
assertEquals("[\"___protobuf_metadata\",\"memcachedCache\",\"___script_cache\"]", await(client.caches()).getBody());
assertEquals("[]", await(client.counters()).getBody());
assertEquals(404, await(client.schemas().get("schema.proto")).getStatus());
}
},
false
);
}

Expand All @@ -228,7 +232,8 @@ public void testClusterBackupUpload() throws Exception {
assertEquals(202, response.getStatus());
return awaitCreated(() -> c.getRestore(name));
},
this::assertWildcardContent
this::assertWildcardContent,
false
);
}

Expand All @@ -249,7 +254,8 @@ public void testClusterBackupFromFile() throws Exception {
assertEquals(202, response.getStatus());
return awaitCreated(() -> c.getRestore(name));
},
this::assertWildcardContent
this::assertWildcardContent,
true
);
}

Expand All @@ -264,7 +270,7 @@ private static RestResponse awaitCreated(Supplier<CompletionStage<RestResponse>>
private void performTest(Function<RestClient, RestResponse> backupAndDownload,
Function<RestClient, RestResponse> delete,
BiFunction<File, RestClient, RestResponse> restore,
Consumer<RestClient> assertTargetContent) throws Exception {
Consumer<RestClient> assertTargetContent, boolean syncToServer) throws Exception {
// Start the source cluster
startSourceCluster();
RestClient client = source.getClient();
Expand Down Expand Up @@ -300,6 +306,10 @@ private void performTest(Function<RestClient, RestResponse> backupAndDownload,
}
getResponse.close();

if (syncToServer) {
backupZip = new File(target.driver.syncFilesToServer(0, backupZip.getAbsolutePath()));
}

// Upload the backup to the target cluster
RestResponse restoreResponse = restore.apply(backupZip, client);
assertEquals(restoreResponse.getBody(), 201, restoreResponse.getStatus());
Expand Down Expand Up @@ -402,6 +412,7 @@ private void assertCounter(RestClient client, String name, Element type, Storage

private void assertNoServerBackupFilesExist(Cluster cluster) {
for (int i = 0; i < 2; i++) {
cluster.driver.syncFilesFromServer(i, "data");
Path root = cluster.driver.getRootDir().toPath();
File workingDir = root.resolve(Integer.toString(i)).resolve("data").resolve("backups").toFile();
assertTrue(workingDir.isDirectory());
Expand Down

0 comments on commit a3dabf6

Please sign in to comment.