diff --git a/pom.xml b/pom.xml index 6e92f2e2759..8e012e32a5e 100644 --- a/pom.xml +++ b/pom.xml @@ -122,7 +122,7 @@ 0.8.10 6.0.0.Beta3 1.0.8.Final - 7.0.0.Beta4 + 7.0.0.Final-SNAPSHOT 1.0.0 5.0.0.Beta3 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 df3e0030e17..44af2a1413a 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 @@ -96,6 +96,9 @@ private static void executeReload(ModelControllerClient client, Parameters param } else { operation.get(OP).set("reload"); } + if (parameters.serverConfig != null) { + operation.get("server-config").set(parameters.serverConfig); + } executeReload(client, operation); } @@ -204,6 +207,8 @@ public static class Parameters { String serverAddress = TestSuiteEnvironment.getServerAddress(); int serverPort = TestSuiteEnvironment.getServerPort(); + String serverConfig = null; + Stability stability = null; public Parameters setTimeout(int timeout) { @@ -235,6 +240,11 @@ public Parameters setStability(Stability stability) { this.stability = stability; return this; } + + public Parameters setServerConfig(String serverConfig) { + this.serverConfig = serverConfig; + return this; + } } } diff --git a/testsuite/shared/src/main/java/org/wildfly/test/snapshot/ServerSnapshot.java b/testsuite/shared/src/main/java/org/wildfly/test/snapshot/ServerSnapshot.java new file mode 100644 index 00000000000..600d61a5ae7 --- /dev/null +++ b/testsuite/shared/src/main/java/org/wildfly/test/snapshot/ServerSnapshot.java @@ -0,0 +1,78 @@ +/* + * 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.snapshot; + +import org.jboss.as.controller.client.helpers.ClientConstants; +import org.jboss.as.controller.descriptions.ModelDescriptionConstants; +import org.jboss.as.test.integration.management.util.ServerReload; +import org.jboss.as.version.Stability; +import org.jboss.dmr.ModelNode; +import org.wildfly.core.testrunner.ManagementClient; + +import java.io.File; + +import static org.junit.Assert.fail; + +public class ServerSnapshot { + public static AutoCloseable takeSnapshot(ManagementClient client) { + return takeSnapshot(client, null); + } + /** + * Takes a snapshot of the current state of the server. + * + * Returns a AutoCloseable that can be used to restore the server state + * @param client The client + * @param reloadToStability the stability the AutoCloseable should reload to + * @return A closeable that can be used to restore the server + */ + public static AutoCloseable takeSnapshot(ManagementClient client, Stability reloadToStability) { + try { + ModelNode node = new ModelNode(); + node.get(ModelDescriptionConstants.OP).set("take-snapshot"); + ModelNode result = client.getControllerClient().execute(node); + if (!"success".equals(result.get(ClientConstants.OUTCOME).asString())) { + fail("Reload operation didn't finish successfully: " + result.asString()); + } + String snapshot = result.get(ModelDescriptionConstants.RESULT).asString(); + final String fileName = snapshot.contains(File.separator) ? snapshot.substring(snapshot.lastIndexOf(File.separator) + 1) : snapshot; + return new AutoCloseable() { + @Override + public void close() throws Exception { + ServerReload.Parameters parameters = new ServerReload.Parameters(); + parameters.setServerConfig(fileName); + if (reloadToStability != null) { + parameters.setStability(reloadToStability); + } + ServerReload.executeReloadAndWaitForCompletion(client.getControllerClient(), parameters); + + ModelNode node = new ModelNode(); + node.get(ModelDescriptionConstants.OP).set("write-config"); + ModelNode result = client.getControllerClient().execute(node); + if (!"success".equals(result.get(ClientConstants.OUTCOME).asString())) { + fail("Failed to write config after restoring from snapshot " + result.asString()); + } + } + }; + } catch (Exception e) { + throw new RuntimeException("Failed to take snapshot", e); + } + } + +} diff --git a/testsuite/shared/src/main/java/org/wildfly/test/stability/StabilityServerSetupTasks.java b/testsuite/shared/src/main/java/org/wildfly/test/stability/StabilityServerSetupSnapshotRestoreTasks.java similarity index 84% rename from testsuite/shared/src/main/java/org/wildfly/test/stability/StabilityServerSetupTasks.java rename to testsuite/shared/src/main/java/org/wildfly/test/stability/StabilityServerSetupSnapshotRestoreTasks.java index c0ac9448c5d..7fb4a62f1fe 100644 --- a/testsuite/shared/src/main/java/org/wildfly/test/stability/StabilityServerSetupTasks.java +++ b/testsuite/shared/src/main/java/org/wildfly/test/stability/StabilityServerSetupSnapshotRestoreTasks.java @@ -30,6 +30,7 @@ import org.junit.Assume; import org.wildfly.core.testrunner.ManagementClient; import org.wildfly.core.testrunner.ServerSetupTask; +import org.wildfly.test.snapshot.ServerSnapshot; import java.util.EnumSet; import java.util.Set; @@ -46,19 +47,24 @@ * 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. + * + * In order to not pollute the configuration with XML from a different stability level following the run of the test, + * it takes a snapshot of the server configuration in the setup() method, and */ -public abstract class StabilityServerSetupTasks implements ServerSetupTask { +public abstract class StabilityServerSetupSnapshotRestoreTasks implements ServerSetupTask { private final Stability desiredStability; private volatile Stability originalStability; + private AutoCloseable snapshot; + - public StabilityServerSetupTasks(Stability desiredStability) { + public StabilityServerSetupSnapshotRestoreTasks(Stability desiredStability) { this.desiredStability = desiredStability; } @Override - public void setup(ManagementClient managementClient) throws Exception { + public final void setup(ManagementClient managementClient) throws Exception { // Make sure the desired stability level is one of the ones supported by the server Set supportedStabilityLevels = getSupportedStabilityLevels(); Assume.assumeTrue( @@ -75,13 +81,27 @@ public void setup(ManagementClient managementClient) throws Exception { Stability reloadOpStability = getReloadEnhancedOperationStabilityLevel(managementClient); Assume.assumeTrue(desiredStability.enables(reloadOpStability)); + + + originalStability = readCurrentStability(managementClient.getControllerClient()); + + // Take a snapshot, indicating that when reloading we want to go back to the original stability + snapshot = ServerSnapshot.takeSnapshot(managementClient, originalStability); + // All good, let's do it! reloadToDesiredStability(managementClient.getControllerClient(), desiredStability); + + // Do any additional setup from the sub-classes + doSetup(managementClient); + } + + protected void doSetup(ManagementClient managementClient) throws Exception { + } @Override public void tearDown(ManagementClient managementClient) throws Exception { - reloadToDesiredStability(managementClient.getControllerClient(), originalStability); + snapshot.close(); } private boolean checkReloadEnhancedOperationIsAvailable(ManagementClient managementClient) throws Exception { @@ -113,11 +133,6 @@ private Set getSupportedStabilityLevels() { private Stability reloadToDesiredStability(ModelControllerClient client, Stability stability) throws Exception { // Check the stability Stability currentStability = readCurrentStability(client); - if (originalStability == null) { - originalStability = currentStability; - } - - if (currentStability == stability) { return originalStability; } @@ -142,7 +157,7 @@ private Stability readCurrentStability(ModelControllerClient client) throws Exce /** * A server setup task that sets the server stability to the default level. */ - public static class Default extends StabilityServerSetupTasks { + public static class Default extends StabilityServerSetupSnapshotRestoreTasks { public Default() { super(Stability.DEFAULT); } @@ -151,7 +166,7 @@ public Default() { /** * A server setup task that sets the server stability to the community level. */ - public static class Community extends StabilityServerSetupTasks { + public static class Community extends StabilityServerSetupSnapshotRestoreTasks { public Community() { super(Stability.COMMUNITY); } @@ -160,7 +175,7 @@ public Community() { /** * A server setup task that sets the server stability to the preview level. */ - public static class Preview extends StabilityServerSetupTasks { + public static class Preview extends StabilityServerSetupSnapshotRestoreTasks { public Preview() { super(Stability.PREVIEW); } @@ -169,7 +184,7 @@ public Preview() { /** * A server setup task that sets the server stability to the experimental level. */ - public static class Experimental extends StabilityServerSetupTasks { + public static class Experimental extends StabilityServerSetupSnapshotRestoreTasks { public Experimental() { super(Stability.EXPERIMENTAL); } diff --git a/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/AbstractStabilityServerSetupTaskTest.java b/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/AbstractStabilityServerSetupTaskTest.java index 220fe12f3b7..5d73772c5b2 100644 --- a/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/AbstractStabilityServerSetupTaskTest.java +++ b/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/AbstractStabilityServerSetupTaskTest.java @@ -31,6 +31,8 @@ import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CORE_SERVICE; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.STABILITY; +import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SYSTEM_PROPERTY; +import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.VALUE; import static org.jboss.as.server.controller.descriptions.ServerDescriptionConstants.SERVER_ENVIRONMENT; public abstract class AbstractStabilityServerSetupTaskTest { @@ -50,4 +52,18 @@ public void testStabilityMatchesSetupTask() throws Exception { Stability stability = Stability.fromString(result.asString()); Assert.assertEquals(desiredStability, stability); } + + @Test + public void testSystemPropertyWasSetByDoSetupCalls() throws Exception { + ModelNode read = Util.getReadAttributeOperation(PathAddress.pathAddress(SYSTEM_PROPERTY, AbstractStabilityServerSetupTaskTest.class.getName()), VALUE); + ModelNode result = ManagementOperations.executeOperation(managementClient.getControllerClient(), read); + Assert.assertEquals(this.getClass().getName(), result.asString()); + } + + + protected static void addSystemProperty(ManagementClient client, Class clazz) throws Exception { + ModelNode add = Util.createAddOperation(PathAddress.pathAddress(SYSTEM_PROPERTY, AbstractStabilityServerSetupTaskTest.class.getName())); + add.get(VALUE).set(clazz.getName()); + ManagementOperations.executeOperation(client.getControllerClient(), add); + } } diff --git a/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityCommunityServerSetupTestCase.java b/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityCommunityServerSetupTestCase.java index 908f0967880..43e9282e529 100644 --- a/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityCommunityServerSetupTestCase.java +++ b/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityCommunityServerSetupTestCase.java @@ -21,14 +21,27 @@ import org.jboss.as.version.Stability; import org.junit.runner.RunWith; +import org.wildfly.core.testrunner.ManagementClient; import org.wildfly.core.testrunner.ServerSetup; import org.wildfly.core.testrunner.WildFlyRunner; -import org.wildfly.test.stability.StabilityServerSetupTasks; +import org.wildfly.test.stability.StabilityServerSetupSnapshotRestoreTasks; -@ServerSetup(StabilityServerSetupTasks.Community.class) +@ServerSetup(StabilityCommunityServerSetupTestCase.CommunityStabilitySetupTask.class) @RunWith(WildFlyRunner.class) public class StabilityCommunityServerSetupTestCase extends AbstractStabilityServerSetupTaskTest { public StabilityCommunityServerSetupTestCase() { super(Stability.COMMUNITY); } + + + public static class CommunityStabilitySetupTask extends StabilityServerSetupSnapshotRestoreTasks.Community { + @Override + protected void doSetup(ManagementClient managementClient) throws Exception { + // Not really needed since the resulting written xml will be of a higher stability level + // than the server. Still we are doing it for experimental preview, so it doesn't hurt to + // do the same here. + AbstractStabilityServerSetupTaskTest.addSystemProperty(managementClient, StabilityCommunityServerSetupTestCase.class); + } + } + } diff --git a/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityDefaultServerSetupTestCase.java b/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityDefaultServerSetupTestCase.java index 7f817551f29..9ae53a2b052 100644 --- a/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityDefaultServerSetupTestCase.java +++ b/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityDefaultServerSetupTestCase.java @@ -21,15 +21,25 @@ import org.jboss.as.version.Stability; import org.junit.runner.RunWith; +import org.wildfly.core.testrunner.ManagementClient; import org.wildfly.core.testrunner.ServerSetup; import org.wildfly.core.testrunner.WildFlyRunner; -import org.wildfly.test.stability.StabilityServerSetupTasks; +import org.wildfly.test.stability.StabilityServerSetupSnapshotRestoreTasks; -@ServerSetup(StabilityServerSetupTasks.Default.class) +@ServerSetup(StabilityDefaultServerSetupTestCase.DefaultStabilitySetupTask.class) @RunWith(WildFlyRunner.class) public class StabilityDefaultServerSetupTestCase extends AbstractStabilityServerSetupTaskTest { public StabilityDefaultServerSetupTestCase() { super(Stability.DEFAULT); } + public static class DefaultStabilitySetupTask extends StabilityServerSetupSnapshotRestoreTasks.Default { + @Override + protected void doSetup(ManagementClient managementClient) throws Exception { + // Not really needed since the resulting written xml will be of a higher stability level + // than the server. Still we are doing it for experimental preview, so it doesn't hurt to + // do the same here. + AbstractStabilityServerSetupTaskTest.addSystemProperty(managementClient, StabilityDefaultServerSetupTestCase.class); + } + } } diff --git a/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityExperimentalServerSetupTestCase.java b/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityExperimentalServerSetupTestCase.java index ecbd1464336..e617db0289b 100644 --- a/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityExperimentalServerSetupTestCase.java +++ b/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityExperimentalServerSetupTestCase.java @@ -21,14 +21,26 @@ import org.jboss.as.version.Stability; import org.junit.runner.RunWith; +import org.wildfly.core.testrunner.ManagementClient; import org.wildfly.core.testrunner.ServerSetup; import org.wildfly.core.testrunner.WildFlyRunner; -import org.wildfly.test.stability.StabilityServerSetupTasks; +import org.wildfly.test.stability.StabilityServerSetupSnapshotRestoreTasks; -@ServerSetup(StabilityServerSetupTasks.Experimental.class) +@ServerSetup(StabilityExperimentalServerSetupTestCase.ExperimentalStabilitySetupTask.class) @RunWith(WildFlyRunner.class) public class StabilityExperimentalServerSetupTestCase extends AbstractStabilityServerSetupTaskTest { public StabilityExperimentalServerSetupTestCase() { super(Stability.EXPERIMENTAL); } + + + public static class ExperimentalStabilitySetupTask extends StabilityServerSetupSnapshotRestoreTasks.Experimental { + @Override + protected void doSetup(ManagementClient managementClient) throws Exception { + // Write a system property so the model ges stored with a lower stability level. + // This is to make sure we can reload back to the higher level from the snapshot + AbstractStabilityServerSetupTaskTest.addSystemProperty(managementClient, StabilityExperimentalServerSetupTestCase.class); + } + } + } diff --git a/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityPreviewServerSetupTestCase.java b/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityPreviewServerSetupTestCase.java index 26be97e0502..e4f48713d39 100644 --- a/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityPreviewServerSetupTestCase.java +++ b/testsuite/standalone/src/test/java/org/wildfly/core/test/standalone/stability/StabilityPreviewServerSetupTestCase.java @@ -21,14 +21,26 @@ import org.jboss.as.version.Stability; import org.junit.runner.RunWith; +import org.wildfly.core.testrunner.ManagementClient; import org.wildfly.core.testrunner.ServerSetup; import org.wildfly.core.testrunner.WildFlyRunner; -import org.wildfly.test.stability.StabilityServerSetupTasks; +import org.wildfly.test.stability.StabilityServerSetupSnapshotRestoreTasks; -@ServerSetup(StabilityServerSetupTasks.Preview.class) +@ServerSetup(StabilityPreviewServerSetupTestCase.PreviewStabilitySetupTask.class) @RunWith(WildFlyRunner.class) public class StabilityPreviewServerSetupTestCase extends AbstractStabilityServerSetupTaskTest { public StabilityPreviewServerSetupTestCase() { super(Stability.PREVIEW); } + + + public static class PreviewStabilitySetupTask extends StabilityServerSetupSnapshotRestoreTasks.Preview { + @Override + protected void doSetup(ManagementClient managementClient) throws Exception { + // Write a system property so the model ges stored with a lower stability level. + // This is to make sure we can reload back to the higher level from the snapshot + AbstractStabilityServerSetupTaskTest.addSystemProperty(managementClient, StabilityPreviewServerSetupTestCase.class); + } + } + }