Skip to content

Commit

Permalink
Minor folder reorg, getting certs, test logs from tests with numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
michalxo authored and tlbueno committed Dec 15, 2023
1 parent 598c22c commit 8286730
Show file tree
Hide file tree
Showing 31 changed files with 151 additions and 46 deletions.
9 changes: 5 additions & 4 deletions common/src/main/java/io/brokerqe/claire/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface Constants {
// Platform related strings
String LINE_SEPARATOR = System.getProperty("line.separator");
String FILE_SEPARATOR = System.getProperty("file.separator");
String DATE_FORMAT = "yyyy-MM-dd_HH:mm:ss";
String DATE_FORMAT = "yyyy-MM-dd_HH-mm-ss";

// Test tags
String TAG_JAAS = "jaas";
Expand Down Expand Up @@ -57,7 +57,7 @@ public interface Constants {

// Test related strings
String DEFAULT_KEYCLOAK_VERSION = "22.0.5";
String DEFAULT_RHSSO_VERSION = "rhbk-operator.v22.0.6-opr.1";
String DEFAULT_RHSSO_VERSION = "rhbk-operator.v22.0.7-opr.1";

// Networking
String AMQP = "amqp";
Expand All @@ -79,14 +79,15 @@ public interface Constants {
long DURATION_2_MINUTES = Duration.ofMinutes(2).toMillis();
long DURATION_3_MINUTES = Duration.ofMinutes(3).toMillis();
long DURATION_5_MINUTES = Duration.ofMinutes(5).toMillis();

// Files
String PROJECT_USER_DIR = System.getProperty("user.dir");
String PROJECT_TEST_DIR = PROJECT_USER_DIR + "/src/test";
String LOGS_DEFAULT_DIR = PROJECT_USER_DIR + "/test-logs";
String TMP_DEFAULT_DIR = PROJECT_USER_DIR + "/test-tmp";
String DUMP_DEFAULT_DIR = PROJECT_USER_DIR + "/serialization-dump";
String DUMP_DEFAULT_TYPE = "yaml";
String CERTS_GENERATION_DIR = PROJECT_USER_DIR + "/certificates/";
String CERTS_GENERATION_DIR = "certificates";
String PROJECT_SETTINGS_PATH = PROJECT_USER_DIR + "/artemis/project-settings.properties";
String OPERATOR_CRDS_DIR_PATH = PROJECT_USER_DIR + "/artemis/crds/";
String OPERATOR_INSTALL_DIR_PATH = PROJECT_USER_DIR + "/artemis/install/";
Expand Down Expand Up @@ -135,7 +136,7 @@ public interface Constants {
String PROP_YACFG_ARTEMIS_TEMPLATES_OVERRIDE_DIR = "yacfg.artemis.templates_override_dir";

String ARTEMIS_DEFAULT_CFG_DIR = "artemis/artemis_default_cfg";
String ARTEMIS_TEST_CFG_DIR = "test_cfg";
String ARTEMIS_TEST_CFG_DIR = "test-cfg";
String ARTEMIS_DEFAULT_CFG_BIN_DIR = ARTEMIS_DEFAULT_CFG_DIR + FILE_SEPARATOR + ArtemisConstants.BIN_DIR;
String ARTEMIS_DEFAULT_CFG_LIB_DIR = ARTEMIS_DEFAULT_CFG_DIR + FILE_SEPARATOR + ArtemisConstants.LIB_DIR;

Expand Down
3 changes: 3 additions & 0 deletions common/src/main/java/io/brokerqe/claire/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public abstract class Environment {
public abstract ArtemisVersion getArtemisTestVersion();
public abstract String getLogsDirLocation();
public abstract String getTmpDirLocation();
public String getCertificatesLocation() {
return getTmpDirLocation() + Constants.FILE_SEPARATOR + Constants.CERTS_GENERATION_DIR;
}
public abstract String getKeycloakVersion();
public abstract boolean isCollectTestData();
public abstract int getCustomExtraDelay();
Expand Down
17 changes: 11 additions & 6 deletions common/src/main/java/io/brokerqe/claire/TestDataCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ public void handleTestExecutionException(ExtensionContext extensionContext, Thro
throw throwable;
}

archiveDir = environment.getLogsDirLocation() + Constants.FILE_SEPARATOR + testClass + "." + testMethod;
String classDir = TestUtils.getClassName(extensionContext);
String testDir = TestUtils.getTestName(extensionContext);
archiveDir = environment.getLogsDirLocation() + Constants.FILE_SEPARATOR + testDir;
String certificatesDir = Environment.get().getCertificatesLocation() + Constants.FILE_SEPARATOR + testDir;
String certificatesDirClass = Environment.get().getCertificatesLocation() + Constants.FILE_SEPARATOR + classDir;

TestUtils.createDirectory(archiveDir);
String certificatesDirectory = archiveDir + Constants.FILE_SEPARATOR + "certificates";
if (TestUtils.directoryExists(Constants.CERTS_GENERATION_DIR)) {
if (TestUtils.directoryExists(certificatesDirectory)) {
LOGGER.info("[TDC] Skipping duplicated copying of certificates into {}", certificatesDirectory);
String certificatesArchiveDirectory = archiveDir + Constants.FILE_SEPARATOR + "certificates";
if (!TestUtils.isEmptyDirectory(certificatesDirClass) || !TestUtils.isEmptyDirectory(certificatesDir)) {
if (TestUtils.directoryExists(certificatesArchiveDirectory)) {
LOGGER.warn("[TDC] Skipping duplicated copying of certificates into {}", certificatesArchiveDirectory);
} else {
TestUtils.copyDirectoryFlat(Constants.CERTS_GENERATION_DIR, certificatesDirectory);
TestUtils.copyDirectoryFlat(certificatesDir, certificatesArchiveDirectory);
TestUtils.copyDirectoryFlat(certificatesDirClass, certificatesArchiveDirectory);
}
}
collectTestData();
Expand Down
67 changes: 67 additions & 0 deletions common/src/main/java/io/brokerqe/claire/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.PreconditionViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -61,11 +64,13 @@
import java.util.Base64;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.UUID;
import java.util.function.BooleanSupplier;
import java.util.stream.Stream;
import java.lang.Runtime.Version;

@SuppressWarnings({"checkstyle:ClassFanOutComplexity"})
public final class TestUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(TestUtils.class);
Expand All @@ -78,6 +83,49 @@ public static Path getProjectRelativeFilePath(String projectRelativeFile) {
return Paths.get(Constants.PROJECT_USER_DIR, projectRelativeFile).toAbsolutePath();
}

// ========== Junit Test Operations ==========
public static String getClassName(ExtensionContext extensionContext) {
return extensionContext.getRequiredTestClass().getName();
}
public static String getClassName(TestInfo testInfo) {
return testInfo.getTestClass().orElseThrow().getName();
}

public static String getTestName(ExtensionContext extensionContext) {
String testClass = extensionContext.getRequiredTestClass().getName();
String testMethod = null;
try {
testMethod = extensionContext.getRequiredTestMethod().getName();
} catch (PreconditionViolationException e) {
LOGGER.trace("No testMethod in extensionContext, skipping methodName usage");
}
return createTestName(extensionContext.getDisplayName(), testClass, testMethod);
}

public static String getTestName(TestInfo testInfo) {
String testClass = testInfo.getTestClass().orElseThrow().getName();
String testMethod = null;
try {
testMethod = testInfo.getTestMethod().orElseThrow().getName();
} catch (NoSuchElementException e) {
LOGGER.trace("No testMethod in testInfo, skipping methodName usage");
}
return createTestName(testInfo.getDisplayName(), testClass, testMethod);
}

private static String createTestName(String displayName, String testClass, String testMethod) {
String sameTestCounter = "";
String testFullName = testClass;
if (testMethod != null) {
testFullName += "." + testMethod;
}
if (displayName.startsWith("[")) {
sameTestCounter = displayName.substring(displayName.indexOf("[") + 1, displayName.indexOf("]"));
testFullName += "#" + sameTestCounter;
}
return testFullName;
}

// ========== Random Operations ==========
public static String getRandomString(int length) {
if (length > 96) {
Expand Down Expand Up @@ -313,6 +361,7 @@ public static String parseVersionMMM(String versionString) {
// ========== File Operations ==========
public static void deleteFile(Path fileName) {
try {
LOGGER.trace("Deleting {}", fileName);
Files.delete(fileName);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -361,6 +410,24 @@ public static boolean directoryExists(String directoryName) {
return Files.exists(path) && Files.isDirectory(path);
}

public static boolean isEmptyDirectory(String directoryName) {
Path path = Paths.get(directoryName);
try {
return Files.exists(path) && Files.isDirectory(path) && Files.list(path).findAny().isEmpty();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void deleteEmptyDirectories(String directoryName) {
for (String filename : new File(directoryName).list()) {
String fileToDelete = new File(Environment.get().getCertificatesLocation()).getAbsolutePath() + Constants.FILE_SEPARATOR + filename;
if (TestUtils.isEmptyDirectory(fileToDelete)) {
TestUtils.deleteFile(Path.of(fileToDelete));
}
}
}

public static void createDirectory(String directoryName) {
try {
LOGGER.trace("Creating directory {}", directoryName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*/
package io.brokerqe.claire.junit;

import io.brokerqe.claire.Environment;
import io.brokerqe.claire.TestUtils;
import io.brokerqe.claire.security.CertificateManager;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -24,13 +27,15 @@ public interface TestSeparator {
static void beforeAllTests(ExtensionContext testContext) {
LOGGER.info((char) 27 + "[34m" + String.join("", Collections.nCopies(76, SEPARATOR_CHAR)) + (char) 27 + "[0m");
LOGGER.info((char) 27 + "[33m" + String.format("Started Class: %s", testContext.getRequiredTestClass().getName()) + (char) 27 + "[0m");
CertificateManager.setCertificateTestDirectory(TestUtils.getTestName(testContext));
}
@BeforeEach
default void beforeEachTest(ExtensionContext testContext) {
LOGGER.info((char) 27 + "[34m" + String.join("", Collections.nCopies(76, SEPARATOR_CHAR)) + (char) 27 + "[0m");
LOGGER.info((char) 27 + "[33m" + String.format("[%s/%s] Started: %s.%s",
ClaireExecutionListener.getCurrentTestCounter(), ClaireExecutionListener.getTotalTestCount(),
testContext.getRequiredTestClass().getName(), testContext.getRequiredTestMethod().getName()) + (char) 27 + "[0m");
CertificateManager.setCertificateTestDirectory(TestUtils.getTestName(testContext));
}

@AfterEach
Expand All @@ -44,5 +49,6 @@ default void afterEachTest(ExtensionContext testContext) {
static void afterAllTests(ExtensionContext testContext) {
LOGGER.info((char) 27 + "[33m" + String.format("Finished Class: %s", testContext.getRequiredTestClass().getName()) + (char) 27 + "[0m");
LOGGER.info((char) 27 + "[34m" + String.join("", Collections.nCopies(76, SEPARATOR_CHAR)) + (char) 27 + "[0m");
TestUtils.deleteEmptyDirectories(Environment.get().getCertificatesLocation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package io.brokerqe.claire.security;

import io.brokerqe.claire.Constants;
import org.bouncycastle.asn1.x509.Extension;

import javax.security.auth.x500.X500PrivateCredential;
Expand Down Expand Up @@ -45,7 +44,7 @@ public CertificateData(String alias, String distinguishedName, List<Extension> e
this.distinguishedName = distinguishedName;
this.certificate = CertificateManager.generate(keyPair, CertificateManager.SIGNATURE_ALGORITHM, distinguishedName, validNotBefore, validNotAfter, extensions, issuer);
this.privateCredential = CertificateManager.createPrivateCredential(certificate, keyPair, alias);
this.fileName = Constants.CERTS_GENERATION_DIR + alias + ".crt";
this.fileName = CertificateManager.getCurrentTestDirectory() + alias + ".crt";
CertificateManager.writeCertificateToFile(certificate, fileName);
}

Expand All @@ -61,7 +60,7 @@ public CertificateData(String alias, String distinguishedName, CertificateData i
this.distinguishedName = distinguishedName;
this.certificate = CertificateManager.generateCA(keyPair, CertificateManager.SIGNATURE_ALGORITHM, distinguishedName, validNotBefore, validNotAfter, issuer);
this.privateCredential = CertificateManager.createPrivateCredential(certificate, keyPair, alias);
this.fileName = Constants.CERTS_GENERATION_DIR + alias + ".crt";
this.fileName = CertificateManager.getCurrentTestDirectory() + alias + ".crt";
CertificateManager.writeCertificateToFile(certificate, fileName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package io.brokerqe.claire.security;

import io.brokerqe.claire.Constants;
import io.brokerqe.claire.Environment;
import io.brokerqe.claire.TestUtils;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
Expand Down Expand Up @@ -74,8 +75,18 @@ public class CertificateManager {
public static final String DEFAULT_CLIENT_ALIAS = "clientUser";
public static final String DEFAULT_CLIENT_PASSWORD = "clientPass";

private static String currentTestDirectory = Environment.get().getCertificatesLocation() + Constants.FILE_SEPARATOR;
static {
TestUtils.createDirectory(Constants.CERTS_GENERATION_DIR);
TestUtils.createDirectory(currentTestDirectory);
}

public static void setCertificateTestDirectory(String testDirectory) {
currentTestDirectory = Environment.get().getCertificatesLocation() + Constants.FILE_SEPARATOR + testDirectory + Constants.FILE_SEPARATOR;
TestUtils.createDirectory(currentTestDirectory);
}

public static String getCurrentTestDirectory() {
return currentTestDirectory;
}

public static TrustManager[] trustAllCertificates = new TrustManager[]{
Expand Down Expand Up @@ -261,8 +272,8 @@ public static X500PrivateCredential createPrivateCredential(X509Certificate cert
*/
public static Map<String, KeyStoreData> createEntityKeystores(CertificateData certificateData, String keystorePassword) {
Map<String, KeyStoreData> keystores = new HashMap<>();
String keyStoreFileName = Constants.CERTS_GENERATION_DIR + certificateData.getAlias() + "_keystore.jks";
String trustStoreFileName = Constants.CERTS_GENERATION_DIR + certificateData.getAlias() + "_truststore.jks";
String keyStoreFileName = currentTestDirectory + certificateData.getAlias() + "_keystore.jks";
String trustStoreFileName = currentTestDirectory + certificateData.getAlias() + "_truststore.jks";
String keyStoreDataName = certificateData.getAlias() + ".ks";
String trustStoreDataName = certificateData.getAlias() + ".ts";

Expand Down Expand Up @@ -294,10 +305,10 @@ public static Map<String, KeyStoreData> createEntityKeystores(CertificateData ce
public static Map<String, KeyStoreData> createKeystores(CertificateData brokerCertificateData, CertificateData clientCertificateData,
String brokerAlias, String brokerPassword, String clientAlias, String clientPassword) {
Map<String, KeyStoreData> keystores = new HashMap<>();
String brokerKeyStoreFileName = Constants.CERTS_GENERATION_DIR + brokerAlias + "_keystore.jks";
String brokerTrustStoreFileName = Constants.CERTS_GENERATION_DIR + brokerAlias + "_truststore.jks";
String clientKeyStoreFileName = Constants.CERTS_GENERATION_DIR + clientAlias + "_keystore.jks";
String clientTrustStoreFileName = Constants.CERTS_GENERATION_DIR + clientAlias + "_truststore.jks";
String brokerKeyStoreFileName = currentTestDirectory + brokerAlias + "_keystore.jks";
String brokerTrustStoreFileName = currentTestDirectory + brokerAlias + "_truststore.jks";
String clientKeyStoreFileName = currentTestDirectory + clientAlias + "_keystore.jks";
String clientTrustStoreFileName = currentTestDirectory + clientAlias + "_truststore.jks";

try {
LOGGER.info("[TLS] Creating Broker keystore");
Expand Down Expand Up @@ -376,12 +387,12 @@ public static void writeCertificateToFile(X509Certificate certificate, String fi
public static Map<String, KeyStoreData> generateDefaultCertificateKeystores(String brokerDN, String clientDN, List<Extension> extensions, CertificateData issuer) {
LOGGER.info("[TLS] Generating Broker KeyPair, Certificates");
CertificateData brokerCertData = new CertificateData(DEFAULT_BROKER_ALIAS, brokerDN, extensions, 30, issuer);
writeCertificateToFile(brokerCertData.getCertificate(), Constants.CERTS_GENERATION_DIR + DEFAULT_BROKER_ALIAS + ".crt");
writeCertificateToFile(brokerCertData.getCertificate(), currentTestDirectory + DEFAULT_BROKER_ALIAS + ".crt");

// Client cert + keypair
LOGGER.info("[TLS] Generating Client KeyPair, Certificates");
CertificateData clientCertData = new CertificateData(DEFAULT_CLIENT_ALIAS, clientDN);
writeCertificateToFile(clientCertData.getCertificate(), Constants.CERTS_GENERATION_DIR + DEFAULT_CLIENT_ALIAS + ".crt");
writeCertificateToFile(clientCertData.getCertificate(), currentTestDirectory + DEFAULT_CLIENT_ALIAS + ".crt");

return CertificateManager.createKeystores(brokerCertData, clientCertData,
DEFAULT_BROKER_ALIAS, DEFAULT_BROKER_PASSWORD, DEFAULT_CLIENT_ALIAS, DEFAULT_CLIENT_PASSWORD);
Expand Down Expand Up @@ -474,11 +485,11 @@ public static void addToTruststore(KeyStoreData keyStoreData, String stringTlsCe
*/
public static Map<String, KeyStoreData> reuseDefaultGeneratedKeystoresFromFiles() {
Map<String, KeyStoreData> keystores = new HashMap<>();
if (TestUtils.directoryExists(Constants.CERTS_GENERATION_DIR)) {
String brokerKeyStoreFileName = Constants.CERTS_GENERATION_DIR + DEFAULT_BROKER_ALIAS + "_keystore.jks";
String brokerTrustStoreFileName = Constants.CERTS_GENERATION_DIR + DEFAULT_BROKER_ALIAS + "_truststore.jks";
String clientKeyStoreFileName = Constants.CERTS_GENERATION_DIR + DEFAULT_CLIENT_ALIAS + "_keystore.jks";
String clientTrustStoreFileName = Constants.CERTS_GENERATION_DIR + DEFAULT_CLIENT_ALIAS + "_truststore.jks";
if (TestUtils.directoryExists(currentTestDirectory)) {
String brokerKeyStoreFileName = currentTestDirectory + DEFAULT_BROKER_ALIAS + "_keystore.jks";
String brokerTrustStoreFileName = currentTestDirectory + DEFAULT_BROKER_ALIAS + "_truststore.jks";
String clientKeyStoreFileName = currentTestDirectory + DEFAULT_CLIENT_ALIAS + "_keystore.jks";
String clientTrustStoreFileName = currentTestDirectory + DEFAULT_CLIENT_ALIAS + "_truststore.jks";

try {
Security.addProvider(new BouncyCastleProvider());
Expand All @@ -503,7 +514,7 @@ public static Map<String, KeyStoreData> reuseDefaultGeneratedKeystoresFromFiles(
throw new RuntimeException(e);
}
} else {
LOGGER.error("[TLS] {} does not exist! Can not reuse it!", Constants.CERTS_GENERATION_DIR);
LOGGER.error("[TLS] {} does not exist! Can not reuse it!", currentTestDirectory);
throw new RuntimeException("Can not find expected directory and load certificates!");
}
}
Expand Down
Loading

0 comments on commit 8286730

Please sign in to comment.