Skip to content

Commit

Permalink
Kubeclient use multiple contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
michalxo committed Jan 2, 2024
1 parent 8286730 commit 6982aee
Show file tree
Hide file tree
Showing 21 changed files with 612 additions and 207 deletions.
1 change: 1 addition & 0 deletions common/src/main/java/io/brokerqe/claire/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface Constants {
String TAG_TLS = "tls";

// Environment Variables
String EV_KUBE_CONTEXT = "KUBE_CONTEXT";
String EV_ARTEMIS_VERSION = "ARTEMIS_VERSION";
String EV_ARTEMIS_TEST_VERSION = "ARTEMIS_TEST_VERSION";
String EV_DISABLE_RANDOM_NAMESPACES = "DISABLE_RANDOM_NAMESPACES";
Expand Down
12 changes: 9 additions & 3 deletions common/src/main/java/io/brokerqe/claire/TestDataCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public abstract class TestDataCollector implements TestWatcher, TestExecutionExc
@Override
public void handleTestExecutionException(ExtensionContext extensionContext, Throwable throwable) throws Throwable {
testClass = extensionContext.getRequiredTestClass().getName();
// .replaceAll(Constants.CLAIRE_TEST_PKG_REGEX, "");
testMethod = extensionContext.getRequiredTestMethod().getName();
testInstance = extensionContext.getRequiredTestInstance();
environment = Environment.get();
Expand All @@ -36,24 +35,31 @@ public void handleTestExecutionException(ExtensionContext extensionContext, Thro

String classDir = TestUtils.getClassName(extensionContext);
String testDir = TestUtils.getTestName(extensionContext);
archiveDir = environment.getLogsDirLocation() + Constants.FILE_SEPARATOR + testDir;
archiveDir = environment.getLogsDirLocation() + Constants.FILE_SEPARATOR + testDir.replaceFirst("io.brokerqe.claire.", "");
String certificatesDir = Environment.get().getCertificatesLocation() + Constants.FILE_SEPARATOR + testDir;
String certificatesDirClass = Environment.get().getCertificatesLocation() + Constants.FILE_SEPARATOR + classDir;

TestUtils.createDirectory(archiveDir);
String certificatesArchiveDirectory = archiveDir + Constants.FILE_SEPARATOR + "certificates";
String certificatesArchiveDirectoryClass = archiveDir + Constants.FILE_SEPARATOR + "class_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(certificatesDir, certificatesArchiveDirectory);
TestUtils.copyDirectoryFlat(certificatesDirClass, certificatesArchiveDirectory);
TestUtils.copyDirectoryFlat(certificatesDirClass, certificatesArchiveDirectoryClass);
}
}
collectTestData();
throw throwable;
}

