|
| 1 | +// Copyright (c) 2023 VMware, Inc. or its affiliates. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | + |
| 15 | + |
| 16 | +package com.rabbitmq.client; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled; |
| 19 | +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled; |
| 20 | + |
| 21 | +import com.rabbitmq.client.test.TestUtils; |
| 22 | +import com.rabbitmq.client.test.functional.FunctionalTestSuite; |
| 23 | +import com.rabbitmq.client.test.server.HaTestSuite; |
| 24 | +import com.rabbitmq.client.test.server.LastHaTestSuite; |
| 25 | +import com.rabbitmq.client.test.server.ServerTestSuite; |
| 26 | +import com.rabbitmq.client.test.ssl.SslTestSuite; |
| 27 | +import com.rabbitmq.tools.Host; |
| 28 | +import java.io.File; |
| 29 | +import java.lang.reflect.Field; |
| 30 | +import java.net.Socket; |
| 31 | +import java.util.Properties; |
| 32 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 33 | +import org.junit.jupiter.api.extension.BeforeAllCallback; |
| 34 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 35 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 36 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 37 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 38 | +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; |
| 39 | +import org.junit.jupiter.api.extension.ExtensionContext.Store; |
| 40 | +import org.slf4j.Logger; |
| 41 | +import org.slf4j.LoggerFactory; |
| 42 | + |
| 43 | +public class AmqpClientTestExtension implements ExecutionCondition, BeforeAllCallback, |
| 44 | + BeforeEachCallback, |
| 45 | + AfterEachCallback { |
| 46 | + |
| 47 | + private static final Logger LOGGER = LoggerFactory.getLogger(AmqpClientTestExtension.class); |
| 48 | + private static final Namespace NAMESPACE = Namespace.create(AmqpClientTestExtension.class); |
| 49 | + |
| 50 | + static { |
| 51 | + Properties TESTS_PROPS = new Properties(System.getProperties()); |
| 52 | + String make = System.getenv("MAKE"); |
| 53 | + if (make != null) { |
| 54 | + TESTS_PROPS.setProperty("make.bin", make); |
| 55 | + } |
| 56 | + try { |
| 57 | + TESTS_PROPS.load(Host.class.getClassLoader().getResourceAsStream("config.properties")); |
| 58 | + } catch (Exception e) { |
| 59 | + System.out.println( |
| 60 | + "\"build.properties\" or \"config.properties\" not found" + |
| 61 | + " in classpath. Please copy \"build.properties\" and" + |
| 62 | + " \"config.properties\" into src/test/resources. Ignore" + |
| 63 | + " this message if running with ant."); |
| 64 | + } finally { |
| 65 | + System.setProperties(TESTS_PROPS); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void maybeSetHaFieldValue(ExtensionContext context, boolean value) { |
| 70 | + if (context.getTestClass().isPresent() && context.getTestInstance().isPresent()) { |
| 71 | + try { |
| 72 | + Field haField = findField(context.getTestClass().get(), "ha"); |
| 73 | + if (haField != null) { |
| 74 | + haField.setAccessible(true); |
| 75 | + haField.set(context.getTestInstance().get(), value); |
| 76 | + } |
| 77 | + } catch (Exception e) { |
| 78 | + // OK |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static Field findField(Class<?> clazz, String fieldName) { |
| 84 | + try { |
| 85 | + return clazz.getDeclaredField(fieldName); |
| 86 | + } catch (NoSuchFieldException e) { |
| 87 | + if (clazz.getSuperclass() != null) { |
| 88 | + return findField(clazz.getSuperclass(), fieldName); |
| 89 | + } |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + private static boolean isFunctionalSuite(ExtensionContext context) { |
| 95 | + return isTestSuite(context, FunctionalTestSuite.class); |
| 96 | + } |
| 97 | + |
| 98 | + private static boolean isSslSuite(ExtensionContext context) { |
| 99 | + return isTestSuite(context, SslTestSuite.class); |
| 100 | + } |
| 101 | + |
| 102 | + private static boolean isServerSuite(ExtensionContext context) { |
| 103 | + return isTestSuite(context, ServerTestSuite.class); |
| 104 | + } |
| 105 | + |
| 106 | + private static boolean isHaSuite(ExtensionContext context) { |
| 107 | + return isTestSuite(context, HaTestSuite.class); |
| 108 | + } |
| 109 | + |
| 110 | + private static boolean isLastHaSuite(ExtensionContext context) { |
| 111 | + return isTestSuite(context, LastHaTestSuite.class); |
| 112 | + } |
| 113 | + |
| 114 | + private static boolean isTestSuite(ExtensionContext context, Class<?> clazz) { |
| 115 | + return context.getUniqueId().contains(clazz.getName()); |
| 116 | + } |
| 117 | + |
| 118 | + public static boolean requiredProperties() { |
| 119 | + /* Path to rabbitmqctl. */ |
| 120 | + String rabbitmqctl = Host.rabbitmqctlCommand(); |
| 121 | + if (rabbitmqctl == null) { |
| 122 | + System.err.println( |
| 123 | + "rabbitmqctl required; please set \"rabbitmqctl.bin\" system" + |
| 124 | + " property"); |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + public static boolean isSSLAvailable() { |
| 132 | + String sslClientCertsDir = System.getProperty("test-client-cert.path"); |
| 133 | + String hostname = System.getProperty("broker.hostname"); |
| 134 | + String port = System.getProperty("broker.sslport"); |
| 135 | + if (sslClientCertsDir == null || hostname == null || port == null) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + // If certificate is present and some server is listening on port 5671 |
| 140 | + if (new File(sslClientCertsDir).exists() && |
| 141 | + checkServerListening(hostname, Integer.parseInt(port))) { |
| 142 | + return true; |
| 143 | + } else { |
| 144 | + return false; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static boolean checkServerListening(String host, int port) { |
| 149 | + Socket s = null; |
| 150 | + try { |
| 151 | + s = new Socket(host, port); |
| 152 | + return true; |
| 153 | + } catch (Exception e) { |
| 154 | + return false; |
| 155 | + } finally { |
| 156 | + if (s != null) { |
| 157 | + try { |
| 158 | + s.close(); |
| 159 | + } catch (Exception e) { |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static Store store(ExtensionContext context) { |
| 166 | + return context.getRoot().getStore(NAMESPACE); |
| 167 | + } |
| 168 | + |
| 169 | + private static boolean hasHaSuiteStarted(ExtensionContext context) { |
| 170 | + return "true".equals(store(context).get("ha")); |
| 171 | + } |
| 172 | + |
| 173 | + private static void markHaSuiteStarted(ExtensionContext context) { |
| 174 | + store(context).put("ha", "true"); |
| 175 | + } |
| 176 | + |
| 177 | + private static void markHaSuiteFinished(ExtensionContext context) { |
| 178 | + store(context).remove("ha"); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 183 | + // HA test suite must be checked first because it contains other test suites |
| 184 | + if (isHaSuite(context)) { |
| 185 | + return requiredProperties() ? enabled("Required properties available") |
| 186 | + : disabled("Required properties not available"); |
| 187 | + } else if (isServerSuite(context)) { |
| 188 | + return requiredProperties() ? enabled("Required properties available") |
| 189 | + : disabled("Required properties not available"); |
| 190 | + } else if (isFunctionalSuite(context)) { |
| 191 | + return requiredProperties() ? enabled("Required properties available") |
| 192 | + : disabled("Required properties not available"); |
| 193 | + } else if (isSslSuite(context)) { |
| 194 | + return requiredProperties() && isSSLAvailable() ? enabled( |
| 195 | + "Required properties and TLS available") |
| 196 | + : disabled("Required properties or TLS not available"); |
| 197 | + } |
| 198 | + return enabled("ok"); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void beforeAll(ExtensionContext context) throws Exception { |
| 203 | + if (isHaSuite(context) && !hasHaSuiteStarted(context)) { |
| 204 | + LOGGER.info("Starting HA test suite"); |
| 205 | + Host.rabbitmqctl("set_policy HA '.*' '{\"ha-mode\":\"all\"}'"); |
| 206 | + markHaSuiteStarted(context); |
| 207 | + } |
| 208 | + if (isLastHaSuite(context)) { |
| 209 | + LOGGER.info("HA suite done, clearing HA state"); |
| 210 | + Host.rabbitmqctl("clear_policy HA"); |
| 211 | + markHaSuiteFinished(context); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public void beforeEach(ExtensionContext context) { |
| 217 | + LOGGER.info( |
| 218 | + "Starting test: {}.{} (nio? {}, HA? {})", |
| 219 | + context.getTestClass().get().getSimpleName(), context.getTestMethod().get().getName(), |
| 220 | + TestUtils.USE_NIO, |
| 221 | + hasHaSuiteStarted(context) |
| 222 | + ); |
| 223 | + if (isHaSuite(context)) { |
| 224 | + maybeSetHaFieldValue(context, true); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + @Override |
| 229 | + public void afterEach(ExtensionContext context) { |
| 230 | + LOGGER.info("Test finished: {}.{}", |
| 231 | + context.getTestClass().get().getSimpleName(), context.getTestMethod().get().getName()); |
| 232 | + if (isHaSuite(context)) { |
| 233 | + maybeSetHaFieldValue(context, false); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | +} |
0 commit comments