Skip to content

Commit

Permalink
add tests for ENTMQBR-8120
Browse files Browse the repository at this point in the history
  • Loading branch information
tlbueno committed Apr 19, 2024
1 parent 1358b83 commit c4a353e
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public abstract class ArtemisCloudClusterOperator {
final static Logger LOGGER = LoggerFactory.getLogger(ArtemisCloudClusterOperator.class);
public static final List<String> ZAP_LOG_LEVELS = List.of("debug", "info", "error");
public static final String ZAP_LOG_LEVEL_OPTION = "--zap-log-level";
public static final String LEASE_DURATION_OPTION = "--lease-duration";
public static final String RENEW_DEADLINE_OPTION = "--renew-deadline";
public static final String RETRY_PERIOD_OPTION = "--retry-period";
protected final String deploymentNamespace;
protected final boolean isNamespaced;
protected final EnvironmentOperator environmentOperator;
Expand Down Expand Up @@ -116,6 +119,13 @@ public String getOperatorOLMVersion(boolean returnMMM) {
throw new ClaireRuntimeException("Operator is not installed using OLM!");
}
}

abstract public void setOperatorLogLevel(String logLevel);

abstract public void setOperatorLeaseDuration(int durationInSeconds);

abstract public void setOperatorRenewDeadlineDuration(int durationInSeconds);

abstract public void setOperatorRetryPeriodDuration(int durationInSeconds);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

public class ArtemisCloudClusterOperatorFile extends ArtemisCloudClusterOperator {

Expand Down Expand Up @@ -292,35 +293,59 @@ public static String getOperatorControllerManagerName(Path yamlFile) {
return operatorCODeployment.getMetadata().getName();
}

@Override
public void setOperatorLogLevel(String logLevel) {
if (ArtemisCloudClusterOperator.ZAP_LOG_LEVELS.contains(logLevel)) {
Deployment deployment = kubeClient.getDeployment(getDeploymentNamespace(), getOperatorName());
Pod podOld = kubeClient.getFirstPodByPrefixName(getDeploymentNamespace(), getOperatorName());
List<String> args = deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getArgs();
List<String> argsUpdated = new ArrayList<>();

for (String arg : args) {
if (arg.contains(ZAP_LOG_LEVEL_OPTION)) {
argsUpdated.add(ZAP_LOG_LEVEL_OPTION + "=" + logLevel.toLowerCase(Locale.ROOT));
public void updateArgs(String oldValue, String newValue) {
Deployment deployment = kubeClient.getDeployment(getDeploymentNamespace(), getOperatorName());
Pod pod = kubeClient.getFirstPodByPrefixName(getDeploymentNamespace(), getOperatorName());
List<String> args = deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getArgs();
List<String> argsUpdated;

if (args.stream().anyMatch(e -> e.contains(oldValue))) {
argsUpdated = args.stream().map(f -> {
if (f.contains(oldValue)) {
return newValue;
} else {
argsUpdated.add(arg);
return f;
}
}
}).collect(Collectors.toList());
} else {
argsUpdated = new ArrayList<>(args);
argsUpdated.add(newValue);
}

if (!args.equals(argsUpdated)) {
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setArgs(argsUpdated);
kubeClient.setDeployment(getDeploymentNamespace(), deployment);
kubeClient.waitForPodReload(getDeploymentNamespace(), podOld, getOperatorName());
LOGGER.info("[{}] Changed operator {} log level to {}", getDeploymentNamespace(), getOperatorName(), logLevel);
} else {
LOGGER.debug("[{}] Reload is not needed, zap-log-level is {} as expected", getDeploymentNamespace(), logLevel);
}
if (!args.equals(argsUpdated)) {
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setArgs(argsUpdated);
kubeClient.setDeployment(getDeploymentNamespace(), deployment);
kubeClient.waitForPodReload(getDeploymentNamespace(), pod, getOperatorName());
LOGGER.info("[{}] Changed operator {} args {} to {}", getDeploymentNamespace(), getOperatorName(), oldValue, newValue);
} else {
LOGGER.debug("[{}] Reload is not needed, operator {} args are the same", getDeploymentNamespace(), getOperatorName());
}
}

@Override
public void setOperatorLogLevel(String logLevel) {
if (ArtemisCloudClusterOperator.ZAP_LOG_LEVELS.contains(logLevel)) {
updateArgs(ZAP_LOG_LEVEL_OPTION, ZAP_LOG_LEVEL_OPTION + "=" + logLevel.toLowerCase(Locale.ROOT));
} else {
LOGGER.error("[{}] Unable to set provided log level to operator {}", getDeploymentNamespace(), getOperatorName());
}
}

@Override
public void setOperatorLeaseDuration(int durationInSeconds) {
updateArgs(LEASE_DURATION_OPTION, LEASE_DURATION_OPTION + "=" + durationInSeconds);
}

@Override
public void setOperatorRenewDeadlineDuration(int durationInSeconds) {
updateArgs(RENEW_DEADLINE_OPTION, RENEW_DEADLINE_OPTION + "=" + durationInSeconds);
}

@Override
public void setOperatorRetryPeriodDuration(int durationInSeconds) {
updateArgs(RETRY_PERIOD_OPTION, RETRY_PERIOD_OPTION + "=" + durationInSeconds);
}

public static String getLastVersionFromOperatorFile(String imageType, Deployment operator) {
List<EnvVar> envVars1 = operator.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv();
List<Integer> versions = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClientTimeoutException;
import io.fabric8.openshift.api.model.operatorhub.lifecyclemanager.v1.PackageChannel;
import io.fabric8.openshift.api.model.operatorhub.lifecyclemanager.v1.PackageManifest;
Expand Down Expand Up @@ -185,38 +186,59 @@ public void undeployOperator(boolean waitForUndeployment) {
LOGGER.info("[{}] [OLM] Successfully undeployed ArtemisCloudOperator", deploymentNamespace);
}

@Override
public void setOperatorLogLevel(String logLevel) {
LOGGER.debug("[{}] Updating subscription {} with log level {}", getDeploymentNamespace(), subscriptionName, logLevel);
public void updateSubscriptionEnvVar(String name, String value) {
Subscription subscription = ((OpenShiftClient) kubeClient.getKubernetesClient()).operatorHub().subscriptions().inNamespace(deploymentNamespace).withName(subscriptionName).get();
SubscriptionConfig subscriptionConfig = subscription.getSpec().getConfig();
if (subscriptionConfig == null) {
// Create new SubscriptionConfig
subscriptionConfig = new SubscriptionConfigBuilder()
.withEnv(List.of(
new EnvVarBuilder()
.withName("ARGS")
.withValue(ZAP_LOG_LEVEL_OPTION + "=" + logLevel)
.build()
)).build();
.withEnv(List.of(
new EnvVarBuilder()
.withName("ARGS")
.withValue(name + "=" + value)
.build()
)).build();
} else {
// Update existing EnvVars
List<EnvVar> envVars = subscriptionConfig.getEnv();
for (EnvVar envVar : envVars) {
if (envVar.getName().equals("ARGS")) {
if (envVar.getValue().contains(ZAP_LOG_LEVEL_OPTION)) {
String newValue = envVar.getValue().replaceFirst(ZAP_LOG_LEVEL_OPTION + "=\\w+",
ZAP_LOG_LEVEL_OPTION + "=" + logLevel);
if (envVar.getValue().contains(name)) {
String newValue = envVar.getValue().replaceFirst(name + "=\\w+",
name + "=" + value);
envVar.setValue(newValue);
} else {
envVar.setValue(envVar.getValue() + " " + ZAP_LOG_LEVEL_OPTION + "=" + logLevel);
envVar.setValue(envVar.getValue() + " " + name + "=" + value);
}
subscriptionConfig.setEnv(envVars);
}
}
}
Pod operatorPod = kubeClient.getFirstPodByPrefixName(getDeploymentNamespace(), getOperatorName());
subscription.getSpec().setConfig(subscriptionConfig);
((OpenShiftClient) kubeClient.getKubernetesClient()).operatorHub().subscriptions().resource(subscription).createOrReplace();
kubeClient.waitForPodReload(getDeploymentNamespace(), operatorPod, getOperatorName());
}

@Override
public void setOperatorLogLevel(String logLevel) {
LOGGER.debug("[{}] Updating subscription {} with log level {}", getDeploymentNamespace(), subscriptionName, logLevel);
updateSubscriptionEnvVar(ZAP_LOG_LEVEL_OPTION, logLevel);
}

@Override
public void setOperatorLeaseDuration(int durationInSeconds) {
updateSubscriptionEnvVar(LEASE_DURATION_OPTION, String.valueOf(durationInSeconds));
}

@Override
public void setOperatorRenewDeadlineDuration(int durationInSeconds) {
updateSubscriptionEnvVar(RENEW_DEADLINE_OPTION, String.valueOf(durationInSeconds));
}

@Override
public void setOperatorRetryPeriodDuration(int durationInSeconds) {
updateSubscriptionEnvVar(RETRY_PERIOD_OPTION, String.valueOf(durationInSeconds));
}

protected void updateSubscription(Subscription updatedSubscription) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright Broker QE authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.brokerqe.claire.configuration;

import io.brokerqe.claire.AbstractSystemTests;
import io.brokerqe.claire.ArtemisVersion;
import io.brokerqe.claire.Constants;
import io.brokerqe.claire.junit.TestValidSince;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClientTimeoutException;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Tag(Constants.TAG_OPERATOR)
@TestValidSince(ArtemisVersion.VERSION_2_33)
public class OperatorConfigurationTests extends AbstractSystemTests {
private static final Logger LOGGER = LoggerFactory.getLogger(OperatorConfigurationTests.class);
private final String testNamespace = getRandomNamespaceName("opr-cfg-tests", 3);

private static final int DEFAULT_LEASE_DURATION = 15;
private static final int DEFAULT_RENEW_DEADLINE = 10;
private static final int DEFAULT_RETRY_PERIOD = 2;

@BeforeAll
void setupClusterOperator() {
setupDefaultClusterOperator(testNamespace);
}

@AfterAll
void teardownClusterOperator() {
teardownDefaultClusterOperator(testNamespace);
}

@Test
void testOperatorValidLeaseDuration() {
Pod operatorPod = getClient().getFirstPodByPrefixName(testNamespace, operator.getOperatorName());
String operatorLog = getClient().getLogsFromPod(operatorPod);
Assertions.assertThat(operatorLog).contains("\"LeaseDuration\": \"" + DEFAULT_LEASE_DURATION + "s\"");

int newLeaseDuration = 20;
operator.setOperatorLeaseDuration(newLeaseDuration);
operatorPod = getClient().getFirstPodByPrefixName(testNamespace, operator.getOperatorName());
operatorLog = getClient().getLogsFromPod(operatorPod);
Assertions.assertThat(operatorLog).contains("\"LeaseDuration\": \"" + newLeaseDuration + "s\"");
operator.setOperatorLeaseDuration(DEFAULT_LEASE_DURATION);
}

@Test
void testOperatorInvalidLeaseDuration() {
Pod operatorPod = getClient().getFirstPodByPrefixName(testNamespace, operator.getOperatorName());
String operatorLog = getClient().getLogsFromPod(operatorPod);
Assertions.assertThat(operatorLog).contains("\"LeaseDuration\": \"" + DEFAULT_LEASE_DURATION + "s\"");
Assertions.assertThat(operatorLog).contains("\"RenewDeadline\": \"" + DEFAULT_RENEW_DEADLINE + "s\"");

int newLeaseDuration = DEFAULT_RENEW_DEADLINE; // Lease duration must be higher than renew-deadline
Assertions.assertThatExceptionOfType(KubernetesClientTimeoutException.class).isThrownBy(() -> {
operator.setOperatorLeaseDuration(newLeaseDuration);
});
operator.setOperatorLeaseDuration(DEFAULT_LEASE_DURATION);
}

@Test
void testOperatorValidRetryPeriod() {
Pod operatorPod = getClient().getFirstPodByPrefixName(testNamespace, operator.getOperatorName());
String operatorLog = getClient().getLogsFromPod(operatorPod);
Assertions.assertThat(operatorLog).contains("\"RetryPeriod\": \"" + DEFAULT_RETRY_PERIOD + "s\"");

int newRetryPeriod = 5;
operator.setOperatorRetryPeriodDuration(newRetryPeriod);
operatorPod = getClient().getFirstPodByPrefixName(testNamespace, operator.getOperatorName());
operatorLog = getClient().getLogsFromPod(operatorPod);
Assertions.assertThat(operatorLog).contains("\"RetryPeriod\": \"" + newRetryPeriod + "s\"");
operator.setOperatorRetryPeriodDuration(DEFAULT_RETRY_PERIOD);
}

@Test
void testOperatorInvalidRetryPeriod() {
Pod operatorPod = getClient().getFirstPodByPrefixName(testNamespace, operator.getOperatorName());
String operatorLog = getClient().getLogsFromPod(operatorPod);
Assertions.assertThat(operatorLog).contains("\"RetryPeriod\": \"" + DEFAULT_RETRY_PERIOD + "s\"");

int newRetryPeriod = 0;
Assertions.assertThatExceptionOfType(KubernetesClientTimeoutException.class).isThrownBy(() -> {
operator.setOperatorRetryPeriodDuration(newRetryPeriod);
});
operator.setOperatorRetryPeriodDuration(DEFAULT_RETRY_PERIOD);
}

@Test
void testOperatorValidRenewDeadline() {
Pod operatorPod = getClient().getFirstPodByPrefixName(testNamespace, operator.getOperatorName());
String operatorLog = getClient().getLogsFromPod(operatorPod);
Assertions.assertThat(operatorLog).contains("\"RenewDeadline\": \"" + DEFAULT_RENEW_DEADLINE + "s\"");

int newRenewDeadline = 3;
operator.setOperatorRenewDeadlineDuration(newRenewDeadline);
operatorPod = getClient().getFirstPodByPrefixName(testNamespace, operator.getOperatorName());
operatorLog = getClient().getLogsFromPod(operatorPod);
Assertions.assertThat(operatorLog).contains("\"RenewDeadline\": \"" + newRenewDeadline + "s\"");
operator.setOperatorRenewDeadlineDuration(DEFAULT_RENEW_DEADLINE);
}

@Test
void testOperatorInvalidRenewDeadline() {
Pod operatorPod = getClient().getFirstPodByPrefixName(testNamespace, operator.getOperatorName());
String operatorLog = getClient().getLogsFromPod(operatorPod);
Assertions.assertThat(operatorLog).contains("\"RenewDeadline\": \"" + DEFAULT_RENEW_DEADLINE + "s\"");
Assertions.assertThat(operatorLog).contains("\"RetryPeriod\": \"" + DEFAULT_RETRY_PERIOD + "s\"");

int newRenewDeadline = DEFAULT_RETRY_PERIOD; // needs to be higher than retry-period
Assertions.assertThatExceptionOfType(KubernetesClientTimeoutException.class).isThrownBy(() -> {
operator.setOperatorRenewDeadlineDuration(newRenewDeadline);
});
operator.setOperatorRenewDeadlineDuration(DEFAULT_RENEW_DEADLINE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.brokerqe.claire.ResourceManager;
import io.brokerqe.claire.TestUtils;
import io.brokerqe.claire.junit.TestValidSince;
import io.brokerqe.claire.smoke.SmokeTests;
import io.fabric8.kubernetes.api.model.Pod;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -37,7 +36,7 @@
@TestValidSince(ArtemisVersion.VERSION_2_28)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class OperatorLoggingTests extends AbstractSystemTests {
private static final Logger LOGGER = LoggerFactory.getLogger(SmokeTests.class);
private static final Logger LOGGER = LoggerFactory.getLogger(OperatorLoggingTests.class);
private final String testNamespace = getRandomNamespaceName("log-tests", 3);
final static String DEBUG = "DEBUG";
final static String INFO = "INFO";
Expand Down

0 comments on commit c4a353e

Please sign in to comment.