/**
* Method is currently not used, but might be useful in future (getting variable from class by name)
* @param testInstance
* @param fieldName
* @return
*/
public Object getTestInstanceDeclaredField(Object testInstance, String fieldName) {
Field field = null;
Class<?> clazz = testInstance.getClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ 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());
TestUtils.deleteEmptyDirectories(Environment.get().getTmpDirLocation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,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 = currentTestDirectory + certificateData.getAlias() + "_keystore.jks";
String trustStoreFileName = currentTestDirectory + certificateData.getAlias() + "_truststore.jks";
String keyStoreFileName = getCurrentTestDirectory() + certificateData.getAlias() + "_keystore.jks";
String trustStoreFileName = getCurrentTestDirectory() + certificateData.getAlias() + "_truststore.jks";
String keyStoreDataName = certificateData.getAlias() + ".ks";
String trustStoreDataName = certificateData.getAlias() + ".ts";

Expand Down Expand Up @@ -305,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 = currentTestDirectory + brokerAlias + "_keystore.jks";
String brokerTrustStoreFileName = currentTestDirectory + brokerAlias + "_truststore.jks";
String clientKeyStoreFileName = currentTestDirectory + clientAlias + "_keystore.jks";
String clientTrustStoreFileName = currentTestDirectory + clientAlias + "_truststore.jks";
String brokerKeyStoreFileName = getCurrentTestDirectory() + brokerAlias + "_keystore.jks";
String brokerTrustStoreFileName = getCurrentTestDirectory() + brokerAlias + "_truststore.jks";
String clientKeyStoreFileName = getCurrentTestDirectory() + clientAlias + "_keystore.jks";
String clientTrustStoreFileName = getCurrentTestDirectory() + clientAlias + "_truststore.jks";

try {
LOGGER.info("[TLS] Creating Broker keystore");
Expand Down Expand Up @@ -385,17 +385,20 @@ public static void writeCertificateToFile(X509Certificate certificate, String fi
}

public static Map<String, KeyStoreData> generateDefaultCertificateKeystores(String brokerDN, String clientDN, List<Extension> extensions, CertificateData issuer) {
return generateCertificateKeystores(brokerDN, DEFAULT_BROKER_ALIAS, clientDN, DEFAULT_CLIENT_ALIAS, extensions, issuer);
}

public static Map<String, KeyStoreData> generateCertificateKeystores(String brokerDN, String brokerAlias, String clientDN, String clientAlias, 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(), currentTestDirectory + DEFAULT_BROKER_ALIAS + ".crt");
CertificateData brokerCertData = new CertificateData(brokerAlias, brokerDN, extensions, 30, issuer);
writeCertificateToFile(brokerCertData.getCertificate(), getCurrentTestDirectory() + brokerAlias + ".crt");

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

return CertificateManager.createKeystores(brokerCertData, clientCertData,
DEFAULT_BROKER_ALIAS, DEFAULT_BROKER_PASSWORD, DEFAULT_CLIENT_ALIAS, DEFAULT_CLIENT_PASSWORD);
return CertificateManager.createKeystores(brokerCertData, clientCertData, brokerAlias, DEFAULT_BROKER_PASSWORD, clientAlias, DEFAULT_CLIENT_PASSWORD);
}

public static String readCertificateFromFile(String certificatePath) {
Expand Down Expand Up @@ -485,11 +488,11 @@ public static void addToTruststore(KeyStoreData keyStoreData, String stringTlsCe
*/
public static Map<String, KeyStoreData> reuseDefaultGeneratedKeystoresFromFiles() {
Map<String, KeyStoreData> keystores = new HashMap<>();
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";
if (TestUtils.directoryExists(getCurrentTestDirectory())) {
String brokerKeyStoreFileName = getCurrentTestDirectory() + DEFAULT_BROKER_ALIAS + "_keystore.jks";
String brokerTrustStoreFileName = getCurrentTestDirectory() + DEFAULT_BROKER_ALIAS + "_truststore.jks";
String clientKeyStoreFileName = getCurrentTestDirectory() + DEFAULT_CLIENT_ALIAS + "_keystore.jks";
String clientTrustStoreFileName = getCurrentTestDirectory() + DEFAULT_CLIENT_ALIAS + "_truststore.jks";

try {
Security.addProvider(new BouncyCastleProvider());
Expand All @@ -514,7 +517,7 @@ public static Map<String, KeyStoreData> reuseDefaultGeneratedKeystoresFromFiles(
throw new RuntimeException(e);
}
} else {
LOGGER.error("[TLS] {} does not exist! Can not reuse it!", currentTestDirectory);
LOGGER.error("[TLS] {} does not exist! Can not reuse it!", getCurrentTestDirectory());
throw new RuntimeException("Can not find expected directory and load certificates!");
}
}
Expand Down
45 changes: 23 additions & 22 deletions operator-suite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,30 @@ If you would want to try your own OLM operator, you would need to specify only f

## List of available Environment Variables

| Name | Description | Default | Possible values |
|---------------------------|---------------------------------------------------------------------|-----------------------------|--------------------------------------------------|
| ARTEMIS_VERSION | ArtemisCloud Version to be used (Makefile) | 7.10.2 | \<major\>.\<minor\>.\<micro\> |
| ARTEMIS_TEST_VERSION | ArtemisCloud Version to be used by tests | not set | \<major\>.\<minor\> |
| OPERATOR_IMAGE | ArtemisCloud Operator image url | not set | \<image registry url\> |
| BROKER_IMAGE | Broker image url | not set | \<image registry url\> |
| BROKER_INIT_IMAGE | Broker init image url | not set | \<image registry url\> |
| BUNDLE_IMAGE | Bundle image url | not set | \<image registry url\> |
| OLM | Whether to install latest available Operator | false | `true`, `false` |
| OLM_LTS | Whether to install lts or latest available operator (tied to `OLM`) | false | `true`, `false` |
| OLM_IIB | OLM Index Image Bundle to use | not set | \<iib image registry url\> |
| OLM_CHANNEL | OLM channel to use with Subscription | not set | \<channel\> |
| DISABLE_RANDOM_NAMESPACES | Whether to use random string suffices | not set (`false`) | `true`, `false` |
| LOGS_LOCATION | Location where to generate collected logs | `test-logs` | \<directory\> |
| Name | Description | Default | Possible values |
|---------------------------|---------------------------------------------------------------------|-----------------------------|-------------------------------------------------|
| ARTEMIS_VERSION | ArtemisCloud Version to be used (Makefile) | 7.10.2 | \<major\>.\<minor\>.\<micro\> |
| ARTEMIS_TEST_VERSION | ArtemisCloud Version to be used by tests | not set | \<major\>.\<minor\> |
| OPERATOR_IMAGE | ArtemisCloud Operator image url | not set | \<image registry url\> |
| BROKER_IMAGE | Broker image url | not set | \<image registry url\> |
| BROKER_INIT_IMAGE | Broker init image url | not set | \<image registry url\> |
| BUNDLE_IMAGE | Bundle image url | not set | \<image registry url\> |
| OLM | Whether to install latest available Operator | false | `true`, `false` |
| OLM_LTS | Whether to install lts or latest available operator (tied to `OLM`) | false | `true`, `false` |
| OLM_IIB | OLM Index Image Bundle to use | not set | \<iib image registry url\> |
| OLM_CHANNEL | OLM channel to use with Subscription | not set | \<channel\> |
| DISABLE_RANDOM_NAMESPACES | Whether to use random string suffices | not set (`false`) | `true`, `false` |
| LOGS_LOCATION | Location where to generate collected logs | `test-logs` | \<directory\> |
| TEST_LOG_LEVEL | Set logging level of test suite | `INFO` set in `logback.xml` | `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `OFF` |
| CLUSTER_OPERATOR_MANAGED | Whether test suite manages CO or not (Makefile) | `true` | `false` |
| COLLECT_TEST_DATA | Whether to gather test data on error or not | `true` | `true`, `false` |
| CUSTOM_EXTRA_DELAY | Prolonged all internal waitFor calls (seconds) | `0` | \<number of seconds\> |
| OPERATOR_INSTALL_ZIP | Url to zip file with install/examples (Makefile) | 7.10.2 url | \<url\> |
| OPERATOR_VERSION_UPSTREAM | Version/branch of repository (Makefile) | main | \<branch\> |
| DUMP_ENABLED | Enabled serialization of deployed resources | `false` | `true`, `false` |
| DUMP_FORMAT | Format of serialized deployed resources | `yaml` | `yaml`, `json` |
| DUMP_LOCATION | Location to dump serialized deployed resources | `serialization-dump` | \<directory\> |
| CLUSTER_OPERATOR_MANAGED | Whether test suite manages CO or not (Makefile) | `true` | `false` |
| COLLECT_TEST_DATA | Whether to gather test data on error or not | `true` | `true`, `false` |
| CUSTOM_EXTRA_DELAY | Prolonged all internal waitFor calls (seconds) | `0` | \<number of seconds\> |
| OPERATOR_INSTALL_ZIP | Url to zip file with install/examples (Makefile) | 7.10.2 url | \<url\> |
| OPERATOR_VERSION_UPSTREAM | Version/branch of repository (Makefile) | main | \<branch\> |
| DUMP_ENABLED | Enabled serialization of deployed resources | `false` | `true`, `false` |
| DUMP_FORMAT | Format of serialized deployed resources | `yaml` | `yaml`, `json` |
| DUMP_LOCATION | Location to dump serialized deployed resources | `serialization-dump` | \<directory\> |
| KUBE_CONTEXT | Provide comma separated context(s) for kubernetes client | `default/null` | null, \<contextA,contextB,contextC,...\> |

## Setting log level
Currently, there is supported `TEST_LOG_LEVEL` environment variable, which can set desired logging level of test suite.
Expand Down
Loading

0 comments on commit 6982aee

Please sign in to comment.