Skip to content

Commit

Permalink
[system test] Move TestUtils methods used only in STs to systemtest m… (
Browse files Browse the repository at this point in the history
#10610)

Signed-off-by: see-quick <maros.orsak159@gmail.com>
  • Loading branch information
see-quick committed Sep 20, 2024
1 parent 3411f61 commit f7c1928
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 89 deletions.
68 changes: 68 additions & 0 deletions systemtest/src/main/java/io/strimzi/systemtest/utils/StUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.function.BiFunction;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
Expand Down Expand Up @@ -580,4 +582,70 @@ public static boolean isUnidirectionalTopicOperatorUsed(String namespaceName, La
LOGGER.warn("Cannot determine if UTO is used or not, because the EO Pod doesn't exist, gonna assume that it's not");
return false;
}

/**
* Indents the input string with for empty spaces at the beginning of each line.
*
* @param input Input string that should be indented
*
* @return Indented string
*/
public static String indent(String input) {
StringBuilder sb = new StringBuilder();
String[] lines = input.split("[\n\r]");

for (String line : lines) {
sb.append(" ").append(line).append(System.lineSeparator());
}

return sb.toString();
}

/**
* Changes the {@code subject} of the RoleBinding in the given YAML resource to be the
* {@code strimzi-cluster-operator} {@code ServiceAccount} in the given namespace.
*
* @param roleBindingFile The RoleBinding YAML file to load and change
* @param namespace Namespace of the service account which should be the subject of this RoleBinding
*
* @return Modified RoleBinding resource YAML
*/
public static String changeRoleBindingSubject(File roleBindingFile, String namespace) {
YAMLMapper mapper = new YAMLMapper();
try {
JsonNode node = mapper.readTree(roleBindingFile);
ArrayNode subjects = (ArrayNode) node.get("subjects");
ObjectNode subject = (ObjectNode) subjects.get(0);
subject.put("kind", "ServiceAccount")
.put("name", "strimzi-cluster-operator")
.put("namespace", namespace);
return mapper.writeValueAsString(node);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* Parse the image map String into a Map.
*
* @param imageMapString String with the image map (contains key-value pairs separated by new lines in a single string)
*
* @return Map with the parsed images
*/
public static Map<String, String> parseImageMap(String imageMapString) {
if (imageMapString != null) {
StringTokenizer tok = new StringTokenizer(imageMapString, ", \t\n\r");
HashMap<String, String> map = new HashMap<>();
while (tok.hasMoreTokens()) {
String versionImage = tok.nextToken();
int endIndex = versionImage.indexOf('=');
String version = versionImage.substring(0, endIndex);
String image = versionImage.substring(endIndex + 1);
map.put(version.trim(), image.trim());
}
return Collections.unmodifiableMap(map);
} else {
return Collections.emptyMap();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.strimzi.systemtest.resources.crd.KafkaResource;
import io.strimzi.systemtest.resources.crd.StrimziPodSetResource;
import io.strimzi.systemtest.utils.kubeUtils.objects.PodUtils;
import io.strimzi.test.TestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -365,14 +364,14 @@ public static void verifyClusterOperatorKafkaDockerImages(String clusterOperator
//Verifying docker image for zookeeper pods
for (int i = 0; i < controllerPods; i++) {
String imgFromPod = PodUtils.getContainerImageNameFromPod(kafkaNamespaceName, KafkaResources.zookeeperPodName(clusterName, i), "zookeeper");
assertThat("ZooKeeper Pod: " + i + " uses wrong image", imgFromPod, containsString(TestUtils.parseImageMap(imgFromDeplConf.get(TestConstants.KAFKA_IMAGE_MAP)).get(kafkaVersion)));
assertThat("ZooKeeper Pod: " + i + " uses wrong image", imgFromPod, containsString(StUtils.parseImageMap(imgFromDeplConf.get(TestConstants.KAFKA_IMAGE_MAP)).get(kafkaVersion)));
}
}

//Verifying docker image for kafka pods
brokerPods.forEach(brokerPod -> {
String imgFromPod = PodUtils.getContainerImageNameFromPod(kafkaNamespaceName, brokerPod, "kafka");
assertThat("Kafka Pod: " + brokerPod + " uses wrong image", imgFromPod, containsString(TestUtils.parseImageMap(imgFromDeplConf.get(TestConstants.KAFKA_IMAGE_MAP)).get(kafkaVersion)));
assertThat("Kafka Pod: " + brokerPod + " uses wrong image", imgFromPod, containsString(StUtils.parseImageMap(imgFromDeplConf.get(TestConstants.KAFKA_IMAGE_MAP)).get(kafkaVersion)));

if (rackAwareEnabled) {
String initContainerImage = PodUtils.getInitContainerImageName(brokerPod);
Expand Down Expand Up @@ -415,7 +414,7 @@ public static void verifyClusterOperatorConnectDockerImage(String clusterOperato
connectVersion = Environment.ST_KAFKA_VERSION;
}

assertThat(TestUtils.parseImageMap(imgFromDeplConf.get(TestConstants.KAFKA_CONNECT_IMAGE_MAP)).get(connectVersion), is(connectImageName));
assertThat(StUtils.parseImageMap(imgFromDeplConf.get(TestConstants.KAFKA_CONNECT_IMAGE_MAP)).get(connectVersion), is(connectImageName));
LOGGER.info("Docker image name of KafkaConnect verified");
}

Expand All @@ -440,7 +439,7 @@ public static void verifyClusterOperatorMM2DockerImage(String clusterOperatorNam
mirrormaker2Version = Environment.ST_KAFKA_VERSION;
}

assertThat(TestUtils.parseImageMap(imgFromDeplConf.get(TestConstants.KAFKA_MIRROR_MAKER_2_IMAGE_MAP)).get(mirrormaker2Version), is(mirrormaker2ImageName));
assertThat(StUtils.parseImageMap(imgFromDeplConf.get(TestConstants.KAFKA_MIRROR_MAKER_2_IMAGE_MAP)).get(mirrormaker2Version), is(mirrormaker2ImageName));
LOGGER.info("Docker image name of MM2 verified");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import static io.strimzi.api.kafka.model.kafka.KafkaResources.zookeeperComponentName;
import static io.strimzi.systemtest.enums.CustomResourceStatus.NotReady;
import static io.strimzi.systemtest.enums.CustomResourceStatus.Ready;
import static io.strimzi.test.TestUtils.indent;
import static io.strimzi.systemtest.utils.StUtils.indent;
import static io.strimzi.test.TestUtils.waitFor;
import static io.strimzi.test.k8s.KubeClusterResource.cmdKubeClient;
import static io.strimzi.test.k8s.KubeClusterResource.kubeClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ protected void modifyApplyClusterOperatorWithCRDsFromFile(String clusterOperator

Arrays.stream(Objects.requireNonNull(root.listFiles())).sorted().forEach(f -> {
if (watchedNsRoleBindingFilePrefixes.stream().anyMatch((rbFilePrefix) -> f.getName().startsWith(rbFilePrefix))) {
cmdKubeClient(componentsNamespaceName).replaceContent(TestUtils.changeRoleBindingSubject(f, clusterOperatorNamespaceName));
cmdKubeClient(componentsNamespaceName).replaceContent(StUtils.changeRoleBindingSubject(f, clusterOperatorNamespaceName));
} else if (f.getName().matches(".*RoleBinding.*")) {
cmdKubeClient(clusterOperatorNamespaceName).replaceContent(TestUtils.changeRoleBindingSubject(f, clusterOperatorNamespaceName));
cmdKubeClient(clusterOperatorNamespaceName).replaceContent(StUtils.changeRoleBindingSubject(f, clusterOperatorNamespaceName));
} else if (f.getName().matches(".*Deployment.*")) {
cmdKubeClient(clusterOperatorNamespaceName).replaceContent(StUtils.changeDeploymentConfiguration(componentsNamespaceName, f, strimziFeatureGatesValue));
} else {
Expand Down Expand Up @@ -345,9 +345,9 @@ protected void deleteInstalledYamls(String clusterOperatorNamespaceName, String
Arrays.stream(Objects.requireNonNull(root.listFiles())).sorted().forEach(f -> {
try {
if (watchedNsRoleBindingFilePrefixes.stream().anyMatch((rbFilePrefix) -> f.getName().startsWith(rbFilePrefix))) {
cmdKubeClient(componentsNamespaceName).deleteContent(TestUtils.changeRoleBindingSubject(f, clusterOperatorNamespaceName));
cmdKubeClient(componentsNamespaceName).deleteContent(StUtils.changeRoleBindingSubject(f, clusterOperatorNamespaceName));
} else if (f.getName().matches(".*RoleBinding.*")) {
cmdKubeClient(clusterOperatorNamespaceName).deleteContent(TestUtils.changeRoleBindingSubject(f, clusterOperatorNamespaceName));
cmdKubeClient(clusterOperatorNamespaceName).deleteContent(StUtils.changeRoleBindingSubject(f, clusterOperatorNamespaceName));
} else {
cmdKubeClient(clusterOperatorNamespaceName).delete(f);
}
Expand Down
79 changes: 0 additions & 79 deletions test/src/main/java/io/strimzi/test/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
*/
package io.strimzi.test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
Expand All @@ -16,19 +12,16 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.ServerSocket;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -144,26 +137,6 @@ public static long waitFor(String description, long pollIntervalMs, long timeout
}
}

/**
* Indents the input string with for empty spaces at the beginning of each line.
*
* TODO: This is used only in system tests and should be moved there.
*
* @param input Input string that should be indented
*
* @return Indented string
*/
public static String indent(String input) {
StringBuilder sb = new StringBuilder();
String[] lines = input.split("[\n\r]");

for (String line : lines) {
sb.append(" ").append(line).append(System.lineSeparator());
}

return sb.toString();
}

/**
* Creates a modifiable set wit the desired elements. Use {@code Set.of()} if immutable set is sufficient.
*
Expand Down Expand Up @@ -221,58 +194,6 @@ public static void checkOwnerReference(HasMetadata resource, HasMetadata owner)
assertThat(or.getController(), is(false));
}

/**
* Changes the {@code subject} of the RoleBinding in the given YAML resource to be the
* {@code strimzi-cluster-operator} {@code ServiceAccount} in the given namespace.
*
* TODO: This is used only in system tests and should be moved there.
*
* @param roleBindingFile The RoleBinding YAML file to load and change
* @param namespace Namespace of the service account which should be the subject of this RoleBinding
*
* @return Modified RoleBinding resource YAML
*/
public static String changeRoleBindingSubject(File roleBindingFile, String namespace) {
YAMLMapper mapper = new YAMLMapper();
try {
JsonNode node = mapper.readTree(roleBindingFile);
ArrayNode subjects = (ArrayNode) node.get("subjects");
ObjectNode subject = (ObjectNode) subjects.get(0);
subject.put("kind", "ServiceAccount")
.put("name", "strimzi-cluster-operator")
.put("namespace", namespace);
return mapper.writeValueAsString(node);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* Parse the image map String into a Map.
*
* TODO: This is used only in system tests and should be moved there.
*
* @param imageMapString String with the image map (contains key-value pairs separated by new lines in a single string)
*
* @return Map with the parsed images
*/
public static Map<String, String> parseImageMap(String imageMapString) {
if (imageMapString != null) {
StringTokenizer tok = new StringTokenizer(imageMapString, ", \t\n\r");
HashMap<String, String> map = new HashMap<>();
while (tok.hasMoreTokens()) {
String versionImage = tok.nextToken();
int endIndex = versionImage.indexOf('=');
String version = versionImage.substring(0, endIndex);
String image = versionImage.substring(endIndex + 1);
map.put(version.trim(), image.trim());
}
return Collections.unmodifiableMap(map);
} else {
return Collections.emptyMap();
}
}

/**
* Finds a free server port which can be used by the web server
*
Expand Down

0 comments on commit f7c1928

Please sign in to comment.