-
Notifications
You must be signed in to change notification settings - Fork 1
Add tests migrations #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.junit.Assert; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.containers.output.Slf4jLogConsumer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Testcontainers | ||
public class TarantoolCartridgeMigrationsTest { | ||
|
||
@Container | ||
private static final TarantoolCartridgeContainer container = | ||
new TarantoolCartridgeContainer( | ||
"Dockerfile", | ||
"cartridge/instances.yml", | ||
"cartridge/topology.lua") | ||
.withDirectoryBinding("cartridge") | ||
.withStartupTimeout(Duration.ofSeconds(300)) | ||
.withLogConsumer(new Slf4jLogConsumer( | ||
LoggerFactory.getLogger(TarantoolCartridgeStaticContainerTest.class))); | ||
|
||
@BeforeAll | ||
public static void start_container(){ | ||
if (!container.isRunning()) { | ||
try { | ||
container.start(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
Voloodya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void test_migrations_migrator() throws Exception { | ||
|
||
if(container.isRunning()){ | ||
try { | ||
List<?> resultMigration = container.executeCommand("require('migrator').up()").get(); | ||
Voloodya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void test_migrations_curl(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use curl please to reduce dependencies on external modules There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand. Only standart container tools are user here |
||
|
||
if(container.isRunning()){ | ||
String urlStr = "http://"+container.getRouterHost()+":"+"8081"+"/migrations/up"; | ||
int code = -1; | ||
try{ | ||
org.testcontainers.containers.Container.ExecResult answer = container.execInContainer("curl", "-X", "POST", urlStr); | ||
String out = answer.getStdout(); | ||
code = answer.getExitCode(); | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
Assert.assertEquals(0,code); | ||
} | ||
} | ||
|
||
@Test | ||
public void test_migrations_http() throws Exception { | ||
|
||
if(container.isRunning()){ | ||
URL url = null; | ||
HttpURLConnection connection = null; | ||
OutputStream os = null; | ||
InputStreamReader inputStreamReader = null; | ||
BufferedReader bfR = null; | ||
StringBuilder strBuilder = new StringBuilder(); | ||
|
||
Map<String, String> bodyHttpPostRequest = new HashMap<>(); | ||
Voloodya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
byte[] outSteamByte = bodyHttpPostRequest.toString().getBytes(StandardCharsets.UTF_8); | ||
|
||
try { | ||
String urlStr = "http://"+container.getRouterHost()+":"+container.getAPIPort()+"/migrations/up"; | ||
url = new URL(urlStr); | ||
connection = (HttpURLConnection) url.openConnection(); | ||
|
||
connection.setRequestMethod("POST"); | ||
connection.setDoOutput(true); | ||
connection.setDoInput(true); | ||
connection.setRequestProperty("Content-Type", "application/json"); | ||
connection.setConnectTimeout(200); | ||
connection.setReadTimeout(200); | ||
connection.connect(); | ||
|
||
try { | ||
os = connection.getOutputStream(); | ||
os.write(outSteamByte); | ||
}catch (Exception e){ | ||
System.err.print(e.getMessage()); | ||
} | ||
|
||
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){ | ||
inputStreamReader = new InputStreamReader(connection.getInputStream()); | ||
bfR = new BufferedReader(inputStreamReader); | ||
String line; | ||
while ((line = bfR.readLine()) != null){ | ||
strBuilder.append(line); | ||
} | ||
} | ||
|
||
}catch (MalformedURLException ex){ | ||
ex.printStackTrace(); | ||
} catch (IOException e){ | ||
System.err.print(e.getMessage()); | ||
}finally { | ||
inputStreamReader.close(); | ||
os.close(); | ||
bfR.close(); | ||
} | ||
Assert.assertTrue(strBuilder.toString().contains("applied")); | ||
} | ||
} | ||
|
||
@AfterAll | ||
public static void clear_stop_container(){ | ||
if (container.isRunning()){ | ||
container.stop(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
return{ | ||
Voloodya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
up = function() | ||
local utils = require('migrator.utils') | ||
|
||
local routerSessionContexts = box.schema.space.create('spaceTest', { if_not_exists = true }) | ||
Voloodya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
routerSessionContexts:format({ | ||
{ name = 'sessionId', type = 'string' }, | ||
{ name = 'data', type = 'map', is_nullable = true }, | ||
{ name = 'ts', type = 'number' }, | ||
|
||
-- vshard bucket id | ||
{ name = 'bucket_id', type = 'unsigned', is_nullable = false }, | ||
}) | ||
|
||
routerSessionContexts:create_index('primary', { parts = { { field = 'sessionId' } }, | ||
unique = true, | ||
if_not_exists = true }) | ||
|
||
routerSessionContexts:create_index('bucket_id', { | ||
parts = { 'bucket_id' }, | ||
unique = false, | ||
if_not_exists = true | ||
}) | ||
|
||
utils.register_sharding_key('spaceTest', { 'sessionId' }) | ||
|
||
return true | ||
|
||
|
||
end | ||
} |
Uh oh!
There was an error while loading. Please reload this page.