diff --git a/testsuite/elytron/src/test/java/org/wildfly/test/integration/elytron/sasl/mgmt/KerberosHttpMgmtSaslTestCase.java b/testsuite/elytron/src/test/java/org/wildfly/test/integration/elytron/sasl/mgmt/KerberosHttpMgmtSaslTestCase.java index c7f093bf149..827c885f96e 100644 --- a/testsuite/elytron/src/test/java/org/wildfly/test/integration/elytron/sasl/mgmt/KerberosHttpMgmtSaslTestCase.java +++ b/testsuite/elytron/src/test/java/org/wildfly/test/integration/elytron/sasl/mgmt/KerberosHttpMgmtSaslTestCase.java @@ -18,7 +18,6 @@ import org.jboss.as.controller.operations.common.Util; import org.jboss.as.test.integration.management.util.ServerReload; import org.jboss.as.test.integration.security.common.CoreUtils; -import org.jboss.as.test.shared.TestSuiteEnvironment; import org.jboss.dmr.ModelNode; import org.junit.runner.RunWith; import org.wildfly.core.testrunner.WildFlyRunner; @@ -81,12 +80,15 @@ protected AutoCloseable configureSaslMechanismOnServer(String mechanism, boolean final HttpMgmtConfigurator httpMgmtConfig = httpMgmtConfigBuilder.build(); httpMgmtConfig.create(client, null); - ServerReload.executeReloadAndWaitForCompletion(client, 30 * 1000, false, "remote", - TestSuiteEnvironment.getServerAddress(), PORT_NATIVE); + ServerReload.Parameters serverReloadParams = new ServerReload.Parameters() + .setTimeout(30 * 1000) + .setProtocol("remote") + .setServerPort(PORT_NATIVE); + + ServerReload.executeReloadAndWaitForCompletion(client, serverReloadParams); return () -> { httpMgmtConfig.remove(client, null); - ServerReload.executeReloadAndWaitForCompletion(client, 30 * 1000, false, "remote", - TestSuiteEnvironment.getServerAddress(), PORT_NATIVE); + ServerReload.executeReloadAndWaitForCompletion(client, serverReloadParams); }; } diff --git a/testsuite/manualmode/src/test/java/org/wildfly/core/test/standalone/mgmt/PreparedResponseTestCase.java b/testsuite/manualmode/src/test/java/org/wildfly/core/test/standalone/mgmt/PreparedResponseTestCase.java index ae20da263f0..300937a6000 100644 --- a/testsuite/manualmode/src/test/java/org/wildfly/core/test/standalone/mgmt/PreparedResponseTestCase.java +++ b/testsuite/manualmode/src/test/java/org/wildfly/core/test/standalone/mgmt/PreparedResponseTestCase.java @@ -78,7 +78,7 @@ public void reloadServer() throws Exception { try (ManagementClient managementClient = getManagementClient()) { block(managementClient); long timeout = SHUTDOWN_WAITING_TIME + System.currentTimeMillis(); - ServerReload.executeReload(managementClient.getControllerClient(), false); + ServerReload.executeReloadAndWaitForCompletion(managementClient.getControllerClient(), false); while (System.currentTimeMillis() < timeout) { Thread.sleep(FREQUENCY); try { diff --git a/testsuite/shared/src/main/java/org/jboss/as/test/integration/management/util/ServerReload.java b/testsuite/shared/src/main/java/org/jboss/as/test/integration/management/util/ServerReload.java index 24ca8e2a857..df3e0030e17 100644 --- a/testsuite/shared/src/main/java/org/jboss/as/test/integration/management/util/ServerReload.java +++ b/testsuite/shared/src/main/java/org/jboss/as/test/integration/management/util/ServerReload.java @@ -22,6 +22,7 @@ import org.jboss.as.controller.client.ModelControllerClient; import org.jboss.as.controller.client.helpers.ClientConstants; import org.jboss.as.test.shared.TestSuiteEnvironment; +import org.jboss.as.version.Stability; import org.jboss.dmr.ModelNode; import org.junit.Assert; import org.wildfly.core.testrunner.ManagementClient; @@ -61,7 +62,9 @@ public static void executeReloadAndWaitForCompletion(ModelControllerClient clien * @throws AssertionError if the reload does not complete within the timeout */ public static void executeReloadAndWaitForCompletion(ModelControllerClient client, boolean adminOnly) { - executeReloadAndWaitForCompletion(client, TIMEOUT, adminOnly, null, -1); + Parameters parameters = new Parameters() + .setAdminOnly(adminOnly); + executeReloadAndWaitForCompletion(client, parameters); } /** @@ -72,57 +75,33 @@ public static void executeReloadAndWaitForCompletion(ModelControllerClient clien * * @throws AssertionError if the reload does not complete within the specified timeout */ - public static void executeReloadAndWaitForCompletion(ModelControllerClient client, int timeout) { - executeReloadAndWaitForCompletion(client, timeout, false, null, -1); + private static void executeReloadAndWaitForCompletion(ModelControllerClient client, int timeout) { + Parameters parameters = new Parameters() + .setTimeout(timeout); + executeReloadAndWaitForCompletion(client, parameters); } - public static void executeReloadAndWaitForCompletion(ModelControllerClient client, ModelNode reloadOp) { - executeReloadAndWaitForCompletion(client, reloadOp, TIMEOUT, null, -1); + public static void executeReloadAndWaitForCompletion(ModelControllerClient client, Parameters parameters) { + executeReload(client, parameters); + waitForLiveServerToReload(parameters); } - public static void executeReloadAndWaitForCompletion(ModelControllerClient client, ModelNode reloadOp, int timeout, String serverAddress, int serverPort) { - executeReload(client, reloadOp); - waitForLiveServerToReload(timeout, "remote+http", - serverAddress != null ? serverAddress : TestSuiteEnvironment.getServerAddress(), - serverPort != -1 ? serverPort : TestSuiteEnvironment.getServerPort()); - } - - /** - * Executes a {@code reload} operation, optionally putting the server into {@code admin-only} - * running mode, and waits a configurable maximum time for the reload to complete. - * - * @param client the client to use for the request. Cannot be {@code null} - * @param timeout maximum time to wait for the reload to complete, in milliseconds - * @param adminOnly if {@code true}, the server will be reloaded in admin-only mode - * @param serverAddress if {@code null}, use {@code TestSuiteEnvironment.getServerAddress()} to create the ModelControllerClient - * @param serverPort if {@code -1}, use {@code TestSuiteEnvironment.getServerPort()} to create the ModelControllerClient - * - * @throws AssertionError if the reload does not complete within the specified timeout - */ - public static void executeReloadAndWaitForCompletion(ModelControllerClient client, int timeout, boolean adminOnly, String serverAddress, int serverPort) { - executeReload(client, adminOnly); - waitForLiveServerToReload(timeout, "remote+http", - serverAddress != null ? serverAddress : TestSuiteEnvironment.getServerAddress(), - serverPort != -1 ? serverPort : TestSuiteEnvironment.getServerPort()); - } - - public static void executeReloadAndWaitForCompletion(ModelControllerClient client, int timeout, boolean adminOnly, String protocol, String serverAddress, int serverPort) { - executeReload(client, adminOnly); - waitForLiveServerToReload(timeout, protocol, - serverAddress != null ? serverAddress : TestSuiteEnvironment.getServerAddress(), - serverPort != -1 ? serverPort : TestSuiteEnvironment.getServerPort()); - } - - public static void executeReload(ModelControllerClient client, boolean adminOnly) { + private static void executeReload(ModelControllerClient client, Parameters parameters) { ModelNode operation = new ModelNode(); operation.get(OP_ADDR).setEmptyList(); - operation.get(OP).set("reload"); - operation.get("admin-only").set(adminOnly); + operation.get("admin-only").set(parameters.adminOnly); + if (parameters.stability != null) { + operation.get(OP).set("reload-enhanced"); + operation.get("stability").set(parameters.stability.toString()); + } else { + operation.get(OP).set("reload"); + } executeReload(client, operation); } - public static void executeReload(ModelControllerClient client, ModelNode reloadOp) { + + private static void executeReload(ModelControllerClient client, ModelNode reloadOp) { try { ModelNode result = client.execute(reloadOp); Assert.assertEquals(SUCCESS, result.get(ClientConstants.OUTCOME).asString()); @@ -157,13 +136,13 @@ public static void reloadIfRequired(final ModelControllerClient controllerClient } } - private static void waitForLiveServerToReload(int timeout, String protocol, String serverAddress, int serverPort) { + private static void waitForLiveServerToReload(Parameters parameters) { long start = System.currentTimeMillis(); ModelNode operation = new ModelNode(); operation.get(OP_ADDR).setEmptyList(); operation.get(OP).set(READ_ATTRIBUTE_OPERATION); operation.get(NAME).set("server-state"); - while (System.currentTimeMillis() - start < timeout) { + while (System.currentTimeMillis() - start < parameters.timeout) { //do the sleep before we check, as the attribute state may not change instantly //also reload generally takes longer than 100ms anyway try { @@ -171,8 +150,8 @@ private static void waitForLiveServerToReload(int timeout, String protocol, Stri } catch (InterruptedException e) { } try { - ModelControllerClient liveClient = ModelControllerClient.Factory.create(protocol, - serverAddress, serverPort); + ModelControllerClient liveClient = ModelControllerClient.Factory.create( + parameters.protocol, parameters.serverAddress, parameters.serverPort); try { ModelNode result = liveClient.execute(operation); if ("running".equals(result.get(RESULT).asString())) { @@ -217,5 +196,46 @@ public void tearDown(ManagementClient managementClient) throws Exception { executeReloadAndWaitForCompletion(managementClient.getControllerClient()); } } + + public static class Parameters { + private int timeout = TIMEOUT; + boolean adminOnly = false; + String protocol = "remote+http"; + String serverAddress = TestSuiteEnvironment.getServerAddress(); + int serverPort = TestSuiteEnvironment.getServerPort(); + + Stability stability = null; + + public Parameters setTimeout(int timeout) { + this.timeout = timeout; + return this; + } + + public Parameters setAdminOnly(boolean adminOnly) { + this.adminOnly = adminOnly; + return this; + } + + public Parameters setProtocol(String protocol) { + this.protocol = protocol; + return this; + } + + public Parameters setServerAddress(String serverAddress) { + this.serverAddress = serverAddress; + return this; + } + + public Parameters setServerPort(int serverPort) { + this.serverPort = serverPort; + return this; + } + + public Parameters setStability(Stability stability) { + this.stability = stability; + return this; + } + } + } diff --git a/testsuite/shared/src/main/java/org/wildfly/test/stability/StabilityServerSetupTasks.java b/testsuite/shared/src/main/java/org/wildfly/test/stability/StabilityServerSetupTasks.java new file mode 100644 index 00000000000..b0cd990aa4d --- /dev/null +++ b/testsuite/shared/src/main/java/org/wildfly/test/stability/StabilityServerSetupTasks.java @@ -0,0 +1,161 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2024 Red Hat, Inc., and individual contributors + * as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.wildfly.test.stability; + +import org.jboss.as.controller.PathAddress; +import org.jboss.as.controller.client.ModelControllerClient; +import org.jboss.as.controller.operations.common.Util; +import org.jboss.as.test.integration.management.ManagementOperations; +import org.jboss.as.test.integration.management.util.ServerReload; +import org.jboss.as.version.Stability; +import org.jboss.dmr.ModelNode; +import org.junit.Assert; +import org.junit.Assume; +import org.wildfly.core.testrunner.ManagementClient; +import org.wildfly.core.testrunner.ServerSetupTask; + +import java.util.EnumSet; +import java.util.Set; + +import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CORE_SERVICE; +import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.READ_OPERATION_NAMES_OPERATION; +import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RELOAD_ENHANCED; +import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.STABILITY; +import static org.jboss.as.server.controller.descriptions.ServerDescriptionConstants.SERVER_ENVIRONMENT; + +/** + * For tests that need to run under a specific server stability level, + * the server setup tasks from the inner classes can be used to change the stability level of the server to the desired level. + * Once the test is done, the original stability level is restored. + */ +public abstract class StabilityServerSetupTasks implements ServerSetupTask { + + private final Stability desiredStability; + private volatile Stability originalStability; + + + public StabilityServerSetupTasks(Stability desiredStability) { + this.desiredStability = desiredStability; + } + + @Override + public void setup(ManagementClient managementClient) throws Exception { + Set supportedStabilityLevels = getSupportedStabilityLevels(); + Assume.assumeTrue( + String.format("%s is not a supported stability level", desiredStability, supportedStabilityLevels), + supportedStabilityLevels.contains(desiredStability)); + + Assume.assumeTrue( + "The reload-enhanced operation is not registered at this stability level", + checkReloadEnhancedOperationIsAvailable(managementClient)); + + reloadToDesiredStability(managementClient.getControllerClient(), desiredStability); + } + + @Override + public void tearDown(ManagementClient managementClient) throws Exception { + reloadToDesiredStability(managementClient.getControllerClient(), originalStability); + } + + private boolean checkReloadEnhancedOperationIsAvailable(ManagementClient managementClient) throws Exception { + ModelNode op = Util.createOperation(READ_OPERATION_NAMES_OPERATION, PathAddress.EMPTY_ADDRESS); + ModelNode result = ManagementOperations.executeOperation(managementClient.getControllerClient(), op); + for (ModelNode name : result.asList()) { + if (name.asString().equals(RELOAD_ENHANCED)) { + return true; + } + } + return false; + } + + private Set getSupportedStabilityLevels() { + // TODO WFCORE-6731 - see https://github.com/wildfly/wildfly-core/pull/5895#discussion_r1520489808 + // This information will be available in a management operation, For now just return a hardcoded set + return EnumSet.allOf(Stability.class); + } + + private Stability reloadToDesiredStability(ModelControllerClient client, Stability stability) throws Exception { + // Check the stability + Stability currentStability = readCurrentStability(client); + if (originalStability == null) { + originalStability = currentStability; + } + + // The stability parameter for the reload opration is only registered below the default level + Assume.assumeFalse("Can't reload to a different stability when server stability level is default", currentStability == Stability.DEFAULT && stability != Stability.DEFAULT); + + if (currentStability == stability) { + return originalStability; + } + + //Reload the server to the desired stability level + ServerReload.Parameters parameters = new ServerReload.Parameters() + .setStability(stability); + // Execute the reload + ServerReload.executeReloadAndWaitForCompletion(client, parameters); + + Stability reloadedStability = readCurrentStability(client); + Assert.assertEquals(stability, reloadedStability); + return originalStability; + } + + private Stability readCurrentStability(ModelControllerClient client) throws Exception { + ModelNode op = Util.getReadAttributeOperation(PathAddress.pathAddress(CORE_SERVICE, SERVER_ENVIRONMENT), STABILITY); + ModelNode result = ManagementOperations.executeOperation(client, op); + return Stability.fromString(result.asString()); + } + + /** + * A server setup task that sets the server stability to the default level. + */ + public static class Default extends StabilityServerSetupTasks { + public Default() { + super(Stability.DEFAULT); + } + } + + /** + * A server setup task that sets the server stability to the community level. + */ + public static class Community extends StabilityServerSetupTasks { + public Community() { + super(Stability.COMMUNITY); + } + } + + /** + * A server setup task that sets the server stability to the preview level. + */ + public static class Preview extends StabilityServerSetupTasks { + public Preview() { + super(Stability.PREVIEW); + } + } + + /** + * A server setup task that sets the server stability to the experimental level. + */ + public static class Experimental extends StabilityServerSetupTasks { + public Experimental() { + super(Stability.EXPERIMENTAL); + } + } + +} diff --git a/testsuite/standalone/src/test/java/org/jboss/as/test/integration/security/perimeter/DisableLocalAuthServerSetupTask.java b/testsuite/standalone/src/test/java/org/jboss/as/test/integration/security/perimeter/DisableLocalAuthServerSetupTask.java index 9bbcc40547b..89ec6e56e7c 100644 --- a/testsuite/standalone/src/test/java/org/jboss/as/test/integration/security/perimeter/DisableLocalAuthServerSetupTask.java +++ b/testsuite/standalone/src/test/java/org/jboss/as/test/integration/security/perimeter/DisableLocalAuthServerSetupTask.java @@ -62,7 +62,11 @@ public void setup(final ManagementClient managementClient) throws Exception { executeForSuccess(client, compositeOp.build()); // Use the current client to execute the reload, but the native client to ensure the reload is complete - ServerReload.executeReloadAndWaitForCompletion(client, ServerReload.TIMEOUT, false, protocol, host, port); + ServerReload.executeReloadAndWaitForCompletion(client, new ServerReload.Parameters() + .setProtocol(protocol) + .setServerAddress(host) + .setServerPort(port) + ); } @Override