diff --git a/systemtest/src/main/java/io/strimzi/systemtest/utils/StUtils.java b/systemtest/src/main/java/io/strimzi/systemtest/utils/StUtils.java index a0bb2e37dd..a75b50d741 100644 --- a/systemtest/src/main/java/io/strimzi/systemtest/utils/StUtils.java +++ b/systemtest/src/main/java/io/strimzi/systemtest/utils/StUtils.java @@ -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; @@ -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 parseImageMap(String imageMapString) { + if (imageMapString != null) { + StringTokenizer tok = new StringTokenizer(imageMapString, ", \t\n\r"); + HashMap 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(); + } + } } diff --git a/systemtest/src/main/java/io/strimzi/systemtest/utils/VerificationUtils.java b/systemtest/src/main/java/io/strimzi/systemtest/utils/VerificationUtils.java index 4e049d7213..6c2ca866cd 100644 --- a/systemtest/src/main/java/io/strimzi/systemtest/utils/VerificationUtils.java +++ b/systemtest/src/main/java/io/strimzi/systemtest/utils/VerificationUtils.java @@ -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; @@ -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); @@ -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"); } @@ -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"); } diff --git a/systemtest/src/main/java/io/strimzi/systemtest/utils/kafkaUtils/KafkaUtils.java b/systemtest/src/main/java/io/strimzi/systemtest/utils/kafkaUtils/KafkaUtils.java index f1b51d222c..8344b9d37d 100644 --- a/systemtest/src/main/java/io/strimzi/systemtest/utils/kafkaUtils/KafkaUtils.java +++ b/systemtest/src/main/java/io/strimzi/systemtest/utils/kafkaUtils/KafkaUtils.java @@ -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; diff --git a/systemtest/src/test/java/io/strimzi/systemtest/upgrade/AbstractUpgradeST.java b/systemtest/src/test/java/io/strimzi/systemtest/upgrade/AbstractUpgradeST.java index 3b2bdbafb9..162ddbad60 100644 --- a/systemtest/src/test/java/io/strimzi/systemtest/upgrade/AbstractUpgradeST.java +++ b/systemtest/src/test/java/io/strimzi/systemtest/upgrade/AbstractUpgradeST.java @@ -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 { @@ -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); } diff --git a/test/src/main/java/io/strimzi/test/TestUtils.java b/test/src/main/java/io/strimzi/test/TestUtils.java index a954d5487e..8d78980d62 100644 --- a/test/src/main/java/io/strimzi/test/TestUtils.java +++ b/test/src/main/java/io/strimzi/test/TestUtils.java @@ -144,26 +144,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. * @@ -221,58 +201,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 parseImageMap(String imageMapString) { - if (imageMapString != null) { - StringTokenizer tok = new StringTokenizer(imageMapString, ", \t\n\r"); - HashMap 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 *