Skip to content

Commit

Permalink
[#8965] Apply SharedTestLifeCycle to Cassandra
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jun 24, 2022
1 parent d66ac86 commit f884324
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.common.profiler.sql.DefaultSqlParser;
import com.navercorp.pinpoint.common.profiler.sql.SqlParser;
import com.navercorp.pinpoint.test.plugin.shared.AfterSharedClass;

import com.navercorp.pinpoint.test.plugin.shared.SharedTestBeforeAllResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.CassandraContainer;

import java.lang.reflect.Method;
import java.util.Properties;

import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.event;
import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.sql;
Expand All @@ -46,13 +46,13 @@
* @author HyunGil Jeong
*/
public abstract class CassandraDatastaxITBase {

private static final Logger logger = LogManager.getLogger(CassandraDatastaxITBase.class);
// com.navercorp.pinpoint.plugin.cassandra.CassandraConstants
private static final String CASSANDRA = "CASSANDRA";
private static final String CASSANDRA_EXECUTE_QUERY = "CASSANDRA_EXECUTE_QUERY";

private static final String TEST_KEYSPACE = "mykeyspace";
private static final String TEST_TABLE = "mytable";
private static final String TEST_KEYSPACE = CassandraITConstants.TEST_KEYSPACE;
private static final String TEST_TABLE = CassandraITConstants.TEST_TABLE;
private static final String TEST_COL_ID = "id";
private static final String TEST_COL_VALUE = "value";

Expand All @@ -66,71 +66,35 @@ public abstract class CassandraDatastaxITBase {

private static String CASSANDRA_ADDRESS = HOST + ":" + CassandraContainer.CQL_PORT;

public static CassandraContainer cassandra;

private static int PORT;
private static Cluster cluster;

public static int getPORT() {
return PORT;
}

public static void setPORT(int port) {
CassandraDatastaxITBase.PORT = port;
@SharedTestBeforeAllResult
public static void setBeforeAllResult(Properties beforeAllResult) {
logger.info("CassandraServer result {}", beforeAllResult);
PORT = Integer.parseInt(beforeAllResult.getProperty("PORT"));
}

private static int PORT;

public static void startCassandra(String dockerImageVersion) {
Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable());

cassandra = new CassandraContainer(dockerImageVersion);
cassandra.start();

// String containerIpAddress = cassandra.getContainerIpAddress();
final int port = cassandra.getMappedPort(CassandraContainer.CQL_PORT);
setPORT(port);

cluster = newCluster(HOST, getPORT());
init(cluster);
public static int getPort() {
return PORT;
}

@AfterSharedClass
public static void sharedTearDown() {
if (cassandra != null) {
cassandra.stop();
}
}

@BeforeClass
public static void setup() {
CASSANDRA_ADDRESS = HOST + ":" + getPORT();
cluster = newCluster(HOST, getPORT());
CASSANDRA_ADDRESS = HOST + ":" + getPort();
cluster = CassandraServer.newCluster(HOST, getPort());
logger.info("setup cluster {}", CASSANDRA_ADDRESS);
}

@AfterClass
public static void tearDown() {
if (cluster == null) {
if (cluster != null) {
cluster.close();
}
}

private static Cluster newCluster(String host, int port) {
Cluster.Builder builder = Cluster.builder();
builder.addContactPoint(host);
builder.withPort(port);
builder.withoutMetrics();
return builder.build();
}

public static void init(Cluster cluster) {
try (Session systemSession = cluster.connect()) {
String createKeyspace = String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = " +
"{'class':'SimpleStrategy','replication_factor':'1'};", TEST_KEYSPACE);
systemSession.execute(createKeyspace);
String createTable = String.format("CREATE TABLE %s.%s (id text, value text, PRIMARY KEY(id))", TEST_KEYSPACE, TEST_TABLE);
systemSession.execute(createTable);
}
}

private static Session openSession(Cluster cluster) {
return cluster.connect(TEST_KEYSPACE);
Expand Down Expand Up @@ -260,4 +224,5 @@ public void testBatchStatement_and_StatementWrapper() throws Exception {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import com.navercorp.pinpoint.test.plugin.ImportPlugin;
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass;

import org.junit.BeforeClass;
import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycleClass;
import org.junit.runner.RunWith;

/**
Expand All @@ -39,11 +37,7 @@
@Dependency({
"com.datastax.cassandra:cassandra-driver-core:[2.0.10,2.0.max]",
PluginITConstants.VERSION, CassandraITConstants.COMMONS_PROFILER, CassandraITConstants.CASSANDRA_TESTCONTAINER})
@SharedTestLifeCycleClass(CassandraServer2X.class)
public class CassandraDatastax_2_0_x_IT extends CassandraDatastaxITBase {

@BeforeSharedClass
public static void sharedSetup() {
startCassandra(CassandraITConstants.CASSANDRA_2_X_IMAGE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import com.navercorp.pinpoint.test.plugin.ImportPlugin;
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass;

import org.junit.BeforeClass;
import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycleClass;
import org.junit.runner.RunWith;

/**
Expand All @@ -39,9 +37,7 @@
@Dependency({
"com.datastax.cassandra:cassandra-driver-core:[2.1.6,2.1.max]",
PluginITConstants.VERSION, CassandraITConstants.COMMONS_PROFILER, CassandraITConstants.CASSANDRA_TESTCONTAINER})
@SharedTestLifeCycleClass(CassandraServer2X.class)
public class CassandraDatastax_2_1_x_IT extends CassandraDatastaxITBase {
@BeforeSharedClass
public static void sharedSetup() {
startCassandra(CassandraITConstants.CASSANDRA_2_X_IMAGE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import com.navercorp.pinpoint.test.plugin.ImportPlugin;
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass;

import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycleClass;
import org.junit.runner.RunWith;

/**
Expand All @@ -35,9 +34,7 @@
@Dependency({
"com.datastax.cassandra:cassandra-driver-core:[2.2.min,2.2.max]",
PluginITConstants.VERSION, CassandraITConstants.COMMONS_PROFILER, CassandraITConstants.CASSANDRA_TESTCONTAINER})
@SharedTestLifeCycleClass(CassandraServer2X.class)
public class CassandraDatastax_2_2_x_IT extends CassandraDatastaxITBase {
@BeforeSharedClass
public static void sharedSetup() {
startCassandra(CassandraITConstants.CASSANDRA_2_X_IMAGE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import com.navercorp.pinpoint.test.plugin.ImportPlugin;
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass;

import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycleClass;
import org.junit.runner.RunWith;

/**
Expand All @@ -37,10 +36,7 @@
// cassandra 4.x not supported
"com.datastax.cassandra:cassandra-driver-core:[3.0.0,3.max)",
PluginITConstants.VERSION, CassandraITConstants.COMMONS_PROFILER, CassandraITConstants.CASSANDRA_TESTCONTAINER})
@SharedTestLifeCycleClass(CassandraServer3X.class)
public class CassandraDatastax_3_0_x_IT extends CassandraDatastaxITBase {

@BeforeSharedClass
public static void sharedSetup() {
startCassandra(CassandraITConstants.CASSANDRA_3_X_IMAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ public final class CassandraITConstants {
// https://hub.docker.com/_/cassandra
public static final String CASSANDRA_2_X_IMAGE = CassandraContainer.IMAGE + ":2.2.16";
public static final String CASSANDRA_3_X_IMAGE = CassandraContainer.IMAGE + ":3.11.6";

public static final String TEST_KEYSPACE = "mykeyspace";
public static final String TEST_TABLE = "mytable";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.navercorp.pinpoint.plugin.cassandra;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Assume;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.CassandraContainer;

import java.util.Objects;
import java.util.Properties;

public class CassandraServer implements SharedTestLifeCycle {
private final Logger logger = LogManager.getLogger(getClass());

private final String dockerImageVersion;

public CassandraContainer cassandra;
private Cluster cluster;

public CassandraServer(String dockerImageVersion) {
this.dockerImageVersion = Objects.requireNonNull(dockerImageVersion, "dockerImageVersion");
}

@Override
public Properties beforeAll() {
Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable());

cassandra = new CassandraContainer(dockerImageVersion);
cassandra.start();

final Integer port = cassandra.getMappedPort(CassandraContainer.CQL_PORT);

this.cluster = newCluster("127.0.0.1", port);
init(cluster);
cluster.close();

Properties properties = new Properties();
properties.setProperty("PORT", port.toString());
return properties;
}

public void init(Cluster cluster) {
try (Session systemSession = cluster.connect()) {
String createKeyspace = String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = " +
"{'class':'SimpleStrategy','replication_factor':'1'};", CassandraITConstants.TEST_KEYSPACE);
systemSession.execute(createKeyspace);
String createTable = String.format("CREATE TABLE %s.%s (id text, value text, PRIMARY KEY(id))",
CassandraITConstants.TEST_KEYSPACE, CassandraITConstants.TEST_TABLE);
systemSession.execute(createTable);
}
}

public static Cluster newCluster(String host, int port) {
Cluster.Builder builder = Cluster.builder();
builder.addContactPoint(host);
builder.withPort(port);
builder.withoutMetrics();
return builder.build();
}

@Override
public void afterAll() {
if (cassandra != null) {
cassandra.stop();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.navercorp.pinpoint.plugin.cassandra;

public class CassandraServer2X extends CassandraServer {

public CassandraServer2X() {
super(CassandraITConstants.CASSANDRA_2_X_IMAGE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.navercorp.pinpoint.plugin.cassandra;

public class CassandraServer3X extends CassandraServer {

public CassandraServer3X() {
super(CassandraITConstants.CASSANDRA_3_X_IMAGE);
}

}

0 comments on commit f884324

Please sign in to comment.