Skip to content

Commit

Permalink
[ST] Use ThreadLocal in ResourceManager (#9687)
Browse files Browse the repository at this point in the history
Signed-off-by: David Kornel <kornys@outlook.com>
  • Loading branch information
kornys committed Feb 15, 2024
1 parent 12a40b2 commit ca37a6f
Show file tree
Hide file tree
Showing 94 changed files with 2,141 additions and 2,199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* <p>A HasAllOfReasons is custom matcher to check the full matching of reasons for actual events.</p>
Expand All @@ -29,11 +28,11 @@ public HasAllOfReasons(Events... eventReasons) {
public boolean matches(Object actualValue) {
List<String> actualReasons = ((List<Event>) actualValue).stream()
.map(Event::getReason)
.collect(Collectors.toList());
.toList();

List<String> expectedReasons = Arrays.stream(eventReasons)
.map(Enum::name)
.collect(Collectors.toList());
.toList();

return actualReasons.containsAll(expectedReasons);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* <p>A HasAllOfReasons is custom matcher to check the partial matching of reasons for actual events.
Expand All @@ -30,11 +29,11 @@ public HasAnyOfReasons(Events... eventReasons) {
public boolean matches(Object actualValue) {
List<String> actualReasons = ((List<Event>) actualValue).stream()
.map(Event::getReason)
.collect(Collectors.toList());
.toList();

List<String> expectedReasons = Arrays.stream(eventReasons)
.map(Enum::name)
.collect(Collectors.toList());
.toList();

for (String actualReason : actualReasons) {
if (expectedReasons.contains(actualReason)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.strimzi.systemtest.TestConstants;
import io.strimzi.systemtest.listeners.ExecutionListener;
import io.strimzi.systemtest.resources.NamespaceManager;
import io.strimzi.systemtest.resources.ResourceManager;
import io.strimzi.systemtest.utils.StUtils;
import io.strimzi.test.logs.CollectorElement;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -42,15 +43,14 @@ public synchronized static TestSuiteNamespaceManager getInstance() {
* {@link io.strimzi.systemtest.annotations.ParallelNamespaceTest}, which creates its own namespace it does not
* make sense to provide another auxiliary namespace (because it will be not used) and thus we are skip such creation.
*
* @param extensionContext extension context for test class
*/
public void createTestSuiteNamespace(ExtensionContext extensionContext) {
final String testSuiteName = extensionContext.getRequiredTestClass().getName();
public void createTestSuiteNamespace() {
final String testSuiteName = ResourceManager.getTestContext().getRequiredTestClass().getName();

if (ExecutionListener.hasSuiteParallelOrIsolatedTest(extensionContext)) {
if (ExecutionListener.hasSuiteParallelOrIsolatedTest(ResourceManager.getTestContext())) {
// if RBAC is enabled we don't run tests in parallel mode and with that said we don't create another namespaces
if (!Environment.isNamespaceRbacScope()) {
NamespaceManager.getInstance().createNamespaceAndPrepare(extensionContext, Environment.TEST_SUITE_NAMESPACE, CollectorElement.createCollectorElement(testSuiteName));
NamespaceManager.getInstance().createNamespaceAndPrepare(Environment.TEST_SUITE_NAMESPACE, CollectorElement.createCollectorElement(testSuiteName));
} else {
LOGGER.info("We are not gonna create additional namespace: {}, because test suite: {} does not " +
"contains @ParallelTest or @IsolatedTest.", Environment.TEST_SUITE_NAMESPACE, testSuiteName);
Expand All @@ -59,15 +59,14 @@ public void createTestSuiteNamespace(ExtensionContext extensionContext) {
}

/**
* Analogically, inverse method to {@link #createTestSuiteNamespace(ExtensionContext)}.
* Analogically, inverse method to {@link #createTestSuiteNamespace()}.
*
* @param extensionContext extension context for test class
*/
public void deleteTestSuiteNamespace(ExtensionContext extensionContext) {
if (ExecutionListener.hasSuiteParallelOrIsolatedTest(extensionContext)) {
public void deleteTestSuiteNamespace() {
if (ExecutionListener.hasSuiteParallelOrIsolatedTest(ResourceManager.getTestContext())) {
// if RBAC is enabled we don't run tests in parallel mode and with that said we don't create another namespaces
if (!Environment.isNamespaceRbacScope()) {
final String testSuiteName = extensionContext.getRequiredTestClass().getName();
final String testSuiteName = ResourceManager.getTestContext().getRequiredTestClass().getName();

LOGGER.info("Deleting Namespace: {} for TestSuite: {}", Environment.TEST_SUITE_NAMESPACE, StUtils.removePackageName(testSuiteName));
NamespaceManager.getInstance().deleteNamespaceWithWaitAndRemoveFromSet(Environment.TEST_SUITE_NAMESPACE, CollectorElement.createCollectorElement(testSuiteName));
Expand All @@ -80,44 +79,42 @@ public void deleteTestSuiteNamespace(ExtensionContext extensionContext) {
* In test cases, where Kafka cluster is deployed we always create another namespace. Such test case is then
* annotated as @ParallelNamespaceTest. This method creates from @code{extensionContext} this type of namespace
* and store it to the @code{KubeClusterResource} instance, which then it will be needed in the @AfterEach phase.
* The inverse operation to this one is implement in {@link #deleteParallelNamespace(ExtensionContext)}.
* The inverse operation to this one is implement in {@link #deleteParallelNamespace()}.
*
* @param extensionContext unifier (id), which distinguished all other test cases
*/
public void createParallelNamespace(ExtensionContext extensionContext) {
final String testCaseName = extensionContext.getRequiredTestMethod().getName();
public void createParallelNamespace() {
final String testCaseName = ResourceManager.getTestContext().getRequiredTestMethod().getName();

// if 'parallel namespace test' we are gonna create namespace
if (StUtils.isParallelNamespaceTest(extensionContext)) {
if (StUtils.isParallelNamespaceTest(ResourceManager.getTestContext())) {
// if RBAC is enable we don't run tests in parallel mode and with that said we don't create another namespaces
if (!Environment.isNamespaceRbacScope()) {
final String namespaceTestCase = "namespace-" + counterOfNamespaces.getAndIncrement();

extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).put(TestConstants.NAMESPACE_KEY, namespaceTestCase);
ResourceManager.getTestContext().getStore(ExtensionContext.Namespace.GLOBAL).put(TestConstants.NAMESPACE_KEY, namespaceTestCase);
// create namespace by
LOGGER.info("Creating Namespace: {} for TestCase: {}", namespaceTestCase, StUtils.removePackageName(testCaseName));

NamespaceManager.getInstance().createNamespaceAndPrepare(extensionContext, namespaceTestCase, CollectorElement.createCollectorElement(extensionContext.getRequiredTestClass().getName(), testCaseName));
NamespaceManager.getInstance().createNamespaceAndPrepare(namespaceTestCase, CollectorElement.createCollectorElement(ResourceManager.getTestContext().getRequiredTestClass().getName(), testCaseName));
}
}
}

/**
* Analogically to the {@link #createParallelNamespace(ExtensionContext)}.
* Analogically to the {@link #createParallelNamespace()}.
*
* @param extensionContext unifier (id), which distinguished all other test cases
*/
public void deleteParallelNamespace(ExtensionContext extensionContext) {
public void deleteParallelNamespace() {
// if 'parallel namespace test' we are gonna delete namespace
if (StUtils.isParallelNamespaceTest(extensionContext)) {
if (StUtils.isParallelNamespaceTest(ResourceManager.getTestContext())) {
// if RBAC is enable we don't run tests in parallel mode and with that said we don't create another namespaces
if (!Environment.isNamespaceRbacScope()) {
final String namespaceToDelete = extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(TestConstants.NAMESPACE_KEY).toString();
final String testCaseName = extensionContext.getRequiredTestMethod().getName();
final String namespaceToDelete = ResourceManager.getTestContext().getStore(ExtensionContext.Namespace.GLOBAL).get(TestConstants.NAMESPACE_KEY).toString();
final String testCaseName = ResourceManager.getTestContext().getRequiredTestMethod().getName();

LOGGER.info("Deleting Namespace: {} for TestCase: {}", namespaceToDelete, StUtils.removePackageName(testCaseName));

NamespaceManager.getInstance().deleteNamespaceWithWaitAndRemoveFromSet(namespaceToDelete, CollectorElement.createCollectorElement(extensionContext.getRequiredTestClass().getName(), testCaseName));
NamespaceManager.getInstance().deleteNamespaceWithWaitAndRemoveFromSet(namespaceToDelete, CollectorElement.createCollectorElement(ResourceManager.getTestContext().getRequiredTestClass().getName(), testCaseName));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import io.strimzi.test.logs.CollectorElement;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -135,13 +134,13 @@ public void createNamespaceAndAddToSet(String namespaceName, CollectorElement co
}

/**
* Overloads {@link #createNamespaceAndPrepare(ExtensionContext, String, CollectorElement)} - with {@code CollectorElement} set to {@code null},
* Overloads {@link #createNamespaceAndPrepare(String, CollectorElement)} - with {@code CollectorElement} set to {@code null},
* so the Namespace will not be added into the {@link #MAP_WITH_SUITE_NAMESPACES}.
*
* @param namespaceName name of Namespace that should be created
*/
public void createNamespaceAndPrepare(ExtensionContext extensionContext, String namespaceName) {
createNamespaceAndPrepare(extensionContext, namespaceName, null);
public void createNamespaceAndPrepare(String namespaceName) {
createNamespaceAndPrepare(namespaceName, null);
}

/**
Expand All @@ -154,9 +153,9 @@ public void createNamespaceAndPrepare(ExtensionContext extensionContext, String
* @param namespaceName name of the Namespace that should be created and prepared
* @param collectorElement "key" for accessing the particular Set of Namespaces
*/
public void createNamespaceAndPrepare(ExtensionContext extensionContext, String namespaceName, CollectorElement collectorElement) {
public void createNamespaceAndPrepare(String namespaceName, CollectorElement collectorElement) {
createNamespaceAndAddToSet(namespaceName, collectorElement);
NetworkPolicyResource.applyDefaultNetworkPolicySettings(extensionContext, Collections.singletonList(namespaceName));
NetworkPolicyResource.applyDefaultNetworkPolicySettings(Collections.singletonList(namespaceName));
StUtils.copyImagePullSecrets(namespaceName);
}

Expand All @@ -168,8 +167,8 @@ public void createNamespaceAndPrepare(ExtensionContext extensionContext, String
* @param collectorElement "key" for accessing the particular Set of Namespaces
* @param namespacesToBeCreated list of Namespaces that should be created
*/
public void createNamespaces(ExtensionContext extensionContext, String useNamespace, CollectorElement collectorElement, List<String> namespacesToBeCreated) {
namespacesToBeCreated.forEach(namespaceToBeCreated -> createNamespaceAndPrepare(extensionContext, namespaceToBeCreated, collectorElement));
public void createNamespaces(String useNamespace, CollectorElement collectorElement, List<String> namespacesToBeCreated) {
namespacesToBeCreated.forEach(namespaceToBeCreated -> createNamespaceAndPrepare(namespaceToBeCreated, collectorElement));

KubeClusterResource.getInstance().setNamespace(useNamespace);
}
Expand Down

0 comments on commit ca37a6f

Please sign in to comment.