|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.jupiter.api.AfterAll; |
| 5 | +import org.junit.jupiter.api.BeforeAll; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 9 | +import org.testcontainers.junit.jupiter.Container; |
| 10 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 11 | + |
| 12 | +import java.io.BufferedReader; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStreamReader; |
| 15 | +import java.io.OutputStream; |
| 16 | +import java.net.HttpURLConnection; |
| 17 | +import java.net.MalformedURLException; |
| 18 | +import java.net.URL; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +@Testcontainers |
| 25 | +public class TarantoolCartridgeMigrationsTest { |
| 26 | + |
| 27 | + @Container |
| 28 | + private static final TarantoolCartridgeContainer container = |
| 29 | + new TarantoolCartridgeContainer( |
| 30 | + "Dockerfile", |
| 31 | + "cartridge/instances.yml", |
| 32 | + "cartridge/topology.lua") |
| 33 | + .withDirectoryBinding("cartridge") |
| 34 | + .withStartupTimeout(Duration.ofSeconds(300)) |
| 35 | + .withLogConsumer(new Slf4jLogConsumer( |
| 36 | + LoggerFactory.getLogger(TarantoolCartridgeStaticContainerTest.class))); |
| 37 | + |
| 38 | + @BeforeAll |
| 39 | + public static void test_start_container() { |
| 40 | + if (!container.isRunning()) { |
| 41 | + try { |
| 42 | + container.start(); |
| 43 | + } catch (Exception e) { |
| 44 | + throw new RuntimeException(e); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @AfterAll |
| 50 | + public static void clear_stop_container() { |
| 51 | + if (container.isRunning()) { |
| 52 | + container.stop(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void test_migrations_migrator() throws Exception { |
| 58 | + container.executeCommand("require('migrator').up()").get(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void test_migrations_curl() throws IOException, InterruptedException { |
| 63 | + |
| 64 | + String urlStr = "http://" + container.getRouterHost() + ":" + "8081" + "/migrations/up"; |
| 65 | + int code = -1; |
| 66 | + org.testcontainers.containers.Container.ExecResult answer = container.execInContainer("curl", "-X", "POST", urlStr); |
| 67 | + code = answer.getExitCode(); |
| 68 | + Assert.assertEquals(0, code); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void test_migrations_http() throws Exception { |
| 73 | + |
| 74 | + HttpURLConnection connection; |
| 75 | + OutputStream os = null; |
| 76 | + InputStreamReader inputStreamReader = null; |
| 77 | + BufferedReader bfR = null; |
| 78 | + StringBuilder strBuilder = new StringBuilder(); |
| 79 | + |
| 80 | + Map<String, String> bodyHttpPostRequest = new HashMap<>(); |
| 81 | + byte[] outSteamByte = bodyHttpPostRequest.toString().getBytes(StandardCharsets.UTF_8); |
| 82 | + |
| 83 | + try { |
| 84 | + String urlStr = "http://" + container.getRouterHost() + ":" + container.getAPIPort() + "/migrations/up"; |
| 85 | + connection = createConnection(urlStr); |
| 86 | + os = connection.getOutputStream(); |
| 87 | + os.write(outSteamByte); |
| 88 | + |
| 89 | + if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { |
| 90 | + inputStreamReader = new InputStreamReader(connection.getInputStream()); |
| 91 | + bfR = new BufferedReader(inputStreamReader); |
| 92 | + String line; |
| 93 | + while ((line = bfR.readLine()) != null) { |
| 94 | + strBuilder.append(line); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + } catch (MalformedURLException ex) { |
| 99 | + } catch (IOException e) { |
| 100 | + } finally { |
| 101 | + inputStreamReader.close(); |
| 102 | + os.close(); |
| 103 | + bfR.close(); |
| 104 | + } |
| 105 | + Assert.assertTrue(strBuilder.toString().contains("applied")); |
| 106 | + } |
| 107 | + |
| 108 | + public HttpURLConnection createConnection(String urlStr) throws IOException { |
| 109 | + HttpURLConnection connection = null; |
| 110 | + URL url = new URL(urlStr); |
| 111 | + |
| 112 | + connection = (HttpURLConnection) url.openConnection(); |
| 113 | + connection.setRequestMethod("POST"); |
| 114 | + connection.setDoOutput(true); |
| 115 | + connection.setDoInput(true); |
| 116 | + connection.setRequestProperty("Content-Type", "application/json"); |
| 117 | + connection.setConnectTimeout(200); |
| 118 | + connection.setReadTimeout(200); |
| 119 | + connection.connect(); |
| 120 | + |
| 121 | + return connection; |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments