Skip to content

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

Merged
merged 3 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,4 @@
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</project>
</project>
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();
throw new RuntimeException(e);
}
}

}

@Test
public void test_migrations_migrator() throws Exception {

if(container.isRunning()){
try {
List<?> resultMigration = container.executeCommand("require('migrator').up()").get();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}

@Test
public void test_migrations_curl(){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use curl please to reduce dependencies on external modules

Copy link
Contributor Author

@Voloodya Voloodya May 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. Only standart container tools are user here
Each migration method is checked separatly, this one too


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<>();
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();
}
}
}
2 changes: 2 additions & 0 deletions src/test/resources/cartridge/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ end

-- For faster set up, discovery all buckets at once
require('vshard.consts').BUCKET_CHUNK_SIZE = 30000
require('migrator')

local cartridge = require('cartridge')
local ok, err = cartridge.cfg({
Expand All @@ -38,6 +39,7 @@ local ok, err = cartridge.cfg({
'app.roles.api_router',
'app.roles.api_storage',
'app.roles.custom',
'migrator',
},
cluster_cookie = 'testapp-cluster-cookie',
})
Expand Down
32 changes: 32 additions & 0 deletions src/test/resources/cartridge/migrations/001_spase_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
return{

up = function()
local utils = require('migrator.utils')

local routerSessionContexts = box.schema.space.create('spaceTest', { if_not_exists = true })
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
}
1 change: 1 addition & 0 deletions src/test/resources/cartridge/testapp-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies = {
'lua >= 5.1',
'cartridge == 2.7.3-1',
'crud == 0.10.0-1',
'migrations == 0.4.1-1',
}
build = {
type = 'none';
Expand Down