|
| 1 | +/* |
| 2 | + * Copyright The WildFly Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package org.wildfly.test.integration.observability.micrometer; |
| 6 | + |
| 7 | +import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM; |
| 8 | +import static org.junit.Assert.assertTrue; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 13 | +import org.jboss.arquillian.container.test.api.RunAsClient; |
| 14 | +import org.jboss.arquillian.junit.Arquillian; |
| 15 | +import org.jboss.arquillian.junit.InSequence; |
| 16 | +import org.jboss.arquillian.testcontainers.api.DockerRequired; |
| 17 | +import org.jboss.as.arquillian.api.ContainerResource; |
| 18 | +import org.jboss.as.arquillian.api.ServerSetup; |
| 19 | +import org.jboss.as.arquillian.container.ManagementClient; |
| 20 | +import org.jboss.as.controller.client.Operation; |
| 21 | +import org.jboss.as.controller.client.helpers.Operations; |
| 22 | +import org.jboss.as.controller.descriptions.ModelDescriptionConstants; |
| 23 | +import org.jboss.as.test.shared.CdiUtils; |
| 24 | +import org.jboss.as.test.shared.ServerReload; |
| 25 | +import org.jboss.as.test.shared.observability.setuptasks.MicrometerSetupTask; |
| 26 | +import org.jboss.as.test.shared.util.AssumeTestGroupUtil; |
| 27 | +import org.jboss.dmr.ModelNode; |
| 28 | +import org.jboss.shrinkwrap.api.Archive; |
| 29 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 30 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 31 | +import org.junit.AssumptionViolatedException; |
| 32 | +import org.junit.BeforeClass; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.wildfly.test.integration.observability.JaxRsActivator; |
| 36 | + |
| 37 | +@RunWith(Arquillian.class) |
| 38 | +@ServerSetup({MicrometerSetupTask.class}) |
| 39 | +@DockerRequired |
| 40 | +@RunAsClient |
| 41 | +public class ConflictingPrometheusContextTestCase { |
| 42 | + private static final ModelNode metricsExtension = Operations.createAddress("extension", "org.wildfly.extension.metrics"); |
| 43 | + private static final ModelNode metricsSubsystem = Operations.createAddress("subsystem", "metrics"); |
| 44 | + |
| 45 | + public static final ModelNode PROMETHEUS_REGISTRY_ADDRESS = Operations.createAddress(SUBSYSTEM, "micrometer", "registry", "prometheus"); |
| 46 | + |
| 47 | + private boolean metricsExtAdded = false; |
| 48 | + private boolean metricsSubsystemAdded = false; |
| 49 | + |
| 50 | + @ContainerResource |
| 51 | + protected ManagementClient managementClient; |
| 52 | + |
| 53 | + @Deployment |
| 54 | + public static Archive<?> deploy() { |
| 55 | + return ShrinkWrap.create(WebArchive.class, "micrometer-prometheus.war") |
| 56 | + .addClasses(JaxRsActivator.class, MicrometerResource.class) |
| 57 | + .addAsWebInfResource(CdiUtils.createBeansXml(), "beans.xml"); |
| 58 | + } |
| 59 | + |
| 60 | + @BeforeClass |
| 61 | + public static void beforeClass() { |
| 62 | + if (AssumeTestGroupUtil.isBootableJar() || AssumeTestGroupUtil.isWildFlyPreview() || isGalleon()) { |
| 63 | + throw new AssumptionViolatedException("Not supported in this configuration"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static boolean isGalleon() { |
| 68 | + return System.getProperty("ts.layers") != null || System.getProperty("ts.galleon") != null; |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @InSequence(1) |
| 73 | + public void setupMetrics() throws IOException { |
| 74 | + if (!Operations.isSuccessfulOutcome(executeRead(managementClient, metricsExtension))) { |
| 75 | + executeOp(managementClient, Operations.createAddOperation(metricsExtension)); |
| 76 | + metricsExtAdded = true; |
| 77 | + } |
| 78 | + |
| 79 | + if (!Operations.isSuccessfulOutcome(executeRead(managementClient, metricsSubsystem))) { |
| 80 | + executeOp(managementClient, Operations.createAddOperation(metricsSubsystem)); |
| 81 | + metricsSubsystemAdded = true; |
| 82 | + } |
| 83 | + |
| 84 | + ServerReload.executeReloadAndWaitForCompletion(managementClient); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + @InSequence(2) |
| 89 | + public void configureConflictingContexts() throws Exception { |
| 90 | + ModelNode addOperation = Operations.createAddOperation(PROMETHEUS_REGISTRY_ADDRESS); |
| 91 | + addOperation.get("context").set("${no.such.property:/metrics}"); |
| 92 | + addOperation.get("security-enabled").set("false"); |
| 93 | + |
| 94 | + ModelNode response = managementClient.getControllerClient().execute(Operation.Factory.create(addOperation)); |
| 95 | + assertTrue(response.asString(), response.get(ModelDescriptionConstants.FAILURE_DESCRIPTION) |
| 96 | + .asString().contains("WFLYCTL0436")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @InSequence(3) |
| 101 | + public void tearDown() throws IOException { |
| 102 | + if (Operations.isSuccessfulOutcome(executeRead(managementClient, PROMETHEUS_REGISTRY_ADDRESS))) { |
| 103 | + executeOp(managementClient, Operations.createRemoveOperation(PROMETHEUS_REGISTRY_ADDRESS)); |
| 104 | + } |
| 105 | + if (metricsSubsystemAdded) { |
| 106 | + executeOp(managementClient, Operations.createRemoveOperation(metricsSubsystem)); |
| 107 | + } |
| 108 | + if (metricsExtAdded) { |
| 109 | + executeOp(managementClient, Operations.createRemoveOperation(metricsExtension)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public ModelNode executeRead(final ManagementClient managementClient, ModelNode address) throws IOException { |
| 114 | + return managementClient.getControllerClient().execute(Operations.createReadResourceOperation(address)); |
| 115 | + } |
| 116 | + |
| 117 | + private void executeOp(final ManagementClient client, final ModelNode op) throws IOException { |
| 118 | + final ModelNode result = client.getControllerClient().execute(Operation.Factory.create(op)); |
| 119 | + if (!Operations.isSuccessfulOutcome(result)) { |
| 120 | + throw new RuntimeException("Failed to execute operation: " + Operations.getFailureDescription(result) |
| 121 | + .asString()); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments