Added pattern matcher to run test on all cql-workloads

This commit is contained in:
MikeYaacoubStax 2023-02-08 09:18:53 -05:00
parent 7375e89c48
commit f5f7bde0e7
2 changed files with 137 additions and 27 deletions

View File

@ -30,7 +30,7 @@ blocks:
create-keyspace:
raw: |
create keyspace if not exists TEMPLATE(keyspace,baselines)
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 'TEMPLATE(rf,1)>>'}
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 'TEMPLATE(rf,1)'}
AND durable_writes = true;
create-table:
raw: |

View File

@ -17,22 +17,28 @@
package io.nosqlbench.nb5.proof;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.containers.CassandraContainer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.CassandraQueryWaitStrategy;
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.List;
import java.util.ArrayList;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ExampleContainersIntegrationTest {
@ -47,9 +53,41 @@ public class ExampleContainersIntegrationTest {
private static String datacenter = "datacenter1";
private static Integer mappedPort9042 = 9042;
private static final Integer EXPOSED_PORT = 9042;
private static final CassandraContainer cass = (CassandraContainer) new CassandraContainer().withExposedPorts(9042);
private static final CassandraContainer cass = (CassandraContainer) new CassandraContainer(DockerImageName.parse("cassandra:latest"))
.withExposedPorts(9042).withAccessToHost(true);
//.waitingFor(new CassandraWaitStrategy());
@BeforeAll
public static void initContainer() {
//List the tests we would like to run
ProcessInvoker invoker = new ProcessInvoker();
//STEP1: Copy the example workload to the local dir
ProcessResult listResult = invoker.run("list-workloads", 30,
"java", "-jar", JARNAME, "--list-workloads", "--include=examples"
);
assertThat(listResult.exception).isNull();
String listOut = String.join("\n", listResult.getStdoutData());
List<String> results = new ArrayList<>();
// Define the regular expression pattern
String regex = "/(.+?/)+.+?\\.yaml";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(listOut);
ArrayList<String> matchedPaths = new ArrayList<>();
while (matcher.find()) {
matchedPaths.add(matcher.group());
}
System.out.println("Matched paths:");
for (String path : matchedPaths) {
System.out.println(path);
}
}
@BeforeEach
public void setUp() {
//STEP0:Start the test container and expose the 9042 port on the local host.
//So that the docker bridge controller exposes the port to our process invoker that will run nb5
//and target cassandra on that docker container
@ -59,44 +97,116 @@ public class ExampleContainersIntegrationTest {
// However, in some CI environments they may instead be reachable on a different host.
mappedPort9042 = cass.getMappedPort(9042);
hostIP = cass.getHost();
}
@BeforeEach
public void setUp() {
System.out.println("setup");
}
@Test
public void testSimplePutAndGet() {
public void testCqlKeyValueWorkload() {
ProcessInvoker invoker = new ProcessInvoker();
invoker.setLogDir("logs/test");
// //STEP1: Copy the example workload to the local dir
//STEP1: Copy the example workload to the local dir
ProcessResult copyResult = invoker.run("copy-workload", 30,
"java", "-jar", JARNAME, "--copy=activities/baselines/cql-keyvalue.yaml"
"java", "-jar", JARNAME, "--copy=/activities/baselinesv2/cql-keyvalue2.yaml"
);
assertThat(copyResult.exception).isNull();
String copyOut = String.join("\n", copyResult.getStdoutData());
//STEP2: Run the example cassandra workload using the schema tag to create the Cass Baselines keyspace
String[] args = new String[]{
"java", "-jar", JARNAME, "cql-keyvalue.yaml", "default", "host="+hostIP, "localdc="+datacenter, "port="+ mappedPort9042.toString()
"java", "-jar", JARNAME, "cql-keyvalue2.yaml", "default", "host="+hostIP, "localdc="+datacenter, "port="+ mappedPort9042.toString(), "rampup-cycles=10", "main-cycles=10"
};
ProcessResult runSchemaResult = invoker.run("run-workload", 30, args);
logger.info("The final command line: " + String.join(" ", args));
ProcessResult runSchemaResult = invoker.run("run-workload", 30, args);
//STEP 3 Check runSchemaOut for errors
logger.info("Checking if the NB5 command resulted in any errors...");
assertThat(runSchemaResult.exception).isNull();
String runSchemaOut = String.join("\n", runSchemaResult.getStdoutData());
System.out.println(runSchemaOut);
assertThat(runSchemaOut.toLowerCase()).doesNotContain("error");
logger.info("NB5 command completed with no errors");
//STEP3: Run the example cassandra workload using the rampup phase to create the data in a specific number of cycles
ProcessResult runRampUpResult = invoker.run("run-workload", 30, "java", "-jar", JARNAME, "run",
"driver=cql", "workload=cql-keyvalue", "host="+hostIP, "localdc="+datacenter, "port="+ mappedPort9042.toString(),
"tags=blocks:rampup", "cycles=100k"
);
assertThat(runRampUpResult.exception).isNull();
String runRampUpOut = String.join("\n", runRampUpResult.getStdoutData());
System.out.println(runRampUpOut);
//STEP 4 Check the cluster for created data
try (CqlSession session = CqlSession.builder().addContactPoint(new InetSocketAddress(hostIP, mappedPort9042)).withLocalDatacenter(datacenter).build()) {
//->Check for the creation of the keyspace baselines
logger.info("Checking for the creation of the keyspace \"baselines\"...");
ResultSet result = session.execute("SELECT keyspace_name FROM system_schema.keyspaces");
List<Row> rows = result.all();
boolean keyspaceFound = false;
for (Row row : rows) {
if (row.getString("keyspace_name").equals("baselines")) {
keyspaceFound = true;
break;
}
}
assertTrue(keyspaceFound);
logger.info("Keyspace \"baselines\" was found, nb5 command had created it successfully");
//->Check for the creation of the baselines keyvalue table
logger.info("Checking for the creation of the table \"baselines.keyvalue\"...");
result = session.execute("SELECT table_name FROM system_schema.tables WHERE keyspace_name='baselines'");
rows = result.all();
boolean tableFound = false;
for (Row row : rows) {
if (row.getString("table_name").equals("keyvalue")) {
tableFound = true;
break;
}
}
assertTrue(tableFound);
logger.info("Table \"baselines.keyvalue\" was found, nb5 command had created it successfully");
//->Check for the creation of the baselines keyvalue table
logger.info("Table \"baselines.keyvalue\" has at least 5 rows of key-value pairs, nb5 command had created them successfully");
result = session.execute("SELECT count(*) FROM baselines.keyvalue");
int rowCount = result.one().getInt(0);
assertTrue(rowCount >= 5);
logger.info("Table \"baselines.keyvalue\" has at least 5 rows of key-value pairs, nb5 command had created them successfully");
} catch (Exception e)
{
System.out.println(e.getMessage());
}
//STEP5 Create a failing test to make sure that the workload won't work, here we use a random wrong IP
String[] args2 = new String[]{
"java", "-jar", JARNAME, "cql-keyvalue2.yaml", "default", "host=0.1.0.1", "localdc="+datacenter, "port="+ mappedPort9042.toString(), "rampup-cycles=10", "main-cycles=10"
};
logger.info("The final command line: " + String.join(" ", args2));
ProcessResult runFailingSchemaResult = invoker.run("run-workload", 30, args2);
assertThat(runFailingSchemaResult.exception).isNull();
String runFailingSchemaOut = String.join("\n", runFailingSchemaResult.getStdoutData());
assertThat(runFailingSchemaOut.toLowerCase()).contains("error");
System.out.println("end");
}
@AfterEach
public void stopContainers(){
cass.stop();
}
static class CassandraWaitStrategy extends AbstractWaitStrategy {
public CassandraWaitStrategy() {
withStartupTimeout(Duration.ofMinutes(2));
}
@Override
protected void waitUntilReady() {
// Custom wait strategy to determine if Cassandra is ready.
// For example, we can check the logs or perform a cql query to verify the status of Cassandra.
String logs = cass.getLogs();
Unreliables.retryUntilSuccess(120, TimeUnit.SECONDS, () -> {
if (logs.contains("Listening for thrift clients...")) {
return true;
}
return false;
});
}
}
}