|
| 1 | +package com.atlassian.localstack; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStreamReader; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.concurrent.atomic.AtomicReference; |
| 10 | +import java.util.logging.Logger; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +import org.junit.runner.notification.RunNotifier; |
| 14 | +import org.junit.runners.BlockJUnit4ClassRunner; |
| 15 | +import org.junit.runners.model.InitializationError; |
| 16 | + |
| 17 | +import com.amazonaws.util.IOUtils; |
| 18 | + |
| 19 | +/** |
| 20 | + * Simple JUnit test runner that automatically downloads, installs, starts, |
| 21 | + * and stops the LocalStack local cloud infrastructure components. |
| 22 | + * |
| 23 | + * Should work cross-OS, however has been only tested under Unix (Linux/MacOS). |
| 24 | + * |
| 25 | + * @author Waldemar Hummer |
| 26 | + */ |
| 27 | +public class LocalstackTestRunner extends BlockJUnit4ClassRunner { |
| 28 | + |
| 29 | + private static final AtomicReference<Process> INFRA_STARTED = new AtomicReference<Process>(); |
| 30 | + private static String CONFIG_FILE_CONTENT = ""; |
| 31 | + |
| 32 | + private static final String INFRA_READY_MARKER = "Ready."; |
| 33 | + private static final String TMP_INSTALL_DIR = System.getProperty("java.io.tmpdir") + |
| 34 | + File.separator + "localstack_install_dir"; |
| 35 | + private static final String ADDITIONAL_PATH = "/usr/local/bin/"; |
| 36 | + private static final String LOCALHOST = "localhost"; |
| 37 | + private static final String LOCALSTACK_REPO_URL = "https://github.com/atlassian/localstack"; |
| 38 | + |
| 39 | + private static final Logger LOG = Logger.getLogger(LocalstackTestRunner.class.getName()); |
| 40 | + |
| 41 | + public LocalstackTestRunner(Class<?> klass) throws InitializationError { |
| 42 | + super(klass); |
| 43 | + } |
| 44 | + |
| 45 | + /* SERVICE ENDPOINTS */ |
| 46 | + |
| 47 | + public static String getEndpointS3() { |
| 48 | + ensureInstallation(); |
| 49 | + return getEndpoint("s3"); |
| 50 | + } |
| 51 | + |
| 52 | + public static String getEndpointKinesis() { |
| 53 | + ensureInstallation(); |
| 54 | + return getEndpoint("kinesis"); |
| 55 | + } |
| 56 | + |
| 57 | + public static String getEndpointLambda() { |
| 58 | + ensureInstallation(); |
| 59 | + return getEndpoint("lambda"); |
| 60 | + } |
| 61 | + |
| 62 | + public static String getEndpointDynamoDB() { |
| 63 | + ensureInstallation(); |
| 64 | + return getEndpoint("dynamodb"); |
| 65 | + } |
| 66 | + |
| 67 | + public static String getEndpointDynamoDBStreams() { |
| 68 | + ensureInstallation(); |
| 69 | + return getEndpoint("dynamodbstreams"); |
| 70 | + } |
| 71 | + |
| 72 | + public static String getEndpointAPIGateway() { |
| 73 | + ensureInstallation(); |
| 74 | + return getEndpoint("apigateway"); |
| 75 | + } |
| 76 | + |
| 77 | + public static String getEndpointElasticsearch() { |
| 78 | + ensureInstallation(); |
| 79 | + return getEndpoint("elasticsearch"); |
| 80 | + } |
| 81 | + |
| 82 | + public static String getEndpointFirehose() { |
| 83 | + ensureInstallation(); |
| 84 | + return getEndpoint("firehose"); |
| 85 | + } |
| 86 | + |
| 87 | + public static String getEndpointSNS() { |
| 88 | + ensureInstallation(); |
| 89 | + return getEndpoint("sns"); |
| 90 | + } |
| 91 | + |
| 92 | + public static String getEndpointSQS() { |
| 93 | + ensureInstallation(); |
| 94 | + return getEndpoint("sns"); |
| 95 | + } |
| 96 | + |
| 97 | + /* UTILITY METHODS */ |
| 98 | + |
| 99 | + @Override |
| 100 | + public void run(RunNotifier notifier) { |
| 101 | + setupInfrastructure(); |
| 102 | + super.run(notifier); |
| 103 | + } |
| 104 | + |
| 105 | + private static void ensureInstallation() { |
| 106 | + File dir = new File(TMP_INSTALL_DIR); |
| 107 | + if(!dir.exists()) { |
| 108 | + LOG.info("Installing LocalStack to temporary directory (this might take a while): " + TMP_INSTALL_DIR); |
| 109 | + exec("git clone " + LOCALSTACK_REPO_URL + " " + TMP_INSTALL_DIR); |
| 110 | + exec("cd " + TMP_INSTALL_DIR + "; make install"); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static void killProcess(Process p) { |
| 115 | + p.destroy(); |
| 116 | + p.destroyForcibly(); |
| 117 | + } |
| 118 | + |
| 119 | + private static String getEndpoint(String service) { |
| 120 | + ensureInstallation(); |
| 121 | + String regex = ".*DEFAULT_PORT_" + service.toUpperCase() + "\\s*=\\s*([0-9]+).*"; |
| 122 | + String port = Pattern.compile(regex, Pattern.DOTALL | Pattern.MULTILINE).matcher(CONFIG_FILE_CONTENT).replaceAll("$1"); |
| 123 | + return "http://" + LOCALHOST + ":" + port + "/"; |
| 124 | + } |
| 125 | + |
| 126 | + private static Process exec(String cmd) { |
| 127 | + return exec(cmd, true); |
| 128 | + } |
| 129 | + |
| 130 | + private static Process exec(String cmd, boolean wait) { |
| 131 | + try { |
| 132 | + Map<String, String> env = System.getenv(); |
| 133 | + final Process p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", cmd}, |
| 134 | + new String[]{"PATH=" + ADDITIONAL_PATH + ":" + env.get("PATH")}); |
| 135 | + if (wait) { |
| 136 | + int code = p.waitFor(); |
| 137 | + if(code != 0) { |
| 138 | + String stderr = IOUtils.toString(p.getErrorStream()); |
| 139 | + String stdout = IOUtils.toString(p.getInputStream()); |
| 140 | + throw new IllegalStateException("Failed to run command '" + cmd + "', return code " + code + |
| 141 | + ".\nSTDOUT: " + stdout + "\nSTDERR: " + stderr); |
| 142 | + } |
| 143 | + } else { |
| 144 | + /* make sure we destroy the process on JVM shutdown */ |
| 145 | + Runtime.getRuntime().addShutdownHook(new Thread() { |
| 146 | + public void run() { |
| 147 | + killProcess(p); |
| 148 | + } |
| 149 | + }); |
| 150 | + } |
| 151 | + return p; |
| 152 | + } catch (Exception e) { |
| 153 | + throw new RuntimeException(e); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private void setupInfrastructure() { |
| 158 | + synchronized (INFRA_STARTED) { |
| 159 | + ensureInstallation(); |
| 160 | + if(INFRA_STARTED.get() != null) return; |
| 161 | + String cmd = "cd " + TMP_INSTALL_DIR + "; exec make infra"; |
| 162 | + Process proc; |
| 163 | + try { |
| 164 | + proc = exec(cmd, false); |
| 165 | + BufferedReader r1 = new BufferedReader(new InputStreamReader(proc.getInputStream())); |
| 166 | + String line; |
| 167 | + LOG.info("Waiting for infrastructure to be spun up"); |
| 168 | + while((line = r1.readLine()) != null) { |
| 169 | + if(INFRA_READY_MARKER.equals(line)) { |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + /* read contents of LocalStack config file */ |
| 174 | + String configFile = TMP_INSTALL_DIR + File.separator + "localstack" + File.separator + "constants.py"; |
| 175 | + CONFIG_FILE_CONTENT = IOUtils.toString(new FileInputStream(configFile)); |
| 176 | + INFRA_STARTED.set(proc); |
| 177 | + } catch (IOException e) { |
| 178 | + throw new RuntimeException(e); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + public static void teardownInfrastructure() { |
| 184 | + Process proc = INFRA_STARTED.get(); |
| 185 | + if(proc == null) { |
| 186 | + return; |
| 187 | + } |
| 188 | + killProcess(proc); |
| 189 | + } |
| 190 | +} |
0 commit comments