Skip to content

Commit

Permalink
Add TestRule for preserving logs
Browse files Browse the repository at this point in the history
This TestRule can be combined with other CCM Rules to prevent them
from deleting logs of failed tests during cleanup.
  • Loading branch information
Bouncheck committed Jan 9, 2023
1 parent 834f231 commit f59a52b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ If you use CCM, it **must** be with `CcmRule`.
For an example of a Simulacron-based parallelizable test, see `NodeTargetingIT`. For a CCM-based
test, see `DirectCompressionIT`.
Currently `CcmRule` based tests will not preserve CCM config directory upon failure. To have CCM
logs available in case of failure during automated testing combine it with `PreserveLogsRule` so
that logs can be uploaded.
##### Serial tests
These tests cannot run in parallel, in general because they require CCM clusters of different sizes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.datastax.oss.driver.core;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
Expand All @@ -24,10 +25,12 @@
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
import com.datastax.oss.driver.api.core.session.Session;
import com.datastax.oss.driver.api.testinfra.ccm.CcmRule;
import com.datastax.oss.driver.api.testinfra.ccm.PreserveLogsRule;
import com.datastax.oss.driver.api.testinfra.session.SessionRule;
import com.datastax.oss.driver.api.testinfra.session.SessionUtils;
import com.datastax.oss.driver.categories.ParallelizableTests;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.RuleChain;
Expand All @@ -42,6 +45,9 @@ public class ConnectKeyspaceIT {
@ClassRule
public static final TestRule CHAIN = RuleChain.outerRule(CCM_RULE).around(SESSION_RULE);

@Rule
public PreserveLogsRule LOGS_RULE = new PreserveLogsRule(CCM_RULE.getCcmBridge());

@Test
public void should_connect_to_existing_keyspace() {
CqlIdentifier keyspace = SESSION_RULE.keyspace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class BaseCcmRule extends CassandraResourceRule {
new Thread(
() -> {
try {
ccmBridge.remove();
ccmBridge.removeOrStop();
} catch (Exception e) {
// silently remove as may have already been removed.
}
Expand All @@ -57,7 +57,7 @@ protected void before() {

@Override
protected void after() {
ccmBridge.remove();
ccmBridge.removeOrStop();
}

private Statement buildErrorStatement(
Expand Down Expand Up @@ -232,4 +232,8 @@ public ProtocolVersion getHighestProtocolVersion() {
return DefaultProtocolVersion.V3;
}
}

public CcmBridge getCcmBridge(){
return ccmBridge;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ public class CcmBridge implements AutoCloseable {
private final List<String> dseWorkloads;
private final String jvmArgs;

private boolean keepLogs = false;

private CcmBridge(
Path configDirectory,
int[] nodes,
Expand Down Expand Up @@ -378,6 +380,15 @@ public void remove() {
execute("remove");
}

public void removeOrStop() {
if (keepLogs) {
stop();
}
else {
remove();
}
}

public void pause(int n) {
execute("node" + n, "pause");
}
Expand Down Expand Up @@ -471,7 +482,11 @@ protected void processLine(String line, int logLevel) {

@Override
public void close() {
remove();
removeOrStop();
}

public void setKeepLogs(boolean keepLogs) {
this.keepLogs = keepLogs;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.datastax.oss.driver.api.testinfra.ccm;

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

public class PreserveLogsRule extends TestWatcher {
private final CcmBridge ccmBridge;

public PreserveLogsRule(CcmBridge ccmBridge) {
this.ccmBridge = ccmBridge;
}

@Override
protected void failed(Throwable e, Description description) {
ccmBridge.setKeepLogs(true);
}
}

0 comments on commit f59a52b

Please sign in to comment.