Skip to content

Commit

Permalink
[WFCORE-6728] Make the stability setup tasks take/restore snapshots o…
Browse files Browse the repository at this point in the history
…f the server config

Otherwise, if we were working on a lower stability level, and made changes to the
model, those could be stored with s schema from a lower stability level, meaning
the reload back to the default community level will fail since it will contain
xml elements from a namespace we can't handle
  • Loading branch information
kabir committed Mar 19, 2024
1 parent 65240c8 commit b1438fc
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<version.jacoco.plugin>0.8.10</version.jacoco.plugin>
<version.org.jboss.galleon>6.0.0.Beta3</version.org.jboss.galleon>
<version.org.wildfly.checkstyle-config>1.0.8.Final</version.org.wildfly.checkstyle-config>
<version.org.wildfly.galleon-plugins>7.0.0.Beta4</version.org.wildfly.galleon-plugins>
<version.org.wildfly.galleon-plugins>7.0.0.Final-SNAPSHOT</version.org.wildfly.galleon-plugins>
<version.org.eclipse.m2e.lifecycle-mapping>1.0.0</version.org.eclipse.m2e.lifecycle-mapping>
<!-- wildfly-maven-plugin-->
<version.wildfly.plugin>5.0.0.Beta3</version.wildfly.plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -235,6 +240,11 @@ public Parameters setStability(Stability stability) {
this.stability = stability;
return this;
}

public Parameters setServerConfig(String serverConfig) {
this.serverConfig = serverConfig;
return this;
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Stability> supportedStabilityLevels = getSupportedStabilityLevels();
Assume.assumeTrue(
Expand All @@ -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 {
Expand Down Expand Up @@ -113,11 +133,6 @@ private Set<Stability> 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;
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 <T extends AbstractStabilityServerSetupTaskTest> void addSystemProperty(ManagementClient client, Class<T> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}

0 comments on commit b1438fc

Please sign in to comment.